git: c1be185e3fb9 - main - libsys, libc: provide rfork_thread() and pdrfork_thread() on all arches

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 28 Jan 2026 02:27:20 UTC
The branch main has been updated by kib:

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

commit c1be185e3fb9afd6743683a8f5a43b9c364ab529
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-01-26 16:04:08 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-01-28 02:26:52 +0000

    libsys, libc: provide rfork_thread() and pdrfork_thread() on all arches
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D54898
---
 lib/libsys/Symbol.sys.map         |  2 ++
 lib/libsys/aarch64/Makefile.sys   |  2 ++
 lib/libsys/amd64/Symbol.sys.map   |  5 -----
 lib/libsys/arm/Makefile.sys       |  2 ++
 lib/libsys/i386/Symbol.sys.map    |  5 -----
 lib/libsys/pdrfork_thread_gen.c   | 34 +++++++++++++++++++++++++++++++++
 lib/libsys/powerpc/Makefile.sys   |  2 ++
 lib/libsys/powerpc64/Makefile.sys |  2 ++
 lib/libsys/rfork_thread_gen.c     | 40 +++++++++++++++++++++++++++++++++++++++
 lib/libsys/riscv/Makefile.sys     |  2 ++
 10 files changed, 86 insertions(+), 10 deletions(-)

diff --git a/lib/libsys/Symbol.sys.map b/lib/libsys/Symbol.sys.map
index 63446888c8d4..46767f5b6a4d 100644
--- a/lib/libsys/Symbol.sys.map
+++ b/lib/libsys/Symbol.sys.map
@@ -182,6 +182,7 @@ FBSD_1.0 {
 	rename;
 	revoke;
 	rfork;
+	rfork_thread;
 	rmdir;
 	rtprio;
 	rtprio_thread;
@@ -391,6 +392,7 @@ FBSD_1.8 {
 
 FBSD_1.9 {
 	pdrfork;
+	pdrfork_thread;
 };
 
 FBSDprivate_1.0 {
diff --git a/lib/libsys/aarch64/Makefile.sys b/lib/libsys/aarch64/Makefile.sys
index 04b95602c580..c7aaf52d535a 100644
--- a/lib/libsys/aarch64/Makefile.sys
+++ b/lib/libsys/aarch64/Makefile.sys
@@ -1,6 +1,8 @@
 MIASM:=	${MIASM:Nfreebsd[467]_*}
 
 SRCS+=	__vdso_gettc.c \
+	pdrfork_thread_gen.c \
+	rfork_thread_gen.c \
 	sched_getcpu_gen.c
 
 MDASM=	cerror.S \
diff --git a/lib/libsys/amd64/Symbol.sys.map b/lib/libsys/amd64/Symbol.sys.map
index b037e12d45f5..67a8df7d2a8c 100644
--- a/lib/libsys/amd64/Symbol.sys.map
+++ b/lib/libsys/amd64/Symbol.sys.map
@@ -1,5 +1,4 @@
 FBSD_1.0 {
-	rfork_thread;
 	amd64_get_fsbase;
 	amd64_get_gsbase;
 	amd64_set_fsbase;
@@ -17,10 +16,6 @@ FBSD_1.8 {
 	amd64_set_tlsbase;
 };
 
-FBSD_1.9 {
-	pdrfork_thread;
-};
-
 FBSDprivate_1.0 {
 	_vfork;
 };
diff --git a/lib/libsys/arm/Makefile.sys b/lib/libsys/arm/Makefile.sys
index 27d78978a2f4..424d2f9cc9d8 100644
--- a/lib/libsys/arm/Makefile.sys
+++ b/lib/libsys/arm/Makefile.sys
@@ -1,4 +1,6 @@
 SRCS+=	__vdso_gettc.c \
+	pdrfork_thread_gen.c \
+	rfork_thread_gen.c \
 	sched_getcpu_gen.c
 
 MDASM= \
diff --git a/lib/libsys/i386/Symbol.sys.map b/lib/libsys/i386/Symbol.sys.map
index a35349e4953b..03a28f9e486c 100644
--- a/lib/libsys/i386/Symbol.sys.map
+++ b/lib/libsys/i386/Symbol.sys.map
@@ -10,7 +10,6 @@ FBSD_1.0 {
 	i386_set_ldt;
 	i386_set_watch;
 	i386_vm86;
-	rfork_thread;
 };
 
 FBSD_1.6 {
@@ -20,10 +19,6 @@ FBSD_1.6 {
 	 x86_pkru_unprotect_range;
 };
 
-FBSD_1.9 {
-	pdrfork_thread;
-};
-
 FBSDprivate_1.0 {
 	_vfork;
 };
diff --git a/lib/libsys/pdrfork_thread_gen.c b/lib/libsys/pdrfork_thread_gen.c
new file mode 100644
index 000000000000..d36b2cc11993
--- /dev/null
+++ b/lib/libsys/pdrfork_thread_gen.c
@@ -0,0 +1,34 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2026 The FreeBSD Foundation
+ *
+ * This software were developed by
+ * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
+ * the FreeBSD Foundation.
+ */
+
+#include <sys/types.h>
+#include <sys/procdesc.h>
+#include <errno.h>
+#include <unistd.h>
+
+pid_t
+pdrfork_thread(int *fdp, int pdflags, int rfflags, void *stack_addr,
+    int (*start_fn)(void *), void *arg)
+{
+	pid_t res;
+	int ret;
+
+	/* See comment in rfork_thread_gen.c. */
+	if (stack_addr != NULL) {
+		errno = EOPNOTSUPP;
+		return (-1);
+	}
+	res = pdrfork(fdp, pdflags, rfflags);
+	if (res == 0) {
+		ret = start_fn(arg);
+		_exit(ret);
+	}
+	return (res);
+}
diff --git a/lib/libsys/powerpc/Makefile.sys b/lib/libsys/powerpc/Makefile.sys
index 9979d5179f51..f066aae1a4fa 100644
--- a/lib/libsys/powerpc/Makefile.sys
+++ b/lib/libsys/powerpc/Makefile.sys
@@ -1,4 +1,6 @@
 SRCS+=	__vdso_gettc.c \
+	pdrfork_thread_gen.c \
+	rfork_thread_gen.c \
 	sched_getcpu_gen.c
 
 MDASM+=	cerror.S
diff --git a/lib/libsys/powerpc64/Makefile.sys b/lib/libsys/powerpc64/Makefile.sys
index 9979d5179f51..f066aae1a4fa 100644
--- a/lib/libsys/powerpc64/Makefile.sys
+++ b/lib/libsys/powerpc64/Makefile.sys
@@ -1,4 +1,6 @@
 SRCS+=	__vdso_gettc.c \
+	pdrfork_thread_gen.c \
+	rfork_thread_gen.c \
 	sched_getcpu_gen.c
 
 MDASM+=	cerror.S
diff --git a/lib/libsys/rfork_thread_gen.c b/lib/libsys/rfork_thread_gen.c
new file mode 100644
index 000000000000..5a8f6e3f650f
--- /dev/null
+++ b/lib/libsys/rfork_thread_gen.c
@@ -0,0 +1,40 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2026 The FreeBSD Foundation
+ *
+ * This software were developed by
+ * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
+ * the FreeBSD Foundation.
+ */
+
+#include <errno.h>
+#include <unistd.h>
+
+pid_t
+rfork_thread(int flags, void *stack_addr, int (*start_fn)(void *), void *arg)
+{
+	pid_t res;
+	int ret;
+
+	/*
+	 * Generic implementation cannot switch stacks.  Only
+	 * architecture-specific code knows how to do it.  Require
+	 * that caller knows that, and refuse to do operate if the
+	 * stack was supplied.
+	 *
+	 * Note that implementations that do switch stack, would fault
+	 * immediately if the passed stack is NULL.  They do not need to
+	 * specifically check for the NULL stack value.
+	 */
+	if (stack_addr != NULL) {
+		errno = EOPNOTSUPP;
+		return (-1);
+	}
+	res = rfork(flags);
+	if (res == 0) {
+		ret = start_fn(arg);
+		_exit(ret);
+	}
+	return (res);
+}
diff --git a/lib/libsys/riscv/Makefile.sys b/lib/libsys/riscv/Makefile.sys
index 2ff84735f484..87181565f0be 100644
--- a/lib/libsys/riscv/Makefile.sys
+++ b/lib/libsys/riscv/Makefile.sys
@@ -1,4 +1,6 @@
 SRCS+=	__vdso_gettc.c \
+	pdrfork_thread_gen.c \
+	rfork_thread_gen.c \
 	sched_getcpu_gen.c
 
 MDASM=	cerror.S \