git: 204306b0532a - stable/13 - daemon: flatten and simplify fork() logic

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 17 Mar 2023 21:01:36 UTC
The branch stable/13 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=204306b0532a9c9d2fdf07b88e33ef71a921cc29

commit 204306b0532a9c9d2fdf07b88e33ef71a921cc29
Author:     Ihor Antonov <ihor@antonovs.family>
AuthorDate: 2023-03-03 05:17:02 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-03-17 21:01:02 +0000

    daemon: flatten and simplify fork() logic
    
    Reviewed by:    kevans
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/672
    
    (cherry picked from commit 75f61ca92098941f73020f46674f0c40db7270fb)
---
 usr.sbin/daemon/daemon.c | 53 ++++++++++++++++++++++++++----------------------
 1 file 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,