1 /* Skeleton for test programs.
2 Copyright (C) 1998,2000-2004, 2005 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, see
18 <http://www.gnu.org/licenses/>. */
29 #include <sys/resource.h>
31 #include <sys/param.h>
35 /* The test function is normally called `do_test' and it is called
36 with argc and argv as the arguments. We nevertheless provide the
37 possibility to overwrite this name. */
39 # define TEST_FUNCTION do_test (argc, argv)
42 #ifndef TEST_DATA_LIMIT
43 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
46 #define OPT_DIRECT 1000
47 #define OPT_TESTDIR 1001
49 static struct option options[] =
51 #ifdef CMDLINE_OPTIONS
54 { "direct", no_argument, NULL, OPT_DIRECT },
55 { "test-dir", required_argument, NULL, OPT_TESTDIR },
59 /* PID of the test itself. */
62 /* Directory to place temporary files in. */
63 static const char *test_dir;
65 /* List of temporary files. */
72 /* Add temporary files in list. */
74 __attribute__ ((unused))
75 add_temp_file (const char *name)
77 struct temp_name_list *newp
78 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
82 if (temp_name_list == NULL)
83 temp_name_list = (struct temp_name_list *) &newp->q;
85 insque (newp, temp_name_list);
89 /* Delete all temporary files. */
91 delete_temp_files (void)
93 while (temp_name_list != NULL)
95 remove (temp_name_list->name);
96 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
100 /* Create a temporary file. */
102 __attribute__ ((unused))
103 create_temp_file (const char *base, char **filename)
108 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
109 + sizeof ("XXXXXX"));
112 puts ("out of memory");
115 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
117 _fd = mkstemp (fname);
120 printf ("cannot open temporary file '%s': %s\n", fname, strerror(errno));
125 add_temp_file (fname);
126 if (filename != NULL)
132 /* Timeout handler. We kill the child and exit with an error. */
134 __attribute__ ((noreturn))
135 signal_handler (int sig __attribute__ ((unused)))
144 /* Wait for it to terminate. */
145 for (i = 0; i < 5; ++i)
148 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
152 /* Delay, give the system time to process the kill. If the
153 nanosleep() call return prematurely, all the better. We
154 won't restart it since this probably means the child process
157 ts.tv_nsec = 100000000;
158 nanosleep (&ts, NULL);
160 if (killed != 0 && killed != pid)
162 perror ("Failed to kill test process");
166 #ifdef CLEANUP_HANDLER
172 signal (sig, SIG_DFL);
176 /* If we expected this signal: good! */
177 #ifdef EXPECTED_SIGNAL
178 if (EXPECTED_SIGNAL == SIGALRM)
182 if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
183 fputs ("Timed out: killed the child process\n", stderr);
184 else if (WIFSTOPPED (status))
185 fprintf (stderr, "Timed out: the child process was %s\n",
186 strsignal (WSTOPSIG (status)));
187 else if (WIFSIGNALED (status))
188 fprintf (stderr, "Timed out: the child process got signal %s\n",
189 strsignal (WTERMSIG (status)));
191 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
192 WEXITSTATUS (status));
194 /* Exit with an error. */
198 #ifdef __XXX_HANDLE_CTRL_C
200 __attribute__ ((noreturn))
201 handler_killpid(int sig)
203 kill(pid, SIGKILL); /* kill test */
204 signal(sig, SIG_DFL);
205 raise(sig); /* kill ourself */
206 _exit(128 + sig); /* paranoia */
210 /* We provide the entry point here. */
212 main (int argc, char *argv[])
214 #ifdef __ARCH_USE_MMU__
215 int direct = 0; /* Directly call the test function? */
221 unsigned int timeoutfactor = 1;
223 char *envstr_timeoutfactor;
225 /* Make uses of freed and uninitialized memory known. */
226 #ifdef __MALLOC_STANDARD__
228 # define M_PERTURB -6
230 mallopt (M_PERTURB, 42);
233 #ifdef STDOUT_UNBUFFERED
234 setbuf (stdout, NULL);
237 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
248 #ifdef CMDLINE_PROCESS
253 /* If set, read the test TIMEOUTFACTOR value from the environment.
254 This value is used to scale the default test timeout values. */
255 envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
256 if (envstr_timeoutfactor != NULL)
258 char *envstr_conv = envstr_timeoutfactor;
259 unsigned long int env_fact;
261 env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
262 if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
263 timeoutfactor = MAX (env_fact, 1);
266 /* Set TMPDIR to specified test directory. */
267 if (test_dir != NULL)
269 setenv ("TMPDIR", test_dir, 1);
271 if (chdir (test_dir) < 0)
279 test_dir = getenv ("TMPDIR");
280 if (test_dir == NULL || test_dir[0] == '\0')
284 /* Make sure we see all message, even those on stdout. */
285 setvbuf (stdout, NULL, _IONBF, 0);
287 /* make sure temporary files are deleted. */
288 atexit (delete_temp_files);
290 /* Correct for the possible parameters. */
291 argv[optind - 1] = argv[0];
295 /* Call the initializing function, if one is available. */
297 PREPARE (argc, argv);
300 /* If we are not expected to fork run the function immediately. */
302 return TEST_FUNCTION;
304 /* Set up the test environment:
307 - fork and execute the function. */
309 #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
313 /* This is the child. */
315 struct rlimit data_limit;
318 /* Try to avoid dumping core. */
319 struct rlimit core_limit;
320 core_limit.rlim_cur = 0;
321 core_limit.rlim_max = 0;
322 setrlimit (RLIMIT_CORE, &core_limit);
326 /* Try to avoid eating all memory if a test leaks. */
327 if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
329 if (TEST_DATA_LIMIT == RLIM_INFINITY)
330 data_limit.rlim_cur = data_limit.rlim_max;
331 else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
332 data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
333 data_limit.rlim_max);
334 if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
335 perror ("setrlimit: RLIMIT_DATA");
338 perror ("getrlimit: RLIMIT_DATA");
341 /* We put the test process in its own pgrp so that if it bogusly
342 generates any job control signals, they won't hit the whole build. */
345 /* Execute the test function and exit with the return value. */
346 exit (TEST_FUNCTION);
351 perror ("Cannot fork test program");
355 #ifdef __XXX_HANDLE_CTRL_C
356 signal (SIGTERM, handler_killpid);
357 signal (SIGINT, handler_killpid);
358 signal (SIGQUIT, handler_killpid);
363 /* Default timeout is two seconds. */
366 signal (SIGALRM, signal_handler);
367 alarm (TIMEOUT * timeoutfactor);
369 /* Make sure we clean up if the wrapper gets interrupted. */
370 signal (SIGINT, signal_handler);
372 /* Wait for the regular termination. */
373 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
376 printf ("Waiting for test program failed: %s\n", strerror(errno));
381 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
382 (long int) pid, (long int) termpid);
386 #ifndef EXPECTED_SIGNAL
387 /* We don't expect any signal. */
388 # define EXPECTED_SIGNAL 0
390 if (WTERMSIG (status) != EXPECTED_SIGNAL)
392 if (EXPECTED_SIGNAL != 0)
394 if (WTERMSIG (status) == 0)
396 "Expected signal '%s' from child, got none\n",
397 strsignal (EXPECTED_SIGNAL));
400 "Incorrect signal from child: got `%s', need `%s'\n",
401 strsignal (WTERMSIG (status)),
402 strsignal (EXPECTED_SIGNAL));
405 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
406 strsignal (WTERMSIG (status)));
410 /* Simply exit with the return value of the test. */
411 #ifndef EXPECTED_STATUS
412 return WEXITSTATUS (status);
414 if (WEXITSTATUS (status) != EXPECTED_STATUS)
416 fprintf (stderr, "Expected status %d, got %d\n",
417 EXPECTED_STATUS, WEXITSTATUS (status));