about summary refs log tree commit diff
path: root/posix
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2000-05-29 07:31:53 +0000
committerUlrich Drepper <drepper@redhat.com>2000-05-29 07:31:53 +0000
commit08c7f6b0082b1b645348518fdc42643b5580d87c (patch)
treeb5280480fa7790ff6f92029378b1311d870c2982 /posix
parent079199de5cdf96f85e29ecedafc69b2fb3004f2b (diff)
downloadglibc-08c7f6b0082b1b645348518fdc42643b5580d87c.tar.gz
glibc-08c7f6b0082b1b645348518fdc42643b5580d87c.tar.xz
glibc-08c7f6b0082b1b645348518fdc42643b5580d87c.zip
Update.
2000-05-29  Ulrich Drepper  <drepper@redhat.com>

	* posix/Makefile (tests): Add tst-spawn.
	(tst-spawn-ARGS): New variable.
	* posix/tst-spawn.c: New file.

	* posix/spawn_faction_addclose.c: Correctly account for new entry.
	* posix/spawn_faction_adddup2.c: Likewise.
	* posix/spawn_faction_addopen.c: Likewise.
	* posix/spawni.c: Correctly test for success of dup2 calls.

	* posix/tst-preadwrite.c: Check success of malloc call.
Diffstat (limited to 'posix')
-rw-r--r--posix/Makefile3
-rw-r--r--posix/spawn_faction_addclose.c5
-rw-r--r--posix/spawn_faction_adddup2.c5
-rw-r--r--posix/spawn_faction_addopen.c5
-rw-r--r--posix/spawni.c6
-rw-r--r--posix/tst-preadwrite.c2
-rw-r--r--posix/tst-spawn.c275
7 files changed, 295 insertions, 6 deletions
diff --git a/posix/Makefile b/posix/Makefile
index 82af69cf92..2e510a1907 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -66,7 +66,7 @@ aux		:= init-posix environ
 tests		:= tstgetopt testfnm runtests runptests	     \
 		   tst-preadwrite tst-preadwrite64 test-vfork regexbug1 \
 		   tst-getlogin tst-mmap tst-getaddrinfo tst-truncate \
-		   tst-truncate64 tst-fork tst-exec
+		   tst-truncate64 tst-fork tst-exec tst-spawn
 ifeq (yes,$(build-shared))
 test-srcs	:= globtest
 tests           += wordexp-test
@@ -106,6 +106,7 @@ tstgetopt-ARGS = -a -b -cfoobar --required foobar --optional=bazbug \
 		--none random
 
 tst-exec-ARGS = -- $(built-program-cmd)
+tst-spawn-ARGS = -- $(built-program-cmd)
 
 $(objpfx)libposix.a: $(dep-dummy-lib); $(make-dummy-lib)
 lib: $(objpfx)libposix.a
diff --git a/posix/spawn_faction_addclose.c b/posix/spawn_faction_addclose.c
index 565f4b43f7..b90abb1f7a 100644
--- a/posix/spawn_faction_addclose.c
+++ b/posix/spawn_faction_addclose.c
@@ -42,9 +42,12 @@ posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *file_actions,
     return ENOMEM;
 
   /* Add the new value.  */
-  rec = &file_actions->__actions[file_actions->__allocated];
+  rec = &file_actions->__actions[file_actions->__used];
   rec->tag = spawn_do_close;
   rec->action.open_action.fd = fd;
 
+  /* Account for the new entry.  */
+  ++file_actions->__used;
+
   return 0;
 }
diff --git a/posix/spawn_faction_adddup2.c b/posix/spawn_faction_adddup2.c
index 482318ff9e..7407cca531 100644
--- a/posix/spawn_faction_adddup2.c
+++ b/posix/spawn_faction_adddup2.c
@@ -42,10 +42,13 @@ posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *file_actions,
     return ENOMEM;
 
   /* Add the new value.  */
-  rec = &file_actions->__actions[file_actions->__allocated];
+  rec = &file_actions->__actions[file_actions->__used];
   rec->tag = spawn_do_dup2;
   rec->action.dup2_action.fd = fd;
   rec->action.dup2_action.newfd = newfd;
 
+  /* Account for the new entry.  */
+  ++file_actions->__used;
+
   return 0;
 }
diff --git a/posix/spawn_faction_addopen.c b/posix/spawn_faction_addopen.c
index 1405e91013..b177f07294 100644
--- a/posix/spawn_faction_addopen.c
+++ b/posix/spawn_faction_addopen.c
@@ -43,12 +43,15 @@ posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *file_actions,
     return ENOMEM;
 
   /* Add the new value.  */
-  rec = &file_actions->__actions[file_actions->__allocated];
+  rec = &file_actions->__actions[file_actions->__used];
   rec->tag = spawn_do_open;
   rec->action.open_action.fd = fd;
   rec->action.open_action.path = path;
   rec->action.open_action.oflag = oflag;
   rec->action.open_action.mode = mode;
 
+  /* Account for the new entry.  */
+  ++file_actions->__used;
+
   return 0;
 }
diff --git a/posix/spawni.c b/posix/spawni.c
index e93f488f04..9f10c930f3 100644
--- a/posix/spawni.c
+++ b/posix/spawni.c
@@ -175,7 +175,8 @@ __spawni (pid_t *pid, const char *file,
 		/* Make sure the desired file descriptor is used.  */
 		if (new_fd != action->action.open_action.fd)
 		  {
-		    if (__dup2 (new_fd, action->action.open_action.fd) != 0)
+		    if (__dup2 (new_fd, action->action.open_action.fd)
+			!= action->action.open_action.fd)
 		      /* The `dup2' call failed.  */
 		      _exit (SPAWN_ERROR);
 
@@ -188,7 +189,8 @@ __spawni (pid_t *pid, const char *file,
 
 	    case spawn_do_dup2:
 	      if (__dup2 (action->action.dup2_action.fd,
-			  action->action.dup2_action.newfd) != 0)
+			  action->action.dup2_action.newfd)
+		  != action->action.dup2_action.newfd)
 		/* The `dup2' call failed.  */
 		_exit (SPAWN_ERROR);
 	      break;
diff --git a/posix/tst-preadwrite.c b/posix/tst-preadwrite.c
index d1dcdcc67c..9d97609836 100644
--- a/posix/tst-preadwrite.c
+++ b/posix/tst-preadwrite.c
@@ -59,6 +59,8 @@ do_prepare (int argc, char *argv[])
 
    name_len = strlen (test_dir);
    name = malloc (name_len + sizeof (FNAME));
+   if (name == NULL)
+     error (EXIT_FAILURE, errno, "cannot allocate file name");
    mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
    add_temp_file (name);
 
diff --git a/posix/tst-spawn.c b/posix/tst-spawn.c
new file mode 100644
index 0000000000..4bc94e0544
--- /dev/null
+++ b/posix/tst-spawn.c
@@ -0,0 +1,275 @@
+/* Tests for spawn.
+   Copyright (C) 2000 Free Software Foundation, Inc.
+   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <error.h>
+#include <fcntl.h>
+#include <spawn.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <wait.h>
+#include <sys/param.h>
+
+
+/* Nonzero if the program gets called via `exec'.  */
+static int restart;
+
+
+#define CMDLINE_OPTIONS \
+  { "restart", no_argument, &restart, 1 },
+
+/* Prototype for our test function.  */
+extern void do_prepare (int argc, char *argv[]);
+extern int do_test (int argc, char *argv[]);
+
+/* We have a preparation function.  */
+#define PREPARE do_prepare
+
+#include "../test-skeleton.c"
+
+
+/* Name of the temporary files.  */
+static char *name1;
+static char *name2;
+static char *name3;
+
+/* The contents of our files.  */
+static const char fd1string[] = "This file should get closed";
+static const char fd2string[] = "This file should stay opened";
+static const char fd3string[] = "This file will be opened";
+
+
+/* We have a preparation function.  */
+void
+do_prepare (int argc, char *argv[])
+{
+   char name_len;
+
+   name_len = strlen (test_dir);
+   name1 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
+   mempcpy (mempcpy (name1, test_dir, name_len),
+	    "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
+   add_temp_file (name1);
+
+   name2 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
+   mempcpy (mempcpy (name2, test_dir, name_len),
+	    "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
+   add_temp_file (name2);
+
+   name3 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
+   mempcpy (mempcpy (name3, test_dir, name_len),
+	    "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
+   add_temp_file (name3);
+}
+
+
+static int
+handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
+		const char *fd4s, const char *name)
+{
+  char buf[100];
+  int fd1;
+  int fd2;
+  int fd3;
+  int fd4;
+
+  /* First get the descriptors.  */
+  fd1 = atol (fd1s);
+  fd2 = atol (fd2s);
+  fd3 = atol (fd3s);
+  fd4 = atol (fd4s);
+
+  /* Sanity check.  */
+  if (fd1 == fd2)
+    error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
+  if (fd1 == fd3)
+    error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same");
+  if (fd1 == fd4)
+    error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same");
+  if (fd2 == fd3)
+    error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same");
+  if (fd2 == fd4)
+    error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same");
+  if (fd3 == fd4)
+    error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same");
+
+  /* First the easy part: read from the file descriptor which is
+     supposed to be open.  */
+  if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
+    error (EXIT_FAILURE, errno, "file 2 not in right position");
+  /* The duped descriptor must have the same position.  */
+  if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string))
+    error (EXIT_FAILURE, errno, "file 4 not in right position");
+  if (lseek (fd2, 0, SEEK_SET) != 0)
+    error (EXIT_FAILURE, 0, "cannot reset position in file 2");
+  if (lseek (fd4, 0, SEEK_CUR) != 0)
+    error (EXIT_FAILURE, errno, "file 4 not set back, too");
+  if (read (fd2, buf, sizeof buf) != strlen (fd2string))
+    error (EXIT_FAILURE, 0, "cannot read file 2");
+  if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
+    error (EXIT_FAILURE, 0, "file 2 does not match");
+
+  /* Now read from the third file.  */
+  if (read (fd3, buf, sizeof buf) != strlen (fd3string))
+    error (EXIT_FAILURE, 0, "cannot read file 3");
+  if (memcmp (fd3string, buf, strlen (fd3string)) != 0)
+    error (EXIT_FAILURE, 0, "file 3 does not match");
+  /* Try to write to the file.  This should not be allowed.  */
+  if (write (fd3, "boo!", 4) != -1 || errno != EBADF)
+    error (EXIT_FAILURE, 0, "file 3 is writable");
+
+  /* Now try to read the first file.  First make sure it is not opened.  */
+  if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
+    error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
+
+  /* Now open the file and read it.  */
+  fd1 = open (name, O_RDONLY);
+  if (fd1 == -1)
+    error (EXIT_FAILURE, errno,
+	   "cannot open first file \"%s\" for verification", name);
+
+  if (read (fd1, buf, sizeof buf) != strlen (fd1string))
+    error (EXIT_FAILURE, errno, "cannot read file 1");
+  if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
+    error (EXIT_FAILURE, 0, "file 1 does not match");
+
+  return 0;
+}
+
+
+int
+do_test (int argc, char *argv[])
+{
+  pid_t pid;
+  int fd1;
+  int fd2;
+  int fd3;
+  int fd4;
+  int status;
+  posix_spawn_file_actions_t actions;
+  char fd1name[18];
+  char fd2name[18];
+  char fd3name[18];
+  char fd4name[18];
+  char *spargv[12];
+
+  /* We must have
+     - five parameters left of called initially
+       + --
+       + path for ld.so
+       + "--library-path"
+       + the library path
+       + the application name
+     - seven parameters left if called through re-execution
+       + --direct
+       + --restart
+       + file descriptor number which is supposed to be closed
+       + the open file descriptor
+       + the newly opened file descriptor
+       + thhe duped second descriptor
+       + the name of the closed descriptor
+  */
+  if (argc != (restart ? 8 : 6))
+    error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
+
+  if (restart)
+    return handle_restart (argv[3], argv[4], argv[5], argv[6], argv[7]);
+
+  /* Prepare the test.  We are creating two files: one which file descriptor
+     will be marked with FD_CLOEXEC, another which is not.  */
+
+   /* Open our test files.   */
+   fd1 = mkstemp (name1);
+   if (fd1 == -1)
+     error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1);
+   fd2 = mkstemp (name2);
+   if (fd2 == -1)
+     error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2);
+   fd3 = mkstemp (name3);
+   if (fd3 == -1)
+     error (EXIT_FAILURE, errno, "cannot open test file `%s'", name3);
+
+   /* Write something in the files.  */
+   if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
+     error (EXIT_FAILURE, errno, "cannot write to first file");
+   if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
+     error (EXIT_FAILURE, errno, "cannot write to second file");
+   if (write (fd3, fd3string, strlen (fd3string)) != strlen (fd3string))
+     error (EXIT_FAILURE, errno, "cannot write to third file");
+
+   /* Close the third file.  It'll be opened by `spawn'.  */
+   close (fd3);
+
+   /* Tell `spawn' what to do.  */
+   if (posix_spawn_file_actions_init (&actions) != 0)
+     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init");
+   /* Close `fd1'.  */
+   if (posix_spawn_file_actions_addclose (&actions, fd1) != 0)
+     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose");
+   /* We want to open the third file.  */
+   if (posix_spawn_file_actions_addopen (&actions, fd3, name3,
+					 O_RDONLY, 0666) != 0)
+     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen");
+   /* We dup the second descriptor.  */
+   fd4 = MAX (2, MAX (fd1, MAX (fd2, fd3))) + 1;
+   if (posix_spawn_file_actions_adddup2 (&actions, fd2, fd4) != 0)
+     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2");
+
+   /* Now spawn the process.  */
+   snprintf (fd1name, sizeof fd1name, "%d", fd1);
+   snprintf (fd2name, sizeof fd2name, "%d", fd2);
+   snprintf (fd3name, sizeof fd3name, "%d", fd3);
+   snprintf (fd4name, sizeof fd4name, "%d", fd4);
+
+   spargv[0] = argv[2];
+   spargv[1] = argv[3];
+   spargv[2] = argv[4];
+   spargv[3] = argv[5];
+   spargv[4] = (char *) "--direct";
+   spargv[5] = (char *) "--restart";
+   spargv[6] = fd1name;
+   spargv[7] = fd2name;
+   spargv[8] = fd3name;
+   spargv[9] = fd4name;
+   spargv[10] = name1;
+   spargv[11] = NULL;
+
+   if (posix_spawn (&pid, argv[2], &actions, NULL, spargv, environ) != 0)
+     error (EXIT_FAILURE, errno, "posix_spawn");
+
+   /* Cleanup.  */
+   if (posix_spawn_file_actions_destroy (&actions) != 0)
+     error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy");
+
+  /* Wait for the child.  */
+  if (waitpid (pid, &status, 0) != pid)
+    error (EXIT_FAILURE, errno, "wrong child");
+
+  if (WTERMSIG (status) != 0)
+    error (EXIT_FAILURE, 0, "Child terminated incorrectly");
+  status = WEXITSTATUS (status);
+
+  /* Remove the test files.  */
+  unlink (name1);
+  unlink (name2);
+  unlink (name3);
+
+  return status;
+}