diff options
| author | Florian Weimer <fweimer@redhat.com> | 2018-12-07 15:00:04 +0100 |
|---|---|---|
| committer | Florian Weimer <fweimer@redhat.com> | 2018-12-07 16:04:05 +0100 |
| commit | 3a3fb7557274108ea3dc5ac62333c808a6c171db (patch) | |
| tree | d021946546bdbbc80dedbf47dc608375cc7092a5 | |
| parent | c37cd4398a684010b39e0c09f1132757e11edbf1 (diff) | |
| download | glibc-3a3fb7557274108ea3dc5ac62333c808a6c171db.tar.xz glibc-3a3fb7557274108ea3dc5ac62333c808a6c171db.zip | |
posix: New function posix_spawn_file_actions_addfchdir_np [BZ #17405]
Along with posix_spawn_file_actions_addchdir,
posix_spawn_file_actions_addfchdir is the subject of a change proposal
for POSIX: <http://austingroupbugs.net/view.php?id=1208>
38 files changed, 291 insertions, 99 deletions
@@ -1,3 +1,71 @@ +2018-12-07 Florian Weimer <fweimer@redhat.com> + + [BZ #17405] + * posix/Makefile (routines): Add spawn_faction_addfchdir. + * posix/Versions (GLIBC_2.29): Export + posix_spawn_file_actions_addfchdir_np. + * posix/spawn.h (posix_spawn_file_actions_addfchdir_np): Declare. + * posix/spawn_faction_destroy.c + (__posix_spawn_file_actions_destroy): Handle spawn_do_fchdir. + * posix/spawn_int.h (struct __spawn_action): Add spawn_do_fchdir + and the field action.fchdir_action. + * posix/tst-spawn-chdir.c (add_chdir): New function. + (do_test): Add do_fchdir loop. Call add_chdir. + * sysdeps/posix/spawni.c (__spawni_child): Handle spawn_do_fchdir. + * sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Likewise. + * sysdeps/mach/hurd/i386/libc.abilist (GLIBC_2.29): Add + posix_spawn_file_actions_addfchdir_np. + * sysdeps/unix/sysv/linux/aarch64/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/alpha/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/arm/libc.abilist (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/hppa/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/i386/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/ia64/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/microblaze/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/nios2/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist + (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/sh/libc.abilist (GLIBC_2.29): Likewise. + * sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/x86_64/64/libc.abilist (GLIBC_2.29): + Likewise. + * sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist (GLIBC_2.29): + Likewise. + 2018-12-06 Joseph Myers <joseph@codesourcery.com> * manual/texinfo.tex: Update to version 2018-09-21.20 with @@ -30,10 +30,12 @@ Major new features: HTM state is saved and restore lazily (the state being saved even when the process actually does not use HTM). -* The posix_spawn_file_actions_addchdir_np function has been added, - enabling posix_spawn and posix_spawnp to run the new process in a - different directory. This is a GNU extension and similar to the - Solaris function of the same name. +* The functions posix_spawn_file_actions_addchdir_np and + posix_spawn_file_actions_addfchdir_np have been added, enabling + posix_spawn and posix_spawnp to run the new process in a different + directory. These functions are GNU extensions. The function + posix_spawn_file_actions_addchdir_np is similar to the Solaris function + of the same name. * The popen and system do not run atfork handlers anymore (BZ#17490). Although it is a possible POSIX violation, the POSIX rationale in diff --git a/posix/Makefile b/posix/Makefile index a8fb1e1839..d5a6844c88 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -55,7 +55,7 @@ routines := \ pread pwrite pread64 pwrite64 \ spawn_faction_init spawn_faction_destroy spawn_faction_addclose \ spawn_faction_addopen spawn_faction_adddup2 spawn_valid_fd \ - spawn_faction_addchdir \ + spawn_faction_addchdir spawn_faction_addfchdir \ spawnattr_init spawnattr_destroy \ spawnattr_getdefault spawnattr_setdefault \ spawnattr_getflags spawnattr_setflags \ diff --git a/posix/Versions b/posix/Versions index 56ab921562..ad693ae9a7 100644 --- a/posix/Versions +++ b/posix/Versions @@ -139,6 +139,7 @@ libc { } GLIBC_2.29 { posix_spawn_file_actions_addchdir_np; + posix_spawn_file_actions_addfchdir_np; } GLIBC_PRIVATE { __libc_fork; __libc_pread; __libc_pwrite; diff --git a/posix/spawn.h b/posix/spawn.h index c84ee4bf72..8c40b4d28b 100644 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -190,6 +190,12 @@ extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t * affects the subsequent file actions. */ extern int posix_spawn_file_actions_addchdir_np (posix_spawn_file_actions_t *, const char *__path) __THROW; + +/* Add an action changing the directory to FD during spawn. This + affects the subsequent file actions. FD is not duplicated and must + be open when the file action is executed. */ +extern int posix_spawn_file_actions_addfchdir_np (posix_spawn_file_actions_t *, + int __fd) __THROW; #endif __END_DECLS diff --git a/posix/spawn_faction_addfchdir.c b/posix/spawn_faction_addfchdir.c new file mode 100644 index 0000000000..6f069df29e --- /dev/null +++ b/posix/spawn_faction_addfchdir.c @@ -0,0 +1,46 @@ +/* Add a directory change to a file action list for posix_spawn. + Copyright (C) 2000-2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <spawn.h> +#include <string.h> + +#include "spawn_int.h" + +int +posix_spawn_file_actions_addfchdir_np (posix_spawn_file_actions_t *actions, + int fd) +{ + struct __spawn_action *rec; + + /* Allocate more memory if needed. */ + if (actions->__used == actions->__allocated + && __posix_spawn_file_actions_realloc (actions) != 0) + /* This can only mean we ran out of memory. */ + return ENOMEM; + + /* Add the new value. */ + rec = &actions->__actions[actions->__used]; + rec->tag = spawn_do_fchdir; + rec->action.fchdir_action.fd = fd; + + /* Account for the new entry. */ + ++actions->__used; + + return 0; +} diff --git a/posix/spawn_faction_destroy.c b/posix/spawn_faction_destroy.c index 05ca9dc13b..da172a894e 100644 --- a/posix/spawn_faction_destroy.c +++ b/posix/spawn_faction_destroy.c @@ -38,6 +38,7 @@ __posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *file_actions) break; case spawn_do_close: case spawn_do_dup2: + case spawn_do_fchdir: /* No cleanup required. */ break; } diff --git a/posix/spawn_int.h b/posix/spawn_int.h index 9db35553c2..0d0e438ff6 100644 --- a/posix/spawn_int.h +++ b/posix/spawn_int.h @@ -31,6 +31,7 @@ struct __spawn_action spawn_do_dup2, spawn_do_open, spawn_do_chdir, + spawn_do_fchdir, } tag; union @@ -55,6 +56,10 @@ struct __spawn_action { char *path; } chdir_action; + struct + { + int fd; + } fchdir_action; } action; }; diff --git a/posix/tst-spawn-chdir.c b/posix/tst-spawn-chdir.c index dc14f2059b..8f447955c1 100644 --- a/posix/tst-spawn-chdir.c +++ b/posix/tst-spawn-chdir.c @@ -20,11 +20,13 @@ #include <errno.h> #include <fcntl.h> #include <spawn.h> +#include <stdbool.h> #include <stdlib.h> #include <string.h> #include <support/check.h> #include <support/support.h> #include <support/temp_file.h> +#include <support/test-driver.h> #include <support/xstdio.h> #include <support/xunistd.h> #include <unistd.h> @@ -64,6 +66,24 @@ get_pwd_program (void) FAIL_EXIT1 ("cannot find pwd program"); } +/* Adds chdir operations to ACTIONS, using PATH. If DO_FCHDIR, use + the open function and TMPFD to emulate chdir using fchdir. */ +static void +add_chdir (posix_spawn_file_actions_t *actions, const char *path, + bool do_fchdir, int tmpfd) +{ + if (do_fchdir) + { + TEST_COMPARE (posix_spawn_file_actions_addopen + (actions, tmpfd, path, O_DIRECTORY | O_RDONLY, 0), 0); + TEST_COMPARE (posix_spawn_file_actions_addfchdir_np + (actions, tmpfd), 0); + TEST_COMPARE (posix_spawn_file_actions_addclose (actions, tmpfd), 0); + } + else + TEST_COMPARE (posix_spawn_file_actions_addchdir_np (actions, path), 0); +} + static int do_test (void) { @@ -87,103 +107,109 @@ do_test (void) int iteration = 0; for (int do_spawnp = 0; do_spawnp < 2; ++do_spawnp) for (int do_overwrite = 0; do_overwrite < 2; ++do_overwrite) - { - ++iteration; - printf ("info: iteration=%d do_spawnp=%d do_overwrite=%d\n", - iteration, do_spawnp, do_overwrite); - - /* The "pwd" program runs in this directory. */ - char *iteration_directory = xasprintf ("%s/%d", directory, iteration); - add_temp_file (iteration_directory); - xmkdir (iteration_directory, 0777); - - /* This file receives output from "pwd". */ - char *output_file_path - = xasprintf ("%s/output-file", iteration_directory); - add_temp_file (output_file_path); - - /* This subdirectory is used for chdir ordering checks. */ - char *subdir_path = xasprintf ("%s/subdir", iteration_directory); - add_temp_file (subdir_path); - xmkdir (subdir_path, 0777); - - /* Also used for checking the order of actions. */ - char *probe_file_path - = xasprintf ("%s/subdir/probe-file", iteration_directory); - add_temp_file (probe_file_path); - TEST_COMPARE (access (probe_file_path, F_OK), -1); - TEST_COMPARE (errno, ENOENT); - - /* This symbolic link is used in a relative path with - posix_spawn. */ - char *pwd_symlink_path - = xasprintf ("%s/subdir/pwd-symlink", iteration_directory); - xsymlink (get_pwd_program (), pwd_symlink_path); - add_temp_file (pwd_symlink_path); - - posix_spawn_file_actions_t actions; - TEST_COMPARE (posix_spawn_file_actions_init (&actions), 0); - TEST_COMPARE (posix_spawn_file_actions_addchdir_np - (&actions, subdir_path), 0); - TEST_COMPARE (posix_spawn_file_actions_addopen - (&actions, 3, /* Arbitrary unused descriptor. */ - "probe-file", - O_WRONLY | O_CREAT | O_EXCL, 0777), 0); - TEST_COMPARE (posix_spawn_file_actions_addclose (&actions, 3), 0); - /* Run the actual in iteration_directory. */ - TEST_COMPARE (posix_spawn_file_actions_addchdir_np (&actions, ".."), 0); - TEST_COMPARE (posix_spawn_file_actions_addopen - (&actions, STDOUT_FILENO, "output-file", - O_WRONLY | O_CREAT | O_EXCL, 0777), 0); - - /* Check that posix_spawn_file_actions_addchdir_np made a copy - of the path. */ - if (do_overwrite) - subdir_path[0] = '\0'; - - char *const argv[] = { (char *) "pwd", NULL }; - char *const envp[] = { NULL } ; - pid_t pid; - if (do_spawnp) - TEST_COMPARE (posix_spawnp (&pid, "pwd", &actions, - NULL, argv, envp), 0); - else - TEST_COMPARE (posix_spawn (&pid, "subdir/pwd-symlink", &actions, - NULL, argv, envp), 0); - TEST_VERIFY (pid > 0); - int status; |
