summaryrefslogtreecommitdiff
path: root/src/libstddjb
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 /src/libstddjb
parente396f10de8804d1603a907069557b80e042f56ca (diff)
downloadskalibs-1b0fce052f0adf2e4f3da7762cd9f4885a5d9da8.tar.xz
Add posix_spawn_file_actions_addchdir() support
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/cspawn.c54
1 files changed, 46 insertions, 8 deletions
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) ;
}