diff options
author | Florian Weimer <fweimer@redhat.com> | 2018-11-06 16:08:12 +0100 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2018-11-06 16:08:12 +0100 |
commit | 4a938cb273e164a475dc123cc80ea6354d7248d4 (patch) | |
tree | 607dad9fb23f1a248a8009758c03115528dde294 /posix | |
parent | 7597b0c7f711a6918d5804e08508817c72916376 (diff) | |
download | glibc-4a938cb273e164a475dc123cc80ea6354d7248d4.zip glibc-4a938cb273e164a475dc123cc80ea6354d7248d4.tar.gz glibc-4a938cb273e164a475dc123cc80ea6354d7248d4.tar.bz2 |
posix: New function posix_spawn_file_actions_addchdir_np [BZ #17405]
Diffstat (limited to 'posix')
-rw-r--r-- | posix/Makefile | 3 | ||||
-rw-r--r-- | posix/Versions | 3 | ||||
-rw-r--r-- | posix/spawn.h | 7 | ||||
-rw-r--r-- | posix/spawn_faction_addchdir.c | 53 | ||||
-rw-r--r-- | posix/spawn_faction_destroy.c | 3 | ||||
-rw-r--r-- | posix/spawn_int.h | 7 | ||||
-rw-r--r-- | posix/tst-spawn-chdir.c | 192 |
7 files changed, 266 insertions, 2 deletions
diff --git a/posix/Makefile b/posix/Makefile index 8316212..d67f68d 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -55,6 +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 \ spawnattr_init spawnattr_destroy \ spawnattr_getdefault spawnattr_setdefault \ spawnattr_getflags spawnattr_setflags \ @@ -96,7 +97,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-posix_fadvise tst-posix_fadvise64 \ tst-sysconf-empty-chroot tst-glob_symlinks tst-fexecve \ tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \ - bug-regex38 tst-regcomp-truncated + bug-regex38 tst-regcomp-truncated tst-spawn-chdir tests-internal := bug-regex5 bug-regex20 bug-regex33 \ tst-rfc3484 tst-rfc3484-2 tst-rfc3484-3 \ tst-glob_lstat_compat tst-spawn4-compat diff --git a/posix/Versions b/posix/Versions index cad4c23..56ab921 100644 --- a/posix/Versions +++ b/posix/Versions @@ -137,6 +137,9 @@ libc { GLIBC_2.27 { glob; glob64; } + GLIBC_2.29 { + posix_spawn_file_actions_addchdir_np; + } GLIBC_PRIVATE { __libc_fork; __libc_pread; __libc_pwrite; __nanosleep_nocancel; __pause_nocancel; diff --git a/posix/spawn.h b/posix/spawn.h index aafb276..c84ee4b 100644 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -185,6 +185,13 @@ extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t * __file_actions, int __fd, int __newfd) __THROW; +#ifdef __USE_GNU +/* Add an action changing the directory to PATH during spawn. This + affects the subsequent file actions. */ +extern int posix_spawn_file_actions_addchdir_np (posix_spawn_file_actions_t *, + const char *__path) __THROW; +#endif + __END_DECLS #endif /* spawn.h */ diff --git a/posix/spawn_faction_addchdir.c b/posix/spawn_faction_addchdir.c new file mode 100644 index 0000000..0e7a45a --- /dev/null +++ b/posix/spawn_faction_addchdir.c @@ -0,0 +1,53 @@ +/* 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_addchdir_np (posix_spawn_file_actions_t *file_actions, + const char *path) +{ + struct __spawn_action *rec; + + char *path_copy = __strdup (path); + if (path_copy == NULL) + return ENOMEM; + + /* Allocate more memory if needed. */ + if (file_actions->__used == file_actions->__allocated + && __posix_spawn_file_actions_realloc (file_actions) != 0) + { + /* This can only mean we ran out of memory. */ + free (path_copy); + return ENOMEM; + } + + /* Add the new value. */ + rec = &file_actions->__actions[file_actions->__used]; + rec->tag = spawn_do_chdir; + rec->action.chdir_action.path = path_copy; + + /* Account for the new entry. */ + ++file_actions->__used; + + return 0; +} diff --git a/posix/spawn_faction_destroy.c b/posix/spawn_faction_destroy.c index 2a2de4e..05ca9dc 100644 --- a/posix/spawn_faction_destroy.c +++ b/posix/spawn_faction_destroy.c @@ -33,6 +33,9 @@ __posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *file_actions) case spawn_do_open: free (sa->action.open_action.path); break; + case spawn_do_chdir: + free (sa->action.chdir_action.path); + break; case spawn_do_close: case spawn_do_dup2: /* No cleanup required. */ diff --git a/posix/spawn_int.h b/posix/spawn_int.h index 171f67c..9db3555 100644 --- a/posix/spawn_int.h +++ b/posix/spawn_int.h @@ -29,7 +29,8 @@ struct __spawn_action { spawn_do_close, spawn_do_dup2, - spawn_do_open + spawn_do_open, + spawn_do_chdir, } tag; union @@ -50,6 +51,10 @@ struct __spawn_action int oflag; mode_t mode; } open_action; + struct + { + char *path; + } chdir_action; } action; }; diff --git a/posix/tst-spawn-chdir.c b/posix/tst-spawn-chdir.c new file mode 100644 index 0000000..dc14f20 --- /dev/null +++ b/posix/tst-spawn-chdir.c @@ -0,0 +1,192 @@ +/* Test the posix_spawn_file_actions_addchdir_np function. + Copyright (C) 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 <array_length.h> +#include <errno.h> +#include <fcntl.h> +#include <spawn.h> +#include <stdlib.h> +#include <string.h> +#include <support/check.h> +#include <support/support.h> +#include <support/temp_file.h> +#include <support/xstdio.h> +#include <support/xunistd.h> +#include <unistd.h> + +/* Reads the file at PATH, which must consist of exactly one line. + Removes the line terminator at the end of the file. */ +static char * +read_one_line (const char *path) +{ + FILE *fp = xfopen (path, "r"); + char *buffer = NULL; + size_t length = 0; + ssize_t ret = getline (&buffer, &length, fp); + if (ferror (fp)) + FAIL_EXIT1 ("getline: %m"); + if (ret < 1) + FAIL_EXIT1 ("getline returned %zd", ret); + if (fgetc (fp) != EOF) + FAIL_EXIT1 ("trailing bytes in %s", path); + if (ferror (fp)) + FAIL_EXIT1 ("fgetc: %m"); + xfclose (fp); + if (buffer[ret - 1] != '\n') + FAIL_EXIT1 ("missing line terminator in %s", path); + buffer[ret - 1] = 0; + return buffer; +} + +/* Return the path to the "pwd" program. */ +const char * +get_pwd_program (void) +{ + const char *const paths[] = { "/bin/pwd", "/usr/bin/pwd" }; + for (size_t i = 0; i < array_length (paths); ++i) + if (access (paths[i], X_OK) == 0) + return paths[i]; + FAIL_EXIT1 ("cannot find pwd program"); +} + +static int +do_test (void) +{ + /* Directory for temporary file data. Each subtest uses a numeric + subdirectory. */ + char *directory = support_create_temp_directory ("tst-spawn-chdir-"); + { + /* Avoid symbolic links, to get more consistent behavior from the + pwd command. */ + char *tmp = realpath (directory, NULL); + if (tmp == NULL) + FAIL_EXIT1 ("realpath: %m"); + free (directory); + directory = tmp; + } + + char *original_cwd = get_current_dir_name (); + if (original_cwd == NULL) + FAIL_EXIT1 ("get_current_dir_name: %m"); + + 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; + xwaitpid (pid, &status, 0); + TEST_COMPARE (status, 0); + + /* Check that the current directory did not change. */ + { + char *cwd = get_current_dir_name (); + if (cwd == NULL) + FAIL_EXIT1 ("get_current_dir_name: %m"); + TEST_COMPARE_BLOB (original_cwd, strlen (original_cwd), + cwd, strlen (cwd)); + free (cwd); + } + + + /* Check the output from "pwd". */ + { + char *pwd = read_one_line (output_file_path); + TEST_COMPARE_BLOB (iteration_directory, strlen (iteration_directory), + pwd, strlen (pwd)); + free (pwd); + } + + /* This file must now exist. */ + TEST_COMPARE (access (probe_file_path, F_OK), 0); + + TEST_COMPARE (posix_spawn_file_actions_destroy (&actions), 0); + free (pwd_symlink_path); + free (probe_file_path); + free (subdir_path); + free (output_file_path); + } + + free (directory); + + return 0; +} + +#include <support/test-driver.c> |