diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2022-08-20 09:20:56 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2022-08-20 09:20:56 +0000 |
commit | 28f5b4e7a5cce43198269f26583e66a232a976b5 (patch) | |
tree | a9fb8a49920db08193aa7ad2d88d533b8e1ea141 /src/libs6 | |
parent | 632acb58b77d08a384ada611b5ab1ebca96ff85d (diff) | |
download | s6-28f5b4e7a5cce43198269f26583e66a232a976b5.tar.xz |
Factor s6_auto_write_logrun into a new libs6auto
libs6auto distinct from libs6 because of execline dep.
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libs6')
-rw-r--r-- | src/libs6/deps-lib/s6auto | 1 | ||||
-rw-r--r-- | src/libs6/s6_auto_write_logrun.c | 62 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/libs6/deps-lib/s6auto b/src/libs6/deps-lib/s6auto new file mode 100644 index 0000000..a9fc8c9 --- /dev/null +++ b/src/libs6/deps-lib/s6auto @@ -0,0 +1 @@ +s6_auto_write_logrun.o diff --git a/src/libs6/s6_auto_write_logrun.c b/src/libs6/s6_auto_write_logrun.c new file mode 100644 index 0000000..f5c4baf --- /dev/null +++ b/src/libs6/s6_auto_write_logrun.c @@ -0,0 +1,62 @@ +/* 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) +{ + 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 ; +} |