1 /* Skeleton for test programs.
2 Copyright (C) 1998-2016 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/>. */
32 #include <sys/resource.h>
34 #include <sys/param.h>
37 /* The test function is normally called `do_test' and it is called
38 with argc and argv as the arguments. We nevertheless provide the
39 possibility to overwrite this name. */
41 # define TEST_FUNCTION do_test (argc, argv)
44 #ifndef TEST_DATA_LIMIT
45 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
49 /* Default timeout is twenty seconds. Tests should normally complete faster
50 than this, but if they don't, that's abnormal (a bug) anyways. */
54 #define OPT_DIRECT 1000
55 #define OPT_TESTDIR 1001
57 static struct option options
[] =
59 #ifdef CMDLINE_OPTIONS
62 { "direct", no_argument
, NULL
, OPT_DIRECT
},
63 { "test-dir", required_argument
, NULL
, OPT_TESTDIR
},
67 /* PID of the test itself. */
70 /* Directory to place temporary files in. */
71 static const char *test_dir
;
74 oom_error (const char *fn
, size_t size
)
76 printf ("%s: unable to allocate %zu bytes: %m\n", fn
, size
);
80 /* Allocate N bytes of memory dynamically, with error checking. */
82 __attribute__ ((used
))
89 oom_error ("malloc", n
);
93 /* Allocate memory for N elements of S bytes, with error checking. */
95 __attribute__ ((used
))
96 xcalloc (size_t n
, size_t s
)
102 oom_error ("calloc", n
* s
);
106 /* Change the size of an allocated block of memory P to N bytes,
107 with error checking. */
109 __attribute__ ((used
))
110 xrealloc (void *p
, size_t n
)
114 oom_error ("realloc", n
);
118 /* List of temporary files. */
119 struct temp_name_list
125 /* Add temporary files in list. */
127 __attribute__ ((unused
))
128 add_temp_file (const char *name
)
130 struct temp_name_list
*newp
131 = (struct temp_name_list
*) xcalloc (sizeof (*newp
), 1);
132 char *newname
= strdup (name
);
135 newp
->name
= newname
;
136 if (temp_name_list
== NULL
)
137 temp_name_list
= (struct temp_name_list
*) &newp
->q
;
139 insque (newp
, temp_name_list
);
145 /* Delete all temporary files. */
147 delete_temp_files (void)
149 while (temp_name_list
!= NULL
)
151 remove (temp_name_list
->name
);
152 free (temp_name_list
->name
);
154 struct temp_name_list
*next
155 = (struct temp_name_list
*) temp_name_list
->q
.q_forw
;
156 free (temp_name_list
);
157 temp_name_list
= next
;
161 /* Create a temporary file. Return the opened file descriptor on
162 success, or -1 on failure. Write the file name to *FILENAME if
163 FILENAME is not NULL. In this case, the caller is expected to free
166 __attribute__ ((unused
))
167 create_temp_file (const char *base
, char **filename
)
172 fname
= (char *) xmalloc (strlen (test_dir
) + 1 + strlen (base
)
173 + sizeof ("XXXXXX"));
174 strcpy (stpcpy (stpcpy (stpcpy (fname
, test_dir
), "/"), base
), "XXXXXX");
176 fd
= mkstemp (fname
);
179 printf ("cannot open temporary file '%s': %m\n", fname
);
184 add_temp_file (fname
);
185 if (filename
!= NULL
)
193 /* Timeout handler. We kill the child and exit with an error. */
195 __attribute__ ((noreturn
))
196 signal_handler (int sig
__attribute__ ((unused
)))
202 /* Kill the whole process group. */
203 kill (-pid
, SIGKILL
);
204 /* In case setpgid failed in the child, kill it individually too. */
207 /* Wait for it to terminate. */
209 for (i
= 0; i
< 5; ++i
)
211 killed
= waitpid (pid
, &status
, WNOHANG
|WUNTRACED
);
215 /* Delay, give the system time to process the kill. If the
216 nanosleep() call return prematurely, all the better. We
217 won't restart it since this probably means the child process
221 ts
.tv_nsec
= 100000000;
222 nanosleep (&ts
, NULL
);
224 if (killed
!= 0 && killed
!= pid
)
226 printf ("Failed to kill test process: %m\n");
230 #ifdef CLEANUP_HANDLER
236 signal (sig
, SIG_DFL
);
240 /* If we expected this signal: good! */
241 #ifdef EXPECTED_SIGNAL
242 if (EXPECTED_SIGNAL
== SIGALRM
)
246 if (killed
== 0 || (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGKILL
))
247 puts ("Timed out: killed the child process");
248 else if (WIFSTOPPED (status
))
249 printf ("Timed out: the child process was %s\n",
250 strsignal (WSTOPSIG (status
)));
251 else if (WIFSIGNALED (status
))
252 printf ("Timed out: the child process got signal %s\n",
253 strsignal (WTERMSIG (status
)));
255 printf ("Timed out: killed the child process but it exited %d\n",
256 WEXITSTATUS (status
));
258 /* Exit with an error. */
262 /* Avoid all the buffer overflow messages on stderr. */
264 __attribute__ ((unused
))
267 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
269 close (STDERR_FILENO
);
272 dup2 (fd
, STDERR_FILENO
);
275 setenv ("LIBC_FATAL_STDERR_", "1", 1);
278 /* Set fortification error handler. Used when tests want to verify that bad
279 code is caught by the library. */
281 __attribute__ ((unused
))
282 set_fortify_handler (void (*handler
) (int sig
))
286 sa
.sa_handler
= handler
;
288 sigemptyset (&sa
.sa_mask
);
290 sigaction (SIGABRT
, &sa
, NULL
);
294 /* Show people how to run the program. */
300 printf ("Usage: %s [options]\n"
302 "Environment Variables:\n"
303 " TIMEOUTFACTOR An integer used to scale the timeout\n"
304 " TMPDIR Where to place temporary files\n"
306 program_invocation_short_name
);
307 printf ("Options:\n");
308 for (i
= 0; options
[i
].name
; ++i
)
312 indent
= printf (" --%s", options
[i
].name
);
313 if (options
[i
].has_arg
== required_argument
)
314 indent
+= printf (" <arg>");
315 printf ("%*s", 25 - indent
, "");
316 switch (options
[i
].val
)
319 printf ("Run the test directly (instead of forking & monitoring)");
322 printf ("Override the TMPDIR env var");
329 /* We provide the entry point here. */
331 main (int argc
, char *argv
[])
333 int direct
= 0; /* Directly call the test function? */
336 unsigned int timeoutfactor
= 1;
339 /* Make uses of freed and uninitialized memory known. */
340 mallopt (M_PERTURB
, 42);
342 #ifdef STDOUT_UNBUFFERED
343 setbuf (stdout
, NULL
);
346 while ((opt
= getopt_long (argc
, argv
, "+", options
, NULL
)) != -1)
358 #ifdef CMDLINE_PROCESS
363 /* If set, read the test TIMEOUTFACTOR value from the environment.
364 This value is used to scale the default test timeout values. */
365 char *envstr_timeoutfactor
= getenv ("TIMEOUTFACTOR");
366 if (envstr_timeoutfactor
!= NULL
)
368 char *envstr_conv
= envstr_timeoutfactor
;
369 unsigned long int env_fact
;
371 env_fact
= strtoul (envstr_timeoutfactor
, &envstr_conv
, 0);
372 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_timeoutfactor
)
373 timeoutfactor
= MAX (env_fact
, 1);
376 /* Set TMPDIR to specified test directory. */
377 if (test_dir
!= NULL
)
379 setenv ("TMPDIR", test_dir
, 1);
381 if (chdir (test_dir
) < 0)
383 printf ("chdir: %m\n");
389 test_dir
= getenv ("TMPDIR");
390 if (test_dir
== NULL
|| test_dir
[0] == '\0')
394 /* Make sure we see all message, even those on stdout. */
395 setvbuf (stdout
, NULL
, _IONBF
, 0);
397 /* Make sure temporary files are deleted. */
398 atexit (delete_temp_files
);
400 /* Correct for the possible parameters. */
401 argv
[optind
- 1] = argv
[0];
405 /* Call the initializing function, if one is available. */
407 PREPARE (argc
, argv
);
410 const char *envstr_direct
= getenv ("TEST_DIRECT");
411 if (envstr_direct
!= NULL
)
413 FILE *f
= fopen (envstr_direct
, "w");
416 printf ("cannot open TEST_DIRECT output file '%s': %m\n",
421 fprintf (f
, "timeout=%u\ntimeoutfactor=%u\n", TIMEOUT
, timeoutfactor
);
422 #ifdef EXPECTED_STATUS
423 fprintf (f
, "exit=%u\n", EXPECTED_STATUS
);
425 #ifdef EXPECTED_SIGNAL
426 switch (EXPECTED_SIGNAL
)
429 # define init_sig(signo, name, text) \
430 case signo: fprintf (f, "signal=%s\n", name); break;
431 # include <siglist.h>
436 if (temp_name_list
!= NULL
)
438 struct temp_name_list
*n
;
439 fprintf (f
, "temp_files=(\n");
440 for (n
= temp_name_list
;
442 n
= (struct temp_name_list
*) n
->q
.q_forw
)
443 fprintf (f
, " '%s'\n", n
->name
);
451 /* If we are not expected to fork run the function immediately. */
453 return TEST_FUNCTION
;
455 /* Set up the test environment:
458 - fork and execute the function. */
463 /* This is the child. */
465 /* Try to avoid dumping core. */
466 struct rlimit core_limit
;
467 core_limit
.rlim_cur
= 0;
468 core_limit
.rlim_max
= 0;
469 setrlimit (RLIMIT_CORE
, &core_limit
);
472 /* We put the test process in its own pgrp so that if it bogusly
473 generates any job control signals, they won't hit the whole build. */
474 if (setpgid (0, 0) != 0)
475 printf ("Failed to set the process group ID: %m\n");
477 /* Execute the test function and exit with the return value. */
478 exit (TEST_FUNCTION
);
482 printf ("Cannot fork test program: %m\n");
487 signal (SIGALRM
, signal_handler
);
488 alarm (TIMEOUT
* timeoutfactor
);
490 /* Make sure we clean up if the wrapper gets interrupted. */
491 signal (SIGINT
, signal_handler
);
493 /* Wait for the regular termination. */
494 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
497 printf ("Waiting for test program failed: %m\n");
502 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
503 (long int) pid
, (long int) termpid
);
507 /* Process terminated normaly without timeout etc. */
508 if (WIFEXITED (status
))
510 #ifndef EXPECTED_STATUS
511 # ifndef EXPECTED_SIGNAL
512 /* Simply exit with the return value of the test. */
513 return WEXITSTATUS (status
);
515 printf ("Expected signal '%s' from child, got none\n",
516 strsignal (EXPECTED_SIGNAL
));
520 if (WEXITSTATUS (status
) != EXPECTED_STATUS
)
522 printf ("Expected status %d, got %d\n",
523 EXPECTED_STATUS
, WEXITSTATUS (status
));
530 /* Process was killed by timer or other signal. */
533 #ifndef EXPECTED_SIGNAL
534 printf ("Didn't expect signal from child: got `%s'\n",
535 strsignal (WTERMSIG (status
)));
538 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
540 printf ("Incorrect signal from child: got `%s', need `%s'\n",
541 strsignal (WTERMSIG (status
)),
542 strsignal (EXPECTED_SIGNAL
));