summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-09-11 18:24:05 +0000
committerLaurent Bercot <ska@appnovation.com>2023-09-11 18:24:05 +0000
commit1b0fce052f0adf2e4f3da7762cd9f4885a5d9da8 (patch)
treeed62766bb1e9c9ec7a326e2a33424840c4dfdb23
parente396f10de8804d1603a907069557b80e042f56ca (diff)
downloadskalibs-1b0fce052f0adf2e4f3da7762cd9f4885a5d9da8.tar.xz
Add posix_spawn_file_actions_addchdir() support
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rwxr-xr-xconfigure2
-rw-r--r--src/include/skalibs/cspawn.h18
-rw-r--r--src/libstddjb/cspawn.c54
-rw-r--r--src/sysdeps/tryposixspawnchdir.c31
-rw-r--r--src/sysdeps/tryposixspawnchdirnp.c31
5 files changed, 122 insertions, 14 deletions
diff --git a/configure b/configure
index 1e058c0..ff11ca6 100755
--- 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
index 01f6570..fa5c934 100644
--- 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
index 461ccd6..2baad0e 100644
--- 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
new file mode 100644
index 0000000..7c08346
--- /dev/null
+++ 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
new file mode 100644
index 0000000..19481f3
--- /dev/null
+++ 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) ;
+}