diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/instance/deps-exe/s6-instance-control | 1 | ||||
-rw-r--r-- | src/instance/deps-exe/s6-instance-create | 2 | ||||
-rw-r--r-- | src/instance/deps-exe/s6-instance-delete | 2 | ||||
-rw-r--r-- | src/instance/s6-instance-control.c | 79 | ||||
-rw-r--r-- | src/instance/s6-instance-create.c | 97 | ||||
-rw-r--r-- | src/instance/s6-instance-delete.c | 61 | ||||
-rw-r--r-- | src/instance/s6-instance-maker.c | 2 | ||||
-rw-r--r-- | src/libs6/s6_supervise_link_names.c | 3 | ||||
-rw-r--r-- | src/supervision/s6-svc.c | 45 |
9 files changed, 283 insertions, 9 deletions
diff --git a/src/instance/deps-exe/s6-instance-control b/src/instance/deps-exe/s6-instance-control new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/instance/deps-exe/s6-instance-control @@ -0,0 +1 @@ +-lskarnet diff --git a/src/instance/deps-exe/s6-instance-create b/src/instance/deps-exe/s6-instance-create new file mode 100644 index 0000000..08815d9 --- /dev/null +++ b/src/instance/deps-exe/s6-instance-create @@ -0,0 +1,2 @@ +${LIBS6} +-lskarnet diff --git a/src/instance/deps-exe/s6-instance-delete b/src/instance/deps-exe/s6-instance-delete new file mode 100644 index 0000000..08815d9 --- /dev/null +++ b/src/instance/deps-exe/s6-instance-delete @@ -0,0 +1,2 @@ +${LIBS6} +-lskarnet diff --git a/src/instance/s6-instance-control.c b/src/instance/s6-instance-control.c new file mode 100644 index 0000000..899ab89 --- /dev/null +++ b/src/instance/s6-instance-control.c @@ -0,0 +1,79 @@ +/* ISC license. */ + +#include <string.h> + +#include <skalibs/bytestr.h> +#include <skalibs/types.h> +#include <skalibs/strerr.h> +#include <skalibs/sgetopt.h> +#include <skalibs/exec.h> + +#include <s6/config.h> + +#define USAGE "s6-instance-control [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -abqhkti12pcyroduDUxOX ] service instance" +#define dieusage() strerr_dieusage(100, USAGE) + +#define DATASIZE 63 + +int main (int argc, char const **argv) +{ + char const **fullargv = argv ; + size_t namelen ; + PROG = "s6-instance-control" ; + + { + subgetopt l = SUBGETOPT_ZERO ; + unsigned int datalen = 1 ; + unsigned int timeout = 0 ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "abqhkti12pcyroduxOT:w:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'a' : + case 'b' : + case 'q' : + case 'h' : + case 'k' : + case 't' : + case 'i' : + case '1' : + case '2' : + case 'p' : + case 'c' : + case 'y' : + case 'r' : + case 'o' : + case 'd' : + case 'u' : + case 'D' : + case 'U' : + case 'x' : + case 'O' : if (datalen++ >= DATASIZE) strerr_dief1x(100, "too many commands") ; break ; + case 'T' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ; + case 'w' : if (!memchr("dDuUrR", l.arg[0], 6)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (argc < 2) dieusage() ; + namelen = strlen(argv[1]) ; + if (!argv[0][0]) strerr_dief1x(100, "invalid service name") ; + if (!argv[1][0] || argv[1][0] == '.' || byte_in(argv[1], namelen, " \t\f\r\n", 5) < 5) + strerr_dief1x(100, "invalid instance name") ; + + { + size_t svlen = strlen(argv[0]) ; + char fn[svlen + 11 + namelen] ; + memcpy(fn, argv[0], svlen) ; + memcpy(fn + svlen, "/instance/", 10) ; + memcpy(fn + svlen + 10, argv[1], namelen + 1) ; + argv[0] = fn ; + argv[1] = 0 ; + fullargv[0] = S6_BINPREFIX "s6-svc" ; + xexec(fullargv) ; + } +} diff --git a/src/instance/s6-instance-create.c b/src/instance/s6-instance-create.c new file mode 100644 index 0000000..2e97618 --- /dev/null +++ b/src/instance/s6-instance-create.c @@ -0,0 +1,97 @@ +/* ISC license. */ + +#include <errno.h> +#include <stdint.h> +#include <string.h> +#include <unistd.h> + +#include <skalibs/bytestr.h> +#include <skalibs/types.h> +#include <skalibs/sgetopt.h> +#include <skalibs/tai.h> +#include <skalibs/strerr.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> + +#include <s6/supervise.h> + +#define USAGE "s6-instance-create [ -d | -D ] [ -f ] [ -P ] [ -t timeout ] service instancename" +#define dieusage() strerr_dieusage(100, USAGE) + +static inline void checkinstanced (char const *s) /* chdirs */ +{ + int fd, r ; + size_t len = strlen(s) ; + char fn[len + 10] ; + memcpy(fn, s, len) ; + memcpy(fn + len, "/instance", 10) ; + if (chdir(fn) == -1) strerr_diefu2sys(111, "chdir to ", fn) ; + fd = open_read(S6_SVSCAN_CTLDIR "/lock") ; + if (fd < 0) strerr_diefu3sys(111, "open ", fn, "/" S6_SVSCAN_CTLDIR "/lock") ; + r = fd_islocked(fd) ; + if (r < 0) strerr_diefu3sys(111, "check lock on ", fn, "/" S6_SVSCAN_CTLDIR "/lock") ; + if (!r) strerr_dief2x(1, "instanced service not running on ", s) ; + fd_close(fd) ; +} + +static void cleanup (char const *s) +{ + int e = errno ; + rm_rf(s) ; + errno = e ; +} + +int main (int argc, char const *const *argv) +{ + tain tto = TAIN_INFINITE_RELATIVE ; + size_t namelen ; + uint32_t options = 16 ; + PROG = "s6-instance-create" ; + { + unsigned int t = 0 ; + subgetopt l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "dDfPt:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'd' : options |= 12 ; break ; + case 'D' : options |= 4 ; options &= ~8U ; break ; + case 'f' : options |= 1 ; break ; + case 'P' : options |= 2 ; break ; + case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + if (t) tain_from_millisecs(&tto, t) ; + } + if (argc < 2) dieusage() ; + + namelen = strlen(argv[1]) ; + if (!argv[0][0]) strerr_dief1x(100, "invalid service path") ; + if (!argv[1][0] || argv[1][0] == '.' || byte_in(argv[1], namelen, " \t\f\r\n", 5) < 5) + strerr_dief1x(100, "invalid instance name") ; + checkinstanced(argv[0]) ; + + tain_now_set_stopwatch_g() ; + tain_add_g(&tto, &tto) ; + + { + char sv[namelen + 14] ; + memcpy(sv, "../instances/", 13) ; + memcpy(sv + 13, argv[1], namelen + 1) ; + if (!hiercopy("../instances/.template", sv)) + { + cleanup(sv) ; + strerr_diefu5sys(111, "copy ", argv[0], "/instances/.template to ", argv[0], sv+2) ; + } + if (s6_supervise_link_names_g(".", (char const *const *)&sv, argv + 1, 1, options, &tto) == -1) + { + cleanup(sv) ; + strerr_diefu4sys(errno == ETIMEDOUT ? 99 : 111, "creatre instance of ", argv[0], " named ", argv[1]) ; + } + } + return 0 ; +} diff --git a/src/instance/s6-instance-delete.c b/src/instance/s6-instance-delete.c new file mode 100644 index 0000000..fc1337a --- /dev/null +++ b/src/instance/s6-instance-delete.c @@ -0,0 +1,61 @@ +/* ISC license. */ + +#include <errno.h> +#include <stdint.h> +#include <string.h> +#include <unistd.h> + +#include <skalibs/bytestr.h> +#include <skalibs/types.h> +#include <skalibs/sgetopt.h> +#include <skalibs/tai.h> +#include <skalibs/strerr.h> +#include <skalibs/stralloc.h> +#include <skalibs/djbunix.h> + +#include <s6/supervise.h> + +#define USAGE "s6-instance-delete [ -X ] [ -t timeout ] service instancename" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv) +{ + tain tto = TAIN_INFINITE_RELATIVE ; + uint32_t options = 1 ; + PROG = "s6-instance-delete" ; + { + unsigned int t = 0 ; + subgetopt l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "Xt:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'X' : options &= ~1U ; break ; + case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + if (t) tain_from_millisecs(&tto, t) ; + } + if (argc < 2) dieusage() ; + if (!argv[0][0]) strerr_dief1x(100, "invalid service path") ; + if (!argv[1][0] || argv[1][0] == '.' || byte_in(argv[1], strlen(argv[1]), " \t\f\r\n", 5) < 5) + strerr_dief1x(100, "invalid instance name") ; + + tain_now_set_stopwatch_g() ; + tain_add_g(&tto, &tto) ; + + { + size_t svlen = strlen(argv[0]) ; + char sc[svlen + 10] ; + memcpy(sc, argv[0], svlen) ; + memcpy(sc + svlen, "/instance", 10) ; + if (s6_supervise_unlink_names_g(sc, argv + 1, 1, options, &tto) == -1) + strerr_diefu4sys(111, "prepare deletion of instance ", argv[1], " of service ", argv[0]) ; + } + + return 0 ; +} diff --git a/src/instance/s6-instance-maker.c b/src/instance/s6-instance-maker.c index 20f557f..e70e569 100644 --- a/src/instance/s6-instance-maker.c +++ b/src/instance/s6-instance-maker.c @@ -37,7 +37,7 @@ static int write_run (buffer *b, void *data) size_t l ; char fmt[UINT_FMT] ; l = uint_fmt(fmt, t->maxinstances) ; - if (buffer_puts(b, EXECLINE_EXTBINPREFIX "execlineb -S1\n\n" + if (buffer_puts(b, EXECLINE_EXTBINPREFIX "execlineb -P\n\n" EXECLINE_EXTBINPREFIX "fdmove -c 2 1\n") < 0) return 0 ; if (t->user) { diff --git a/src/libs6/s6_supervise_link_names.c b/src/libs6/s6_supervise_link_names.c index 9af573f..de8e5c5 100644 --- a/src/libs6/s6_supervise_link_names.c +++ b/src/libs6/s6_supervise_link_names.c @@ -42,6 +42,7 @@ static uint16_t registerit (ftrigr_t *a, char *fn, size_t len, gid_t gid, uint32 bit 1: make event/ public bit 2: don't start the service bit 3: remove down files after starting supervisors + bit 4: allow links to relative paths */ int s6_supervise_link_names (char const *scdir, char const *const *servicedirs, char const *const *names, size_t n, uint32_t options, tain const *deadline, tain *stamp) @@ -118,7 +119,7 @@ int s6_supervise_link_names (char const *scdir, char const *const *servicedirs, } fn[len] = 0 ; strcpy(lname + scdirlen + 1, names[i]) ; - if (servicedirs[i][0] != '/') + if (!(options & 16) && servicedirs[i][0] != '/') { rpsa.len = 0 ; if (sarealpath(&rpsa, servicedirs[i]) < 0 || !stralloc_0(&rpsa)) goto err ; diff --git a/src/supervision/s6-svc.c b/src/supervision/s6-svc.c index 7e00f17..5982248 100644 --- a/src/supervision/s6-svc.c +++ b/src/supervision/s6-svc.c @@ -7,28 +7,31 @@ #include <skalibs/types.h> #include <skalibs/sgetopt.h> #include <skalibs/strerr.h> +#include <skalibs/djbunix.h> #include <skalibs/exec.h> #include <s6/config.h> #include <s6/supervise.h> -#define USAGE "s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -abqhkti12pcyroduxOX ] servicedir" +#define USAGE "s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -abqhkti12pcyroduDUxOX ] servicedir" #define dieusage() strerr_dieusage(100, USAGE) #define DATASIZE 63 int main (int argc, char const *const *argv) { - char data[DATASIZE+1] = "-" ; + size_t len ; + int downfile = -1 ; unsigned int datalen = 1 ; unsigned int timeout = 0 ; + char data[DATASIZE+1] = "-" ; char updown[3] = "-\0" ; PROG = "s6-svc" ; { subgetopt l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "abqhkti12pcyroduxOT:w:", &l) ; + int opt = subgetopt_r(argc, argv, "abqhkti12pcyroduDUxOT:w:", &l) ; if (opt == -1) break ; switch (opt) { @@ -55,6 +58,20 @@ int main (int argc, char const *const *argv) data[datalen++] = opt ; break ; } + case 'D' : + { + if (datalen >= DATASIZE) strerr_dief1x(100, "too many commands") ; + data[datalen++] = 'd' ; + downfile = 1 ; + break ; + } + case 'U' : + { + if (datalen >= DATASIZE) strerr_dief1x(100, "too many commands") ; + data[datalen++] = 'u' ; + downfile = 0 ; + break ; + } case 'T' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ; case 'w' : { @@ -69,13 +86,14 @@ int main (int argc, char const *const *argv) } if (!argc) dieusage() ; if (argc > 1) strerr_warnw1x("ignoring extra arguments") ; + len = strlen(argv[0]) ; + if (!len) strerr_dief1x(100, "invalid service path") ; if (updown[1] == 'U' || updown[1] == 'R') { - size_t arglen = strlen(argv[0]) ; - char fn[arglen + 17] ; - memcpy(fn, argv[0], arglen) ; - memcpy(fn + arglen, "/notification-fd", 17) ; + char fn[len + 17] ; + memcpy(fn, argv[0], len) ; + memcpy(fn + len, "/notification-fd", 17) ; if (access(fn, F_OK) < 0) { if (errno != ENOENT) strerr_diefu2sys(111, "access ", fn) ; @@ -84,6 +102,19 @@ int main (int argc, char const *const *argv) } } + if (downfile >= 0) + { + char fn[len + 6] ; + memcpy(fn, argv[0], len) ; + memcpy(fn + len, "/down", 6) ; + if (downfile) + { + if (!openwritenclose_unsafe(fn, "", 0)) + strerr_diefu2sys(111, "touch ", fn) ; + } + else unlink_void(fn) ; + } + if (updown[1]) { char const *newargv[11] ; |