about summary refs log tree commit diff
path: root/posix/tst-spawn-chdir.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2018-12-07 15:00:04 +0100
committerFlorian Weimer <fweimer@redhat.com>2018-12-07 16:04:05 +0100
commit3a3fb7557274108ea3dc5ac62333c808a6c171db (patch)
treed021946546bdbbc80dedbf47dc608375cc7092a5 /posix/tst-spawn-chdir.c
parentc37cd4398a684010b39e0c09f1132757e11edbf1 (diff)
downloadglibc-3a3fb7557274108ea3dc5ac62333c808a6c171db.tar.gz
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>
Diffstat (limited to 'posix/tst-spawn-chdir.c')
-rw-r--r--posix/tst-spawn-chdir.c214
1 files changed, 120 insertions, 94 deletions
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;
-        xwaitpid (pid, &status, 0);
-        TEST_COMPARE (status, 0);
-
-        /* Check that the current directory did not change.  */
+      for (int do_fchdir = 0; do_fchdir < 2; ++do_fchdir)
         {
-          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);
+          /* This subtest does not make sense for fchdir.  */
+          if (do_overwrite && do_fchdir)
+            continue;
+
+          ++iteration;
+          if (test_verbose > 0)
+            printf ("info: iteration=%d do_spawnp=%d do_overwrite=%d"
+                    " do_fchdir=%d\n",
+                    iteration, do_spawnp, do_overwrite, do_fchdir);
+
+          /* 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);
+          add_chdir (&actions, subdir_path, do_fchdir, 4);
+          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.  */
+          add_chdir (&actions, "..", do_fchdir, 5);
+          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);
         }
 
-
-        /* 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;