From 2c61fb1d1ce85fe400a8668e85fc0da42813a45a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 23 Dec 2022 22:53:07 +0000 Subject: Refactor libs6auto, add s6-instance-maker (untested) Signed-off-by: Laurent Bercot --- src/daemontools-extras/s6-setuidgid.c | 3 +- src/include/s6/auto.h | 12 +- src/instance/deps-exe/s6-instance-maker | 2 + src/instance/s6-instance-maker.c | 181 ++++++++++++++++++++++++++ src/libs6/deps-lib/s6auto | 4 + src/libs6/s6_auto_write_logger.c | 10 ++ src/libs6/s6_auto_write_logger_tmp.c | 53 ++++++++ src/libs6/s6_auto_write_logrun.c | 56 +------- src/libs6/s6_auto_write_logrun_tmp.c | 68 ++++++++++ src/libs6/s6_auto_write_service.c | 59 +++++++++ src/libs6/s6_servicedir_file_list.c | 2 + src/usertree/s6-usertree-maker.c | 219 ++++++++++++-------------------- 12 files changed, 477 insertions(+), 192 deletions(-) create mode 100644 src/instance/deps-exe/s6-instance-maker create mode 100644 src/instance/s6-instance-maker.c create mode 100644 src/libs6/s6_auto_write_logger.c create mode 100644 src/libs6/s6_auto_write_logger_tmp.c create mode 100644 src/libs6/s6_auto_write_logrun_tmp.c create mode 100644 src/libs6/s6_auto_write_service.c (limited to 'src') diff --git a/src/daemontools-extras/s6-setuidgid.c b/src/daemontools-extras/s6-setuidgid.c index 26256b8..a1afc66 100644 --- a/src/daemontools-extras/s6-setuidgid.c +++ b/src/daemontools-extras/s6-setuidgid.c @@ -10,7 +10,7 @@ #define USAGE "s6-setuidgid username prog..." #define dieusage() strerr_dieusage(100, USAGE) -int main (int argc, char *const *argv) +int main (int argc, char const *const *argv) { char const *newargv[argc + 7] ; char *colon ; @@ -18,6 +18,7 @@ int main (int argc, char *const *argv) PROG = "s6-setuidgid" ; if (argc < 3) dieusage() ; argv++ ; + if (!argv[0][0]) xexec(argv+1) ; colon = strchr(argv[0], ':') ; if (colon) { diff --git a/src/include/s6/auto.h b/src/include/s6/auto.h index b8faff6..cab4b80 100644 --- a/src/include/s6/auto.h +++ b/src/include/s6/auto.h @@ -4,8 +4,18 @@ #define S6_AUTO_H #include +#include #include -int s6_auto_write_logrun (char const *, char const *, char const *, unsigned int, unsigned int, uint64_t, uint64_t, stralloc *) ; +typedef int s6_buffer_writer_func (buffer *, void *) ; +typedef s6_buffer_writer_func *s6_buffer_writer_func_ref ; + +extern int s6_auto_write_logrun (char const *, char const *, char const *, unsigned int, unsigned int, uint64_t, uint64_t, char const *) ; +extern int s6_auto_write_logrun_tmp (char const *, char const *, char const *, unsigned int, unsigned int, uint64_t, uint64_t, char const *, stralloc *) ; + +extern void s6_auto_write_logger (char const *, char const *, char const *, unsigned int, unsigned int, uint64_t, uint64_t, char const *, char const *, char const *) ; +extern void s6_auto_write_logger_tmp (char const *, char const *, char const *, unsigned int, unsigned int, uint64_t, uint64_t, char const *, char const *, char const *, stralloc *sa) ; + +extern void s6_auto_write_service (char const *, unsigned int, s6_buffer_writer_func_ref, void *, char const *) ; #endif diff --git a/src/instance/deps-exe/s6-instance-maker b/src/instance/deps-exe/s6-instance-maker new file mode 100644 index 0000000..750e758 --- /dev/null +++ b/src/instance/deps-exe/s6-instance-maker @@ -0,0 +1,2 @@ +libs6auto.a.xyzzy +-lskarnet diff --git a/src/instance/s6-instance-maker.c b/src/instance/s6-instance-maker.c new file mode 100644 index 0000000..20f557f --- /dev/null +++ b/src/instance/s6-instance-maker.c @@ -0,0 +1,181 @@ +/* ISC license. */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#define USAGE "s6-instance-maker [ -c max ] [ -u user ] [ -l loguser ] [ -L logdir ] [ -t stamptype ] [ -n nfiles ] [ -s filesize ] [ -S maxsize ] [ -P prefix ] [ -r service[/logger[/pipeline]] ] template dir" +#define dieusage() strerr_dieusage(100, USAGE) + +typedef struct svinfo_s svinfo, *svinfo_ref ; +struct svinfo_s +{ + char const *user ; + unsigned int maxinstances ; +} ; + +static stralloc sa = STRALLOC_ZERO ; + +static int write_run (buffer *b, void *data) +{ + svinfo *t = data ; + size_t l ; + char fmt[UINT_FMT] ; + l = uint_fmt(fmt, t->maxinstances) ; + if (buffer_puts(b, EXECLINE_EXTBINPREFIX "execlineb -S1\n\n" + EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n") < 0) return 0 ; + if (t->user) + { + if (!string_quote(&sa, t->user, strlen(t->user))) return 0 ; + if (buffer_puts(b, S6_EXTBINPREFIX "s6-setuidgid ") < 0 + || 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-svscan -d3 -c") < 0 + || buffer_put(b, fmt, l) < 0 + || buffer_putsflush(b, " -- instance\n") < 0) return 0 ; + return 1 ; + +err: + sa.len = 0 ; + return 0 ; +} + +static void write_service (char const *dir, char const *template, char const *user, unsigned int maxinstances, char const *logger) +{ + svinfo data = { .user = user, .maxinstances = maxinstances } ; + size_t dirlen = strlen(dir) ; + mode_t m = umask(0) ; + char fn[dirlen + 21] ; + memcpy(fn, dir, dirlen) ; + memcpy(fn + dirlen, "/instance", 10) ; + m = umask(0) ; + if (mkdir(fn, 0755) == -1) strerr_diefu2sys(111, "mkdir ", fn) ; + memcpy(fn + dirlen + 9, "s", 2) ; + if (mkdir(fn, 0755) == -1) strerr_diefu2sys(111, "mkdir ", fn) ; + umask(m) ; + memcpy(fn + dirlen + 10, "/.template", 11) ; + if (!hiercopy_tmp(template, fn, &sa)) + strerr_diefu4sys(111, "copy file hierarchy from ", template, " to ", fn) ; + s6_auto_write_service(dir, 3, &write_run, &data, logger) ; +} + +int main (int argc, char const *const *argv) +{ + char const *user = "" ; + char const *loguser = 0 ; + char const *logdir = 0 ; + unsigned int maxinstances = 500 ; + unsigned int stamptype = 1 ; + unsigned int nfiles = 10 ; + uint64_t filesize = 1000000 ; + uint64_t maxsize = 0 ; + char const *prefix = 0 ; + char *rcinfo[3] = { 0, 0, 0 } ; + size_t dirlen ; + PROG = "s6-instance-maker" ; + { + subgetopt l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "u:l:L:c:t:n:s:S:P:r:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'u' : user = l.arg ; break ; + case 'l' : loguser = l.arg ; break ; + case 'L' : logdir = l.arg ; break ; + case 'c' : if (!uint0_scan(l.arg, &maxinstances)) dieusage() ; break ; + case 't' : if (!uint0_scan(l.arg, &stamptype)) dieusage() ; break ; + case 'n' : if (!uint0_scan(l.arg, &nfiles)) dieusage() ; break ; + case 's' : if (!uint640_scan(l.arg, &filesize)) dieusage() ; break ; + case 'S' : if (!uint640_scan(l.arg, &maxsize)) dieusage() ; break ; + case 'P' : prefix = l.arg ; break ; + case 'r' : rcinfo[0] = (char *)l.arg ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (argc < 2) dieusage() ; + if (logdir && logdir[0] != '/') + strerr_dief1x(100, "logdir must be absolute") ; + if (stamptype > 3) strerr_dief1x(100, "stamptype must be 0, 1, 2 or 3") ; + if (strchr(user, ' ') || strchr(user, '\t') || strchr(user, '\n')) + strerr_dief1x(100, "invalid user") ; + if (maxinstances < 1) maxinstances = 1 ; + if (maxinstances > 90000) maxinstances = 90000 ; + if (rcinfo[0]) + { + if (strchr(rcinfo[0], '\n')) + strerr_dief2x(100, "newlines", " are forbidden in s6-rc names") ; + if (rcinfo[0][0] == '/') + strerr_dief2x(100, "service", " name cannot be empty") ; + rcinfo[1] = strchr(rcinfo[0], '/') ; + if (rcinfo[1]) + { + *rcinfo[1]++ = 0 ; + if (!rcinfo[1][0]) strerr_dief1x(100, "argument to -r must be: service or service/logger or service/logger/pipeline") ; + if (rcinfo[1][0] == '/') + strerr_dief2x(100, "logger", " name cannot be empty") ; + if (!logdir) strerr_dief1x(100, "logger specifiec (-r) but logdir not specified (-L)") ; + rcinfo[2] = strchr(rcinfo[1], '/') ; + if (rcinfo[2]) + { + *rcinfo[2]++ = 0 ; + if (!rcinfo[2][0]) strerr_dief2x(100, "pipeline", " name cannot be empty") ; + if (strchr(rcinfo[2], '/')) strerr_dief2x(100, "slashes", " are forbidden in s6-rc names") ; + } + } + else if (logdir) + strerr_dief1x(100, "logdir specified (-L) but logger name not specified (-r)") ; + } + + dirlen = strlen(argv[1]) ; + if (rcinfo[0]) + { + size_t svclen = strlen(rcinfo[0]) ; + size_t loglen = rcinfo[1] ? strlen(rcinfo[1]) : 0 ; + char dir[dirlen + 2 + (svclen > loglen ? svclen : loglen)] ; + memcpy(dir, argv[1], dirlen) ; + dir[dirlen] = '/' ; + if (mkdir(argv[1], 0755) < 0 && errno != EEXIST) + strerr_diefu2sys(111, "mkdir ", argv[2]) ; + memcpy(dir + dirlen + 1, rcinfo[0], svclen + 1) ; + write_service(dir, user, argv[0], maxinstances, rcinfo[1] ? rcinfo[1] : "") ; + if (rcinfo[1]) + { + memcpy(dir + dirlen + 1, rcinfo[1], loglen + 1) ; + s6_auto_write_logger_tmp(dir, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, rcinfo[0], rcinfo[2], &sa) ; + } + } + else + { + write_service(argv[1], user, argv[0], maxinstances, 0) ; + if (logdir) + { + char dir[dirlen + 5] ; + memcpy(dir, argv[1], dirlen) ; + memcpy(dir + dirlen, "/log", 5) ; + s6_auto_write_logger_tmp(dir, loguser, logdir, stamptype, nfiles, filesize, maxsize, prefix, 0, 0, &sa) ; + } + } + return 0 ; +} 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 + +#include + +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 +#include +#include +#include + +#include +#include +#include +#include + +#include + +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 - -#include -#include -#include -#include -#include -#include #include -#include - -#include #include -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 + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +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 +#include +#include +#include + +#include +#include +#include +#include + +#include + +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 } diff --git a/src/usertree/s6-usertree-maker.c b/src/usertree/s6-usertree-maker.c index 3edbd8b..f8740aa 100644 --- a/src/usertree/s6-usertree-maker.c +++ b/src/usertree/s6-usertree-maker.c @@ -22,186 +22,139 @@ #include #include -#define USAGE "s6-usertree-maker [ -d userscandir ] [ -p path ] [ -E envdir [ -e var ... ] ] [ -r service/logger[/pipeline] ] [ -l loguser ] [ -t stamptype ] [ -n nfiles ] [ -s filesize ] [ -S maxsize ] user logdir dir" +#define USAGE "s6-usertree-maker [ -d userscandir ] [ -p path ] [ -E envdir [ -e var ... ] ] [ -r service/logger[/pipeline] ] [ -l loguser ] [ -t stamptype ] [ -n nfiles ] [ -s filesize ] [ -S maxsize ] [ -P prefix ] user logdir dir" #define dieusage() strerr_dieusage(100, USAGE) #define VARS_MAX 64 -static mode_t mask ; static stralloc sa = STRALLOC_ZERO ; -static inline void write_run (char const *runfile, char const *user, char const *sc, char const *path, char const *userenvdir, char const *const *vars, size_t varlen) +typedef struct svinfo_s svinfo, *svinfo_ref ; +struct svinfo_s { - buffer b ; - char buf[2048] ; - int fd = open_trunc(runfile) ; - if (fd < 0) strerr_diefu3sys(111, "open ", runfile, " for writing") ; - buffer_init(&b, &buffer_write, fd, buf, 2048) ; - if (!string_quote(&sa, user, strlen(user))) goto errq ; - if (buffer_puts(&b, "#!" EXECLINE_SHEBANGPREFIX "execlineb -P\n" + char const *user ; + char const *sc ; + char const *logger ; + char const *path ; + char const *userenvdir ; + char const **vars ; + size_t varlen ; +} ; + +static int write_run (buffer *b, void *data) +{ + svinfo *t = data ; + if (!string_quote(&sa, t->user, strlen(t->user))) return 0 ; + if (buffer_puts(b, "#!" EXECLINE_SHEBANGPREFIX "execlineb -P\n" EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n" EXECLINE_EXTBINPREFIX "emptyenv -p\n" EXECLINE_EXTBINPREFIX "export USER ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_puts(&b, "\n" + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_puts(b, "\n" S6_EXTBINPREFIX "s6-envuidgid -i -- ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_puts(&b, "\n" + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_puts(b, "\n" S6_EXTBINPREFIX "s6-applyuidgid -U --\n" EXECLINE_EXTBINPREFIX "backtick -in HOME { " EXECLINE_EXTBINPREFIX "homeof ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_put(&b, " }\n", 3) < 0) goto err ; + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_put(b, " }\n", 3) < 0) goto err ; sa.len = 0 ; - if (userenvdir) + if (t->userenvdir) { - if (!string_quote(&sa, userenvdir, strlen(userenvdir))) goto errq ; - if (buffer_puts(&b, S6_EXTBINPREFIX "s6-envdir -i -- ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_put(&b, "\n", 1) < 0) goto err ; + if (!string_quote(&sa, t->userenvdir, strlen(t->userenvdir))) return 0 ; + if (buffer_puts(b, S6_EXTBINPREFIX "s6-envdir -i -- ") < 0 + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_put(b, "\n", 1) < 0) goto err ; sa.len = 0 ; - if (varlen) + if (t->varlen) { - if (buffer_puts(&b, EXECLINE_EXTBINPREFIX "multisubstitute\n{\n") < 0) goto err ; - for (size_t i = 0 ; i < varlen ; i++) + if (buffer_puts(b, EXECLINE_EXTBINPREFIX "multisubstitute\n{\n") < 0) return 0 ; + for (size_t i = 0 ; i < t->varlen ; i++) { - if (!string_quote(&sa, vars[i], strlen(vars[i]))) goto errq ; - if (buffer_puts(&b, " importas -D \"\" -- ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_put(&b, " ", 1) < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_put(&b, "\n", 1) < 0) goto err ; + if (!string_quote(&sa, t->vars[i], strlen(t->vars[i]))) return 0 ; + if (buffer_puts(b, " importas -D \"\" -- ") < 0 + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_put(b, " ", 1) < 0 + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_put(b, "\n", 1) < 0) goto err ; sa.len = 0 ; } - if (buffer_put(&b, "}\n", 2) < 0) goto err ; + if (buffer_put(b, "}\n", 2) < 0) return 0 ; } } - if (buffer_puts(&b, EXECLINE_EXTBINPREFIX "multisubstitute\n{\n" + if (buffer_puts(b, EXECLINE_EXTBINPREFIX "multisubstitute\n{\n" " importas -i USER USER\n" " importas -i HOME HOME\n" " importas -i UID UID\n" " importas -i GID GID\n" - " importas -i GIDLIST GIDLIST\n}\n") < 0) goto err ; - if (userenvdir && varlen) + " importas -i GIDLIST GIDLIST\n}\n") < 0) return 0 ; + if (t->userenvdir && t->varlen) { - for (size_t i = 0 ; i < varlen ; i++) + for (size_t i = 0 ; i < t->varlen ; i++) { - if (!string_quote(&sa, vars[i], strlen(vars[i]))) goto errq ; - if (buffer_puts(&b, EXECLINE_EXTBINPREFIX "export ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_put(&b, " ${", 3) < 0 - || buffer_put(&b, sa.s, sa.len) < 0 - || buffer_put(&b, "}\n", 2) < 0) goto err ; + if (!string_quote(&sa, t->vars[i], strlen(t->vars[i]))) return 0 ; + if (buffer_puts(b, EXECLINE_EXTBINPREFIX "export ") < 0 + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_put(b, " ${", 3) < 0 + || buffer_put(b, sa.s, sa.len) < 0 + || buffer_put(b, "}\n", 2) < 0) goto err ; sa.len = 0 ; } } - if (!string_quote(&sa, path, strlen(path))) goto errq ; - if (buffer_puts(&b, EXECLINE_EXTBINPREFIX "export PATH ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0) goto err ; + if (!string_quote(&sa, t->path, strlen(t->path))) return 0 ; + if (buffer_puts(b, EXECLINE_EXTBINPREFIX "export PATH ") < 0 + || buffer_put(b, sa.s, sa.len) < 0) goto err ; sa.len = 0 ; - if (!string_quote(&sa, sc, strlen(sc))) goto errq ; - if (buffer_puts(&b, "\n" + if (!string_quote(&sa, t->sc, strlen(t->sc))) return 0 ; + if (buffer_puts(b, "\n" S6_EXTBINPREFIX "s6-svscan -d3 -- ") < 0 - || buffer_put(&b, sa.s, sa.len) < 0) goto err ; + || buffer_put(b, sa.s, sa.len) < 0) goto err ; sa.len = 0 ; - if (!buffer_putflush(&b, "\n", 1)) goto err ; - fd_close(fd) ; - return ; - err: - strerr_diefu2sys(111, "write to ", runfile) ; - errq: - strerr_diefu1sys(111, "quote string") ; -} + if (!buffer_putflush(b, "\n", 1)) return 0 ; + return 1 ; -static void write_service (char const *dir, char const *user, char const *sc, char const *logger, char const *path, char const *userenvdir, char const *const *vars, size_t varlen) -{ - size_t dirlen = strlen(dir) ; - char fn[dirlen + 17] ; - memcpy(fn, dir, dirlen) ; - memcpy(fn + dirlen, "/notification-fd", 17) ; - if (!openwritenclose_unsafe(fn, "3\n", 2)) strerr_diefu2sys(111, "write to ", fn) ; - memcpy(fn + dirlen + 1, "run", 4) ; - write_run(fn, user, sc, path, userenvdir, vars, varlen) ; - if (logger) - { - struct iovec v[2] = { { .iov_base = (char *)logger, .iov_len = strlen(logger) }, { .iov_base = "\n", .iov_len = 1 } } ; - memcpy(fn + dirlen + 1, "type", 5) ; - if (!openwritenclose_unsafe(fn, "longrun\n", 8)) strerr_diefu2sys(111, "write to ", fn) ; - memcpy(fn + dirlen + 1, "producer-for", 13) ; - if (!openwritevnclose_unsafe(fn, v, 2)) strerr_diefu2sys(111, "write to ", fn) ; - } - else - { - if (chmod(fn, mask | ((mask >> 2) & 0111)) < 0) - strerr_diefu2sys(111, "chmod ", fn) ; - } -} - -static void write_logger (char const *dir, char const *user, char const *logdir, unsigned int stamptype, unsigned int nfiles, uint64_t filesize, uint64_t maxsize, char const *service, char const *pipelinename) -{ - size_t dirlen = strlen(dir) ; - char fn[dirlen + 17] ; - memcpy(fn, dir, dirlen) ; - memcpy(fn + dirlen, "/notification-fd", 17) ; - if (!openwritenclose_unsafe(fn, "3\n", 2)) goto err ; - memcpy(fn + dirlen + 1, "run", 4) ; - if (!s6_auto_write_logrun(fn, user, logdir, stamptype, nfiles, filesize, maxsize, &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, mask | ((mask >> 2) & 0111)) < 0) - strerr_diefu2sys(111, "chmod ", fn) ; - } - return ; err: - strerr_diefu2sys(111, "write to ", fn) ; + sa.len = 0 ; + return 0 ; } int main (int argc, char *const *argv) { - char const *userscandir = "${HOME}/service" ; - char const *path = SKALIBS_DEFAULTPATH ; - char const *userenvdir = 0 ; + char const *vars[VARS_MAX] ; + svinfo t = + { + .sc = "${HOME}/service", + .logger = 0, + .path = SKALIBS_DEFAULTPATH, + .userenvdir = 0, + .vars = vars, + .varlen = 0 + } ; char *rcinfo[3] = { 0, 0, 0 } ; char const *loguser = 0 ; unsigned int stamptype = 1 ; unsigned int nfiles = 10 ; uint64_t filesize = 1000000 ; uint64_t maxsize = 0 ; + char const *prefix = 0 ; size_t dirlen ; - size_t varlen = 0 ; - mode_t m ; - char const *vars[VARS_MAX] ; PROG = "s6-usertree-maker" ; { subgetopt l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, (char const *const *)argv, "d:p:E:e:r:l:t:n:s:S:", &l) ; + int opt = subgetopt_r(argc, (char const *const *)argv, "d:p:E:e:r:l:t:n:s:S:P:", &l) ; if (opt == -1) break ; switch (opt) { - case 'd' : userscandir = l.arg ; break ; - case 'p' : path = l.arg ; break ; - case 'E' : userenvdir = l.arg ; break ; + case 'd' : t.sc = l.arg ; break ; + case 'p' : t.path = l.arg ; break ; + case 'E' : t.userenvdir = l.arg ; break ; case 'e' : - if (varlen >= VARS_MAX) strerr_dief1x(100, "too many -v variables") ; + if (t.varlen >= VARS_MAX) strerr_dief1x(100, "too many -v variables") ; if (strchr(l.arg, '=')) strerr_dief2x(100, "invalid variable name: ", l.arg) ; - vars[varlen++] = l.arg ; + t.vars[t.varlen++] = l.arg ; break ; case 'r' : rcinfo[0] = (char *)l.arg ; break ; case 'l' : loguser = l.arg ; break ; @@ -209,6 +162,7 @@ int main (int argc, char *const *argv) case 'n' : if (!uint0_scan(l.arg, &nfiles)) dieusage() ; break ; case 's' : if (!uint640_scan(l.arg, &filesize)) dieusage() ; break ; case 'S' : if (!uint640_scan(l.arg, &maxsize)) dieusage() ; break ; + case 'P' : prefix = l.arg ; break ; default : dieusage() ; } } @@ -216,7 +170,7 @@ int main (int argc, char *const *argv) } if (argc < 3) dieusage() ; if (argv[1][0] != '/') strerr_dief1x(100, "logdir must be absolute") ; - if (userscandir[0] != '/' && !str_start(userscandir, "${HOME}/")) + if (t.sc[0] != '/' && !str_start(t.sc, "${HOME}/")) strerr_dief1x(100, "userscandir must be absolute or start with ${HOME}/") ; if (stamptype > 3) strerr_dief1x(100, "stamptype must be 0, 1, 2 or 3") ; if (rcinfo[0]) @@ -239,8 +193,7 @@ int main (int argc, char *const *argv) if (strchr(rcinfo[2], '/')) strerr_dief2x(100, "slashes", " are forbidden in s6-rc names") ; } } - m = umask(0) ; - mask = ~m & 0666 ; + t.user = argv[0] ; dirlen = strlen(argv[2]) ; if (rcinfo[0]) @@ -252,25 +205,19 @@ int main (int argc, char *const *argv) dir[dirlen] = '/' ; if (mkdir(argv[2], 0755) < 0 && errno != EEXIST) strerr_diefu2sys(111, "mkdir ", argv[2]) ; + t.logger = rcinfo[1] ; memcpy(dir + dirlen + 1, rcinfo[0], svclen + 1) ; - if (mkdir(dir, 0755) < 0) strerr_diefu2sys(111, "mkdir ", dir) ; + s6_auto_write_service(dir, 3, &write_run, &t, rcinfo[1]) ; memcpy(dir + dirlen + 1, rcinfo[1], loglen + 1) ; - if (mkdir(dir, 0755) < 0) strerr_diefu2sys(111, "mkdir ", dir) ; - umask(m) ; - write_logger(dir, loguser, argv[1], stamptype, nfiles, filesize, maxsize, rcinfo[0], rcinfo[2]) ; - memcpy(dir + dirlen + 1, rcinfo[0], svclen + 1) ; - write_service(dir, argv[0], userscandir, rcinfo[1], path, userenvdir, vars, varlen) ; + s6_auto_write_logger_tmp(dir, loguser, argv[1], stamptype, nfiles, filesize, maxsize, prefix, rcinfo[0], rcinfo[2], &sa) ; } else { char dir[dirlen + 5] ; memcpy(dir, argv[2], dirlen) ; memcpy(dir + dirlen, "/log", 5) ; - if (mkdir(argv[2], 0755) < 0) strerr_diefu2sys(111, "mkdir ", argv[2]) ; - if (mkdir(dir, 0755) < 0) strerr_diefu2sys(111, "mkdir ", argv[2]) ; - umask(m) ; - write_service(argv[2], argv[0], userscandir, 0, path, userenvdir, vars, varlen) ; - write_logger(dir, loguser, argv[1], stamptype, nfiles, filesize, maxsize, 0, 0) ; + s6_auto_write_service(argv[2], 3, &write_run, &t, 0) ; + s6_auto_write_logger_tmp(dir, loguser, argv[1], stamptype, nfiles, filesize, maxsize, prefix, 0, 0, &sa) ; } return 0 ; } -- cgit v1.2.3