LibC: Implement simple posix_spawn{,p}

This does not support file_actions or attributes
This commit is contained in:
Bananymous 2025-11-09 16:14:37 +02:00
parent 4b36e5197d
commit 2f38306c6b
3 changed files with 71 additions and 2 deletions

View File

@ -34,6 +34,7 @@ set(LIBC_SOURCES
semaphore.cpp
setjmp.cpp
signal.cpp
spawn.cpp
stdio.cpp
stdlib.cpp
string.cpp

View File

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

View File

@ -0,0 +1,68 @@
#include <BAN/Debug.h>
#include <errno.h>
#include <spawn.h>
#include <stdlib.h>
#include <unistd.h>
#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)