Update.
[glibc.git] / test-skeleton.c
blob5bb5c44c162e2c4f12d28609bd633493f8c35682
1 /* Skeleton for test programs.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/resource.h>
26 #include <sys/wait.h>
28 /* The test function is normally called `do_test' and it is called
29 with argc and argv as the arguments. We nevertheless provide the
30 possibility to overwrite this name. */
31 #ifndef TEST_FUNCTION
32 # define TEST_FUNCTION do_test (argc, argv)
33 #endif
36 #define OPT_DIRECT 1000
37 #define OPT_TESTDIR 1001
39 static struct option options[] =
41 #ifdef CMDLINE_OPTIONS
42 CMDLINE_OPTIONS
43 #endif
44 { "direct", no_argument, NULL, OPT_DIRECT },
45 { "test-dir", required_argument, NULL, OPT_TESTDIR },
46 { NULL, 0, NULL, 0 }
49 /* PID of the test itself. */
50 static int pid;
52 /* Directory to place temporary files in. */
53 static const char *test_dir;
55 /* Timeout handler. We kill the child and exit with an error. */
56 void
57 timeout_handler (int sig __attribute__ ((unused)))
59 int killed;
61 /* Send signal. */
62 kill (pid, SIGKILL);
64 /* Wait for it to terminate. */
65 killed = waitpid (pid, NULL, WNOHANG);
66 if (killed != 0 && killed != pid)
68 perror ("Failed to killed test process");
69 exit (1);
72 #ifdef CLEANUP_HANDLER
73 CLEANUP_HANDLER;
74 #endif
76 fputs ("Timed out: killed the child process\n", stderr);
78 /* Exit with an error. */
79 exit (1);
82 /* We provide the entry point here. */
83 int
84 main (int argc, char *argv[])
86 int direct = 0; /* Directly call the test function? */
87 int status;
88 int opt;
90 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
91 switch (opt)
93 case '?':
94 exit (1);
95 case OPT_DIRECT:
96 direct = 1;
97 break;
98 case OPT_TESTDIR:
99 test_dir = optarg;
100 break;
101 #ifdef CMDLINE_PROCESS
102 CMDLINE_PROCESS
103 #endif
106 /* Set TMPDIR to specified test directory. */
107 if (test_dir != NULL)
109 setenv ("TMPDIR", test_dir, 1);
111 if (chdir (test_dir) < 0)
113 perror ("chdir");
114 exit (1);
118 /* If we are not expected to fork run the function immediately. */
119 if (direct)
120 return TEST_FUNCTION;
122 /* Set up the test environment:
123 - prevent core dumps
124 - set up the timer
125 - fork and execute the function. */
127 pid = fork ();
128 if (pid == 0)
130 /* This is the child. */
131 #ifdef RLIMIT_CORE
132 /* Try to avoid dumping core. */
133 struct rlimit core_limit;
134 core_limit.rlim_cur = 0;
135 core_limit.rlim_max = 0;
136 setrlimit (RLIMIT_CORE, &core_limit);
137 #endif
139 /* Execute the test function and exit with the return value. */
140 exit (TEST_FUNCTION);
142 else if (pid < 0)
144 perror ("Cannot fork test program");
145 exit (1);
148 /* Set timeout. */
149 #ifndef TIMEOUT
150 /* Default timeout is two seconds. */
151 # define TIMEOUT 2
152 #endif
153 alarm (TIMEOUT);
154 signal (SIGALRM, timeout_handler);
156 /* Wait for the regular termination. */
157 if (waitpid (pid, &status, 0) != pid)
159 perror ("Oops, wrong test program terminated");
160 exit (1);
163 #ifndef EXPECTED_SIGNAL
164 /* We don't expect any signal. */
165 # define EXPECTED_SIGNAL 0
166 #endif
167 if (WTERMSIG (status) != EXPECTED_SIGNAL)
169 fprintf (stderr, "Incorrect signal from child: got `%s', need `%s'\n",
170 strsignal (WTERMSIG (status)), strsignal (EXPECTED_SIGNAL));
171 exit (1);
174 /* Simply exit with the return value of the test. */
175 return WEXITSTATUS (status);