diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2022-12-23 22:53:07 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2022-12-23 22:53:07 +0000 |
commit | 2c61fb1d1ce85fe400a8668e85fc0da42813a45a (patch) | |
tree | 68e0e63c747a08c3f3c38c7088a0e16c343afdb3 /src/libs6/s6_auto_write_service.c | |
parent | 977d6ca915355954ae6215b0aa577c24fc257efe (diff) | |
download | s6-2c61fb1d1ce85fe400a8668e85fc0da42813a45a.tar.gz s6-2c61fb1d1ce85fe400a8668e85fc0da42813a45a.tar.xz s6-2c61fb1d1ce85fe400a8668e85fc0da42813a45a.zip |
Refactor libs6auto, add s6-instance-maker (untested)
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libs6/s6_auto_write_service.c')
-rw-r--r-- | src/libs6/s6_auto_write_service.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/libs6/s6_auto_write_service.c b/src/libs6/s6_auto_write_service.c new file mode 100644 index 0000000..c341a55 --- /dev/null +++ b/src/libs6/s6_auto_write_service.c @@ -0,0 +1,59 @@ +/* ISC license. */ + +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/uio.h> + +#include <skalibs/types.h> +#include <skalibs/buffer.h> +#include <skalibs/djbunix.h> +#include <skalibs/strerr.h> + +#include <s6/auto.h> + +void s6_auto_write_service (char const *dir, unsigned int nfd, s6_buffer_writer_func_ref f, void *data, char const *logger) +{ + int fd ; + buffer b ; + size_t dirlen = strlen(dir) ; + mode_t m = umask(0) ; + char buf[4096] ; + char fn[dirlen + 17] ; + if (mkdir(dir, 0755) == -1) strerr_diefu2sys(111, "mkdir ", dir) ; + umask(m) ; + m = ~m & 0666 ; + memcpy(fn, dir, dirlen) ; + memcpy(fn + dirlen, "/run", 5) ; + fd = open_trunc(fn) ; + if (fd == -1) strerr_diefu2sys(111, "open ", fn) ; + buffer_init(&b, &buffer_write, fd, buf, 4096) ; + if (!(*f)(&b, data)) strerr_diefu2sys(111, "write to ", fn) ; + fd_close(fd) ; + if (nfd) + { + char fmt[UINT_FMT] ; + size_t l = uint_fmt(fmt, nfd) ; + fmt[l++] = '\n' ; + memcpy(fn + dirlen + 1, "notification-fd", 16) ; + if (!openwritenclose_unsafe(fn, fmt, l)) strerr_diefu2sys(111, "write to ", fn) ; + } + if (logger) + { + memcpy(fn + dirlen + 1, "type", 5) ; + if (!openwritenclose_unsafe(fn, "longrun\n", 8)) strerr_diefu2sys(111, "write to ", fn) ; + if (logger[0]) + { + struct iovec v[2] = { { .iov_base = (char *)logger, .iov_len = strlen(logger) }, { .iov_base = "\n", .iov_len = 1 } } ; + memcpy(fn + dirlen + 1, "producer-for", 13) ; + if (!openwritevnclose_unsafe(fn, v, 2)) strerr_diefu2sys(111, "write to ", fn) ; + } + } + else + { + if (chmod(fn, m | ((m >> 2) & 0111)) < 0) + strerr_diefu2sys(111, "chmod ", fn) ; + if (!(m & 0400)) + strerr_warnw2x("weird umask, check permissions manually on ", fn) ; + } +} |