about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2024-05-07 22:06:36 +0000
committerLaurent Bercot <ska@appnovation.com>2024-05-07 22:06:36 +0000
commit15e3e402c59089514f590bab8804f0ed60f0fee3 (patch)
treee29847fcb9b5e3913c3e4f83b11af62b30d225bc
parentb827410052a640f65650d1b72d5f63d9ada28933 (diff)
downloads6-15e3e402c59089514f590bab8804f0ed60f0fee3.tar.gz
s6-15e3e402c59089514f590bab8804f0ed60f0fee3.tar.xz
s6-15e3e402c59089514f590bab8804f0ed60f0fee3.zip
Add process group support to s6-supervise
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--NEWS6
-rw-r--r--doc/index.html2
-rw-r--r--doc/s6-svc.html10
-rw-r--r--doc/s6-svstat.html5
-rw-r--r--doc/servicedir.html11
-rw-r--r--doc/upgrade.html9
-rw-r--r--package/info2
-rw-r--r--src/include/s6/supervise.h4
-rw-r--r--src/libs6/s6_svstatus_pack.c5
-rw-r--r--src/libs6/s6_svstatus_unpack.c12
-rw-r--r--src/supervision/s6-supervise.c69
-rw-r--r--src/supervision/s6-svc.c7
-rw-r--r--src/supervision/s6-svstat.c58
13 files changed, 142 insertions, 58 deletions
diff --git a/NEWS b/NEWS
index 9a65dda..fcd95a9 100644
--- a/NEWS
+++ b/NEWS
@@ -1,9 +1,13 @@
 Changelog for s6.
 
-In 2.12.0.5
+In 2.13.0.0
 -----------
 
  - Bugfixes.
+ - s6-svstat -o pgid (or -g) prints the service's pgid
+ - s6-supervise now passes the service's pgid to ./finish
+ - New s6-svc -P/-C/-K sends SIGSTOP, SIGCONT or SIGKILL to
+the service's process group.
 
 
 In 2.12.0.4
diff --git a/doc/index.html b/doc/index.html
index f5afce2..b3bb158 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -115,7 +115,7 @@ want nsswitch-like functionality:
 <h3> Download </h3>
 
 <ul>
- <li> The current released version of s6 is <a href="s6-2.12.0.5.tar.gz">2.12.0.5</a>. </li>
+ <li> The current released version of s6 is <a href="s6-2.13.0.0.tar.gz">2.13.0.0</a>. </li>
  <li> Alternatively, you can checkout a copy of the
 <a href="//git.skarnet.org/cgi-bin/cgit.cgi/s6/">s6
 git repository</a>:
diff --git a/doc/s6-svc.html b/doc/s6-svc.html
index 208ee9c..de25dd0 100644
--- a/doc/s6-svc.html
+++ b/doc/s6-svc.html
@@ -28,7 +28,7 @@ knowing their PIDs, and without using horrible hacks such as .pid files.
 <h2> Interface </h2>
 
 <pre>
-     s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T <em>timeout</em> ] [ -s <em>signal</em> | -abqhkti12pcy ] [ -roduDUxO ] <em>servicedir</em>
+     s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T <em>timeout</em> ] [ -s <em>signal</em> | -abqhkti12pcyrPCK ] [ -oduDUxO ] <em>servicedir</em>
 </pre>
 
 <p>
@@ -58,7 +58,7 @@ supervised process. <em>signal</em> can be given as its name (case-
 insensitive) or its number, but only the signals listed above are
 accepted - you cannot, for instance, manually send a SIGSEGV to the
 supervised process. </li>
-</ul> <br />
+</ul> <br>
 <ul>
  <li> <tt>-o</tt>&nbsp;: once. Equivalent to "-uO". </li>
  <li> <tt>-d</tt>&nbsp;: down. If the supervised process is up, send it
@@ -117,7 +117,11 @@ option had been given instead. </li>
 started or restarted. </li>
  <li> <tt>-wR</tt>&nbsp;: s6-svc will not exit until the service has been
 started or restarted and has notified readiness. </li>
-
+</ul> <br>
+<ul>
+ <li> <tt>-P</tt>&nbsp;: send a SIGSTOP to the <em>process group</em> of the supervised process </li>
+ <li> <tt>-C</tt>&nbsp;: send a SIGCONT to the <em>process group</em> of the supervised process </li>
+ <li> <tt>-K</tt>&nbsp;: send a SIGKILL to the <em>process group</em> of the supervised process </li>
 </ul>
 
 <h2> Usage examples </h2>
diff --git a/doc/s6-svstat.html b/doc/s6-svstat.html
index ba62dbb..20a966a 100644
--- a/doc/s6-svstat.html
+++ b/doc/s6-svstat.html
@@ -27,7 +27,7 @@ summary of the state of a process monitored by
 <h2> Interface </h2>
 
 <pre>
-     s6-svstat [ -uwNrpest | -o up,wantedup,normallyup,ready,paused,pid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] <em>servicedir</em>
+     s6-svstat [ -uwNrpest | -o up,wantedup,normallyup,ready,paused,pid,pgid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] <em>servicedir</em>
 </pre>
 
 <p>
@@ -97,6 +97,8 @@ currently stopped) and <tt>false</tt> if it is not. It is a rare flag, you shoul
 need to use this option. </li>
    <li> <tt>pid</tt>: print the pid of the supervised process. If the service is currently down,
 <tt>-1</tt> is printed instead. </li>
+   <li> <tt>pgid</tt>: print the process group of the supervised process. If the service is
+currently down, print the process group of the last living instance of the service. </li>
    <li> <tt>exitcode</tt>: print the exit code of the last execution of <tt>./run</tt>. If the
 service is currently up, or the last <tt>./run</tt> process was killed by a signal,
 <tt>-1</tt> is printed instead. </li>
@@ -123,6 +125,7 @@ service last became ready (or ready to be started if it's currently not up and r
  <li> <tt>-N</tt>: equivalent to <tt>-o&nbsp;normallyup</tt>. </li>
  <li> <tt>-r</tt>: equivalent to <tt>-o&nbsp;ready</tt>. </li>
  <li> <tt>-p</tt>: equivalent to <tt>-o&nbsp;pid</tt>. </li>
+ <li> <tt>-g</tt>: equivalent to <tt>-o&nbsp;pgid</tt>. </li>
  <li> <tt>-e</tt>: equivalent to <tt>-o&nbsp;exitcode</tt>. </li>
  <li> <tt>-s</tt>: equivalent to <tt>-o&nbsp;signal</tt>. </li>
  <li> <tt>-t</tt>: equivalent to <tt>-o&nbsp;updownfor</tt>. </li>
diff --git a/doc/servicedir.html b/doc/servicedir.html
index a49a38d..f08c3a4 100644
--- a/doc/servicedir.html
+++ b/doc/servicedir.html
@@ -46,7 +46,7 @@ but most of the time it will be a script, called <em>run script</em>.
 This file is the most important one in your service directory: it
 contains the commands that will setup and run your <em>foo</em> service.
  <ul>
-  <li> It is forked and executed by <a href="s6-supervise.html">s6-supervise</a>
+  <li> It is spawned by <a href="s6-supervise.html">s6-supervise</a>
 every time the service must be started, i.e. normally when
 <a href="s6-supervise.html">s6-supervise</a> starts, and whenever
 the service goes down when it is supposed to be up. </li>
@@ -118,11 +118,16 @@ process has been killed. If the <em>foo</em> service is supposed to be up,
 script, not the finish script, should be running; the finish script should really
 be short-lived.) The maximum duration of a <tt>finish</tt> execution can be
 configured via the <tt>timeout-finish</tt> file, see below. </li>
-  <li> The finish script is executed with three arguments:
+  <li> The finish script is executed with four arguments:
    <ol>
     <li> the exit code from the run script (resp. 256 if the run script was killed by a signal) </li>
     <li> an undefined number (resp. the number of the signal that killed the run script) </li>
-    <li> the name of the service directory, the same that has been given to <tt>./run</tt>. </li>
+    <li> the name of the service directory, the same that has been given to <tt>./run</tt> </li>
+    <li> the process group id of the defunct run script. This is useful to clean up
+services that leave children behind: for instance, <tt>if test "$1" -gt 255 ; then kill -9 -- -"$4" ; fi</tt>
+in the finish script will SIGKILL all children processes if the service crashed.
+This is not an entirely reliable mechanism, because an annoying service could spawn
+children processes in a different process group, but it should catch most offenders. </li>
    </ol>
   <li> If the finish script exits 125, then <a href="s6-supervise.html">s6-supervise</a>
 interprets this as a permanent failure for the service, and does not restart it,
diff --git a/doc/upgrade.html b/doc/upgrade.html
index dfd79c6..546dab8 100644
--- a/doc/upgrade.html
+++ b/doc/upgrade.html
@@ -18,13 +18,20 @@
 
 <h1> What has changed in s6 </h1>
 
-<h2> in 2.12.0.5 </h2>
+<h2> in 2.13.0.0 </h2>
 
 <ul>
  <li> <a href="//skarnet.org/software/skalibs/">skalibs</a>
 dependency bumped to 2.14.2.0. </li>
  <li> <a href="//skarnet.org/software/execline/">execline</a>
 recommended dependency bumped to 2.9.5.1. </li>
+ <li> <a href="s6-svstat.html">s6-svstat</a> has new options,
+<tt>-g</tt> and <tt>-o pgid</tt>, to print the pgid of the service. </li>
+ <li> <a href="s6-supervise.html">s6-supervise</a> now passes the
+pgid of the defunct service as 4th argument to the finish script. </li>
+ <li> <a href="s6-svc.html">s6-svc</a> can now send SIGSTOP, SIGCONT
+and SIGKILL to the service's process group, via the <tt>-P</tt>, <tt>-C</tt>
+and <tt>-K</tt> options. </li>
 </ul>
 
 <h2> in 2.12.0.4 </h2>
diff --git a/package/info b/package/info
index 616e4bf..4f92433 100644
--- a/package/info
+++ b/package/info
@@ -1,4 +1,4 @@
 package=s6
-version=2.12.0.5
+version=2.13.0.0
 category=admin
 package_macro_name=S6
diff --git a/src/include/s6/supervise.h b/src/include/s6/supervise.h
index 066cb50..dc71cea 100644
--- a/src/include/s6/supervise.h
+++ b/src/include/s6/supervise.h
@@ -12,7 +12,7 @@
 #define S6_SUPERVISE_EVENTDIR "event"
 #define S6_SVSCAN_CTLDIR ".s6-svscan"
 #define S6_SVSTATUS_FILENAME S6_SUPERVISE_CTLDIR "/status"
-#define S6_SVSTATUS_SIZE 35
+#define S6_SVSTATUS_SIZE 43
 #define S6_DTALLY_FILENAME S6_SUPERVISE_CTLDIR "/death_tally"
 #define S6_MAX_DEATH_TALLY 4096
 
@@ -26,6 +26,7 @@ struct s6_svstatus_s
   tain stamp ;
   tain readystamp ;
   pid_t pid ;
+  pid_t pgid ;
   int wstat ;
   unsigned int flagpaused : 1 ;
   unsigned int flagfinishing : 1 ;
@@ -38,6 +39,7 @@ struct s6_svstatus_s
   .stamp = TAIN_ZERO, \
   .readystamp = TAIN_ZERO, \
   .pid = 0, \
+  .pgid = 0, \
   .wstat = 0, \
   .flagpaused = 0, \
   .flagfinishing = 0, \
diff --git a/src/libs6/s6_svstatus_pack.c b/src/libs6/s6_svstatus_pack.c
index a769c08..5e1c7d8 100644
--- a/src/libs6/s6_svstatus_pack.c
+++ b/src/libs6/s6_svstatus_pack.c
@@ -11,8 +11,9 @@ void s6_svstatus_pack (char *pack, s6_svstatus_t const *sv)
   tain_pack(pack, &sv->stamp) ;
   tain_pack(pack + 12, &sv->readystamp) ;
   uint64_pack_big(pack + 24, (uint64_t)sv->pid) ;
-  uint16_pack_big(pack + 32, (uint16_t)sv->wstat) ;
-  pack[34] =
+  uint64_pack_big(pack + 32, (uint64_t)sv->pgid) ;
+  uint16_pack_big(pack + 40, (uint16_t)sv->wstat) ;
+  pack[42] =
     sv->flagpaused |
     (sv->flagfinishing << 1) |
     (sv->flagwantup << 2) |
diff --git a/src/libs6/s6_svstatus_unpack.c b/src/libs6/s6_svstatus_unpack.c
index 59df671..b9b5d78 100644
--- a/src/libs6/s6_svstatus_unpack.c
+++ b/src/libs6/s6_svstatus_unpack.c
@@ -14,10 +14,12 @@ void s6_svstatus_unpack (char const *pack, s6_svstatus_t *sv)
   tain_unpack(pack + 12, &sv->readystamp) ;
   uint64_unpack_big(pack + 24, &pid) ;
   sv->pid = pid ;
-  uint16_unpack_big(pack + 32, &wstat) ;
+  uint64_unpack_big(pack + 32, &pid) ;
+  sv->pgid = pid ;
+  uint16_unpack_big(pack + 40, &wstat) ;
   sv->wstat = wstat ;
-  sv->flagpaused = pack[34] & 1 ;
-  sv->flagfinishing = !!(pack[34] & 2) ;
-  sv->flagwantup = !!(pack[34] & 4) ;
-  sv->flagready = !!(pack[34] & 8) ;
+  sv->flagpaused = pack[42] & 1 ;
+  sv->flagfinishing = !!(pack[42] & 2) ;
+  sv->flagwantup = !!(pack[42] & 4) ;
+  sv->flagready = !!(pack[42] & 8) ;
 }
diff --git a/src/supervision/s6-supervise.c b/src/supervision/s6-supervise.c
index 2f3b9a7..3201127 100644
--- a/src/supervision/s6-supervise.c
+++ b/src/supervision/s6-supervise.c
@@ -43,7 +43,7 @@ typedef enum trans_e trans_t, *trans_t_ref ;
 enum trans_e
 {
   V_TIMEOUT, V_CHLD, V_TERM, V_HUP, V_QUIT, V_INT,
-  V_a, V_b, V_q, V_h, V_k, V_t, V_i, V_1, V_2, V_p, V_c, V_y, V_r,
+  V_a, V_b, V_q, V_h, V_k, V_t, V_i, V_1, V_2, V_p, V_c, V_y, V_r, V_P, V_C, V_K,
   V_o, V_d, V_u, V_D, V_U, V_x, V_O, V_Q
 } ;
 
@@ -73,6 +73,7 @@ typedef action_t *action_t_ref ;
 static tain deadline ;
 static tain nextstart = TAIN_ZERO ;
 static s6_svstatus_t status = S6_SVSTATUS_ZERO ;
+static int finish_wstat ;
 static state_t state = DOWN ;
 static int notifyfd = -1 ;
 static char const *servicename = 0 ;
@@ -130,6 +131,7 @@ static inline int read_downsig (void)
 static void set_down_and_ready (char const *s, unsigned int n)
 {
   status.pid = 0 ;
+  status.pgid = 0 ;
   status.flagfinishing = 0 ;
   status.flagready = 1 ;
   state = DOWN ;
@@ -152,11 +154,14 @@ static void bail (void)
   gflags.cont = 0 ;
 }
 
+static void killI (void)
+{
+  if (status.pgid > 0) killpg(status.pgid, SIGINT) ;
+}
+
 static void sigint (void)
 {
-  pid_t pgid = getpgid(status.pid) ;
-  if (pgid == -1) strerr_warnwu1sys("getpgid") ;
-  else killpg(pgid, SIGINT) ;
+  killI() ;
   bail() ;
 }
 
@@ -253,6 +258,31 @@ static void killr (void)
   kill(status.pid, read_downsig()) ;
 }
 
+static void killP (void)
+{
+  if (status.pgid > 0)
+  {
+    killpg(status.pgid, SIGSTOP) ;
+    status.flagpaused = 1 ;
+    announce() ;
+  }
+}
+
+static void killC (void)
+{
+  if (status.pgid > 0)
+  {
+    killpg(status.pgid, SIGCONT) ;
+    status.flagpaused = 0 ;
+    announce() ;
+  }
+}
+
+static void killK (void)
+{
+  if (status.pgid > 0) killpg(status.pgid, SIGKILL) ;
+}
+
 static void trystart (void)
 {
   cspawn_fileaction fa[2] =
@@ -341,6 +371,9 @@ static void trystart (void)
     fd_close(notifyp[1]) ;
     notifyfd = notifyp[0] ;
   }
+  status.pgid = getpgid(status.pid) ;
+  if (status.pgid == -1)
+    strerr_warnwu1sys("getpgid() (process group control commands will have no effect)") ;
   settimeout_infinite() ;
   nextstart = tain_zero ;
   state = UP ;
@@ -411,9 +444,9 @@ static int uplastup_z (void)
   unsigned int n ;
   char fmt0[UINT_FMT] ;
   char fmt1[UINT_FMT] ;
-  char const *cargv[5] = { "finish", fmt0, fmt1, servicename, 0 } ;
+  char fmt2[PID_FMT] ;
+  char const *cargv[6] = { "finish", fmt0, fmt1, servicename, status.pgid > 0 ? fmt2 : "-1", 0 } ;
 
-  status.wstat = (int)status.pid ;
   status.flagpaused = 0 ;
   status.flagready = 0 ;
   gflags.dying = 0 ;
@@ -425,6 +458,7 @@ static int uplastup_z (void)
   }
   fmt0[uint_fmt(fmt0, WIFSIGNALED(status.wstat) ? 256 : WEXITSTATUS(status.wstat))] = 0 ;
   fmt1[uint_fmt(fmt1, WTERMSIG(status.wstat))] = 0 ;
+  if (status.pgid > 0) fmt2[pid_fmt(fmt2, status.pgid)] = 0 ;
 
   if (!read_uint("max-death-tally", &n)) n = 100 ;
   if (n > S6_MAX_DEATH_TALLY) n = S6_MAX_DEATH_TALLY ;
@@ -458,6 +492,7 @@ static int uplastup_z (void)
       tain_add_g(&deadline, &tto) ;
     else settimeout_infinite() ;
   }
+  status.pgid = getpgid(status.pid) ;
   status.flagfinishing = 1 ;
   announce() ;
   ftrigw_notifyb_nosig(S6_SUPERVISE_EVENTDIR, "d", 1) ;
@@ -532,8 +567,7 @@ static void finishtimeout (void)
 
 static void finish_z (void)
 {
-  int wstat = (int)status.pid ;
-  if (WIFEXITED(wstat) && WEXITSTATUS(wstat) == 125)
+  if (WIFEXITED(finish_wstat) && WEXITSTATUS(finish_wstat) == 125)
   {
     status.flagwantup = 0 ;
     set_down_and_ready("OD", 2) ;
@@ -553,22 +587,22 @@ static void lastfinish_z (void)
   bail() ;
 }
 
-static action_t_ref const actions[5][27] =
+static action_t_ref const actions[5][30] =
 {
   { &downtimeout, &nop, &bail, &bail, &bail, &bail,
-    &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop,
+    &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop,
     &down_o, &wantdown, &down_u, &wantDOWN, &down_U, &bail, &wantdown, &wantDOWN },
   { &uptimeout, &up_z, &up_term, &up_x, &bail, &sigint,
-    &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr,
+    &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr, &killP, &killC, &killK,
     &wantdown, &up_d, &wantup, &up_D, &wantUP, &up_x, &wantdown, &wantDOWN },
   { &finishtimeout, &finish_z, &finish_x, &finish_x, &bail, &sigint,
-    &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop,
+    &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop,
     &wantdown, &wantdown, &wantup, &wantDOWN, &wantUP, &finish_x, &wantdown, &wantDOWN },
   { &uptimeout, &lastup_z, &up_d, &closethem, &bail, &sigint,
-    &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr,
+    &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr, &killP, &killC, &killK,
     &wantdown, &up_d, &wantup, &up_D, &wantUP, &closethem, &wantdown, &wantDOWN },
   { &finishtimeout, &lastfinish_z, &nop, &closethem, &bail, &sigint,
-    &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop,
+    &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop,
     &wantdown, &wantdown, &wantup, &wantDOWN, &wantUP, &closethem, &wantdown, &wantDOWN }
 } ;
 
@@ -620,7 +654,8 @@ static inline void handle_signals (void)
             if (errno != ECHILD) strerr_diefu1sys(111, "wait_pid_nohang") ;
             else break ;
           else if (!r) break ;
-          status.pid = (pid_t)wstat ; /* don't overwrite status.wstat if it's ./finish */
+          if (status.flagfinishing) finish_wstat = wstat ;
+          else status.wstat = wstat ;
           (*actions[state][V_CHLD])() ;
         }
         break ;
@@ -652,8 +687,8 @@ static inline void handle_control (int fd)
     else if (!r) break ;
     else
     {
-      size_t pos = byte_chr("abqhkti12pcyroduDUxOQ", 21, c) ;
-      if (pos < 21) (*actions[state][V_a + pos])() ;
+      size_t pos = byte_chr("abqhkti12pcyrPCKoduDUxOQ", 24, c) ;
+      if (pos < 24) (*actions[state][V_a + pos])() ;
     }
   }
 }
diff --git a/src/supervision/s6-svc.c b/src/supervision/s6-svc.c
index c84d62b..40a2cd8 100644
--- a/src/supervision/s6-svc.c
+++ b/src/supervision/s6-svc.c
@@ -18,7 +18,7 @@
 #include <s6/config.h>
 #include <s6/supervise.h>
 
-#define USAGE "s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -s signal | -abqhkti12pcy ] [ -roduDUxOQ ] servicedir"
+#define USAGE "s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -s signal | -abqhkti12pcyrPCK ] [ -oduDUxOQ ] servicedir"
 #define dieusage() strerr_dieusage(100, USAGE)
 
 #define DATASIZE 63
@@ -50,7 +50,7 @@ int main (int argc, char const *const *argv)
     subgetopt l = SUBGETOPT_ZERO ;
     for (;;)
     {
-      int opt = subgetopt_r(argc, argv, "s:abqhkti12pcyroduDUxOQT:w:", &l) ;
+      int opt = subgetopt_r(argc, argv, "s:abqhkti12pcyrPCKoduDUxOQT:w:", &l) ;
       if (opt == -1) break ;
       switch (opt)
       {
@@ -74,6 +74,9 @@ int main (int argc, char const *const *argv)
         case 'c' :
         case 'y' :
         case 'r' :
+        case 'P' :
+        case 'C' :
+        case 'K' :
         case 'o' :
         case 'd' :
         case 'u' :
diff --git a/src/supervision/s6-svstat.c b/src/supervision/s6-svstat.c
index f683ef8..502991a 100644
--- a/src/supervision/s6-svstat.c
+++ b/src/supervision/s6-svstat.c
@@ -3,7 +3,9 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <unistd.h>
+#include <stdlib.h>
 #include <errno.h>
+
 #include <skalibs/uint64.h>
 #include <skalibs/types.h>
 #include <skalibs/bytestr.h>
@@ -13,9 +15,10 @@
 #include <skalibs/sig.h>
 #include <skalibs/tai.h>
 #include <skalibs/djbunix.h>
+
 #include <s6/supervise.h>
 
-#define USAGE "s6-svstat [ -uwNrpest | -o up,wantedup,normallyup,ready,paused,pid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] servicedir"
+#define USAGE "s6-svstat [ -uwNrpgest | -o up,wantedup,normallyup,ready,paused,pid,pgid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] servicedir"
 #define dieusage() strerr_dieusage(100, USAGE)
 
 #define MAXFIELDS 16
@@ -63,6 +66,16 @@ static void pr_pid (buffer *b, s6_svstatus_t const *st)
   else buffer_putsnoflush(b, "-1") ;
 }
 
+static void pr_pgid (buffer *b, s6_svstatus_t const *st)
+{
+  if (st->pgid > 0)
+  {
+    char fmt[PID_FMT] ;
+    buffer_putnoflush(b, fmt, pid_fmt(fmt, st->pgid)) ;
+  }
+  else buffer_putsnoflush(b, "-1") ;
+}
+
 static void pr_tain (buffer *b, tain const *a)
 {
   char fmt[TIMESTAMP] ;
@@ -133,38 +146,40 @@ static void pr_normallyup (buffer *b, s6_svstatus_t const *st)
 
 static funcmap_t const fmtable[] =
 {
-  { .s = "up", .f = &pr_up },
-  { .s = "wantedup", .f = &pr_wantedup },
+  { .s = "exitcode", .f = &pr_exitcode },
   { .s = "normallyup", .f = &pr_normallyup },
-  { .s = "ready", .f = &pr_ready },
   { .s = "paused", .f = &pr_paused },
+  { .s = "pgid", .f = &pr_pgid },
   { .s = "pid", .f = &pr_pid },
-  { .s = "exitcode", .f = &pr_exitcode },
+  { .s = "ready", .f = &pr_ready },
+  { .s = "readyfor", .f = &pr_readyseconds },
+  { .s = "readysince", .f = &pr_readystamp },
   { .s = "signal", .f = &pr_signal },
   { .s = "signum", .f = &pr_signum },
-  { .s = "updownsince", .f = &pr_stamp },
-  { .s = "readysince", .f = &pr_readystamp },
+  { .s = "up", .f = &pr_up },
   { .s = "updownfor", .f = &pr_upseconds },
-  { .s = "readyfor", .f = &pr_readyseconds },
-  { .s = 0, .f = 0 }
+  { .s = "updownsince", .f = &pr_stamp },
+  { .s = "wantedup", .f = &pr_wantedup },
 } ;
 
+static int funcmap_bcmp (void const *a, void const *b)
+{
+  return strcmp((char const *)a, ((funcmap_t const *)b)->s) ;
+}
+
+#define BSEARCH(key, array) bsearch(key, (array), sizeof(array)/sizeof(funcmap_t), sizeof(funcmap_t), &funcmap_bcmp)
 
 static unsigned int parse_options (char const *arg, pr_func_ref *fields, unsigned int n)
 {
   while (*arg)
   {
+    funcmap_t const *p ;
     size_t pos = str_chr(arg, ',') ;
-    funcmap_t const *p = fmtable ;
-    if (!pos) strerr_dief1x(100, "invalid null option field") ;
-    for (; p->s ; p++) if (!strncmp(arg, p->s, pos)) break ;
-    if (!p->s)
-    {
-      char blah[pos+1] ;
-      memcpy(blah, arg, pos) ;
-      blah[pos] = 0 ;
-      strerr_dief2x(100, "invalid option field: ", blah) ;
-    }
+    char blah[pos+1] ;
+    memcpy(blah, arg, pos) ;
+    blah[pos] = 0 ;
+    p = BSEARCH(blah, fmtable) ;
+    if (!p) strerr_dief2x(100, "invalid option field: ", blah) ;
     checkfields() ;
     fields[n++] = p->f ;
     arg += pos ; if (*arg) arg++ ;
@@ -182,6 +197,8 @@ static void legacy (s6_svstatus_t *st, int flagnum)
   {
     buffer_putnoflush(buffer_1small,"up (pid ", 8) ;
     buffer_putnoflush(buffer_1small, fmt, pid_fmt(fmt, status.pid)) ;
+    buffer_putnoflush(buffer_1small, " pgid ", 6) ;
+    buffer_putnoflush(buffer_1small, fmt, pid_fmt(fmt, status.pgid)) ;
     buffer_putnoflush(buffer_1small, ") ", 2) ;
   }
   else
@@ -242,7 +259,7 @@ int main (int argc, char const *const *argv)
     subgetopt l = SUBGETOPT_ZERO ;
     for (;;)
     {
-      int opt = subgetopt_r(argc, argv, "no:uwNrpest", &l) ;
+      int opt = subgetopt_r(argc, argv, "no:uwNrpgest", &l) ;
       if (opt == -1) break ;
       switch (opt)
       {
@@ -253,6 +270,7 @@ int main (int argc, char const *const *argv)
         case 'N' : checkfields() ; fields[n++] = &pr_normallyup ; break ;
         case 'r' : checkfields() ; fields[n++] = &pr_ready ; break ;
         case 'p' : checkfields() ; fields[n++] = &pr_pid ; break ;
+        case 'g' : checkfields() ; fields[n++] = &pr_pgid ; break ;
         case 'e' : checkfields() ; fields[n++] = &pr_exitcode ; break ;
         case 's' : checkfields() ; fields[n++] = &pr_signal ; break ;
         case 't' : checkfields() ; fields[n++] = &pr_upseconds ; break ;