Remove use of INTDEF/INTUSE in libio
[platform/upstream/glibc.git] / test-skeleton.c
index 47b0143..9ff4228 100644 (file)
@@ -1,5 +1,5 @@
 /* Skeleton for test programs.
-   Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1998,2000-2004, 2005, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
    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.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
+#include <errno.h>
 #include <getopt.h>
+#include <malloc.h>
 #include <search.h>
 #include <signal.h>
 #include <stdio.h>
@@ -27,6 +28,8 @@
 #include <unistd.h>
 #include <sys/resource.h>
 #include <sys/wait.h>
+#include <sys/param.h>
+#include <time.h>
 
 /* The test function is normally called `do_test' and it is called
    with argc and argv as the arguments.  We nevertheless provide the
@@ -35,6 +38,9 @@
 # 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
@@ -50,7 +56,7 @@ static struct option options[] =
 };
 
 /* PID of the test itself.  */
-static int pid;
+static pid_t pid;
 
 /* Directory to place temporary files in.  */
 static const char *test_dir;
@@ -125,18 +131,34 @@ create_temp_file (const char *base, char **filename)
 /* Timeout handler.  We kill the child and exit with an error.  */
 static void
 __attribute__ ((noreturn))
-timeout_handler (int sig __attribute__ ((unused)))
+signal_handler (int sig __attribute__ ((unused)))
 {
   int killed;
+  int status;
 
   /* Send signal.  */
   kill (pid, SIGKILL);
 
   /* Wait for it to terminate.  */
-  killed = waitpid (pid, NULL, WNOHANG);
+  int i;
+  for (i = 0; i < 5; ++i)
+    {
+      killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
+      if (killed != 0)
+       break;
+
+      /* Delay, give the system time to process the kill.  If the
+        nanosleep() call return prematurely, all the better.  We
+        won't restart it since this probably means the child process
+        finally died.  */
+      struct timespec ts;
+      ts.tv_sec = 0;
+      ts.tv_nsec = 100000000;
+      nanosleep (&ts, NULL);
+    }
   if (killed != 0 && killed != pid)
     {
-      perror ("Failed to killed test process");
+      perror ("Failed to kill test process");
       exit (1);
     }
 
@@ -144,7 +166,29 @@ timeout_handler (int sig __attribute__ ((unused)))
   CLEANUP_HANDLER;
 #endif
 
-  fputs ("Timed out: killed the child process\n", stderr);
+  if (sig == SIGINT)
+    {
+      signal (sig, SIG_DFL);
+      raise (sig);
+    }
+
+  /* If we expected this signal: good!  */
+#ifdef EXPECTED_SIGNAL
+  if (EXPECTED_SIGNAL == SIGALRM)
+    exit (0);
+#endif
+
+  if (killed == 0 || (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);
@@ -157,12 +201,17 @@ main (int argc, char *argv[])
   int direct = 0;      /* Directly call the test function?  */
   int status;
   int opt;
+  unsigned int timeoutfactor = 1;
+  pid_t termpid;
+
+  /* Make uses of freed and uninitialized memory known.  */
+  mallopt (M_PERTURB, 42);
 
 #ifdef STDOUT_UNBUFFERED
   setbuf (stdout, NULL);
 #endif
 
-  while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
+  while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
     switch (opt)
       {
       case '?':
@@ -178,6 +227,19 @@ main (int argc, char *argv[])
 #endif
       }
 
+  /* If set, read the test TIMEOUTFACTOR value from the environment.
+     This value is used to scale the default test timeout values. */
+  char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
+  if (envstr_timeoutfactor != NULL)
+    {
+      char *envstr_conv = envstr_timeoutfactor;
+      unsigned long int env_fact;
+
+      env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
+      if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
+       timeoutfactor = MAX (env_fact, 1);
+    }
+
   /* Set TMPDIR to specified test directory.  */
   if (test_dir != NULL)
     {
@@ -203,6 +265,7 @@ main (int argc, char *argv[])
   atexit (delete_temp_files);
 
   /* Correct for the possible parameters.  */
+  argv[optind - 1] = argv[0];
   argv += optind - 1;
   argc -= optind - 1;
 
@@ -232,6 +295,27 @@ main (int argc, char *argv[])
       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);
     }
@@ -246,13 +330,23 @@ main (int argc, char *argv[])
   /* Default timeout is two seconds.  */
 # define TIMEOUT 2
 #endif
-  alarm (TIMEOUT);
-  signal (SIGALRM, timeout_handler);
+  signal (SIGALRM, signal_handler);
+  alarm (TIMEOUT * timeoutfactor);
+
+  /* Make sure we clean up if the wrapper gets interrupted.  */
+  signal (SIGINT, signal_handler);
 
   /* Wait for the regular termination.  */
-  if (waitpid (pid, &status, 0) != pid)
+  termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
+  if (termpid == -1)
     {
-      perror ("Oops, wrong test program terminated");
+      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);
     }
 
@@ -281,5 +375,16 @@ main (int argc, char *argv[])
     }
 
   /* Simply exit with the return value of the test.  */
+#ifndef EXPECTED_STATUS
   return WEXITSTATUS (status);
+#else
+  if (WEXITSTATUS (status) != EXPECTED_STATUS)
+    {
+      fprintf (stderr, "Expected status %d, got %d\n",
+              EXPECTED_STATUS, WEXITSTATUS (status));
+      exit (1);
+    }
+
+  return 0;
+#endif
 }