summaryrefslogtreecommitdiff
path: root/src/instance
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-01-09 11:09:14 +0000
committerLaurent Bercot <ska@appnovation.com>2023-01-09 11:09:14 +0000
commit09750ce3525129a676dec4f579aea3ad2eca1b19 (patch)
treeacae6b5f47914d7ed85bbb8fe9780ed34a85cfd0 /src/instance
parent191b46491407632f2431c46eab97dbf5543fa26d (diff)
downloads6-09750ce3525129a676dec4f579aea3ad2eca1b19.tar.xz
Add instances implementation (still needs testing)
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/instance')
-rw-r--r--src/instance/deps-exe/s6-instance-control1
-rw-r--r--src/instance/deps-exe/s6-instance-create2
-rw-r--r--src/instance/deps-exe/s6-instance-delete2
-rw-r--r--src/instance/s6-instance-control.c79
-rw-r--r--src/instance/s6-instance-create.c97
-rw-r--r--src/instance/s6-instance-delete.c61
-rw-r--r--src/instance/s6-instance-maker.c2
7 files changed, 243 insertions, 1 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)
{