Browse Source

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

master
Con Kolivas 11 years ago
parent
commit
f48558d2b5
  1. 2
      configure.ac
  2. 21
      src/ckpool.c
  3. 2
      src/ckpool.h
  4. 7
      src/connector.c
  5. 7
      src/generator.c
  6. 8
      src/stratifier.c

2
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(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(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(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) AC_CHECK_HEADERS(jansson.h)
PTHREAD_LIBS="-lpthread" PTHREAD_LIBS="-lpthread"

21
src/ckpool.c

@ -7,6 +7,7 @@
* any later version. See COPYING for more details. * any later version. See COPYING for more details.
*/ */
#include <sys/ioctl.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -147,6 +148,8 @@ static void childsighandler(int sig)
{ {
pid_t ppid = getppid(); pid_t ppid = getppid();
LOGWARNING("Child process received signal %d, forwarding signal to %s main process",
sig, global_ckp->name);
kill(ppid, sig); kill(ppid, sig);
} }
@ -167,6 +170,9 @@ static void launch_process(proc_instance_t *pi)
sigaction(SIGTERM, &handler, NULL); sigaction(SIGTERM, &handler, NULL);
sigaction(SIGINT, &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); rename_proc(pi->processname);
write_namepid(pi); write_namepid(pi);
ret = pi->process(pi); ret = pi->process(pi);
@ -185,6 +191,21 @@ static void launch_processes(ckpool_t *ckp)
launch_process(ckp->children[i]); 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) static void clean_up(ckpool_t *ckp)
{ {
int i, children = ckp->proc_instances; int i, children = ckp->proc_instances;

2
src/ckpool.h

@ -103,6 +103,8 @@ struct ckpool_instance {
ckpool_t *global_ckp; 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 */ /* Placeholders for when we have more comprehensive logging facilities */
#define LOGMSG(_loglevel, fmt, ...) do { \ #define LOGMSG(_loglevel, fmt, ...) do { \

7
src/connector.c

@ -593,10 +593,5 @@ int connector(proc_instance_t *pi)
//join_pthread(pth_acceptor); //join_pthread(pth_acceptor);
out: out:
LOGINFO("%s connector exiting with return code %d", ckp->name, ret); return process_exit(ckp, pi, ret);
if (ret) {
send_proc(&ckp->main, "shutdown");
sleep(1);
}
exit(ret? 1: 0);
} }

7
src/generator.c

@ -1244,10 +1244,5 @@ int generator(proc_instance_t *pi)
else else
ret = server_mode(ckp, pi); ret = server_mode(ckp, pi);
LOGINFO("%s generator exiting with return code %d", ckp->name, ret); return process_exit(ckp, pi, ret);
if (ret) {
send_proc(&ckp->main, "shutdown");
sleep(1);
}
exit(ret? 1: 0);
} }

8
src/stratifier.c

@ -2037,11 +2037,5 @@ int stratifier(proc_instance_t *pi)
cklock_init(&share_lock); cklock_init(&share_lock);
ret = stratum_loop(ckp, pi); ret = stratum_loop(ckp, pi);
LOGINFO("%s stratifier exiting with return code %d", ckp->name, ret); return process_exit(ckp, pi, ret);
if (ret) {
send_proc(&ckp->main, "shutdown");
sleep(1);
}
exit(ret? 1: 0);
} }

Loading…
Cancel
Save