Update.
[glibc.git] / test-skeleton.c
blob9d9a68b62c2c214a84f52250db6b26a9da384cb0
1 /* Skeleton for test programs.
2 Copyright (C) 1998,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <getopt.h>
23 #include <search.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/resource.h>
30 #include <sys/wait.h>
31 #include <sys/param.h>
32 #include <time.h>
34 /* The test function is normally called `do_test' and it is called
35 with argc and argv as the arguments. We nevertheless provide the
36 possibility to overwrite this name. */
37 #ifndef TEST_FUNCTION
38 # define TEST_FUNCTION do_test (argc, argv)
39 #endif
41 #ifndef TEST_DATA_LIMIT
42 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
43 #endif
45 #define OPT_DIRECT 1000
46 #define OPT_TESTDIR 1001
48 static struct option options[] =
50 #ifdef CMDLINE_OPTIONS
51 CMDLINE_OPTIONS
52 #endif
53 { "direct", no_argument, NULL, OPT_DIRECT },
54 { "test-dir", required_argument, NULL, OPT_TESTDIR },
55 { NULL, 0, NULL, 0 }
58 /* PID of the test itself. */
59 static pid_t pid;
61 /* Directory to place temporary files in. */
62 static const char *test_dir;
64 /* List of temporary files. */
65 struct temp_name_list
67 struct qelem q;
68 const char *name;
69 } *temp_name_list;
71 /* Add temporary files in list. */
72 static void
73 __attribute__ ((unused))
74 add_temp_file (const char *name)
76 struct temp_name_list *newp
77 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
78 if (newp != NULL)
80 newp->name = name;
81 if (temp_name_list == NULL)
82 temp_name_list = (struct temp_name_list *) &newp->q;
83 else
84 insque (newp, temp_name_list);
88 /* Delete all temporary files. */
89 static void
90 delete_temp_files (void)
92 while (temp_name_list != NULL)
94 remove (temp_name_list->name);
95 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
99 /* Create a temporary file. */
100 static int
101 __attribute__ ((unused))
102 create_temp_file (const char *base, char **filename)
104 char *fname;
105 int fd;
107 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
108 + sizeof ("XXXXXX"));
109 if (fname == NULL)
111 puts ("out of memory");
112 return -1;
114 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
116 fd = mkstemp (fname);
117 if (fd == -1)
119 printf ("cannot open temporary file '%s': %m\n", fname);
120 free (fname);
121 return -1;
124 add_temp_file (fname);
125 if (filename != NULL)
126 *filename = fname;
128 return fd;
131 /* Timeout handler. We kill the child and exit with an error. */
132 static void
133 __attribute__ ((noreturn))
134 timeout_handler (int sig __attribute__ ((unused)))
136 int killed;
137 int status;
139 /* Send signal. */
140 kill (pid, SIGKILL);
142 /* Wait for it to terminate. */
143 int i;
144 for (i = 0; i < 5; ++i)
146 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
147 if (killed != 0)
148 break;
150 /* Delay, give the system time to process the kill. If the
151 nanosleep() call return prematurely, all the better. We
152 won't restart it since this probably means the child process
153 finally died. */
154 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
155 nanosleep (&ts, NULL);
157 if (killed != 0 && killed != pid)
159 perror ("Failed to kill test process");
160 exit (1);
163 #ifdef CLEANUP_HANDLER
164 CLEANUP_HANDLER;
165 #endif
167 /* If we expected this signal: good! */
168 #ifdef EXPECTED_SIGNAL
169 if (EXPECTED_SIGNAL == SIGALRM)
170 exit (0);
171 #endif
173 if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
174 fputs ("Timed out: killed the child process\n", stderr);
175 else if (WIFSTOPPED (status))
176 fprintf (stderr, "Timed out: the child process was %s\n",
177 strsignal (WSTOPSIG (status)));
178 else if (WIFSIGNALED (status))
179 fprintf (stderr, "Timed out: the child process got signal %s\n",
180 strsignal (WTERMSIG (status)));
181 else
182 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
183 WEXITSTATUS (status));
185 /* Exit with an error. */
186 exit (1);
189 /* We provide the entry point here. */
191 main (int argc, char *argv[])
193 int direct = 0; /* Directly call the test function? */
194 int status;
195 int opt;
196 unsigned int timeoutfactor = 1;
197 pid_t termpid;
199 #ifdef STDOUT_UNBUFFERED
200 setbuf (stdout, NULL);
201 #endif
203 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
204 switch (opt)
206 case '?':
207 exit (1);
208 case OPT_DIRECT:
209 direct = 1;
210 break;
211 case OPT_TESTDIR:
212 test_dir = optarg;
213 break;
214 #ifdef CMDLINE_PROCESS
215 CMDLINE_PROCESS
216 #endif
219 /* If set, read the test TIMEOUTFACTOR value from the environment.
220 This value is used to scale the default test timeout values. */
221 char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
222 if (envstr_timeoutfactor != NULL)
224 char *envstr_conv = envstr_timeoutfactor;
225 unsigned long int env_fact;
227 env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
228 if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
229 timeoutfactor = MAX (env_fact, 1);
232 /* Set TMPDIR to specified test directory. */
233 if (test_dir != NULL)
235 setenv ("TMPDIR", test_dir, 1);
237 if (chdir (test_dir) < 0)
239 perror ("chdir");
240 exit (1);
243 else
245 test_dir = getenv ("TMPDIR");
246 if (test_dir == NULL || test_dir[0] == '\0')
247 test_dir = "/tmp";
250 /* Make sure we see all message, even those on stdout. */
251 setvbuf (stdout, NULL, _IONBF, 0);
253 /* make sure temporary files are deleted. */
254 atexit (delete_temp_files);
256 /* Correct for the possible parameters. */
257 argv[optind - 1] = argv[0];
258 argv += optind - 1;
259 argc -= optind - 1;
261 /* Call the initializing function, if one is available. */
262 #ifdef PREPARE
263 PREPARE (argc, argv);
264 #endif
266 /* If we are not expected to fork run the function immediately. */
267 if (direct)
268 return TEST_FUNCTION;
270 /* Set up the test environment:
271 - prevent core dumps
272 - set up the timer
273 - fork and execute the function. */
275 pid = fork ();
276 if (pid == 0)
278 /* This is the child. */
279 #ifdef RLIMIT_CORE
280 /* Try to avoid dumping core. */
281 struct rlimit core_limit;
282 core_limit.rlim_cur = 0;
283 core_limit.rlim_max = 0;
284 setrlimit (RLIMIT_CORE, &core_limit);
285 #endif
287 #ifdef RLIMIT_DATA
288 /* Try to avoid eating all memory if a test leaks. */
289 struct rlimit data_limit;
290 if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
292 if (TEST_DATA_LIMIT == RLIM_INFINITY)
293 data_limit.rlim_cur = data_limit.rlim_max;
294 else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
295 data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
296 data_limit.rlim_max);
297 if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
298 perror ("setrlimit: RLIMIT_DATA");
300 else
301 perror ("getrlimit: RLIMIT_DATA");
302 #endif
304 /* We put the test process in its own pgrp so that if it bogusly
305 generates any job control signals, they won't hit the whole build. */
306 setpgid (0, 0);
308 /* Execute the test function and exit with the return value. */
309 exit (TEST_FUNCTION);
311 else if (pid < 0)
313 perror ("Cannot fork test program");
314 exit (1);
317 /* Set timeout. */
318 #ifndef TIMEOUT
319 /* Default timeout is two seconds. */
320 # define TIMEOUT 2
321 #endif
322 signal (SIGALRM, timeout_handler);
323 alarm (TIMEOUT * timeoutfactor);
325 /* Wait for the regular termination. */
326 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
327 if (termpid == -1)
329 printf ("Waiting for test program failed: %m\n");
330 exit (1);
332 if (termpid != pid)
334 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
335 (long int) pid, (long int) termpid);
336 exit (1);
339 #ifndef EXPECTED_SIGNAL
340 /* We don't expect any signal. */
341 # define EXPECTED_SIGNAL 0
342 #endif
343 if (WTERMSIG (status) != EXPECTED_SIGNAL)
345 if (EXPECTED_SIGNAL != 0)
347 if (WTERMSIG (status) == 0)
348 fprintf (stderr,
349 "Expected signal '%s' from child, got none\n",
350 strsignal (EXPECTED_SIGNAL));
351 else
352 fprintf (stderr,
353 "Incorrect signal from child: got `%s', need `%s'\n",
354 strsignal (WTERMSIG (status)),
355 strsignal (EXPECTED_SIGNAL));
357 else
358 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
359 strsignal (WTERMSIG (status)));
360 exit (1);
363 /* Simply exit with the return value of the test. */
364 #ifndef EXPECTED_STATUS
365 return WEXITSTATUS (status);
366 #else
367 if (WEXITSTATUS (status) != EXPECTED_STATUS)
369 fprintf (stderr, "Expected status %d, got %d\n",
370 EXPECTED_STATUS, WEXITSTATUS (status));
371 exit (1);
374 return 0;
375 #endif