aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-11-09 22:13:42 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-11-09 22:13:42 +0000
commit425e4c102f8aedfc7c439bb01d648aabac798888 (patch)
tree86fe96099422d1d745a3be92f015cb66d588e1c1
parent71979d8d10c187f195dfd1fbb438d578f1aa1ac8 (diff)
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
-rw-r--r--lib/libcasper/libcasper/libcasper_service.c12
-rw-r--r--lib/libcasper/libcasper/zygote.c6
2 files changed, 11 insertions, 7 deletions
diff --git a/lib/libcasper/libcasper/libcasper_service.c b/lib/libcasper/libcasper/libcasper_service.c
index b951de58d229..402a266bce68 100644
--- a/lib/libcasper/libcasper/libcasper_service.c
+++ b/lib/libcasper/libcasper/libcasper_service.c
@@ -222,8 +222,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);
/*
@@ -232,9 +233,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 574d80a16b01..2736957e82a1 100644
--- a/lib/libcasper/libcasper/zygote.c
+++ b/lib/libcasper/libcasper/zygote.c
@@ -188,13 +188,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, PD_NOWAITPID);
switch (pid) {
case -1:
/* Failure. */
@@ -213,7 +213,7 @@ zygote_init(void)
/* Parent. */
zygote_sock = sp[0];
close(sp[1]);
- return (0);
+ return (pfd);
}
/* NOTREACHED */
}