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/>. */
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 /* PID of the test itself. */
50 /* Directory to place temporary files in. */
51 static const char *test_dir
;
53 /* List of temporary files. */
60 /* Add temporary files in list. */
62 __attribute__ ((unused
))
63 add_temp_file (const char *name
)
65 struct temp_name_list
*newp
66 = (struct temp_name_list
*) calloc (sizeof (*newp
), 1);
67 char *newname
= strdup (name
);
68 if (newp
!= NULL
&& newname
!= NULL
)
71 if (temp_name_list
== NULL
)
72 temp_name_list
= (struct temp_name_list
*) &newp
->q
;
74 insque (newp
, temp_name_list
);
80 /* Delete all temporary files. */
82 delete_temp_files (void)
84 while (temp_name_list
!= NULL
)
86 remove (temp_name_list
->name
);
87 free (temp_name_list
->name
);
89 struct temp_name_list
*next
90 = (struct temp_name_list
*) temp_name_list
->q
.q_forw
;
91 free (temp_name_list
);
92 temp_name_list
= next
;
96 /* Create a temporary file. Return the opened file descriptor on
97 success, or -1 on failure. Write the file name to *FILENAME if
98 FILENAME is not NULL. In this case, the caller is expected to free
101 __attribute__ ((unused
))
102 create_temp_file (const char *base
, char **filename
)
107 fname
= (char *) malloc (strlen (test_dir
) + 1 + strlen (base
)
108 + sizeof ("XXXXXX"));
111 puts ("out of memory");
114 strcpy (stpcpy (stpcpy (stpcpy (fname
, test_dir
), "/"), base
), "XXXXXX");
116 _fd
= mkstemp (fname
);
119 printf ("cannot open temporary file '%s': %s\n", fname
, strerror(errno
));
124 add_temp_file (fname
);
125 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
)))
143 /* Kill the whole process group. */
144 kill (-pid
, SIGKILL
);
145 /* In case setpgid failed in the child, kill it individually too. */
148 /* Wait for it to terminate. */
149 for (i
= 0; i
< 5; ++i
)
151 #ifdef __UCLIBC_HAS_REALTIME__
154 killed
= waitpid (pid
, &status
, WNOHANG
|WUNTRACED
);
158 /* Delay, give the system time to process the kill. If the
159 nanosleep() call return prematurely, all the better. We
160 won't restart it since this probably means the child process
162 #ifdef __UCLIBC_HAS_REALTIME__
164 ts
.tv_nsec
= 100000000;
165 nanosleep (&ts
, NULL
);
167 /* No nanosleep, just sleep 1s instead of 0.1s */
171 if (killed
!= 0 && killed
!= pid
)
173 perror ("Failed to kill test process");
177 #ifdef CLEANUP_HANDLER
183 signal (sig
, SIG_DFL
);
187 /* If we expected this signal: good! */
188 #ifdef EXPECTED_SIGNAL
189 if (EXPECTED_SIGNAL
== SIGALRM
)
193 if (killed
== 0 || (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGKILL
))
194 fputs ("Timed out: killed the child process\n", stderr
);
195 else if (WIFSTOPPED (status
))
196 fprintf (stderr
, "Timed out: the child process was %s\n",
197 strsignal (WSTOPSIG (status
)));
198 else if (WIFSIGNALED (status
))
199 fprintf (stderr
, "Timed out: the child process got signal %s\n",
200 strsignal (WTERMSIG (status
)));
202 fprintf (stderr
, "Timed out: killed the child process but it exited %d\n",
203 WEXITSTATUS (status
));
205 /* Exit with an error. */
209 #ifdef __XXX_HANDLE_CTRL_C
211 __attribute__ ((noreturn
))
212 handler_killpid(int sig
)
214 kill(pid
, SIGKILL
); /* kill test */
215 signal(sig
, SIG_DFL
);
216 raise(sig
); /* kill ourself */
217 _exit(128 + sig
); /* paranoia */
221 /* We provide the entry point here. */
223 main (int argc
, char *argv
[])
225 #ifdef __ARCH_USE_MMU__
226 int direct
= 0; /* Directly call the test function? */
232 unsigned int timeoutfactor
= 1;
234 char *envstr_timeoutfactor
;
236 /* Make uses of freed and uninitialized memory known. */
237 #ifdef __MALLOC_STANDARD__
239 # define M_PERTURB -6
241 mallopt (M_PERTURB
, 42);
244 #ifdef STDOUT_UNBUFFERED
245 setbuf (stdout
, NULL
);
248 # ifndef CMDLINE_OPTIONS
249 # define CMDLINE_OPTIONS ""
251 while ((opt
= getopt (argc
, argv
, "+dt:" CMDLINE_OPTIONS
)) >= 0)
262 #ifdef CMDLINE_PROCESS
267 /* If set, read the test TIMEOUTFACTOR value from the environment.
268 This value is used to scale the default test timeout values. */
269 envstr_timeoutfactor
= getenv ("TIMEOUTFACTOR");
270 if (envstr_timeoutfactor
!= NULL
)
272 char *envstr_conv
= envstr_timeoutfactor
;
273 unsigned long int env_fact
;
275 env_fact
= strtoul (envstr_timeoutfactor
, &envstr_conv
, 0);
276 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_timeoutfactor
)
277 timeoutfactor
= MAX (env_fact
, 1);
280 /* Set TMPDIR to specified test directory. */
281 if (test_dir
!= NULL
)
283 setenv ("TMPDIR", test_dir
, 1);
285 if (chdir (test_dir
) < 0)
293 test_dir
= getenv ("TMPDIR");
294 if (test_dir
== NULL
|| test_dir
[0] == '\0')
298 /* Make sure we see all message, even those on stdout. */
299 setvbuf (stdout
, NULL
, _IONBF
, 0);
301 /* make sure temporary files are deleted. */
302 atexit (delete_temp_files
);
304 /* Correct for the possible parameters. */
305 argv
[optind
- 1] = argv
[0];
309 /* Call the initializing function, if one is available. */
311 PREPARE (argc
, argv
);
314 /* If we are not expected to fork run the function immediately. */
316 return TEST_FUNCTION
;
318 /* Set up the test environment:
321 - fork and execute the function. */
323 #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
327 /* This is the child. */
329 struct rlimit data_limit
;
332 /* Try to avoid dumping core. */
333 struct rlimit core_limit
;
334 core_limit
.rlim_cur
= 0;
335 core_limit
.rlim_max
= 0;
336 setrlimit (RLIMIT_CORE
, &core_limit
);
340 /* Try to avoid eating all memory if a test leaks. */
341 if (getrlimit (RLIMIT_DATA
, &data_limit
) == 0)
343 if (TEST_DATA_LIMIT
== RLIM_INFINITY
)
344 data_limit
.rlim_cur
= data_limit
.rlim_max
;
345 else if (data_limit
.rlim_cur
> (rlim_t
) TEST_DATA_LIMIT
)
346 data_limit
.rlim_cur
= MIN ((rlim_t
) TEST_DATA_LIMIT
,
347 data_limit
.rlim_max
);
348 if (setrlimit (RLIMIT_DATA
, &data_limit
) < 0)
349 perror ("setrlimit: RLIMIT_DATA");
352 perror ("getrlimit: RLIMIT_DATA");
355 /* We put the test process in its own pgrp so that if it bogusly
356 generates any job control signals, they won't hit the whole build. */
357 if (setpgid (0, 0) != 0)
358 printf ("Failed to set the process group ID: %m\n");
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
));