commit 3b3867f76d97f19db41389505954f29400488c4f Author: Dave Cottlehuber Date: Thu Sep 22 11:00:45 2016 +0200 add optional flag -H to propagate SIGHUP to child process diff --git a/daemon.c b/daemon.c index 23051fd..2d8bec7 100644 --- a/daemon.c +++ b/daemon.c @@ -55,14 +55,14 @@ main(int argc, char *argv[]) { struct pidfh *ppfh, *pfh; sigset_t mask, oldmask; - int ch, nochdir, noclose, restart, serrno; + int ch, nochdir, noclose, nohup, restart, serrno; const char *pidfile, *ppidfile, *title, *user; pid_t otherpid, pid; nochdir = noclose = 1; - restart = 0; + restart = nohup = 0 ; ppidfile = pidfile = title = user = NULL; - while ((ch = getopt(argc, argv, "cfp:P:rt:u:")) != -1) { + while ((ch = getopt(argc, argv, "cfp:P:rt:u:H")) != -1) { switch (ch) { case 'c': nochdir = 0; @@ -85,6 +85,9 @@ main(int argc, char *argv[]) case 'u': user = optarg; break; + case 'H': + nohup = 1; + break; default: usage(); } @@ -165,6 +168,9 @@ main(int argc, char *argv[]) sigemptyset(&mask); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGCHLD); + if (nohup) { + sigaddset(&mask, SIGHUP); + } if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) { warn("sigprocmask"); goto exit; @@ -265,7 +271,13 @@ wait_child(pid_t pid, sigset_t *mask) return (-1); } continue; - default: + case SIGHUP: + if (nohup) {} + if (kill(pid, signo) == -1) { + warn("hup"); + return (-1); + } + continue; default: warnx("sigwaitinfo: invalid signal: %d", signo); return (-1); } @@ -276,7 +288,7 @@ static void usage(void) { (void)fprintf(stderr, "%s\n\t%s\n", - "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile]", + "usage: daemon [-cfrH] [-p child_pidfile] [-P supervisor_pidfile]", "[-t title] [-u user] command arguments ..."); exit(1); }