[PATCH 3/4] Add info on why process is down to statusfile

From: Olivier Brunel <jjk_at_jjacky.com>
Date: Sun, 18 Jan 2015 21:13:57 +0100

That is, add the wstat allowing to know the return code/signal that
occured when process went down.

Because in addition to those info being provided to the optional finish
script, it is useful to have them always for all services.

Signed-off-by: Olivier Brunel <jjk_at_jjacky.com>
---
 src/include/s6/s6-supervise.h  |  6 ++++--
 src/libs6/s6_svstatus_pack.c   |  3 ++-
 src/libs6/s6_svstatus_unpack.c |  4 ++++
 src/supervision/s6-supervise.c | 13 +++++++------
 src/supervision/s6-svstat.c    | 20 +++++++++++++++++++-
 5 files changed, 36 insertions(+), 10 deletions(-)
diff --git a/src/include/s6/s6-supervise.h b/src/include/s6/s6-supervise.h
index c8d72d7..f2b64d8 100644
--- a/src/include/s6/s6-supervise.h
+++ b/src/include/s6/s6-supervise.h
_at_@ -10,7 +10,7 @@
 #define S6_SVSCAN_CTLDIR ".s6-svscan"
 #define S6_SVSTATUS_FILENAME S6_SUPERVISE_CTLDIR "/status"
 #define S6_SUPERVISE_READY_FILENAME S6_SUPERVISE_CTLDIR "/ready"
-#define S6_SVSTATUS_SIZE 18
+#define S6_SVSTATUS_SIZE 22
 
 extern int s6_svc_write (char const *, char const *, unsigned int) ;
 extern int s6_svc_main (int, char const *const *, char const *, char const *, char const *) ;
_at_@ -24,9 +24,11 @@ struct s6_svstatus_s
   unsigned int flagwantup : 1 ;
   unsigned int flagpaused : 1 ;
   unsigned int flagfinishing : 1 ;
+  unsigned int felldown : 1 ;
+  unsigned int wstat ;
 } ;
 
-#define S6_SVSTATUS_ZERO { .stamp = TAIN_ZERO, .pid = 0, .flagwant = 0, .flagwantup = 0, .flagpaused = 0, .flagfinishing = 0 }
+#define S6_SVSTATUS_ZERO { .stamp = TAIN_ZERO, .pid = 0, .flagwant = 0, .flagwantup = 0, .flagpaused = 0, .flagfinishing = 0, .felldown = 0, .wstat = 0 }
 
 
 extern void s6_svstatus_pack (char *, s6_svstatus_t const *) ;
diff --git a/src/libs6/s6_svstatus_pack.c b/src/libs6/s6_svstatus_pack.c
index 2d5baf6..bebbcf9 100644
--- a/src/libs6/s6_svstatus_pack.c
+++ b/src/libs6/s6_svstatus_pack.c
_at_@ -8,6 +8,7 @@ void s6_svstatus_pack (char *pack, s6_svstatus_t const *sv)
 {
   tain_pack(pack, &sv->stamp) ;
   uint32_pack(pack + 12, (uint32)sv->pid) ;
-  pack[16] = sv->flagpaused | (sv->flagfinishing << 1) ;
+  pack[16] = sv->flagpaused | (sv->flagfinishing << 1) | (sv->felldown << 2) ;
   pack[17] = sv->flagwant ? sv->flagwantup ? 'u' : 'd' : 0 ;
+  uint32_pack(pack + 18, (uint32)sv->wstat) ;
 }
diff --git a/src/libs6/s6_svstatus_unpack.c b/src/libs6/s6_svstatus_unpack.c
index cce6989..d7aac26 100644
--- a/src/libs6/s6_svstatus_unpack.c
+++ b/src/libs6/s6_svstatus_unpack.c
_at_@ -7,11 +7,13 @@
 void s6_svstatus_unpack (char const *pack, s6_svstatus_t_ref sv)
 {
   uint32 pid ;
+  uint32 wstat ;
   tain_unpack(pack, &sv->stamp) ;
   uint32_unpack(pack + 12, &pid) ;
   sv->pid = (int)pid ;
   sv->flagpaused = pack[16] & 1 ;
   sv->flagfinishing = (pack[16] >> 1) & 1 ;
+  sv->felldown = (pack[16] >> 2) & 1 ;
   switch (pack[17])
   {
     case 'u' :
_at_@ -26,4 +28,6 @@ void s6_svstatus_unpack (char const *pack, s6_svstatus_t_ref sv)
       sv->flagwant = 0 ;
       sv->flagwantup = 0 ;
   }
+  uint32_unpack(pack + 18, &wstat) ;
+  sv->wstat = (int)wstat ;
 }
diff --git a/src/supervision/s6-supervise.c b/src/supervision/s6-supervise.c
index 1ea45d0..3776be6 100644
--- a/src/supervision/s6-supervise.c
+++ b/src/supervision/s6-supervise.c
_at_@ -192,6 +192,7 @@ static void trystart (void)
   settimeout_infinite() ;
   state = UP ;
   status.pid = pid ;
+  status.felldown = 0 ;
   tain_copynow(&status.stamp) ;
   announce() ;
   ftrigw_notify(S6_SUPERVISE_EVENTDIR, 'u') ;
_at_@ -230,7 +231,7 @@ static void down_d (void)
   announce() ;
 }
 
-static void tryfinish (int wstat, int islast)
+static void tryfinish (int islast)
 {
   register pid_t pid = fork() ;
   if (pid < 0)
_at_@ -248,8 +249,8 @@ static void tryfinish (int wstat, int islast)
     char fmt1[UINT_FMT] ;
     char *cargv[4] = { "finish", fmt0, fmt1, 0 } ;
     selfpipe_finish() ;
-    fmt0[uint_fmt(fmt0, WIFSIGNALED(wstat) ? 256 : WEXITSTATUS(wstat))] = 0 ;
-    fmt1[uint_fmt(fmt1, WTERMSIG(wstat))] = 0 ;
+    fmt0[uint_fmt(fmt0, WIFSIGNALED(status.wstat) ? 256 : WEXITSTATUS(status.wstat))] = 0 ;
+    fmt1[uint_fmt(fmt1, WTERMSIG(status.wstat))] = 0 ;
     if (flagsetsid) setsid() ;
     execve("./finish", cargv, (char *const *)environ) ;
     _exit(111) ;
_at_@ -268,14 +269,14 @@ static void uptimeout (void)
 
 static void uplastup_z (int islast)
 {
-  int wstat = status.pid ;
   status.pid = 0 ;
+  status.felldown = 1 ;
   tain_copynow(&status.stamp) ;
   announce() ;
   ftrigw_notify(S6_SUPERVISE_EVENTDIR, 'd') ;
   if (unlink(S6_SUPERVISE_READY_FILENAME) < 0 && errno != ENOENT)
     strerr_warnwu1sys("unlink " S6_SUPERVISE_READY_FILENAME) ;
-  tryfinish(wstat, islast) ;
+  tryfinish(islast) ;
 }
 
 static void up_z (void)
_at_@ -401,7 +402,7 @@ static void handle_signals (void)
             if (errno != ECHILD) strerr_diefu1sys(111, "wait_pid_nohang") ;
             else break ;
           else if (!r) break ;
-          status.pid = wstat ;
+          if (!status.flagfinishing) status.wstat = wstat ;
           (*actions[state][V_CHLD])() ;
         }
         break ;
diff --git a/src/supervision/s6-svstat.c b/src/supervision/s6-svstat.c
index bc27c32..b98e69e 100644
--- a/src/supervision/s6-svstat.c
+++ b/src/supervision/s6-svstat.c
_at_@ -60,7 +60,25 @@ int main (int argc, char const *const *argv)
     if (ready) buffer_putnoflush(buffer_1small,"; ready", 7) ;
     buffer_putnoflush(buffer_1small, ") ", 2) ;
   }
-  else buffer_putnoflush(buffer_1small, "down ", 5) ;
+  else
+  {
+    buffer_putnoflush(buffer_1small, "down ", 5) ;
+    if (status.felldown)
+    {
+      buffer_putnoflush(buffer_1small, "(", 1) ;
+      if (WIFSIGNALED(status.wstat))
+      {
+        buffer_putnoflush(buffer_1small, "signal=", 7) ;
+        buffer_putnoflush(buffer_1small, fmt, uint_fmt(fmt, WTERMSIG(status.wstat))) ;
+      }
+      else
+      {
+        buffer_putnoflush(buffer_1small, "rc=", 3) ;
+        buffer_putnoflush(buffer_1small, fmt, uint_fmt(fmt, WEXITSTATUS(status.wstat))) ;
+      }
+      buffer_putnoflush(buffer_1small, ") ", 2) ;
+    }
+  }
 
   buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, status.stamp.sec.x)) ;
   buffer_putnoflush(buffer_1small," seconds", 8) ;
-- 
2.2.2
Received on Sun Jan 18 2015 - 20:13:57 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:44:19 UTC