Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 3 Mar 2023 05:17:23 GMT
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 75f61ca92098 - main - daemon: flatten and simplify fork() logic
Message-ID:  <202303030517.3235HN2N019606@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=75f61ca92098941f73020f46674f0c40db7270fb

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

    daemon: flatten and simplify fork() logic
    
    Reviewed by:    kevans
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/672
---
 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,



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202303030517.3235HN2N019606>