From c3eca9fe8f3f6e011e10ec49b04405047f1bfb1d Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Wed, 5 Feb 2020 07:04:33 +0100 Subject: [PATCH] Save process exit status to svc_t Signed-off-by: Joachim Nilsson --- src/private.h | 2 +- src/service.c | 9 ++++++--- src/sig.c | 5 +++-- src/svc.h | 1 + 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/private.h b/src/private.h index b1eeff7c..4a637a3c 100644 --- a/src/private.h +++ b/src/private.h @@ -33,7 +33,7 @@ int api_exit (void); int client (int argc, char *argv[]); -void service_monitor (pid_t lost); +void service_monitor (pid_t lost, int status); const char *plugin_hook_str(hook_point_t no); int plugin_exists (hook_point_t no); diff --git a/src/service.c b/src/service.c index 094560ba..8d05f76f 100644 --- a/src/service.c +++ b/src/service.c @@ -375,7 +375,8 @@ static int service_start(svc_t *svc) switch (svc->type) { case SVC_TYPE_RUN: - result = WEXITSTATUS(complete(svc->cmd, pid)); + svc->status = complete(svc->cmd, pid); + result = WEXITSTATUS(svc->status); svc->start_time = svc->pid = 0; svc->once++; svc_set_state(svc, SVC_STOPPING_STATE); @@ -1029,7 +1030,7 @@ void service_unregister(svc_t *svc) svc_del(svc); } -void service_monitor(pid_t lost) +void service_monitor(pid_t lost, int status) { svc_t *svc; @@ -1045,7 +1046,9 @@ void service_monitor(pid_t lost) return; } - _d("collected %s(%d)", svc->cmd, lost); + _d("collected %s(%d), normal exit: %d, signaled: %d, exit code: %d", + svc->cmd, lost, WIFEXITED(status), WIFSIGNALED(status), WEXITSTATUS(status)); + svc->status = status; /* Try removing PID file (in case service does not clean up after itself) */ if (svc_is_daemon(svc)) { diff --git a/src/sig.c b/src/sig.c index 96a67ba5..be52d85c 100644 --- a/src/sig.c +++ b/src/sig.c @@ -316,6 +316,7 @@ static void sigterm_cb(uev_t *w, void *arg, int events) static void sigchld_cb(uev_t *w, void *arg, int events) { pid_t pid; + int status; if (UEV_ERROR == events) { _e("Unrecoverable error in signal watcher"); @@ -324,10 +325,10 @@ static void sigchld_cb(uev_t *w, void *arg, int events) /* Reap all the children! */ do { - pid = waitpid(-1, NULL, WNOHANG); + pid = waitpid(-1, &status, WNOHANG); if (pid > 0) { _d("Collected child %d", pid); - service_monitor(pid); + service_monitor(pid, status); } } while (pid > 0); } diff --git a/src/svc.h b/src/svc.h index 61a9ab3f..a67341fe 100644 --- a/src/svc.h +++ b/src/svc.h @@ -98,6 +98,7 @@ typedef struct svc { pid_t pid; char pidfile[256]; long start_time; /* Start time, as seconds since boot, from sysinfo() */ + int status; /* From waitpid() when process is collected */ const svc_state_t state; /* Paused, Reloading, Restart, Running, ... */ svc_type_t type; /* Service, run, task, inetd, ... */ int protect; /* Services like dbus-daemon & udev by Finit */