changeset 30238:0545c9f5a831

A new test for posix_spawn.
author Bruno Haible <bruno@clisp.org>
date Mon, 29 Sep 2008 21:42:51 +0200
parents 83d95ecaf5ea
children aa4b5a65b7dd
files ChangeLog modules/posix_spawnp-tests tests/test-posix_spawn.c tests/test-posix_spawn.in.sh tests/test-posix_spawn1.c tests/test-posix_spawn1.in.sh tests/test-posix_spawn2.c tests/test-posix_spawn2.in.sh
diffstat 8 files changed, 372 insertions(+), 183 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Sep 29 15:25:12 2008 +0200
+++ b/ChangeLog	Mon Sep 29 21:42:51 2008 +0200
@@ -1,3 +1,13 @@
+2008-09-29  Bruno Haible  <bruno@clisp.org>
+
+	* tests/test-posix_spawn1.c: Renamed from tests/test-posix_spawn.c.
+	* tests/test-posix_spawn1.in.sh: Renamed from
+	tests/test-posix_spawn.in.sh.
+	* tests/test-posix_spawn2.c: New file.
+	* tests/test-posix_spawn2.in.sh: New file.
+	* modules/posix_spawnp-tests (Files): Update.
+	(Makefile.am): Update. Add test-posix_spawn2 to the tests.
+
 2008-09-29  Bruno Haible  <bruno@clisp.org>
 
 	Propagate effects of putenv/setenv/unsetenv to child processes.
--- a/modules/posix_spawnp-tests	Mon Sep 29 15:25:12 2008 +0200
+++ b/modules/posix_spawnp-tests	Mon Sep 29 21:42:51 2008 +0200
@@ -1,6 +1,8 @@
 Files:
-tests/test-posix_spawn.c
-tests/test-posix_spawn.in.sh
+tests/test-posix_spawn1.c
+tests/test-posix_spawn1.in.sh
+tests/test-posix_spawn2.c
+tests/test-posix_spawn2.in.sh
 
 Depends-on:
 posix_spawn_file_actions_init
@@ -19,11 +21,17 @@
 configure.ac:
 
 Makefile.am:
-TESTS += test-posix_spawn
-check_PROGRAMS += test-posix_spawn
+TESTS += test-posix_spawn1 test-posix_spawn2
+check_PROGRAMS += test-posix_spawn1 test-posix_spawn2
 
-BUILT_SOURCES += test-posix_spawn.sh
-test-posix_spawn.sh: test-posix_spawn.in.sh
-	cp $(srcdir)/test-posix_spawn.in.sh $@-t
+BUILT_SOURCES += test-posix_spawn1.sh
+test-posix_spawn1.sh: test-posix_spawn1.in.sh
+	cp $(srcdir)/test-posix_spawn1.in.sh $@-t
 	mv $@-t $@
-MOSTLYCLEANFILES += test-posix_spawn.sh test-posix_spawn.sh-t
+MOSTLYCLEANFILES += test-posix_spawn1.sh test-posix_spawn1.sh-t
+
+BUILT_SOURCES += test-posix_spawn2.sh
+test-posix_spawn2.sh: test-posix_spawn2.in.sh
+	cp $(srcdir)/test-posix_spawn2.in.sh $@-t
+	mv $@-t $@
+MOSTLYCLEANFILES += test-posix_spawn2.sh test-posix_spawn2.sh-t
--- a/tests/test-posix_spawn.c	Mon Sep 29 15:25:12 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,173 +0,0 @@
-/* Test of posix_spawn() function.
-   Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.  */
-
-/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
-
-#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>
-
-extern char **environ;
-
-#ifndef STDIN_FILENO
-# define STDIN_FILENO 0
-#endif
-#ifndef STDOUT_FILENO
-# define STDOUT_FILENO 1
-#endif
-#ifndef STDERR_FILENO
-# define STDERR_FILENO 2
-#endif
-
-#ifndef WTERMSIG
-# define WTERMSIG(x) ((x) & 0x7f)
-#endif
-#ifndef WCOREDUMP
-# define WCOREDUMP(x) ((x) & 0x80)
-#endif
-#ifndef WEXITSTATUS
-# define WEXITSTATUS(x) (((x) >> 8) & 0xff)
-#endif
-#ifndef WIFSIGNALED
-# define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
-#endif
-#ifndef WIFEXITED
-# define WIFEXITED(x) (WTERMSIG (x) == 0)
-#endif
-#ifndef WIFSTOPPED
-# define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
-#endif
-
-#define CHILD_PROGRAM_FILENAME "test-posix_spawn.sh"
-
-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;
-}  
-
-int
-main ()
-{
-  char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
-  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;
-
-  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_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, "/bin/sh", &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) < 12)
-    {
-      fprintf (stderr, "could not read expected output\n");
-      exit (1);
-    }
-  if (memcmp (line, "Halle Potta", 11) != 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);
-    }
-  return 0;
-}
--- a/tests/test-posix_spawn.in.sh	Mon Sep 29 15:25:12 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-#!/bin/sh
-echo "Halle Potta"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-posix_spawn1.c	Mon Sep 29 21:42:51 2008 +0200
@@ -0,0 +1,173 @@
+/* Test of posix_spawn() function.
+   Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
+
+#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>
+
+extern char **environ;
+
+#ifndef STDIN_FILENO
+# define STDIN_FILENO 0
+#endif
+#ifndef STDOUT_FILENO
+# define STDOUT_FILENO 1
+#endif
+#ifndef STDERR_FILENO
+# define STDERR_FILENO 2
+#endif
+
+#ifndef WTERMSIG
+# define WTERMSIG(x) ((x) & 0x7f)
+#endif
+#ifndef WCOREDUMP
+# define WCOREDUMP(x) ((x) & 0x80)
+#endif
+#ifndef WEXITSTATUS
+# define WEXITSTATUS(x) (((x) >> 8) & 0xff)
+#endif
+#ifndef WIFSIGNALED
+# define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
+#endif
+#ifndef WIFEXITED
+# define WIFEXITED(x) (WTERMSIG (x) == 0)
+#endif
+#ifndef WIFSTOPPED
+# define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
+#endif
+
+#define CHILD_PROGRAM_FILENAME "test-posix_spawn1.sh"
+
+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;
+}  
+
+int
+main ()
+{
+  char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
+  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;
+
+  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_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, "/bin/sh", &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) < 12)
+    {
+      fprintf (stderr, "could not read expected output\n");
+      exit (1);
+    }
+  if (memcmp (line, "Halle Potta", 11) != 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);
+    }
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-posix_spawn1.in.sh	Mon Sep 29 21:42:51 2008 +0200
@@ -0,0 +1,2 @@
+#!/bin/sh
+echo "Halle Potta"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-posix_spawn2.c	Mon Sep 29 21:42:51 2008 +0200
@@ -0,0 +1,168 @@
+/* Test of posix_spawn() function.
+   Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
+
+#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>
+
+extern char **environ;
+
+#ifndef STDIN_FILENO
+# define STDIN_FILENO 0
+#endif
+#ifndef STDOUT_FILENO
+# define STDOUT_FILENO 1
+#endif
+#ifndef STDERR_FILENO
+# define STDERR_FILENO 2
+#endif
+
+#ifndef WTERMSIG
+# define WTERMSIG(x) ((x) & 0x7f)
+#endif
+#ifndef WCOREDUMP
+# define WCOREDUMP(x) ((x) & 0x80)
+#endif
+#ifndef WEXITSTATUS
+# define WEXITSTATUS(x) (((x) >> 8) & 0xff)
+#endif
+#ifndef WIFSIGNALED
+# define WIFSIGNALED(x) (WTERMSIG (x) != 0 && WTERMSIG(x) != 0x7f)
+#endif
+#ifndef WIFEXITED
+# define WIFEXITED(x) (WTERMSIG (x) == 0)
+#endif
+#ifndef WIFSTOPPED
+# define WIFSTOPPED(x) (WTERMSIG (x) == 0x7f)
+#endif
+
+#define CHILD_PROGRAM_FILENAME "test-posix_spawn2.sh"
+
+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;
+}  
+
+int
+main ()
+{
+  char *argv[3] = { "/bin/sh", CHILD_PROGRAM_FILENAME, NULL };
+  int ofd[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;
+  int written;
+  int status;
+  int exitstatus;
+
+  if (pipe (ofd) < 0 || (ofd[1] = fd_safer (ofd[1])) < 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, ofd[0], STDIN_FILENO)) != 0
+          || (err = posix_spawn_file_actions_addclose (&actions, ofd[0])) != 0
+          || (err = posix_spawn_file_actions_addclose (&actions, ofd[1])) != 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, "/bin/sh", &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 (ofd[0]);
+  fd = ofd[1];
+  fp = fdopen (fd, "w");
+  if (fp == NULL)
+    {
+      fprintf (stderr, "fdopen() failed\n");
+      exit (1);
+    }
+  written = fwrite ("Halle Potta\n", 1, 12, fp);
+  if (written < 12)
+    {
+      fprintf (stderr, "could not write input\n");
+      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);
+    }
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-posix_spawn2.in.sh	Mon Sep 29 21:42:51 2008 +0200
@@ -0,0 +1,3 @@
+#!/bin/sh
+read line
+test "$line" = "Halle Potta"