git: bab6bfa1181d - stable/15 - ctld: Update nchildren directly in wait_for_children

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 23 Jun 2026 16:28:11 UTC
The branch stable/15 has been updated by jhb:

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

commit bab6bfa1181df69dbd34aad1c8fc3048a558db7c
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-04-20 17:19:49 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-06-23 16:05:43 +0000

    ctld: Update nchildren directly in wait_for_children
    
    This results in slightly less duplicated code.
    
    Reviewed by:    asomers
    Sponsored by:   Chelsio Communications
    Differential Revision:  https://reviews.freebsd.org/D56526
    
    (cherry picked from commit 4b79ee8eb139696c1fe845d25a56439d02ab4131)
---
 usr.sbin/ctld/ctld.cc | 28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/usr.sbin/ctld/ctld.cc b/usr.sbin/ctld/ctld.cc
index 2dda88f97b73..8fdb3824bcf7 100644
--- a/usr.sbin/ctld/ctld.cc
+++ b/usr.sbin/ctld/ctld.cc
@@ -2310,18 +2310,17 @@ start_timer(int timeout, bool fatal)
 		log_err(1, "setitimer");
 }
 
-static int
+static void
 wait_for_children(bool block)
 {
 	pid_t pid;
 	int status;
-	int num = 0;
 
-	for (;;) {
-		/*
-		 * If "block" is true, wait for at least one process.
-		 */
-		if (block && num == 0)
+	/*
+	 * If "block" is true, wait for at least one process.
+	 */
+	while (nchildren > 0) {
+		if (block)
 			pid = wait4(-1, &status, 0, NULL);
 		else
 			pid = wait4(-1, &status, WNOHANG, NULL);
@@ -2336,10 +2335,10 @@ wait_for_children(bool block)
 		} else {
 			log_debugx("child process %d terminated gracefully", pid);
 		}
-		num++;
-	}
+		nchildren--;
 
-	return (num);
+		block = false;
+	}
 }
 
 static void
@@ -2358,15 +2357,13 @@ handle_connection(struct portal *portal, freebsd::fd_up fd,
 	if (dont_fork) {
 		log_debugx("incoming connection; not forking due to -d flag");
 	} else {
-		nchildren -= wait_for_children(false);
-		assert(nchildren >= 0);
+		wait_for_children(false);
 
 		while (conf->maxproc() > 0 && nchildren >= conf->maxproc()) {
 			log_debugx("maxproc limit of %d child processes hit; "
 			    "waiting for child process to exit",
 			    conf->maxproc());
-			nchildren -= wait_for_children(true);
-			assert(nchildren >= 0);
+			wait_for_children(true);
 		}
 		log_debugx("incoming connection; forking child process #%d",
 		    nchildren);
@@ -2806,8 +2803,7 @@ main(int argc, char **argv)
 			log_warnx("exiting on signal");
 			return (0);
 		} else {
-			nchildren -= wait_for_children(false);
-			assert(nchildren >= 0);
+			wait_for_children(false);
 			if (timed_out()) {
 				newconf->isns_update();
 			}