add posix_spawn [f]chdir file actions
authorRich Felker <dalias@aerifal.cx>
Fri, 30 Aug 2019 20:21:36 +0000 (16:21 -0400)
committerRich Felker <dalias@aerifal.cx>
Fri, 30 Aug 2019 20:21:36 +0000 (16:21 -0400)
these are presently extensions, thus named with _np to match glibc and
other implementations that provide them; however they are likely to be
standardized in the future without the _np suffix as a result of
Austin Group issue 1208. if so, both names will be kept as aliases.

include/spawn.h
src/process/fdop.h
src/process/posix_spawn.c
src/process/posix_spawn_file_actions_addchdir.c [new file with mode: 0644]
src/process/posix_spawn_file_actions_addfchdir.c [new file with mode: 0644]

index c9bd1939e05b108ea6c42ac5100f84a6b9dbb2bc..8eb73e003a65e3cb40abdc3c47371084522b9864 100644 (file)
@@ -71,6 +71,11 @@ int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *__restrict, int
 int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *, int);
 int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *, int, int);
 
+#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
+int posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t *__restrict, const char *__restrict);
+int posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t *, int);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index 00b875143afa674eade2f27fc98df08cce8a7133..5adf1443874854c6a7a33c4b641ff18c152a0f38 100644 (file)
@@ -1,6 +1,8 @@
 #define FDOP_CLOSE 1
 #define FDOP_DUP2 2
 #define FDOP_OPEN 3
+#define FDOP_CHDIR 4
+#define FDOP_FCHDIR 5
 
 struct fdop {
        struct fdop *next, *prev;
index 306faa055af0512d06327a4e794bc2697777e5e9..29652197c58cbff007a3251b89a699e69dbbb2f7 100644 (file)
@@ -125,6 +125,14 @@ static int child(void *args_vp)
                                        __syscall(SYS_close, fd);
                                }
                                break;
+                       case FDOP_CHDIR:
+                               ret = __syscall(SYS_chdir, op->path);
+                               if (ret<0) goto fail;
+                               break;
+                       case FDOP_FCHDIR:
+                               ret = __syscall(SYS_fchdir, op->fd);
+                               if (ret<0) goto fail;
+                               break;
                        }
                }
        }
diff --git a/src/process/posix_spawn_file_actions_addchdir.c b/src/process/posix_spawn_file_actions_addchdir.c
new file mode 100644 (file)
index 0000000..7f2590a
--- /dev/null
@@ -0,0 +1,18 @@
+#include <spawn.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "fdop.h"
+
+int posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t *restrict fa, const char *restrict path)
+{
+       struct fdop *op = malloc(sizeof *op + strlen(path) + 1);
+       if (!op) return ENOMEM;
+       op->cmd = FDOP_CHDIR;
+       op->fd = -1;
+       strcpy(op->path, path);
+       if ((op->next = fa->__actions)) op->next->prev = op;
+       op->prev = 0;
+       fa->__actions = op;
+       return 0;
+}
diff --git a/src/process/posix_spawn_file_actions_addfchdir.c b/src/process/posix_spawn_file_actions_addfchdir.c
new file mode 100644 (file)
index 0000000..436c683
--- /dev/null
@@ -0,0 +1,17 @@
+#include <spawn.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "fdop.h"
+
+int posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t *fa, int fd)
+{
+       struct fdop *op = malloc(sizeof *op);
+       if (!op) return ENOMEM;
+       op->cmd = FDOP_FCHDIR;
+       op->fd = fd;
+       if ((op->next = fa->__actions)) op->next->prev = op;
+       op->prev = 0;
+       fa->__actions = op;
+       return 0;
+}