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/>. */
28 #include <sys/resource.h>
30 #include <sys/param.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. */
38 # define TEST_FUNCTION do_test (argc, argv)
41 #ifndef TEST_DATA_LIMIT
42 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
45 #define OPT_DIRECT 1000
46 #define OPT_TESTDIR 1001
48 #if 0 /* Not used in uClibc */
49 static struct option options[] =
51 #ifdef CMDLINE_OPTIONS
54 { "direct", no_argument, NULL, OPT_DIRECT },
55 { "test-dir", required_argument, NULL, OPT_TESTDIR },
60 /* PID of the test itself. */
63 /* Directory to place temporary files in. */
64 static const char *test_dir;
66 /* List of temporary files. */
73 /* Add temporary files in list. */
75 __attribute__ ((unused))
76 add_temp_file (const char *name)
78 struct temp_name_list *newp
79 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
83 if (temp_name_list == NULL)
84 temp_name_list = (struct temp_name_list *) &newp->q;
86 insque (newp, temp_name_list);
90 /* Delete all temporary files. */
92 delete_temp_files (void)
94 while (temp_name_list != NULL)
96 remove (temp_name_list->name);
97 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
101 /* Create a temporary file. */
103 __attribute__ ((unused))
104 create_temp_file (const char *base, char **filename)
109 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
110 + sizeof ("XXXXXX"));
113 puts ("out of memory");
116 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
118 _fd = mkstemp (fname);
121 printf ("cannot open temporary file '%s': %s\n", fname, strerror(errno));
126 add_temp_file (fname);
127 if (filename != NULL)
133 /* Timeout handler. We kill the child and exit with an error. */
135 __attribute__ ((noreturn))
136 signal_handler (int sig __attribute__ ((unused)))
145 /* Wait for it to terminate. */
146 for (i = 0; i < 5; ++i)
148 #ifdef __UCLIBC_HAS_REALTIME__
151 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
155 /* Delay, give the system time to process the kill. If the
156 nanosleep() call return prematurely, all the better. We
157 won't restart it since this probably means the child process
159 #ifdef __UCLIBC_HAS_REALTIME__
161 ts.tv_nsec = 100000000;
162 nanosleep (&ts, NULL);
164 /* No nanosleep, just sleep 1s instead of 0.1s */
168 if (killed != 0 && killed != pid)
170 perror ("Failed to kill test process");
174 #ifdef CLEANUP_HANDLER
180 signal (sig, SIG_DFL);
184 /* If we expected this signal: good! */
185 #ifdef EXPECTED_SIGNAL
186 if (EXPECTED_SIGNAL == SIGALRM)
190 if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
191 fputs ("Timed out: killed the child process\n", stderr);
192 else if (WIFSTOPPED (status))
193 fprintf (stderr, "Timed out: the child process was %s\n",
194 strsignal (WSTOPSIG (status)));
195 else if (WIFSIGNALED (status))
196 fprintf (stderr, "Timed out: the child process got signal %s\n",
197 strsignal (WTERMSIG (status)));
199 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
200 WEXITSTATUS (status));
202 /* Exit with an error. */
206 #ifdef __XXX_HANDLE_CTRL_C
208 __attribute__ ((noreturn))
209 handler_killpid(int sig)
211 kill(pid, SIGKILL); /* kill test */
212 signal(sig, SIG_DFL);
213 raise(sig); /* kill ourself */
214 _exit(128 + sig); /* paranoia */
218 /* We provide the entry point here. */
220 main (int argc, char *argv[])
222 #ifdef __ARCH_USE_MMU__
223 int direct = 0; /* Directly call the test function? */
229 unsigned int timeoutfactor = 1;
231 char *envstr_timeoutfactor;
233 /* Make uses of freed and uninitialized memory known. */
234 #ifdef __MALLOC_STANDARD__
236 # define M_PERTURB -6
238 mallopt (M_PERTURB, 42);
241 #ifdef STDOUT_UNBUFFERED
242 setbuf (stdout, NULL);
245 #if 0 /* Not used in uClibc */
246 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
248 # ifndef CMDLINE_OPTIONS
249 # define CMDLINE_OPTIONS ""
251 while ((opt = getopt (argc, argv, "+" CMDLINE_OPTIONS)) >= 0)
263 #ifdef CMDLINE_PROCESS
268 /* If set, read the test TIMEOUTFACTOR value from the environment.
269 This value is used to scale the default test timeout values. */
270 envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
271 if (envstr_timeoutfactor != NULL)
273 char *envstr_conv = envstr_timeoutfactor;
274 unsigned long int env_fact;
276 env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
277 if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
278 timeoutfactor = MAX (env_fact, 1);
281 /* Set TMPDIR to specified test directory. */
282 if (test_dir != NULL)
284 setenv ("TMPDIR", test_dir, 1);
286 if (chdir (test_dir) < 0)
294 test_dir = getenv ("TMPDIR");
295 if (test_dir == NULL || test_dir[0] == '\0')
299 /* Make sure we see all message, even those on stdout. */
300 setvbuf (stdout, NULL, _IONBF, 0);
302 /* make sure temporary files are deleted. */
303 atexit (delete_temp_files);
305 /* Correct for the possible parameters. */
306 argv[optind - 1] = argv[0];
310 /* Call the initializing function, if one is available. */
312 PREPARE (argc, argv);
315 /* If we are not expected to fork run the function immediately. */
317 return TEST_FUNCTION;
319 /* Set up the test environment:
322 - fork and execute the function. */
324 #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
328 /* This is the child. */
330 struct rlimit data_limit;
333 /* Try to avoid dumping core. */
334 struct rlimit core_limit;
335 core_limit.rlim_cur = 0;
336 core_limit.rlim_max = 0;
337 setrlimit (RLIMIT_CORE, &core_limit);
341 /* Try to avoid eating all memory if a test leaks. */
342 if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
344 if (TEST_DATA_LIMIT == RLIM_INFINITY)
345 data_limit.rlim_cur = data_limit.rlim_max;
346 else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
347 data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
348 data_limit.rlim_max);
349 if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
350 perror ("setrlimit: RLIMIT_DATA");
353 perror ("getrlimit: RLIMIT_DATA");
356 /* We put the test process in its own pgrp so that if it bogusly
357 generates any job control signals, they won't hit the whole build. */
360 /* Execute the test function and exit with the return value. */
361 exit (TEST_FUNCTION);
366 perror ("Cannot fork test program");
370 #ifdef __XXX_HANDLE_CTRL_C
371 signal (SIGTERM, handler_killpid);
372 signal (SIGINT, handler_killpid);
373 signal (SIGQUIT, handler_killpid);
378 /* Default timeout is two seconds. */
381 signal (SIGALRM, signal_handler);
382 alarm (TIMEOUT * timeoutfactor);
384 /* Make sure we clean up if the wrapper gets interrupted. */
385 signal (SIGINT, signal_handler);
387 /* Wait for the regular termination. */
388 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
391 printf ("Waiting for test program failed: %s\n", strerror(errno));
396 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
397 (long int) pid, (long int) termpid);
401 #ifndef EXPECTED_SIGNAL
402 /* We don't expect any signal. */
403 # define EXPECTED_SIGNAL 0
405 if (WTERMSIG (status) != EXPECTED_SIGNAL)
407 if (EXPECTED_SIGNAL != 0)
409 if (WTERMSIG (status) == 0)
411 "Expected signal '%s' from child, got none\n",
412 strsignal (EXPECTED_SIGNAL));
415 "Incorrect signal from child: got `%s', need `%s'\n",
416 strsignal (WTERMSIG (status)),
417 strsignal (EXPECTED_SIGNAL));
420 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
421 strsignal (WTERMSIG (status)));
425 /* Simply exit with the return value of the test. */
426 #ifndef EXPECTED_STATUS
427 return WEXITSTATUS (status);
429 if (WEXITSTATUS (status) != EXPECTED_STATUS)
431 fprintf (stderr, "Expected status %d, got %d\n",
432 EXPECTED_STATUS, WEXITSTATUS (status));