Perform most of the same pre- and post-fork work in posix_spawn as in fork

This commit is contained in:
Ariel Abreu 2023-10-04 23:34:10 -04:00
parent 99020685f1
commit 8327ec8edd
No known key found for this signature in database
GPG Key ID: 5B88AAAF4280706F
2 changed files with 16 additions and 2 deletions

View File

@ -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;

View File

@ -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;