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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include <sys/resource.h>
32 #include <sys/param.h>
36 /* The test function is normally called `do_test' and it is called
37 with argc and argv as the arguments. We nevertheless provide the
38 possibility to overwrite this name. */
40 # define TEST_FUNCTION do_test (argc, argv)
43 #ifndef TEST_DATA_LIMIT
44 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
47 #define OPT_DIRECT 1000
48 #define OPT_TESTDIR 1001
50 static struct option options
[] =
52 #ifdef CMDLINE_OPTIONS
55 { "direct", no_argument
, NULL
, OPT_DIRECT
},
56 { "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 timeout_handler (int sig
__attribute__ ((unused
)))
145 /* Wait for it to terminate. */
146 for (i
= 0; i
< 5; ++i
)
149 killed
= waitpid (pid
, &status
, WNOHANG
|WUNTRACED
);
153 /* Delay, give the system time to process the kill. If the
154 nanosleep() call return prematurely, all the better. We
155 won't restart it since this probably means the child process
158 ts
.tv_nsec
= 100000000;
159 nanosleep (&ts
, NULL
);
161 if (killed
!= 0 && killed
!= pid
)
163 perror ("Failed to kill test process");
167 #ifdef CLEANUP_HANDLER
171 /* If we expected this signal: good! */
172 #ifdef EXPECTED_SIGNAL
173 if (EXPECTED_SIGNAL
== SIGALRM
)
177 if (killed
== 0 || (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGKILL
))
178 fputs ("Timed out: killed the child process\n", stderr
);
179 else if (WIFSTOPPED (status
))
180 fprintf (stderr
, "Timed out: the child process was %s\n",
181 strsignal (WSTOPSIG (status
)));
182 else if (WIFSIGNALED (status
))
183 fprintf (stderr
, "Timed out: the child process got signal %s\n",
184 strsignal (WTERMSIG (status
)));
186 fprintf (stderr
, "Timed out: killed the child process but it exited %d\n",
187 WEXITSTATUS (status
));
189 /* Exit with an error. */
194 __attribute__ ((noreturn
))
195 handler_killpid(int sig
)
197 kill(pid
, SIGKILL
); /* kill test */
198 signal(sig
, SIG_DFL
);
199 raise(sig
); /* kill ourself */
200 _exit(128 + sig
); /* paranoia */
203 /* We provide the entry point here. */
205 main (int argc
, char *argv
[])
207 #ifdef __ARCH_USE_MMU__
208 int direct
= 0; /* Directly call the test function? */
214 unsigned int timeoutfactor
= 1;
216 char *envstr_timeoutfactor
;
218 /* Make uses of freed and uninitialized memory known. */
219 #ifdef __MALLOC_STANDARD__
221 # define M_PERTURB -6
223 mallopt (M_PERTURB
, 42);
226 #ifdef STDOUT_UNBUFFERED
227 setbuf (stdout
, NULL
);
230 while ((opt
= getopt_long (argc
, argv
, "+", options
, NULL
)) != -1)
241 #ifdef CMDLINE_PROCESS
246 /* If set, read the test TIMEOUTFACTOR value from the environment.
247 This value is used to scale the default test timeout values. */
248 envstr_timeoutfactor
= getenv ("TIMEOUTFACTOR");
249 if (envstr_timeoutfactor
!= NULL
)
251 char *envstr_conv
= envstr_timeoutfactor
;
252 unsigned long int env_fact
;
254 env_fact
= strtoul (envstr_timeoutfactor
, &envstr_conv
, 0);
255 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_timeoutfactor
)
256 timeoutfactor
= MAX (env_fact
, 1);
259 /* Set TMPDIR to specified test directory. */
260 if (test_dir
!= NULL
)
262 setenv ("TMPDIR", test_dir
, 1);
264 if (chdir (test_dir
) < 0)
272 test_dir
= getenv ("TMPDIR");
273 if (test_dir
== NULL
|| test_dir
[0] == '\0')
277 /* Make sure we see all message, even those on stdout. */
278 setvbuf (stdout
, NULL
, _IONBF
, 0);
280 /* make sure temporary files are deleted. */
281 atexit (delete_temp_files
);
283 /* Correct for the possible parameters. */
284 argv
[optind
- 1] = argv
[0];
288 /* Call the initializing function, if one is available. */
290 PREPARE (argc
, argv
);
293 /* If we are not expected to fork run the function immediately. */
295 return TEST_FUNCTION
;
297 /* Set up the test environment:
300 - fork and execute the function. */
302 #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
306 /* This is the child. */
308 struct rlimit data_limit
;
311 /* Try to avoid dumping core. */
312 struct rlimit core_limit
;
313 core_limit
.rlim_cur
= 0;
314 core_limit
.rlim_max
= 0;
315 setrlimit (RLIMIT_CORE
, &core_limit
);
319 /* Try to avoid eating all memory if a test leaks. */
320 if (getrlimit (RLIMIT_DATA
, &data_limit
) == 0)
322 if (TEST_DATA_LIMIT
== RLIM_INFINITY
)
323 data_limit
.rlim_cur
= data_limit
.rlim_max
;
324 else if (data_limit
.rlim_cur
> (rlim_t
) TEST_DATA_LIMIT
)
325 data_limit
.rlim_cur
= MIN ((rlim_t
) TEST_DATA_LIMIT
,
326 data_limit
.rlim_max
);
327 if (setrlimit (RLIMIT_DATA
, &data_limit
) < 0)
328 perror ("setrlimit: RLIMIT_DATA");
331 perror ("getrlimit: RLIMIT_DATA");
334 /* We put the test process in its own pgrp so that if it bogusly
335 generates any job control signals, they won't hit the whole build. */
338 /* Execute the test function and exit with the return value. */
339 exit (TEST_FUNCTION
);
344 perror ("Cannot fork test program");
348 signal (SIGTERM
, handler_killpid
);
349 signal (SIGINT
, handler_killpid
);
350 signal (SIGQUIT
, handler_killpid
);
354 /* Default timeout is two seconds. */
357 signal (SIGALRM
, timeout_handler
);
358 alarm (TIMEOUT
* timeoutfactor
);
360 /* Wait for the regular termination. */
361 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
364 printf ("Waiting for test program failed: %s\n", strerror(errno
));
369 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
370 (long int) pid
, (long int) termpid
);
374 #ifndef EXPECTED_SIGNAL
375 /* We don't expect any signal. */
376 # define EXPECTED_SIGNAL 0
378 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
380 if (EXPECTED_SIGNAL
!= 0)
382 if (WTERMSIG (status
) == 0)
384 "Expected signal '%s' from child, got none\n",
385 strsignal (EXPECTED_SIGNAL
));
388 "Incorrect signal from child: got `%s', need `%s'\n",
389 strsignal (WTERMSIG (status
)),
390 strsignal (EXPECTED_SIGNAL
));
393 fprintf (stderr
, "Didn't expect signal from child: got `%s'\n",
394 strsignal (WTERMSIG (status
)));
398 /* Simply exit with the return value of the test. */
399 #ifndef EXPECTED_STATUS
400 return WEXITSTATUS (status
);
402 if (WEXITSTATUS (status
) != EXPECTED_STATUS
)
404 fprintf (stderr
, "Expected status %d, got %d\n",
405 EXPECTED_STATUS
, WEXITSTATUS (status
));