Index: trunk/src/emx/testcase/libc/posix/tst-spawn.c
===================================================================
--- trunk/src/emx/testcase/libc/posix/tst-spawn.c	(revision 1046)
+++ trunk/src/emx/testcase/libc/posix/tst-spawn.c	(revision 1046)
@@ -0,0 +1,273 @@
+/* Tests for spawn.
+   Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   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 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, 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
+     - four parameters left of called initially
+       + path for ld.so
+       + "--library-path"
+       + the library path
+       + the application name
+     - five parameters left if called through re-execution
+       + 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 ? 6 : 5))
+    error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
+
+  if (restart)
+    return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);
+
+  /* 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[1];
+   spargv[1] = argv[2];
+   spargv[2] = argv[3];
+   spargv[3] = argv[4];
+   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[1], &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;
+}
Index: trunk/src/emx/testcase/libc/test-skeleton.c
===================================================================
--- trunk/src/emx/testcase/libc/test-skeleton.c	(revision 1046)
+++ trunk/src/emx/testcase/libc/test-skeleton.c	(revision 1046)
@@ -0,0 +1,336 @@
+/* Skeleton for test programs.
+   Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <getopt.h>
+#include <search.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+#include <sys/param.h>
+
+/* The test function is normally called `do_test' and it is called
+   with argc and argv as the arguments.  We nevertheless provide the
+   possibility to overwrite this name.  */
+#ifndef TEST_FUNCTION
+# define TEST_FUNCTION do_test (argc, argv)
+#endif
+
+#ifndef TEST_DATA_LIMIT
+# define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with.  */
+#endif
+
+#define OPT_DIRECT 1000
+#define OPT_TESTDIR 1001
+
+static struct option options[] =
+{
+#ifdef CMDLINE_OPTIONS
+  CMDLINE_OPTIONS
+#endif
+  { "direct", no_argument, NULL, OPT_DIRECT },
+  { "test-dir", required_argument, NULL, OPT_TESTDIR },
+  { NULL, 0, NULL, 0 }
+};
+
+/* PID of the test itself.  */
+static pid_t pid;
+
+/* Directory to place temporary files in.  */
+static const char *test_dir;
+
+/* List of temporary files.  */
+struct temp_name_list
+{
+  struct qelem q;
+  const char *name;
+} *temp_name_list;
+
+/* Add temporary files in list.  */
+static void
+__attribute__ ((unused))
+add_temp_file (const char *name)
+{
+  struct temp_name_list *newp
+    = (struct temp_name_list *) calloc (sizeof (*newp), 1);
+  if (newp != NULL)
+    {
+      newp->name = name;
+      if (temp_name_list == NULL)
+	temp_name_list = (struct temp_name_list *) &newp->q;
+      else
+	insque (newp, temp_name_list);
+    }
+}
+
+/* Delete all temporary files.  */
+static void
+delete_temp_files (void)
+{
+  while (temp_name_list != NULL)
+    {
+      remove (temp_name_list->name);
+      temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
+    }
+}
+
+/* Create a temporary file.  */
+static int
+__attribute__ ((unused))
+create_temp_file (const char *base, char **filename)
+{
+  char *fname;
+  int fd;
+
+  fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
+			   + sizeof ("XXXXXX"));
+  if (fname == NULL)
+    {
+      puts ("out of memory");
+      return -1;
+    }
+  strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
+
+  fd = mkstemp (fname);
+  if (fd == -1)
+    {
+      printf ("cannot open temporary file '%s': %m\n", fname);
+      free (fname);
+      return -1;
+    }
+
+  add_temp_file (fname);
+  if (filename != NULL)
+    *filename = fname;
+
+  return fd;
+}
+
+/* Timeout handler.  We kill the child and exit with an error.  */
+static void
+__attribute__ ((noreturn))
+timeout_handler (int sig __attribute__ ((unused)))
+{
+  int killed;
+  int status;
+
+  /* Send signal.  */
+  kill (pid, SIGKILL);
+
+  /* Wait for it to terminate.  */
+  killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
+  if (killed != 0 && killed != pid)
+    {
+      perror ("Failed to killed test process");
+      exit (1);
+    }
+
+#ifdef CLEANUP_HANDLER
+  CLEANUP_HANDLER;
+#endif
+
+  /* If we expected this signal: good!  */
+#ifdef EXPECTED_SIGNAL
+  if (EXPECTED_SIGNAL == SIGALRM)
+    exit (0);
+#endif
+
+  if (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
+    fputs ("Timed out: killed the child process\n", stderr);
+  else if (WIFSTOPPED (status))
+    fprintf (stderr, "Timed out: the child process was %s\n",
+	     strsignal (WSTOPSIG (status)));
+  else if (WIFSIGNALED (status))
+    fprintf (stderr, "Timed out: the child process got signal %s\n",
+	     strsignal (WTERMSIG (status)));
+  else
+    fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
+	     WEXITSTATUS (status));
+
+  /* Exit with an error.  */
+  exit (1);
+}
+
+/* We provide the entry point here.  */
+int
+main (int argc, char *argv[])
+{
+  int direct = 0;	/* Directly call the test function?  */
+  int status;
+  int opt;
+  pid_t termpid;
+
+#ifdef STDOUT_UNBUFFERED
+  setbuf (stdout, NULL);
+#endif
+
+  while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
+    switch (opt)
+      {
+      case '?':
+	exit (1);
+      case OPT_DIRECT:
+	direct = 1;
+	break;
+      case OPT_TESTDIR:
+	test_dir = optarg;
+	break;
+#ifdef CMDLINE_PROCESS
+	CMDLINE_PROCESS
+#endif
+      }
+
+  /* Set TMPDIR to specified test directory.  */
+  if (test_dir != NULL)
+    {
+      setenv ("TMPDIR", test_dir, 1);
+
+      if (chdir (test_dir) < 0)
+	{
+	  perror ("chdir");
+	  exit (1);
+	}
+    }
+  else
+    {
+      test_dir = getenv ("TMPDIR");
+      if (test_dir == NULL || test_dir[0] == '\0')
+	test_dir = "/tmp";
+    }
+
+  /* Make sure we see all message, even those on stdout.  */
+  setvbuf (stdout, NULL, _IONBF, 0);
+
+  /* make sure temporary files are deleted.  */
+  atexit (delete_temp_files);
+
+  /* Correct for the possible parameters.  */
+  argv[optind - 1] = argv[0];
+  argv += optind - 1;
+  argc -= optind - 1;
+
+  /* Call the initializing function, if one is available.  */
+#ifdef PREPARE
+  PREPARE (argc, argv);
+#endif
+
+  /* If we are not expected to fork run the function immediately.  */
+  if (direct)
+    return TEST_FUNCTION;
+
+  /* Set up the test environment:
+     - prevent core dumps
+     - set up the timer
+     - fork and execute the function.  */
+
+  pid = fork ();
+  if (pid == 0)
+    {
+      /* This is the child.  */
+#ifdef RLIMIT_CORE
+      /* Try to avoid dumping core.  */
+      struct rlimit core_limit;
+      core_limit.rlim_cur = 0;
+      core_limit.rlim_max = 0;
+      setrlimit (RLIMIT_CORE, &core_limit);
+#endif
+
+#ifdef RLIMIT_DATA
+      /* Try to avoid eating all memory if a test leaks.  */
+      struct rlimit data_limit;
+      if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
+	{
+	  if (TEST_DATA_LIMIT == RLIM_INFINITY)
+	    data_limit.rlim_cur = data_limit.rlim_max;
+	  else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
+	    data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
+				       data_limit.rlim_max);
+	  if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
+	    perror ("setrlimit: RLIMIT_DATA");
+	}
+      else
+	perror ("getrlimit: RLIMIT_DATA");
+#endif
+
+      /* We put the test process in its own pgrp so that if it bogusly
+	 generates any job control signals, they won't hit the whole build.  */
+      setpgid (0, 0);
+
+      /* Execute the test function and exit with the return value.   */
+      exit (TEST_FUNCTION);
+    }
+  else if (pid < 0)
+    {
+      perror ("Cannot fork test program");
+      exit (1);
+    }
+
+  /* Set timeout.  */
+#ifndef TIMEOUT
+  /* Default timeout is two seconds.  */
+# define TIMEOUT 2
+#endif
+  alarm (TIMEOUT);
+  signal (SIGALRM, timeout_handler);
+
+  /* Wait for the regular termination.  */
+  termpid = waitpid (pid, &status, 0);
+  if (termpid == -1)
+    {
+      printf ("Waiting for test program failed: %m\n");
+      exit (1);
+    }
+  if (termpid != pid)
+    {
+      printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
+	      (long int) pid, (long int) termpid);
+      exit (1);
+    }
+
+#ifndef EXPECTED_SIGNAL
+  /* We don't expect any signal.  */
+# define EXPECTED_SIGNAL 0
+#endif
+  if (WTERMSIG (status) != EXPECTED_SIGNAL)
+    {
+      if (EXPECTED_SIGNAL != 0)
+	{
+	  if (WTERMSIG (status) == 0)
+	    fprintf (stderr,
+		     "Expected signal '%s' from child, got none\n",
+		     strsignal (EXPECTED_SIGNAL));
+	  else
+	    fprintf (stderr,
+		     "Incorrect signal from child: got `%s', need `%s'\n",
+		     strsignal (WTERMSIG (status)),
+		     strsignal (EXPECTED_SIGNAL));
+	}
+      else
+	fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
+		 strsignal (WTERMSIG (status)));
+      exit (1);
+    }
+
+  /* Simply exit with the return value of the test.  */
+  return WEXITSTATUS (status);
+}
