summaryrefslogtreecommitdiffstats
path: root/usr.sbin/daemon/daemon.c
diff options
context:
space:
mode:
authortrociny <trociny@FreeBSD.org>2012-02-19 10:23:51 +0000
committertrociny <trociny@FreeBSD.org>2012-02-19 10:23:51 +0000
commit03503d2c1ce95da32a8d84e8865164a2880f1dba (patch)
tree386f36276396af6e54542e886cacf202a3440a79 /usr.sbin/daemon/daemon.c
parent15a9e8816230fd94e949ae7c2b2d89774c12c511 (diff)
downloadFreeBSD-src-03503d2c1ce95da32a8d84e8865164a2880f1dba.zip
FreeBSD-src-03503d2c1ce95da32a8d84e8865164a2880f1dba.tar.gz
If the supervising process receives SIGTERM, forward it to the spawned
process. Normally it will cause the child to exit followed by the termination of the supervisor after removing the pidfile. This looks like desirable behavior, because termination of a supervisor usually supposes termination of its charge. Also it will fix the issue with stale pid files after reboot due to init kills a supervisor before its child exits. MFC after: 2 weeks
Diffstat (limited to 'usr.sbin/daemon/daemon.c')
-rw-r--r--usr.sbin/daemon/daemon.c77
1 files changed, 66 insertions, 11 deletions
diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c
index d7e85e5..05432f0 100644
--- a/usr.sbin/daemon/daemon.c
+++ b/usr.sbin/daemon/daemon.c
@@ -36,21 +36,24 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <errno.h>
-#include <pwd.h>
#include <libutil.h>
#include <login_cap.h>
+#include <pwd.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+static void dummy_sighandler(int);
static void restrict_process(const char *);
-static void wait_child(pid_t pid);
+static void wait_child(pid_t pid, sigset_t *mask);
static void usage(void);
int
main(int argc, char *argv[])
{
struct pidfh *pfh = NULL;
+ sigset_t mask, oldmask;
int ch, nochdir, noclose;
const char *pidfile, *user;
pid_t otherpid, pid;
@@ -100,9 +103,37 @@ main(int argc, char *argv[])
if (daemon(nochdir, noclose) == -1)
err(1, NULL);
- pid = 0;
+ /*
+ * If the pidfile option is specified the daemon executes the
+ * command in a forked process and wait on child exit to
+ * remove the pidfile. Normally we don't want the monitoring
+ * daemon to be terminated leaving the running process and the
+ * stale pidfile, so we catch SIGTERM and pass it to the
+ * children expecting to get SIGCHLD eventually.
+ */
+ pid = -1;
if (pidfile != NULL) {
/*
+ * Restore default action for SIGTERM in case the
+ * parent process decided to ignore it.
+ */
+ if (signal(SIGTERM, SIG_DFL) == SIG_ERR)
+ err(1, "signal");
+ /*
+ * Because SIGCHLD is ignored by default, setup dummy handler
+ * for it, so we can mask it.
+ */
+ if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR)
+ err(1, "signal");
+ /*
+ * Block interesting signals.
+ */
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGTERM);
+ sigaddset(&mask, SIGCHLD);
+ if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1)
+ err(1, "sigprocmask");
+ /*
* Spawn a child to exec the command, so in the parent
* we could wait for it to exit and remove pidfile.
*/
@@ -112,7 +143,12 @@ main(int argc, char *argv[])
err(1, "fork");
}
}
- if (pid == 0) {
+ if (pid <= 0) {
+ if (pid == 0) {
+ /* Restore old sigmask in the child. */
+ if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
+ err(1, "sigprocmask");
+ }
/* Now that we are the child, write out the pid. */
pidfile_write(pfh);
@@ -128,12 +164,18 @@ main(int argc, char *argv[])
err(1, "%s", argv[0]);
}
setproctitle("%s[%d]", argv[0], pid);
- wait_child(pid);
+ wait_child(pid, &mask);
pidfile_remove(pfh);
exit(0); /* Exit status does not matter. */
}
static void
+dummy_sighandler(int sig __unused)
+{
+ /* Nothing to do. */
+}
+
+static void
restrict_process(const char *user)
{
struct passwd *pw = NULL;
@@ -147,14 +189,27 @@ restrict_process(const char *user)
}
static void
-wait_child(pid_t pid)
+wait_child(pid_t pid, sigset_t *mask)
{
- int status;
+ int signo;
- while (waitpid(pid, &status, 0) == -1) {
- if (errno != EINTR) {
- warn("waitpid");
- break;
+ for (;;) {
+ if (sigwait(mask, &signo) == -1) {
+ warn("sigwaitinfo");
+ return;
+ }
+ switch (signo) {
+ case SIGCHLD:
+ return;
+ case SIGTERM:
+ if (kill(pid, signo) == -1) {
+ warn("kill");
+ return;
+ }
+ continue;
+ default:
+ warnx("sigwaitinfo: invalid signal: %d", signo);
+ return;
}
}
}
OpenPOWER on IntegriCloud