From 2f38306c6beb4db1621ef33222b3e51c53382283 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 9 Nov 2025 16:14:37 +0200 Subject: [PATCH] LibC: Implement simple posix_spawn{,p} This does not support file_actions or attributes --- userspace/libraries/LibC/CMakeLists.txt | 1 + userspace/libraries/LibC/include/spawn.h | 4 +- userspace/libraries/LibC/spawn.cpp | 68 ++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 userspace/libraries/LibC/spawn.cpp diff --git a/userspace/libraries/LibC/CMakeLists.txt b/userspace/libraries/LibC/CMakeLists.txt index 85cd0ade..e280df63 100644 --- a/userspace/libraries/LibC/CMakeLists.txt +++ b/userspace/libraries/LibC/CMakeLists.txt @@ -34,6 +34,7 @@ set(LIBC_SOURCES semaphore.cpp setjmp.cpp signal.cpp + spawn.cpp stdio.cpp stdlib.cpp string.cpp diff --git a/userspace/libraries/LibC/include/spawn.h b/userspace/libraries/LibC/include/spawn.h index 8319cf2d..c1f022d1 100644 --- a/userspace/libraries/LibC/include/spawn.h +++ b/userspace/libraries/LibC/include/spawn.h @@ -24,7 +24,7 @@ typedef int posix_spawn_file_actions_t; #define POSIX_SPAWN_SETSIGDEF 0x10 #define POSIX_SPAWN_SETSIGMASK 0x20 -int posix_spawn(pid_t* __restrict pid, const char* __restrict path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argv[__restrict], char* const envp[__restrict]); +int posix_spawn(pid_t* __restrict pid, const char* __restrict path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argv[], char* const envp[]); int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* file_actions, int fildes); int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* file_actions, int fildes, int newfildes); int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* __restrict file_actions, int fildes, const char* __restrict path, int oflag, mode_t mode); @@ -44,7 +44,7 @@ int posix_spawnattr_setschedparam(posix_spawnattr_t* __restrict attr, const stru int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int schedpolicy); int posix_spawnattr_setsigdefault(posix_spawnattr_t* __restrict attr, const sigset_t* __restrict sigdefault); int posix_spawnattr_setsigmask(posix_spawnattr_t* __restrict attr, const sigset_t* __restrict sigmask); -int posix_spawnp(pid_t* __restrict pid, const char* __restrict file, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argc[__restrict], char* const envp[__restrict]); +int posix_spawnp(pid_t* __restrict pid, const char* __restrict file, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argc[], char* const envp[]); __END_DECLS diff --git a/userspace/libraries/LibC/spawn.cpp b/userspace/libraries/LibC/spawn.cpp new file mode 100644 index 00000000..081b4753 --- /dev/null +++ b/userspace/libraries/LibC/spawn.cpp @@ -0,0 +1,68 @@ +#include + +#include +#include +#include +#include + +#define TODO_FUNC(name, ...) int name(__VA_ARGS__) { dwarnln("TODO: " #name); errno = ENOTSUP; return -1; } + +static int do_posix_spawn(pid_t* __restrict pid, const char* __restrict path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argv[], char* const envp[], bool do_path_resolution) +{ + if (file_actions != nullptr) + { + dwarnln("TODO: posix_spawn with file actions"); + errno = ENOTSUP; + return -1; + } + + if (attrp != nullptr) + { + dwarnln("TODO: posix_spawn with attributes"); + errno = ENOTSUP; + return -1; + } + + const pid_t child_pid = fork(); + if (child_pid == 0) + { + auto* func = do_path_resolution ? execvpe : execve; + func(path, argv, envp); + exit(128 + errno); + } + + if (pid != nullptr) + *pid = child_pid; + + return 0; +} + +int posix_spawn(pid_t* __restrict pid, const char* __restrict path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argv[], char* const envp[]) +{ + return do_posix_spawn(pid, path,file_actions, attrp, argv, envp, false); +} + +int posix_spawnp(pid_t* __restrict pid, const char* __restrict path, const posix_spawn_file_actions_t* file_actions, const posix_spawnattr_t* __restrict attrp, char* const argv[], char* const envp[]) +{ + return do_posix_spawn(pid, path,file_actions, attrp, argv, envp, true); +} + +TODO_FUNC(posix_spawn_file_actions_addclose, posix_spawn_file_actions_t*, int) +TODO_FUNC(posix_spawn_file_actions_adddup2, posix_spawn_file_actions_t*, int, int) +TODO_FUNC(posix_spawn_file_actions_addopen, posix_spawn_file_actions_t* __restrict, int, const char* __restrict, int, mode_t) +TODO_FUNC(posix_spawn_file_actions_destroy, posix_spawn_file_actions_t*) +TODO_FUNC(posix_spawn_file_actions_init, posix_spawn_file_actions_t*) +TODO_FUNC(posix_spawnattr_destroy, posix_spawnattr_t*) +TODO_FUNC(posix_spawnattr_getflags, const posix_spawnattr_t* __restrict, short* __restrict) +TODO_FUNC(posix_spawnattr_getpgroup, const posix_spawnattr_t* __restrict, pid_t* __restrict) +TODO_FUNC(posix_spawnattr_getschedparam, const posix_spawnattr_t* __restrict, struct sched_param* __restrict) +TODO_FUNC(posix_spawnattr_getschedpolicy, const posix_spawnattr_t* __restrict, int* __restrict) +TODO_FUNC(posix_spawnattr_getsigdefault, const posix_spawnattr_t* __restrict, sigset_t* __restrict) +TODO_FUNC(posix_spawnattr_getsigmask, const posix_spawnattr_t* __restrict, sigset_t* __restrict) +TODO_FUNC(posix_spawnattr_init, posix_spawnattr_t*) +TODO_FUNC(posix_spawnattr_setflags, posix_spawnattr_t*, short) +TODO_FUNC(posix_spawnattr_setpgroup, posix_spawnattr_t*, pid_t) +TODO_FUNC(posix_spawnattr_setschedparam, posix_spawnattr_t* __restrict, const struct sched_param* __restrict) +TODO_FUNC(posix_spawnattr_setschedpolicy, posix_spawnattr_t*, int) +TODO_FUNC(posix_spawnattr_setsigdefault, posix_spawnattr_t* __restrict, const sigset_t* __restrict) +TODO_FUNC(posix_spawnattr_setsigmask, posix_spawnattr_t* __restrict, const sigset_t* __restrict)