]> Savannah Git Hosting - gnulib.git/commitdiff
posix_spawn_file_actions_addfchdir-tests: Rename test.
authorBruno Haible <bruno@clisp.org>
Thu, 17 Dec 2020 02:00:25 +0000 (03:00 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 17 Dec 2020 02:00:25 +0000 (03:00 +0100)
* tests/test-posix_spawn-fchdir.c: Renamed from
tests/test-posix_spawn5.c.
* modules/posix_spawn_file_actions_addfchdir-tests (Files, Makefile.am):
Update.

ChangeLog
modules/posix_spawn_file_actions_addfchdir-tests
tests/test-posix_spawn-fchdir.c [new file with mode: 0644]
tests/test-posix_spawn5.c [deleted file]

index db3ed06a09ac17787d4eff47d59fe4436bd62293..d50539adcafd7cde59a46d934ac3a3688840ba7c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2020-12-16  Bruno Haible  <bruno@clisp.org>
 
+       posix_spawn_file_actions_addfchdir-tests: Rename test.
+       * tests/test-posix_spawn-fchdir.c: Renamed from
+       tests/test-posix_spawn5.c.
+       * modules/posix_spawn_file_actions_addfchdir-tests (Files, Makefile.am):
+       Update.
+
        posix_spawn_file_actions_addchdir-tests: Rename test.
        * tests/test-posix_spawn-chdir.c: Renamed from
        tests/test-posix_spawn4.c.
index 809d2381d8d7d7259440a4ab92a6fdefa2c3d4d2..0d8c9903f7be0d36bc0525a68b0dcfd8b467a81a 100644 (file)
@@ -1,6 +1,6 @@
 Files:
 tests/test-posix_spawn_file_actions_addfchdir.c
-tests/test-posix_spawn5.c
+tests/test-posix_spawn-fchdir.c
 tests/signature.h
 tests/macros.h
 
@@ -16,6 +16,6 @@ TESTS += test-posix_spawn_file_actions_addfchdir
 check_PROGRAMS += test-posix_spawn_file_actions_addfchdir
 
 if POSIX_SPAWN_PORTED
-TESTS += test-posix_spawn5
-check_PROGRAMS += test-posix_spawn5
+TESTS += test-posix_spawn-fchdir
+check_PROGRAMS += test-posix_spawn-fchdir
 endif
diff --git a/tests/test-posix_spawn-fchdir.c b/tests/test-posix_spawn-fchdir.c
new file mode 100644 (file)
index 0000000..c5ca2a3
--- /dev/null
@@ -0,0 +1,169 @@
+/* Test of posix_spawn() function with 'fchdir' action.
+   Copyright (C) 2008-2020 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2018.  */
+
+#include <config.h>
+
+#include <spawn.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include "findprog.h"
+
+static int
+fd_safer (int fd)
+{
+  if (0 <= fd && fd <= 2)
+    {
+      int f = fd_safer (dup (fd));
+      int e = errno;
+      close (fd);
+      errno = e;
+      fd = f;
+    }
+
+  return fd;
+}
+
+static void
+test (const char *pwd_prog)
+{
+  char *argv[2] = { (char *) "pwd", NULL };
+  int rootfd;
+  int ifd[2];
+  sigset_t blocked_signals;
+  sigset_t fatal_signal_set;
+  posix_spawn_file_actions_t actions;
+  bool actions_allocated;
+  posix_spawnattr_t attrs;
+  bool attrs_allocated;
+  int err;
+  pid_t child;
+  int fd;
+  FILE *fp;
+  char line[80];
+  int status;
+  int exitstatus;
+
+  rootfd = open ("/", O_RDONLY);
+  if (rootfd < 0)
+    {
+      perror ("cannot open directory");
+      exit (1);
+    }
+  if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
+    {
+      perror ("cannot create pipe");
+      exit (1);
+    }
+  sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
+  sigemptyset (&fatal_signal_set);
+  sigaddset (&fatal_signal_set, SIGINT);
+  sigaddset (&fatal_signal_set, SIGTERM);
+  sigaddset (&fatal_signal_set, SIGHUP);
+  sigaddset (&fatal_signal_set, SIGPIPE);
+  sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
+  actions_allocated = false;
+  attrs_allocated = false;
+  if ((err = posix_spawn_file_actions_init (&actions)) != 0
+      || (actions_allocated = true,
+          (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
+          || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
+          || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
+          || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
+          || (err = posix_spawn_file_actions_addfchdir (&actions, rootfd)) != 0
+          || (err = posix_spawnattr_init (&attrs)) != 0
+          || (attrs_allocated = true,
+              (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
+              || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
+          || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0))
+    {
+      if (actions_allocated)
+        posix_spawn_file_actions_destroy (&actions);
+      if (attrs_allocated)
+        posix_spawnattr_destroy (&attrs);
+      sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
+      errno = err;
+      perror ("subprocess failed");
+      exit (1);
+    }
+  posix_spawn_file_actions_destroy (&actions);
+  posix_spawnattr_destroy (&attrs);
+  sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
+  close (ifd[1]);
+  fd = ifd[0];
+  fp = fdopen (fd, "r");
+  if (fp == NULL)
+    {
+      fprintf (stderr, "fdopen() failed\n");
+      exit (1);
+    }
+  if (fread (line, 1, 80, fp) < 2)
+    {
+      fprintf (stderr, "could not read expected output\n");
+      exit (1);
+    }
+  if (memcmp (line, "/\n", 2) != 0)
+    {
+      fprintf (stderr, "read output is not the expected output");
+      exit (1);
+    }
+  fclose (fp);
+  status = 0;
+  while (waitpid (child, &status, 0) != child)
+    ;
+  if (!WIFEXITED (status))
+    {
+      fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
+      exit (1);
+    }
+  exitstatus = WEXITSTATUS (status);
+  if (exitstatus != 0)
+    {
+      fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
+      exit (1);
+    }
+}
+
+int
+main ()
+{
+  test ("pwd");
+
+  /* Verify that if a program is given as a relative file name with at least one
+     slash, it is interpreted w.r.t. the current directory after fchdir has been
+     executed.  */
+  {
+    const char *abs_pwd_prog = find_in_path ("pwd");
+
+    if (abs_pwd_prog != NULL
+        && abs_pwd_prog[0] == '/'
+        && abs_pwd_prog[1] != '0' && abs_pwd_prog[1] != '/')
+      test (&abs_pwd_prog[1]);
+  }
+
+  return 0;
+}
diff --git a/tests/test-posix_spawn5.c b/tests/test-posix_spawn5.c
deleted file mode 100644 (file)
index c5ca2a3..0000000
+++ /dev/null
@@ -1,169 +0,0 @@
-/* Test of posix_spawn() function with 'fchdir' action.
-   Copyright (C) 2008-2020 Free Software Foundation, Inc.
-
-   This program is free software: you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program 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 General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
-
-/* Written by Bruno Haible <bruno@clisp.org>, 2018.  */
-
-#include <config.h>
-
-#include <spawn.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include "findprog.h"
-
-static int
-fd_safer (int fd)
-{
-  if (0 <= fd && fd <= 2)
-    {
-      int f = fd_safer (dup (fd));
-      int e = errno;
-      close (fd);
-      errno = e;
-      fd = f;
-    }
-
-  return fd;
-}
-
-static void
-test (const char *pwd_prog)
-{
-  char *argv[2] = { (char *) "pwd", NULL };
-  int rootfd;
-  int ifd[2];
-  sigset_t blocked_signals;
-  sigset_t fatal_signal_set;
-  posix_spawn_file_actions_t actions;
-  bool actions_allocated;
-  posix_spawnattr_t attrs;
-  bool attrs_allocated;
-  int err;
-  pid_t child;
-  int fd;
-  FILE *fp;
-  char line[80];
-  int status;
-  int exitstatus;
-
-  rootfd = open ("/", O_RDONLY);
-  if (rootfd < 0)
-    {
-      perror ("cannot open directory");
-      exit (1);
-    }
-  if (pipe (ifd) < 0 || (ifd[0] = fd_safer (ifd[0])) < 0)
-    {
-      perror ("cannot create pipe");
-      exit (1);
-    }
-  sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
-  sigemptyset (&fatal_signal_set);
-  sigaddset (&fatal_signal_set, SIGINT);
-  sigaddset (&fatal_signal_set, SIGTERM);
-  sigaddset (&fatal_signal_set, SIGHUP);
-  sigaddset (&fatal_signal_set, SIGPIPE);
-  sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL);
-  actions_allocated = false;
-  attrs_allocated = false;
-  if ((err = posix_spawn_file_actions_init (&actions)) != 0
-      || (actions_allocated = true,
-          (err = posix_spawn_file_actions_adddup2 (&actions, ifd[1], STDOUT_FILENO)) != 0
-          || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0
-          || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0
-          || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0
-          || (err = posix_spawn_file_actions_addfchdir (&actions, rootfd)) != 0
-          || (err = posix_spawnattr_init (&attrs)) != 0
-          || (attrs_allocated = true,
-              (err = posix_spawnattr_setsigmask (&attrs, &blocked_signals)) != 0
-              || (err = posix_spawnattr_setflags (&attrs, POSIX_SPAWN_SETSIGMASK)) != 0)
-          || (err = posix_spawnp (&child, pwd_prog, &actions, &attrs, argv, environ)) != 0))
-    {
-      if (actions_allocated)
-        posix_spawn_file_actions_destroy (&actions);
-      if (attrs_allocated)
-        posix_spawnattr_destroy (&attrs);
-      sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
-      errno = err;
-      perror ("subprocess failed");
-      exit (1);
-    }
-  posix_spawn_file_actions_destroy (&actions);
-  posix_spawnattr_destroy (&attrs);
-  sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL);
-  close (ifd[1]);
-  fd = ifd[0];
-  fp = fdopen (fd, "r");
-  if (fp == NULL)
-    {
-      fprintf (stderr, "fdopen() failed\n");
-      exit (1);
-    }
-  if (fread (line, 1, 80, fp) < 2)
-    {
-      fprintf (stderr, "could not read expected output\n");
-      exit (1);
-    }
-  if (memcmp (line, "/\n", 2) != 0)
-    {
-      fprintf (stderr, "read output is not the expected output");
-      exit (1);
-    }
-  fclose (fp);
-  status = 0;
-  while (waitpid (child, &status, 0) != child)
-    ;
-  if (!WIFEXITED (status))
-    {
-      fprintf (stderr, "subprocess terminated with unexpected wait status %d\n", status);
-      exit (1);
-    }
-  exitstatus = WEXITSTATUS (status);
-  if (exitstatus != 0)
-    {
-      fprintf (stderr, "subprocess terminated with unexpected exit status %d\n", exitstatus);
-      exit (1);
-    }
-}
-
-int
-main ()
-{
-  test ("pwd");
-
-  /* Verify that if a program is given as a relative file name with at least one
-     slash, it is interpreted w.r.t. the current directory after fchdir has been
-     executed.  */
-  {
-    const char *abs_pwd_prog = find_in_path ("pwd");
-
-    if (abs_pwd_prog != NULL
-        && abs_pwd_prog[0] == '/'
-        && abs_pwd_prog[1] != '0' && abs_pwd_prog[1] != '/')
-      test (&abs_pwd_prog[1]);
-  }
-
-  return 0;
-}