skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit 1b0fce052f0adf2e4f3da7762cd9f4885a5d9da8
parent e396f10de8804d1603a907069557b80e042f56ca
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Mon, 11 Sep 2023 18:24:05 +0000

 Add posix_spawn_file_actions_addchdir() support

Signed-off-by: Laurent Bercot <ska@appnovation.com>

Diffstat:
Mconfigure | 2++
Msrc/include/skalibs/cspawn.h | 18++++++++++++------
Msrc/libstddjb/cspawn.c | 54++++++++++++++++++++++++++++++++++++++++++++++--------
Asrc/sysdeps/tryposixspawnchdir.c | 31+++++++++++++++++++++++++++++++
Asrc/sysdeps/tryposixspawnchdirnp.c | 31+++++++++++++++++++++++++++++++
5 files changed, 122 insertions(+), 14 deletions(-)

diff --git a/configure b/configure @@ -642,6 +642,8 @@ choose cl getrandom 'getrandom()' choose cl grndinsecure 'GRND_INSECURE' choose cl chroot 'chroot()' choose cl posixspawnsetsid 'POSIX_SPAWN_SETSID' $spawn_lib +choose cl posixspawnchdir 'posix_spawn_file_actions_addchdir()' $spawn_lib +choose cl posixspawnchdirnp 'posix_spawn_file_actions_addchdir_np()' $spawn_lib # Here are the evil irreducible run-time sysdeps. choose clr devurandom '/dev/urandom' diff --git a/src/include/skalibs/cspawn.h b/src/include/skalibs/cspawn.h @@ -10,10 +10,15 @@ #define CSPAWN_FLAGS_SIGBLOCKNONE 0x0002U #define CSPAWN_FLAGS_SETSID 0x0004U -#define CSPAWN_FA_CLOSE 0x0000U -#define CSPAWN_FA_COPY 0x0001U -#define CSPAWN_FA_MOVE 0x0002U -#define CSPAWN_FA_OPEN 0x0003U +enum cspawn_fileaction_type_e +{ + CSPAWN_FA_CLOSE, + CSPAWN_FA_COPY, + CSPAWN_FA_MOVE, + CSPAWN_FA_OPEN, + CSPAWN_FA_CHDIR, + CSPAWN_FA_FCHDIR +} ; struct cspawn_fa_openinfo_s { @@ -27,20 +32,21 @@ union cspawn_fileaction_u { int fd ; int fd2[2] ; + char const *path ; struct cspawn_fa_openinfo_s openinfo ; } ; typedef struct cspawn_fileaction_s cspawn_fileaction, *cspawn_fileaction_ref ; struct cspawn_fileaction_s { - uint32_t type ; + enum cspawn_fileaction_type_e type ; union cspawn_fileaction_u x ; } ; /* Generic interface for posix_spawn() with a fork()+execve() fallback */ -extern pid_t cspawn (char const *, char const *const *, char const *const *, uint32_t, cspawn_fileaction const *, size_t) ; +extern pid_t cspawn (char const *, char const *const *, char const *const *, uint16_t, cspawn_fileaction const *, size_t) ; /* Simple spawn functions with 0 or 1 communicating fds. */ diff --git a/src/libstddjb/cspawn.c b/src/libstddjb/cspawn.c @@ -18,7 +18,7 @@ #include <skalibs/cspawn.h> -static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) +static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) { for (size_t i = 0 ; i < n ; i++) { @@ -38,6 +38,12 @@ static inline void cspawn_child_exec (char const *prog, char const *const *argv, if (fd_move(fa[i].x.openinfo.fd, fd) == -1) return ; break ; } + case CSPAWN_FA_CHDIR : + if (chdir(fa[i].x.path) == -1) return ; + break ; + case CSPAWN_FA_FCHDIR : + if (fchdir(fa[i].x.fd) == -1) return ; + break ; default : errno = EINVAL ; return ; } @@ -50,7 +56,7 @@ static inline void cspawn_child_exec (char const *prog, char const *const *argv, exec_ae(prog, argv, envp) ; } -static inline pid_t cspawn_fork (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) +static inline pid_t cspawn_fork (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) { pid_t pid ; int p[2] ; @@ -131,7 +137,7 @@ static inline pid_t cspawn_workaround (pid_t pid, int const *p) #endif -static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) +static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) { pid_t pid ; posix_spawnattr_t attr ; @@ -192,6 +198,27 @@ static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, ch e = posix_spawn_file_actions_addopen(&actions, fa[i].x.openinfo.fd, fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ; if (e) goto erractions ; break ; +#ifdef SKALIBS_HASPOSIXSPAWNCHDIR + case CSPAWN_FA_CHDIR : + e = posix_spawn_file_actions_addchdir(&actions, fa[i].x.path) ; + if (e) goto erractions ; + break ; + case CSPAWN_FA_FCHDIR : + e = posix_spawn_file_actions_addfchdir(&actions, fa[i].x.fd) ; + if (e) goto erractions ; + break ; +#else +#ifdef SKALIBS_HASPOSIXSPAWNCHDIRNP + case CSPAWN_FA_CHDIR : + e = posix_spawn_file_actions_addchdir_np(&actions, fa[i].x.path) ; + if (e) goto erractions ; + break ; + case CSPAWN_FA_FCHDIR : + e = posix_spawn_file_actions_addfchdir_np(&actions, fa[i].x.fd) ; + if (e) goto erractions ; + break ; +#endif +#endif default : e = EINVAL ; goto erractions ; @@ -225,25 +252,36 @@ static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, ch return 0 ; } -#ifdef SKALIBS_HASPOSIXSPAWNSETSID +#if defined(SKALIBS_HASPOSIXSPAWNSETSID) && (defined(SKALIBS_HASPOSIXSPAWNCHDIR) || defined(SKALIBS_HASPOSIXSPAWNCHDIRNP)) -pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) { return cspawn_pspawn(prog, argv, envp, flags, fa, n) ; } #else -pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) { - return flags & CSPAWN_FLAGS_SETSID ? cspawn_fork(prog, argv, envp, flags, fa, n) : cspawn_pspawn(prog, argv, envp, flags, fa, n) ; +#if !defined(SKALIBS_HASPOSIXSPAWNSETSID) + if (flags & CSPAWN_FLAGS_SETSID) goto dofork ; +#endif +#if !defined(SKALIBS_HASPOSIXSPAWNCHDIR) && !defined(SKALIBS_HASPOSIXSPAWNCHDIRNP) + for (size_t i = 0 ; i < n ; i++) + if (fa[i].type == CSPAWN_FA_CHDIR || fa[i].type == CSPAWN_FA_FCHDIR) + goto dofork ; +#endif + return cspawn_pspawn(prog, argv, envp, flags, fa, n) ; + + dofork: + return cspawn_fork(prog, argv, envp, flags, fa, n) ; } #endif #else -pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) { return cspawn_fork(prog, argv, envp, flags, fa, n) ; } diff --git a/src/sysdeps/tryposixspawnchdir.c b/src/sysdeps/tryposixspawnchdir.c @@ -0,0 +1,31 @@ +/* ISC license. */ + +#undef _POSIX_C_SOURCE +#undef _XOPEN_SOURCE + +#ifndef _XPG4_2 +#define _XPG4_2 +#endif + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#ifndef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#endif + +#include <spawn.h> + +int main (void) +{ + pid_t pid ; + posix_spawn_file_actions_t actions ; + posix_spawnattr_t attr ; + char *const argv[2] = { "/bin/true", 0 } ; + char *const envp[1] = { 0 } ; + posix_spawn_file_actions_init(&actions) ; + posix_spawn_file_actions_addchdir(&actions, "/") ; + posix_spawn_file_actions_addfchdir(&actions, -1) ; + return posix_spawn(&pid, argv[0], 0, 0, argv, envp) ; +} diff --git a/src/sysdeps/tryposixspawnchdirnp.c b/src/sysdeps/tryposixspawnchdirnp.c @@ -0,0 +1,31 @@ +/* ISC license. */ + +#undef _POSIX_C_SOURCE +#undef _XOPEN_SOURCE + +#ifndef _XPG4_2 +#define _XPG4_2 +#endif + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#ifndef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#endif + +#include <spawn.h> + +int main (void) +{ + pid_t pid ; + posix_spawn_file_actions_t actions ; + posix_spawnattr_t attr ; + char *const argv[2] = { "/bin/true", 0 } ; + char *const envp[1] = { 0 } ; + posix_spawn_file_actions_init(&actions) ; + posix_spawn_file_actions_addchdir_np(&actions, "/") ; + posix_spawn_file_actions_addfchdir_np(&actions, -1) ; + return posix_spawn(&pid, argv[0], 0, 0, argv, envp) ; +}