From 472bc0151e93a5a7247830ccfe947bb335e66cb4 Mon Sep 17 00:00:00 2001 From: Simon Batardiere Date: Thu, 4 Jun 2020 17:59:22 -0500 Subject: [PATCH 1/2] daemon: Daemon exit status available in the API Exit status of a daemon could be useful for managers to know if the daemon executed sucessfully or not, and can be used then in some logic. This commit makes the exit status of the daemon availble in the API. Signed-off-by: Simon Batardiere --- src/lib/daemon/inc/daemon.h | 1 + src/lib/daemon/src/daemon.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/lib/daemon/inc/daemon.h b/src/lib/daemon/inc/daemon.h index fdf12805..6b311393 100644 --- a/src/lib/daemon/inc/daemon.h +++ b/src/lib/daemon/inc/daemon.h @@ -76,6 +76,7 @@ struct __daemon bool dn_auto_restart; /* Enable daemoness auto restart on error */ char *dn_pidfile_path; /* PID file */ bool dn_pidfile_create; /* true whether we should create the PID file */ + int dn_exit_status; /* Exit status of the daemon */ }; extern bool daemon_init(daemon_t *self, const char *exe_path, int flags); diff --git a/src/lib/daemon/src/daemon.c b/src/lib/daemon/src/daemon.c index 0380cff9..df5dd3c9 100644 --- a/src/lib/daemon/src/daemon.c +++ b/src/lib/daemon/src/daemon.c @@ -414,6 +414,7 @@ void __daemon_child_ev(struct ev_loop *loop, ev_child *w, int revent) void __daemon_teardown(daemon_t *self, int wstatus) { /* Figure out the reason the process died */ + self->dn_exit_status = WEXITSTATUS(wstatus); if (WIFSIGNALED(wstatus)) { #if defined(WCOREDUMP) From 6d64c080904be4a0cd09ca0ca8a04e5a2e36fb6a Mon Sep 17 00:00:00 2001 From: Simon Batardiere Date: Fri, 12 Jun 2020 18:05:51 -0500 Subject: [PATCH 2/2] daemon: Daemon exit status available in the API Create a getter in the daemon API to hide the daemon structure details outside the API. Signed-off-by: Simon Batardiere --- src/lib/daemon/inc/daemon.h | 1 + src/lib/daemon/src/daemon.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/lib/daemon/inc/daemon.h b/src/lib/daemon/inc/daemon.h index 6b311393..52239668 100644 --- a/src/lib/daemon/inc/daemon.h +++ b/src/lib/daemon/inc/daemon.h @@ -92,6 +92,7 @@ extern bool daemon_atexit(daemon_t *self, daemon_atexit_fn_t *fn); extern bool daemon_atrestart(daemon_t *self, daemon_atrestart_fn_t *fn); extern bool daemon_pidfile_set(daemon_t *self, const char *path, bool create); extern bool daemon_is_started(daemon_t *self, bool *started); +extern bool daemon_get_exit_status(const daemon_t *self, int *exit_status); #define daemon_arg_add(self, ...) \ daemon_arg_add_a(self, C_VPACK(__VA_ARGS__)) diff --git a/src/lib/daemon/src/daemon.c b/src/lib/daemon/src/daemon.c index df5dd3c9..23c809d4 100644 --- a/src/lib/daemon/src/daemon.c +++ b/src/lib/daemon/src/daemon.c @@ -1014,3 +1014,13 @@ static bool __daemon_set_nonblock(int fd, bool enable) return true; } + +bool daemon_get_exit_status(const daemon_t *self, int *exit_status) +{ + if (!self) + return false; + if (exit_status) + *exit_status = self->dn_exit_status; + return true; +} +