aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIhor Antonov <ihor@antonovs.family>2023-03-03 05:17:02 +0000
committerKyle Evans <kevans@FreeBSD.org>2023-03-03 05:17:02 +0000
commit75f61ca92098941f73020f46674f0c40db7270fb (patch)
treee85b78393b38cbe29b021a8b0d8ce02240f0f502
parent39ea4280e4c5e41f237e55770f4f62b04b24d899 (diff)
downloadsrc-75f61ca92098941f73020f46674f0c40db7270fb.tar.gz
src-75f61ca92098941f73020f46674f0c40db7270fb.zip
daemon: flatten and simplify fork() logic
Reviewed by: kevans Pull Request: https://github.com/freebsd/freebsd-src/pull/672
-rw-r--r--usr.sbin/daemon/daemon.c53
1 files changed, 29 insertions, 24 deletions
diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c
index 16e22efed68e..964a77deb0f9 100644
--- a/usr.sbin/daemon/daemon.c
+++ b/usr.sbin/daemon/daemon.c
@@ -84,7 +84,7 @@ static void daemon_sleep(time_t, long);
static volatile sig_atomic_t terminate = 0;
static volatile sig_atomic_t child_gone = 0;
-static volatile sig_atomic_t pid = -1;
+static volatile sig_atomic_t pid = 0;
static volatile sig_atomic_t do_log_reopen = 0;
static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h";
@@ -368,34 +368,27 @@ restart:
*/
child_gone = 0;
pid = fork();
- if (pid == -1) {
- warn("fork");
- goto exit;
- } else if (pid > 0) {
- /*
- * Unblock SIGTERM after we know we have a valid
- * child PID to signal.
- */
- if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) {
- warn("sigprocmask");
- goto exit;
- }
- close(pfd[1]);
- pfd[1] = -1;
- }
}
- if (pid <= 0) {
- /* Now that we are the child, write out the pid. */
+
+ /* fork failed, this can only happen when supervision is enabled */
+ if (pid == -1) {
+ warn("fork");
+ goto exit;
+ }
+
+
+ /* fork succeeded, this is child's branch or supervision is disabled */
+ if (pid == 0) {
pidfile_write(child_pidfh);
if (user != NULL) {
restrict_process(user);
}
/*
- * When forking, the child gets the original sigmask,
+ * In supervision mode, the child gets the original sigmask,
* and dup'd pipes.
*/
- if (pid == 0) {
+ if (supervision_enabled) {
close(pfd[0]);
if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) {
err(1, "sigprogmask");
@@ -416,12 +409,24 @@ restart:
}
}
execvp(argv[0], argv);
- /*
- * execvp() failed -- report the error. The child is
- * now running, so the exit status doesn't matter.
- */
+ /* execvp() failed - report error and exit this process */
err(1, "%s", argv[0]);
}
+
+ /*
+ * else: pid > 0
+ * fork succeeded, this is the parent branch, this can only happen when
+ * supervision is enabled
+ *
+ * Unblock SIGTERM after we know we have a valid child PID to signal.
+ */
+ if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) {
+ warn("sigprocmask");
+ goto exit;
+ }
+ close(pfd[1]);
+ pfd[1] = -1;
+
setproctitle("%s[%d]", title, (int)pid);
/*
* As we have closed the write end of pipe for parent process,