diff --git a/src/ckpool.c b/src/ckpool.c index 1694ac6b..09669d44 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -21,6 +21,9 @@ #include "libckpool.h" #include "generator.h" +/* Only global variable, to be used only by sighandler */ +static ckpool_t *global_ckp; + static void rename_proc(const char *name) { char buf[16]; @@ -137,14 +140,23 @@ static void launch_process(proc_instance_t *pi) } } +static void sighandler(int __maybe_unused sig) +{ + /* Restore signal handlers so we can still quit if shutdown fails */ + sigaction(SIGTERM, &global_ckp->termhandler, NULL); + sigaction(SIGINT, &global_ckp->inthandler, NULL); +} + int main(int argc, char **argv) { + struct sigaction handler; proc_instance_t proc_main; proc_instance_t proc_generator; pthread_t pth_listener; ckpool_t ckp; int c, ret; + global_ckp = &ckp; memset(&ckp, 0, sizeof(ckp)); while ((c = getopt(argc, argv, "c:gn:s:")) != -1) { switch (c) { @@ -167,6 +179,15 @@ int main(int argc, char **argv) ckp.socket_dir = strdup("/tmp/ckpool"); realloc_strcat(&ckp.socket_dir, "/"); + + /* Install signal handlers to shut down all child processes */ + handler.sa_handler = &sighandler; + handler.sa_flags = 0; + sigemptyset(&handler.sa_mask); + sigaction(SIGTERM, &handler, &ckp.termhandler); + sigaction(SIGINT, &handler, &ckp.inthandler); + signal(SIGPIPE, SIG_IGN); + ret = mkdir(ckp.socket_dir, 0700); if (ret && errno != EEXIST) quit(1, "Failed to make directory %s", ckp.socket_dir); diff --git a/src/ckpool.h b/src/ckpool.h index 66775bde..11f50af1 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -121,11 +121,4 @@ static inline void flip_80(void *dest_p, const void *src_p) dest[i] = bswap_32(src[i]); } -struct ckpool_instance { - char *socket_dir; - char *config; - char *name; -}; - -typedef struct ckpool_instance ckpool_t; #endif /* CKPOOL_H */ diff --git a/src/libckpool.h b/src/libckpool.h index 35ebd2e8..5b87eb52 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -12,12 +12,13 @@ #ifndef LIBCKPOOL_H #define LIBCKPOOL_H -#include #include +#include #include +#include +#include #include #include -#include #define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__) #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__) @@ -105,6 +106,19 @@ struct cklock { typedef struct cklock cklock_t; +struct proc_instance; +typedef struct proc_instance proc_instance_t; + +struct ckpool_instance { + char *socket_dir; + char *config; + char *name; + struct sigaction termhandler; + struct sigaction inthandler; +}; + +typedef struct ckpool_instance ckpool_t; + struct connsock { int fd; char *url; @@ -122,9 +136,6 @@ struct unixsock { typedef struct unixsock unixsock_t; -struct proc_instance; -typedef struct proc_instance proc_instance_t; - struct proc_instance { ckpool_t *ckp; unixsock_t us;