diff --git a/lib/libc/gen/posix_spawn.c b/lib/libc/gen/posix_spawn.c --- a/lib/libc/gen/posix_spawn.c +++ b/lib/libc/gen/posix_spawn.c @@ -236,7 +236,6 @@ #define PSPAWN_STACK_ALIGN(sz) \ (((sz) + PSPAWN_STACK_ALIGNBYTES) & ~PSPAWN_STACK_ALIGNBYTES) -#if defined(__i386__) || defined(__amd64__) /* * Below we'll assume that _RFORK_THREAD_STACK_SIZE is appropriately aligned for * the posix_spawn() case where we do not end up calling execvpe and won't ever @@ -245,7 +244,6 @@ #define _RFORK_THREAD_STACK_SIZE 4096 _Static_assert((_RFORK_THREAD_STACK_SIZE % PSPAWN_STACK_ALIGNMENT) == 0, "Inappropriate stack size alignment"); -#endif static int _posix_spawn_thr(void *data) @@ -287,10 +285,10 @@ pid_t p; int pfd; bool do_pfd; -#ifdef _RFORK_THREAD_STACK_SIZE char *stack; size_t cnt, stacksz; +#if defined(__i386__) || defined(__amd64__) stacksz = _RFORK_THREAD_STACK_SIZE; if (use_env_path) { /* @@ -319,6 +317,9 @@ return (ENOMEM); stacksz = (((uintptr_t)stack + stacksz) & ~PSPAWN_STACK_ALIGNBYTES) - (uintptr_t)stack; +#else + stack = NULL; + stacksz = 0; #endif psa.path = path; psa.fa = fa; @@ -341,13 +342,16 @@ * a special property of the libthr rtld locks ensure that * rtld is operational in the child. In particular, libthr * rtld locks do not store owner' tid into the lock word. - */ -#ifdef _RFORK_THREAD_STACK_SIZE - /* - * x86 stores the return address on the stack, so rfork(2) cannot work - * as-is because the child would clobber the return address of the - * parent. Because of this, we must use rfork_thread instead while - * almost every other arch stores the return address in a register. + * + * x86 stores the return address on the stack, so rfork(2) + * cannot work as-is because the child would clobber the + * return address of the parent. Because of this, we must use + * rfork_thread instead. + * + * Every other architecture stores the return address in a + * register, the trivial rfork_thread() wrapper is provided + * for them. The only minor drawback is that the stack is + * temporarily allocated. */ if (do_pfd) { p = pdrfork_thread(&pfd, PD_CLOEXEC | (*sa)->sa_pdflags, @@ -357,16 +361,7 @@ &psa); } free(stack); -#else - if (do_pfd) { - p = pdrfork(&pfd, PD_CLOEXEC | (*sa)->sa_pdflags, RFSPAWN); - } else { - p = rfork(RFSPAWN); - } - if (p == 0) - /* _posix_spawn_thr does not return */ - _posix_spawn_thr(&psa); -#endif + /* * The above block should leave us in a state where we've either * succeeded and we're ready to process the results, or we need to diff --git a/lib/libsys/Symbol.sys.map b/lib/libsys/Symbol.sys.map --- a/lib/libsys/Symbol.sys.map +++ b/lib/libsys/Symbol.sys.map @@ -182,6 +182,7 @@ rename; revoke; rfork; + rfork_thread; rmdir; rtprio; rtprio_thread; @@ -391,6 +392,7 @@ FBSD_1.9 { pdrfork; + pdrfork_thread; }; FBSDprivate_1.0 { diff --git a/lib/libsys/aarch64/Makefile.sys b/lib/libsys/aarch64/Makefile.sys --- 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 --- 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 @@ 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 --- 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 --- a/lib/libsys/i386/Symbol.sys.map +++ b/lib/libsys/i386/Symbol.sys.map @@ -10,7 +10,6 @@ i386_set_ldt; i386_set_watch; i386_vm86; - rfork_thread; }; FBSD_1.6 { @@ -20,10 +19,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 --- /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 under sponsorship from + * the FreeBSD Foundation. + */ + +#include +#include +#include +#include + +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 --- 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 --- 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 --- /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 under sponsorship from + * the FreeBSD Foundation. + */ + +#include +#include + +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 + * 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 --- 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 \