From f48558d2b57d051f48cd6511e2f32098caffa91b Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 23 May 2014 10:05:22 +1000 Subject: [PATCH] Detach child processes from the tty leaving only the main process to talk to it and exit all processes in a consistent way with more information on termination --- configure.ac | 2 +- src/ckpool.c | 21 +++++++++++++++++++++ src/ckpool.h | 2 ++ src/connector.c | 7 +------ src/generator.c | 7 +------ src/stratifier.c | 8 +------- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/configure.ac b/configure.ac index 042c953f..3af808b4 100644 --- a/configure.ac +++ b/configure.ac @@ -39,7 +39,7 @@ AC_CHECK_HEADERS(ctype.h errno.h byteswap.h string.h time.h) AC_CHECK_HEADERS(endian.h sys/endian.h arpa/inet.h sys/poll.h syslog.h) AC_CHECK_HEADERS(alloca.h pthread.h stdio.h math.h signal.h sys/prctl.h) AC_CHECK_HEADERS(sys/types.h sys/socket.h sys/stat.h linux/un.h netdb.h) -AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h) +AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h sys/ioctl.h) AC_CHECK_HEADERS(jansson.h) PTHREAD_LIBS="-lpthread" diff --git a/src/ckpool.c b/src/ckpool.c index bfdd20be..01a9639e 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -7,6 +7,7 @@ * any later version. See COPYING for more details. */ +#include #include #include #include @@ -147,6 +148,8 @@ static void childsighandler(int sig) { pid_t ppid = getppid(); + LOGWARNING("Child process received signal %d, forwarding signal to %s main process", + sig, global_ckp->name); kill(ppid, sig); } @@ -167,6 +170,9 @@ static void launch_process(proc_instance_t *pi) sigaction(SIGTERM, &handler, NULL); sigaction(SIGINT, &handler, NULL); + /* Detach child processes leaving main only to communicate with + * the terminal. */ + ioctl(0, TIOCNOTTY, NULL); rename_proc(pi->processname); write_namepid(pi); ret = pi->process(pi); @@ -185,6 +191,21 @@ static void launch_processes(ckpool_t *ckp) launch_process(ckp->children[i]); } +int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret) +{ + if (ret) { + /* Abnormal termination, kill entire process */ + LOGWARNING("%s %s exiting with return code %d, shutting down!", + ckp->name, pi->processname, ret); + send_proc(&ckp->main, "shutdown"); + sleep(1); + ret = 1; + } else /* Should be part of a normal shutdown */ + LOGNOTICE("%s %s exited normally", ckp->name, pi->processname); + + return ret; +} + static void clean_up(ckpool_t *ckp) { int i, children = ckp->proc_instances; diff --git a/src/ckpool.h b/src/ckpool.h index 1482ba60..f59004dd 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -103,6 +103,8 @@ struct ckpool_instance { ckpool_t *global_ckp; +int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret); + /* Placeholders for when we have more comprehensive logging facilities */ #define LOGMSG(_loglevel, fmt, ...) do { \ diff --git a/src/connector.c b/src/connector.c index 6ed11b33..0ef18cdf 100644 --- a/src/connector.c +++ b/src/connector.c @@ -593,10 +593,5 @@ int connector(proc_instance_t *pi) //join_pthread(pth_acceptor); out: - LOGINFO("%s connector exiting with return code %d", ckp->name, ret); - if (ret) { - send_proc(&ckp->main, "shutdown"); - sleep(1); - } - exit(ret? 1: 0); + return process_exit(ckp, pi, ret); } diff --git a/src/generator.c b/src/generator.c index 4af6da6c..63775138 100644 --- a/src/generator.c +++ b/src/generator.c @@ -1244,10 +1244,5 @@ int generator(proc_instance_t *pi) else ret = server_mode(ckp, pi); - LOGINFO("%s generator exiting with return code %d", ckp->name, ret); - if (ret) { - send_proc(&ckp->main, "shutdown"); - sleep(1); - } - exit(ret? 1: 0); + return process_exit(ckp, pi, ret); } diff --git a/src/stratifier.c b/src/stratifier.c index 1a803d04..d3b479ad 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2037,11 +2037,5 @@ int stratifier(proc_instance_t *pi) cklock_init(&share_lock); ret = stratum_loop(ckp, pi); - LOGINFO("%s stratifier exiting with return code %d", ckp->name, ret); - if (ret) { - send_proc(&ckp->main, "shutdown"); - sleep(1); - } - - exit(ret? 1: 0); + return process_exit(ckp, pi, ret); }