From: Martin Mares Date: Wed, 18 Jul 2012 10:15:36 +0000 (+0200) Subject: Daemon: Better toy daemon X-Git-Tag: v5.99~143 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=238ce95c214b9bfc607c921ef6ee120690802fab;p=libucw.git Daemon: Better toy daemon --- diff --git a/ucw/daemon.c b/ucw/daemon.c index 7606f807..00313b94 100644 --- a/ucw/daemon.c +++ b/ucw/daemon.c @@ -178,12 +178,43 @@ daemon_exit(struct daemon_params *dp) #ifdef TEST +#include + +static volatile sig_atomic_t terminate; + +static void term_handler(int sig UNUSED) +{ + msg(L_INFO | L_SIGHANDLER, "SIGTERM received, terminating in a while"); + terminate = 1; +} + +static void hup_handler(int sig UNUSED) +{ + msg(L_INFO | L_SIGHANDLER, "SIGHUP received"); +} + static void body(struct daemon_params *dp) { log_fork(); msg(L_INFO, "Daemon is running"); msg(L_INFO, "uid=%d/%d gid=%d/%d", (int) getuid(), (int) geteuid(), (int) getgid(), (int) getegid()); - sleep(60); + + struct sigaction sa_term = { .sa_handler = term_handler }; + struct sigaction sa_hup = { .sa_handler = hup_handler }; + if (sigaction(SIGTERM, &sa_term, NULL) < 0 || + sigaction(SIGHUP, &sa_hup, NULL) < 0) + ASSERT(0); + + while (!terminate) + { + if (!sleep(60)) + { + msg(L_INFO, "Timeout elapsed, terminating in a while"); + break; + } + } + + sleep(2); msg(L_INFO, "Daemon is shutting down"); daemon_exit(dp); }