about summary refs log tree commit diff
path: root/src/instance
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/instance
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/instance')
-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
7 files changed, 167 insertions, 34 deletions
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) ;
+  }
+}