git: 7352ad7b2c0b - stable/14 - libcasper: Use pdfork() for the zygote

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sun, 15 Nov 2026 17:28:48 UTC
The branch stable/14 has been updated by des:

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

commit 7352ad7b2c0b384b307468ccd7c0af6c1b544f41
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-09 22:13:42 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-14 15:49:24 +0000

    libcasper: Use pdfork() for the zygote
    
    Use pdfork(2) rather than fork(2) to fork the zygote, and close the
    descriptor when the main loop terminates.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Sponsored by:   Qualys, Inc.
    Reviewed by:    oshogbo
    Differential Revision:  https://reviews.freebsd.org/D60176
    
    (cherry picked from commit 425e4c102f8aedfc7c439bb01d648aabac798888)
---
 lib/libcasper/libcasper/libcasper_service.c | 12 ++++++++----
 lib/libcasper/libcasper/zygote.c            |  6 +++---
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/lib/libcasper/libcasper/libcasper_service.c b/lib/libcasper/libcasper/libcasper_service.c
index 6e9cd9d3d338..5ea3fa7127be 100644
--- a/lib/libcasper/libcasper/libcasper_service.c
+++ b/lib/libcasper/libcasper/libcasper_service.c
@@ -223,8 +223,9 @@ service_register_core(int fd)
 void
 casper_main_loop(int fd)
 {
+	int pfd, ret = 0;
 
-	if (zygote_init() < 0)
+	if ((pfd = zygote_init()) < 0)
 		_exit(1);
 
 	/*
@@ -233,9 +234,12 @@ casper_main_loop(int fd)
 	service_register_core(fd);
 
 	while (service_have_connections()) {
-		if (!service_poll_dispatch())
-			_exit(1);
+		if (!service_poll_dispatch()) {
+			ret = 1;
+			break;
+		}
 	}
 
-	_exit(0);
+	(void)close(pfd);
+	_exit(ret);
 }
diff --git a/lib/libcasper/libcasper/zygote.c b/lib/libcasper/libcasper/zygote.c
index e0dcc665ee9e..c99e854cbf10 100644
--- a/lib/libcasper/libcasper/zygote.c
+++ b/lib/libcasper/libcasper/zygote.c
@@ -189,13 +189,13 @@ send:
 int
 zygote_init(void)
 {
-	int serrno, sp[2];
+	int pfd, serrno, sp[2];
 	pid_t pid;
 
 	if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sp) == -1)
 		return (-1);
 
-	pid = fork();
+	pid = pdfork(&pfd, 0);
 	switch (pid) {
 	case -1:
 		/* Failure. */
@@ -214,7 +214,7 @@ zygote_init(void)
 		/* Parent. */
 		zygote_sock = sp[0];
 		close(sp[1]);
-		return (0);
+		return (pfd);
 	}
 	/* NOTREACHED */
 }