From 8327ec8edd8336d377e1f11e6b43406c26760234 Mon Sep 17 00:00:00 2001 From: Ariel Abreu Date: Wed, 4 Oct 2023 23:34:10 -0400 Subject: [PATCH] Perform most of the same pre- and post-fork work in posix_spawn as in fork --- .../emulation/linux/process/posix_spawn.c | 15 +++++++++++++-- libsyscall/wrappers/_libkernel_init.h | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/darling/src/libsystem_kernel/emulation/linux/process/posix_spawn.c b/darling/src/libsystem_kernel/emulation/linux/process/posix_spawn.c index 66a189e..50a74c8 100644 --- a/darling/src/libsystem_kernel/emulation/linux/process/posix_spawn.c +++ b/darling/src/libsystem_kernel/emulation/linux/process/posix_spawn.c @@ -27,6 +27,7 @@ #include "../unistd/fchdir.h" #include "../fcntl/fcntl.h" #include "../dirent/getdirentries.h" +#include "../../../libsyscall/wrappers/_libkernel_init.h" // for debugging only; remove before committing #include "../signal/kill.h" @@ -40,6 +41,8 @@ #define LINUX_ADDR_NO_RANDOMIZE 0x40000 +extern _libkernel_functions_t _libkernel_functions; + long sys_posix_spawn(int* pid, const char* path, const struct _posix_spawn_args_desc* desc, char** argvp, char** envp) { @@ -53,14 +56,20 @@ long sys_posix_spawn(int* pid, const char* path, const struct _posix_spawn_args_ if (ret < 0) return ret; + // we don't want to call user atfork callbacks, but we *do* want to call most + // libsystem atfork callbacks to set up the environment for some of the calls we need + // to make for the setup + + _libkernel_functions->posix_spawn_prepare(); + if ((my_pid = sys_fork()) == 0) { - mach_driver_init(NULL); - // child // close the reading side close_internal(pipe[0]); + _libkernel_functions->posix_spawn_child(); + no_fork: if (desc && desc->attrp) { @@ -343,6 +352,8 @@ fail: // close the writing side close_internal(pipe[1]); + _libkernel_functions->posix_spawn_parent(); + if (my_pid < 0) { ret = my_pid; diff --git a/libsyscall/wrappers/_libkernel_init.h b/libsyscall/wrappers/_libkernel_init.h index 7ade448..cdc793e 100644 --- a/libsyscall/wrappers/_libkernel_init.h +++ b/libsyscall/wrappers/_libkernel_init.h @@ -71,6 +71,9 @@ typedef const struct _libkernel_functions { /* Subsequent versions must only add pointers! */ #ifdef DARLING int (*dyld_func_lookup)(const char*,void**); + void (*posix_spawn_prepare)(void); + void (*posix_spawn_parent)(void); + void (*posix_spawn_child)(void); #endif } *_libkernel_functions_t;