about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-01-10 01:06:48 +0000
committerLaurent Bercot <ska@appnovation.com>2023-01-10 01:06:48 +0000
commit955d4078d0709372129fb883d3ea4bd6d7ad735e (patch)
tree04077179cce9eab1f9d4187c12c36c3e496146d6 /src
parentc026dea4c33adc3f18b9788cab4d6ce7d56345c8 (diff)
downloads6-955d4078d0709372129fb883d3ea4bd6d7ad735e.tar.gz
s6-955d4078d0709372129fb883d3ea4bd6d7ad735e.tar.xz
s6-955d4078d0709372129fb883d3ea4bd6d7ad735e.zip
Add s6-instance-list, s6-instance-status (no doc yet)
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/include/s6/supervise.h2
-rw-r--r--src/instance/deps-exe/s6-instance-list2
-rw-r--r--src/instance/deps-exe/s6-instance-status1
-rw-r--r--src/instance/s6-instance-control.c2
-rw-r--r--src/instance/s6-instance-create.c21
-rw-r--r--src/instance/s6-instance-delete.c21
-rw-r--r--src/instance/s6-instance-list.c52
-rw-r--r--src/instance/s6-instance-status.c102
-rw-r--r--src/libs6/deps-lib/s61
-rw-r--r--src/libs6/s6_instance_chdirservice.c26
-rw-r--r--src/libs6/s6_supervise_link_names.c3
11 files changed, 196 insertions, 37 deletions
diff --git a/src/include/s6/supervise.h b/src/include/s6/supervise.h
index 914bff3..30e36ed 100644
--- a/src/include/s6/supervise.h
+++ b/src/include/s6/supervise.h
@@ -75,4 +75,6 @@ extern void s6_dtally_unpack (char const *, s6_dtally_t *) ;
 extern ssize_t s6_dtally_read (char const *, s6_dtally_t *, size_t) ;
 extern int s6_dtally_write (char const *, s6_dtally_t const *, size_t) ;
 
+extern void s6_instance_chdirservice (char const *s) ;  /* chdirs to s/instance */
+
 #endif
diff --git a/src/instance/deps-exe/s6-instance-list b/src/instance/deps-exe/s6-instance-list
new file mode 100644
index 0000000..08815d9
--- /dev/null
+++ b/src/instance/deps-exe/s6-instance-list
@@ -0,0 +1,2 @@
+${LIBS6}
+-lskarnet
diff --git a/src/instance/deps-exe/s6-instance-status b/src/instance/deps-exe/s6-instance-status
new file mode 100644
index 0000000..e7187fe
--- /dev/null
+++ b/src/instance/deps-exe/s6-instance-status
@@ -0,0 +1 @@
+-lskarnet
diff --git a/src/instance/s6-instance-control.c b/src/instance/s6-instance-control.c
index ef064e6..483f388 100644
--- a/src/instance/s6-instance-control.c
+++ b/src/instance/s6-instance-control.c
@@ -10,7 +10,7 @@
 
 #include <s6/config.h>
 
-#define USAGE "s6-instance-control [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -abqhkti12pcyroduDUxO ] service instance"
+#define USAGE "s6-instance-control [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -abqhkti12pcyroduDUxO ] service instancename"
 #define dieusage() strerr_dieusage(100, USAGE)
 
 #define DATASIZE 63
diff --git a/src/instance/s6-instance-create.c b/src/instance/s6-instance-create.c
index 5e3d172..5c25433 100644
--- a/src/instance/s6-instance-create.c
+++ b/src/instance/s6-instance-create.c
@@ -3,7 +3,6 @@
 #include <errno.h>
 #include <stdint.h>
 #include <string.h>
-#include <unistd.h>
 #include <sys/stat.h>
 
 #include <skalibs/bytestr.h>
@@ -11,7 +10,6 @@
 #include <skalibs/sgetopt.h>
 #include <skalibs/tai.h>
 #include <skalibs/strerr.h>
-#include <skalibs/stralloc.h>
 #include <skalibs/djbunix.h>
 
 #include <s6/supervise.h>
@@ -19,22 +17,6 @@
 #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 ;
@@ -71,10 +53,9 @@ int main (int argc, char const *const *argv)
   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) < namelen)
     strerr_dief1x(100, "invalid instance name") ;
-  checkinstanced(argv[0]) ;
+  s6_instance_chdirservice(argv[0]) ;
 
   tain_now_set_stopwatch_g() ;
   tain_add_g(&tto, &tto) ;
diff --git a/src/instance/s6-instance-delete.c b/src/instance/s6-instance-delete.c
index 92078e0..193d2b8 100644
--- a/src/instance/s6-instance-delete.c
+++ b/src/instance/s6-instance-delete.c
@@ -1,16 +1,13 @@
 /* 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>
@@ -42,25 +39,23 @@ int main (int argc, char const *const *argv)
     if (t) tain_from_millisecs(&tto, t) ;
   }
   if (argc < 2) dieusage() ;
-  if (!argv[0][0]) strerr_dief1x(100, "invalid service path") ;
 
   namelen = strlen(argv[1]) ;
   if (!argv[1][0] || argv[1][0] == '.' || byte_in(argv[1], namelen, " \t\f\r\n", 5) < namelen)
     strerr_dief1x(100, "invalid instance name") ;
+  s6_instance_chdirservice(argv[0]) ;
 
   tain_now_set_stopwatch_g() ;
   tain_add_g(&tto, &tto) ;
 
+  if (s6_supervise_unlink_names_g(".", argv + 1, 1, options, &tto) == -1)
+    strerr_diefu4sys(111, "prepare deletion of instance ", argv[1], " of service ", argv[0]) ;
+
   {
-    size_t svlen = strlen(argv[0]) ;
-    char sc[svlen + 12 + namelen] ;
-    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]) ;
-    memcpy(sc + svlen + 9, "s/", 2) ;
-    memcpy(sc + svlen + 11, argv[1], namelen + 1) ;
-    rm_rf(sc) ;
+    char fn[14 + namelen] ;
+    memcpy(fn, "../instances/", 13) ;
+    memcpy(fn + 13, argv[1], namelen + 1) ;
+    rm_rf(fn) ;
   }
 
   return 0 ;
diff --git a/src/instance/s6-instance-list.c b/src/instance/s6-instance-list.c
new file mode 100644
index 0000000..20fff9e
--- /dev/null
+++ b/src/instance/s6-instance-list.c
@@ -0,0 +1,52 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <string.h>
+
+#include <skalibs/buffer.h>
+#include <skalibs/strerr.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/direntry.h>
+
+#include <s6/supervise.h>
+
+#define USAGE "s6-instance-list service"
+#define dieusage() strerr_dieusage(100, USAGE)
+
+int main (int argc, char const *const *argv)
+{
+  DIR *dir ;
+  PROG = "s6-instance-list" ;
+  {
+    subgetopt l = SUBGETOPT_ZERO ;
+    for (;;)
+    {
+      int opt = subgetopt_r(argc, argv, "", &l) ;
+      if (opt == -1) break ;
+      switch (opt)
+      {
+        default : dieusage() ;
+      }
+    }
+    argc -= l.ind ; argv += l.ind ;
+  }
+  if (!argc) dieusage() ;
+
+  s6_instance_chdirservice(argv[0]) ;
+  dir = opendir(".") ;
+  for (;;)
+  {
+    direntry *d ;
+    errno = 0 ;
+    d = readdir(dir) ;
+    if (!d) break ;
+    if (d->d_name[0] == '.') continue ;
+    if (buffer_puts(buffer_1, d->d_name) < 0
+     || buffer_put(buffer_1, "\n", 1) < 0)
+      strerr_diefu1sys(111, "write to stdout") ;
+  }
+  if (errno) strerr_diefu3sys(111, "readdir ", argv[0], "/instance") ;
+  dir_close(dir) ;
+  if (!buffer_flush(buffer_1)) strerr_diefu1sys(111, "write to stdout") ;
+  return 0 ;
+}
diff --git a/src/instance/s6-instance-status.c b/src/instance/s6-instance-status.c
new file mode 100644
index 0000000..2c00a6e
--- /dev/null
+++ b/src/instance/s6-instance-status.c
@@ -0,0 +1,102 @@
+/* ISC license. */
+
+#include <string.h>
+
+#include <skalibs/bytestr.h>
+#include <skalibs/strerr.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/exec.h>
+
+#include <s6/config.h>
+
+#define USAGE "s6-instance-status [ -uwNrpest | -o up,wantedup,normallyup,ready,paused,pid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] service name"
+#define dieusage() strerr_dieusage(100, USAGE)
+
+#define MAXFIELDS 16
+#define checkfields() if (n++ >= MAXFIELDS) strerr_dief1x(100, "too many option fields")
+
+static unsigned int check_options (char const *arg, unsigned int n)
+{
+  static char const *table[] =
+  {
+    "up",
+    "wantedup",
+    "normallyup",
+    "ready",
+    "paused",
+    "pid",
+    "exitcode",
+    "signal",
+    "signum",
+    "updownsince",
+    "readysince",
+    "updownfor",
+    "readyfor",
+    0
+  } ;
+  while (*arg)
+  {
+    size_t pos = str_chr(arg, ',') ;
+    char const *const *p = table ;
+    if (!pos) strerr_dief1x(100, "invalid null option field") ;
+    for (; *p ; p++) if (!strncmp(arg, *p, pos)) break ;
+    if (!p)
+    {
+      char blah[pos+1] ;
+      memcpy(blah, arg, pos) ;
+      blah[pos] = 0 ;
+      strerr_dief2x(100, "invalid option field: ", blah) ;
+    }
+    checkfields() ;
+    arg += pos ; if (*arg) arg++ ;
+  }
+  return n ;
+}
+
+int main (int argc, char const **argv)
+{
+  char const **fullargv = argv ;
+  size_t namelen ;
+  unsigned int n = 0 ;
+  PROG = "s6-instance-status" ;
+  {
+    subgetopt l = SUBGETOPT_ZERO ;
+    for (;;)
+    {
+      int opt = subgetopt_r(argc, argv, "no:uwNrpest", &l) ;
+      if (opt == -1) break ;
+      switch (opt)
+      {
+        case 'n' : break ;
+        case 'o' : n = check_options(l.arg, n) ; break ;
+        case 'u' :
+        case 'w' :
+        case 'N' :
+        case 'r' :
+        case 'p' :
+        case 'e' :
+        case 's' :
+        case 't' : if (n++ >= MAXFIELDS) strerr_dief1x(100, "too many option fields") ; 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) < namelen)
+    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-svstat" ;
+    xexec(fullargv) ;
+  }
+}
diff --git a/src/libs6/deps-lib/s6 b/src/libs6/deps-lib/s6
index 2fa977c..732e071 100644
--- a/src/libs6/deps-lib/s6
+++ b/src/libs6/deps-lib/s6
@@ -31,6 +31,7 @@ s6_dtally_pack.o
 s6_dtally_unpack.o
 s6_dtally_read.o
 s6_dtally_write.o
+s6_instance_chdirservice.o
 s6_servicedir_file_list.o
 s6_svc_ok.o
 s6_svc_write.o
diff --git a/src/libs6/s6_instance_chdirservice.c b/src/libs6/s6_instance_chdirservice.c
new file mode 100644
index 0000000..356ee20
--- /dev/null
+++ b/src/libs6/s6_instance_chdirservice.c
@@ -0,0 +1,26 @@
+/* ISC license. */
+
+#include <string.h>
+#include <unistd.h>
+
+#include <skalibs/strerr.h>
+#include <skalibs/djbunix.h>
+
+#include <s6/supervise.h>
+
+void s6_instance_chdirservice (char const *s)
+{
+  int fd, r ;
+  size_t len = strlen(s) ;
+  char fn[len + 10] ;
+  if (!*s) strerr_dief1x(100, "invalid service path") ;
+  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) ;
+}
diff --git a/src/libs6/s6_supervise_link_names.c b/src/libs6/s6_supervise_link_names.c
index 7712e7e..de8e5c5 100644
--- a/src/libs6/s6_supervise_link_names.c
+++ b/src/libs6/s6_supervise_link_names.c
@@ -11,7 +11,6 @@
 #include <skalibs/tai.h>
 #include <skalibs/stralloc.h>
 #include <skalibs/djbunix.h>
-#include <skalibs/lolstdio.h>
 
 #include <s6/ftrigr.h>
 #include <s6/ftrigw.h>
@@ -56,10 +55,8 @@ int s6_supervise_link_names (char const *scdir, char const *const *servicedirs,
   memset(locked, 0, bitarray_div8(n)) ;
   memset(logged, 0, bitarray_div8(n)) ;
 
-  LOLDEBUG("s6_supervise_link_names: scdir = %s  n = %zu  options = %u", scdir, n, options) ;
   for (size_t i = 0 ; i < n ; i++)
   {
-    LOLDEBUG("i = %zu  dir = %s  name = %s", i, servicedirs[i], names[i]) ;
     struct stat st ;
     size_t len = strlen(servicedirs[i]) ;
     size_t nlen = strlen(names[i]) ;