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 | |
parent | 977d6ca915355954ae6215b0aa577c24fc257efe (diff) | |
download | s6-2c61fb1d1ce85fe400a8668e85fc0da42813a45a.tar.xz |
Refactor libs6auto, add s6-instance-maker (untested)
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libs6')
-rw-r--r-- | src/libs6/deps-lib/s6auto | 4 | ||||
-rw-r--r-- | src/libs6/s6_auto_write_logger.c | 10 | ||||
-rw-r--r-- | src/libs6/s6_auto_write_logger_tmp.c | 53 | ||||
-rw-r--r-- | src/libs6/s6_auto_write_logrun.c | 56 | ||||
-rw-r--r-- | src/libs6/s6_auto_write_logrun_tmp.c | 68 | ||||
-rw-r--r-- | src/libs6/s6_auto_write_service.c | 59 | ||||
-rw-r--r-- | src/libs6/s6_servicedir_file_list.c | 2 |
7 files changed, 198 insertions, 54 deletions
diff --git a/src/libs6/deps-lib/s6auto b/src/libs6/deps-lib/s6auto index a9fc8c9..df59808 100644 --- a/src/libs6/deps-lib/s6auto +++ b/src/libs6/deps-lib/s6auto @@ -1 +1,5 @@ +s6_auto_write_logger.o +s6_auto_write_logger_tmp.o s6_auto_write_logrun.o +s6_auto_write_logrun_tmp.o +s6_auto_write_service.o diff --git a/src/libs6/s6_auto_write_logger.c b/src/libs6/s6_auto_write_logger.c new file mode 100644 index 0000000..25c463a --- /dev/null +++ b/src/libs6/s6_auto_write_logger.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <skalibs/skamisc.h> + +#include <s6/auto.h> + +void s6_auto_write_logger (char const *dir, char const *loguser, char const *logdir, unsigned int stamptype, unsigned int nfiles, uint64_t filesize, uint64_t maxsize, char const *prefix, char const *service, char const *pipelinename) +{ + return s6_auto_write_logger_tmp(dir, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, service, pipelinename, &satmp) ; +} diff --git a/src/libs6/s6_auto_write_logger_tmp.c b/src/libs6/s6_auto_write_logger_tmp.c new file mode 100644 index 0000000..03d6aee --- /dev/null +++ b/src/libs6/s6_auto_write_logger_tmp.c @@ -0,0 +1,53 @@ +/* ISC license. */ + +#include <unistd.h> +#include <string.h> +#include <sys/uio.h> +#include <sys/stat.h> + +#include <skalibs/uint64.h> +#include <skalibs/strerr.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> + +#include <s6/auto.h> + +void s6_auto_write_logger_tmp (char const *dir, char const *loguser, char const *logdir, unsigned int stamptype, unsigned int nfiles, uint64_t filesize, uint64_t maxsize, char const *prefix, char const *service, char const *pipelinename, stralloc *sa) +{ + mode_t m = umask(0) ; + size_t dirlen = strlen(dir) ; + char fn[dirlen + 17] ; + memcpy(fn, dir, dirlen) ; + memcpy(fn + dirlen, "/notification-fd", 17) ; + if (mkdir(dir, 0755) == -1) strerr_diefu2sys(111, "mkdir ", dir) ; + umask(m) ; + m = ~m & 0666 ; + if (!openwritenclose_unsafe(fn, "3\n", 2)) goto err ; + memcpy(fn + dirlen + 1, "run", 4) ; + if (!s6_auto_write_logrun_tmp(fn, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, sa)) goto err ; + if (service) + { + struct iovec v[2] = { { .iov_base = (char *)service, .iov_len = strlen(service) }, { .iov_base = "\n", .iov_len = 1 } } ; + memcpy(fn + dirlen + 1, "type", 5) ; + if (!openwritenclose_unsafe(fn, "longrun\n", 8)) goto err ; + memcpy(fn + dirlen + 1, "consumer-for", 13) ; + if (!openwritevnclose_unsafe(fn, v, 2)) goto err ; + if (pipelinename) + { + v[0].iov_base = (char *)pipelinename ; + v[0].iov_len = strlen(pipelinename) ; + memcpy(fn + dirlen + 1, "pipeline-name", 14) ; + if (!openwritevnclose_unsafe(fn, v, 2)) goto err ; + } + } + else + { + if (chmod(fn, m | ((m >> 2) & 0111)) == -1) + strerr_diefu2sys(111, "chmod ", fn) ; + if (!(m & 0400)) + strerr_warnw2x("weird umask, check permissions manually on ", fn) ; + } + return ; + err: + strerr_diefu2sys(111, "write to ", fn) ; +} diff --git a/src/libs6/s6_auto_write_logrun.c b/src/libs6/s6_auto_write_logrun.c index f5c4baf..d909500 100644 --- a/src/libs6/s6_auto_write_logrun.c +++ b/src/libs6/s6_auto_write_logrun.c @@ -1,62 +1,10 @@ /* ISC license. */ -#include <string.h> - -#include <skalibs/posixplz.h> -#include <skalibs/uint64.h> -#include <skalibs/types.h> -#include <skalibs/buffer.h> -#include <skalibs/stralloc.h> -#include <skalibs/djbunix.h> #include <skalibs/skamisc.h> -#include <execline/config.h> - -#include <s6/config.h> #include <s6/auto.h> -int s6_auto_write_logrun (char const *runfile, char const *loguser, char const *logdir, unsigned int stamptype, unsigned int nfiles, uint64_t filesize, uint64_t maxsize, stralloc *sa) +int s6_auto_write_logrun (char const *runfile, char const *loguser, char const *logdir, unsigned int stamptype, unsigned int nfiles, uint64_t filesize, uint64_t maxsize, char const *prefix) { - buffer b ; - char buf[1024] ; - char fmt[UINT64_FMT] ; - int fd = open_trunc(runfile) ; - if (fd < 0) return 0 ; - buffer_init(&b, &buffer_write, fd, buf, 1024) ; - if (buffer_puts(&b, "#!" EXECLINE_SHEBANGPREFIX "execlineb -P\n") < 0) goto err ; - if (loguser) - { - if (buffer_puts(&b, S6_EXTBINPREFIX "s6-setuidgid ") < 0 - || !string_quote(sa, loguser, strlen(loguser)) - || buffer_put(&b, sa->s, sa->len) < 0 - || buffer_put(&b, "\n", 1) < 0) goto err ; - sa->len = 0 ; - } - if (buffer_puts(&b, S6_EXTBINPREFIX "s6-log -bd3 -- ") < 0 - || (stamptype & 1 && buffer_put(&b, "t ", 2) < 0) - || (stamptype & 2 && buffer_put(&b, "T ", 2) < 0) - || buffer_put(&b, "n", 1) < 0 - || buffer_put(&b, fmt, uint_fmt(fmt, nfiles)) < 0 - || buffer_put(&b, " s", 2) < 0 - || buffer_put(&b, fmt, uint64_fmt(fmt, filesize)) < 0 - || buffer_put(&b, " ", 1) < 0) goto err ; - if (maxsize) - { - if (buffer_put(&b, "S", 1) < 0 - || buffer_put(&b, fmt, uint64_fmt(fmt, maxsize)) < 0 - || buffer_put(&b, " ", 1) < 0) goto err ; - } - if (!string_quote(sa, logdir, strlen(logdir)) - || buffer_put(&b, sa->s, sa->len) < 0 - || buffer_put(&b, "\n", 1) < 0) goto err ; - sa->len = 0 ; - - if (!buffer_flush(&b)) goto err ; - fd_close(fd) ; - return 1 ; - - err: - fd_close(fd) ; - unlink_void(runfile) ; - return 0 ; + return s6_auto_write_logrun_tmp(runfile, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, &satmp) ; } diff --git a/src/libs6/s6_auto_write_logrun_tmp.c b/src/libs6/s6_auto_write_logrun_tmp.c new file mode 100644 index 0000000..24163fb --- /dev/null +++ b/src/libs6/s6_auto_write_logrun_tmp.c @@ -0,0 +1,68 @@ +/* ISC license. */ + +#include <string.h> + +#include <skalibs/posixplz.h> +#include <skalibs/uint64.h> +#include <skalibs/types.h> +#include <skalibs/buffer.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> +#include <skalibs/skamisc.h> + +#include <execline/config.h> + +#include <s6/config.h> +#include <s6/auto.h> + +int s6_auto_write_logrun_tmp (char const *runfile, char const *loguser, char const *logdir, unsigned int stamptype, unsigned int nfiles, uint64_t filesize, uint64_t maxsize, char const *prefix, stralloc *sa) +{ + buffer b ; + char buf[1024] ; + char fmt[UINT64_FMT] ; + int fd = open_trunc(runfile) ; + if (fd < 0) return 0 ; + buffer_init(&b, &buffer_write, fd, buf, 1024) ; + if (buffer_puts(&b, "#!" EXECLINE_SHEBANGPREFIX "execlineb -P\n") < 0) goto err ; + if (loguser) + { + if (buffer_puts(&b, S6_EXTBINPREFIX "s6-setuidgid ") < 0 + || !string_quote(sa, loguser, strlen(loguser)) + || buffer_put(&b, sa->s, sa->len) < 0 + || buffer_put(&b, "\n", 1) < 0) goto err ; + sa->len = 0 ; + } + if (buffer_puts(&b, S6_EXTBINPREFIX "s6-log -bd3 -- ") < 0 + || (stamptype & 1 && buffer_put(&b, "t ", 2) < 0) + || (stamptype & 2 && buffer_put(&b, "T ", 2) < 0) + || buffer_put(&b, "n", 1) < 0 + || buffer_put(&b, fmt, uint_fmt(fmt, nfiles)) < 0 + || buffer_put(&b, " s", 2) < 0 + || buffer_put(&b, fmt, uint64_fmt(fmt, filesize)) < 0 + || buffer_put(&b, " ", 1) < 0) goto err ; + if (maxsize) + { + if (buffer_put(&b, "S", 1) < 0 + || buffer_put(&b, fmt, uint64_fmt(fmt, maxsize)) < 0 + || buffer_put(&b, " ", 1) < 0) goto err ; + } + if (prefix) + { + if (buffer_put(&b, "p", 1) < 0 + || buffer_puts(&b, prefix) < 0 + || buffer_put(&b, " ", 1) < 0) goto err ; + } + if (!string_quote(sa, logdir, strlen(logdir)) + || buffer_put(&b, sa->s, sa->len) < 0 + || buffer_put(&b, "\n", 1) < 0) goto err ; + sa->len = 0 ; + + if (!buffer_flush(&b)) goto err ; + fd_close(fd) ; + return 1 ; + + err: + fd_close(fd) ; + unlink_void(runfile) ; + return 0 ; +} 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) ; + } +} diff --git a/src/libs6/s6_servicedir_file_list.c b/src/libs6/s6_servicedir_file_list.c index bf55d0d..b01a59f 100644 --- a/src/libs6/s6_servicedir_file_list.c +++ b/src/libs6/s6_servicedir_file_list.c @@ -12,6 +12,8 @@ static s6_servicedir_desc const s6_servicedir_file_list_[] = { .name = "timeout-finish", .type = S6_FILETYPE_UINT, .options = 0 }, { .name = "max-death-tally", .type = S6_FILETYPE_UINT, .options = 0 }, { .name = "down-signal", .type = S6_FILETYPE_NORMAL, .options = 0 }, + { .name = "instance", .type = S6_FILETYPE_DIR, .options = 0 }, + { .name = "instances", .type = S6_FILETYPE_DIR, .options = 0 }, { .name = "data", .type = S6_FILETYPE_DIR, .options = 0 }, { .name = "env", .type = S6_FILETYPE_DIR, .options = 0 }, { .name = 0, .options = 0 } |