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
;
73 /* List of temporary files. */
80 /* Add temporary files in list. */
82 __attribute__ ((unused
))
83 add_temp_file (const char *name
)
85 struct temp_name_list
*newp
86 = (struct temp_name_list
*) calloc (sizeof (*newp
), 1);
87 char *newname
= strdup (name
);
88 if (newp
!= NULL
&& newname
!= NULL
)
91 if (temp_name_list
== NULL
)
92 temp_name_list
= (struct temp_name_list
*) &newp
->q
;
94 insque (newp
, temp_name_list
);
100 /* Delete all temporary files. */
102 delete_temp_files (void)
104 while (temp_name_list
!= NULL
)
106 remove (temp_name_list
->name
);
107 free (temp_name_list
->name
);
109 struct temp_name_list
*next
110 = (struct temp_name_list
*) temp_name_list
->q
.q_forw
;
111 free (temp_name_list
);
112 temp_name_list
= next
;
116 /* Create a temporary file. Return the opened file descriptor on
117 success, or -1 on failure. Write the file name to *FILENAME if
118 FILENAME is not NULL. In this case, the caller is expected to free
121 __attribute__ ((unused
))
122 create_temp_file (const char *base
, char **filename
)
127 fname
= (char *) malloc (strlen (test_dir
) + 1 + strlen (base
)
128 + sizeof ("XXXXXX"));
131 puts ("out of memory");
134 strcpy (stpcpy (stpcpy (stpcpy (fname
, test_dir
), "/"), base
), "XXXXXX");
136 fd
= mkstemp (fname
);
139 printf ("cannot open temporary file '%s': %m\n", fname
);
144 add_temp_file (fname
);
145 if (filename
!= NULL
)
153 /* Timeout handler. We kill the child and exit with an error. */
155 __attribute__ ((noreturn
))
156 signal_handler (int sig
__attribute__ ((unused
)))
162 /* Kill the whole process group. */
163 kill (-pid
, SIGKILL
);
164 /* In case setpgid failed in the child, kill it individually too. */
167 /* Wait for it to terminate. */
169 for (i
= 0; i
< 5; ++i
)
171 killed
= waitpid (pid
, &status
, WNOHANG
|WUNTRACED
);
175 /* Delay, give the system time to process the kill. If the
176 nanosleep() call return prematurely, all the better. We
177 won't restart it since this probably means the child process
181 ts
.tv_nsec
= 100000000;
182 nanosleep (&ts
, NULL
);
184 if (killed
!= 0 && killed
!= pid
)
186 printf ("Failed to kill test process: %m\n");
190 #ifdef CLEANUP_HANDLER
196 signal (sig
, SIG_DFL
);
200 /* If we expected this signal: good! */
201 #ifdef EXPECTED_SIGNAL
202 if (EXPECTED_SIGNAL
== SIGALRM
)
206 if (killed
== 0 || (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGKILL
))
207 puts ("Timed out: killed the child process");
208 else if (WIFSTOPPED (status
))
209 printf ("Timed out: the child process was %s\n",
210 strsignal (WSTOPSIG (status
)));
211 else if (WIFSIGNALED (status
))
212 printf ("Timed out: the child process got signal %s\n",
213 strsignal (WTERMSIG (status
)));
215 printf ("Timed out: killed the child process but it exited %d\n",
216 WEXITSTATUS (status
));
218 /* Exit with an error. */
222 /* Avoid all the buffer overflow messages on stderr. */
224 __attribute__ ((unused
))
227 int fd
= open (_PATH_DEVNULL
, O_WRONLY
);
229 close (STDERR_FILENO
);
232 dup2 (fd
, STDERR_FILENO
);
235 setenv ("LIBC_FATAL_STDERR_", "1", 1);
238 /* Set fortification error handler. Used when tests want to verify that bad
239 code is caught by the library. */
241 __attribute__ ((unused
))
242 set_fortify_handler (void (*handler
) (int sig
))
246 sa
.sa_handler
= handler
;
248 sigemptyset (&sa
.sa_mask
);
250 sigaction (SIGABRT
, &sa
, NULL
);
254 /* Show people how to run the program. */
260 printf ("Usage: %s [options]\n"
262 "Environment Variables:\n"
263 " TIMEOUTFACTOR An integer used to scale the timeout\n"
264 " TMPDIR Where to place temporary files\n"
266 program_invocation_short_name
);
267 printf ("Options:\n");
268 for (i
= 0; options
[i
].name
; ++i
)
272 indent
= printf (" --%s", options
[i
].name
);
273 if (options
[i
].has_arg
== required_argument
)
274 indent
+= printf (" <arg>");
275 printf ("%*s", 25 - indent
, "");
276 switch (options
[i
].val
)
279 printf ("Run the test directly (instead of forking & monitoring)");
282 printf ("Override the TMPDIR env var");
289 /* We provide the entry point here. */
291 main (int argc
, char *argv
[])
293 int direct
= 0; /* Directly call the test function? */
296 unsigned int timeoutfactor
= 1;
299 /* Make uses of freed and uninitialized memory known. */
300 mallopt (M_PERTURB
, 42);
302 #ifdef STDOUT_UNBUFFERED
303 setbuf (stdout
, NULL
);
306 while ((opt
= getopt_long (argc
, argv
, "+", options
, NULL
)) != -1)
318 #ifdef CMDLINE_PROCESS
323 /* If set, read the test TIMEOUTFACTOR value from the environment.
324 This value is used to scale the default test timeout values. */
325 char *envstr_timeoutfactor
= getenv ("TIMEOUTFACTOR");
326 if (envstr_timeoutfactor
!= NULL
)
328 char *envstr_conv
= envstr_timeoutfactor
;
329 unsigned long int env_fact
;
331 env_fact
= strtoul (envstr_timeoutfactor
, &envstr_conv
, 0);
332 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_timeoutfactor
)
333 timeoutfactor
= MAX (env_fact
, 1);
336 /* Set TMPDIR to specified test directory. */
337 if (test_dir
!= NULL
)
339 setenv ("TMPDIR", test_dir
, 1);
341 if (chdir (test_dir
) < 0)
343 printf ("chdir: %m\n");
349 test_dir
= getenv ("TMPDIR");
350 if (test_dir
== NULL
|| test_dir
[0] == '\0')
354 /* Make sure we see all message, even those on stdout. */
355 setvbuf (stdout
, NULL
, _IONBF
, 0);
357 /* Make sure temporary files are deleted. */
358 atexit (delete_temp_files
);
360 /* Correct for the possible parameters. */
361 argv
[optind
- 1] = argv
[0];
365 /* Call the initializing function, if one is available. */
367 PREPARE (argc
, argv
);
370 const char *envstr_direct
= getenv ("TEST_DIRECT");
371 if (envstr_direct
!= NULL
)
373 FILE *f
= fopen (envstr_direct
, "w");
376 printf ("cannot open TEST_DIRECT output file '%s': %m\n",
381 fprintf (f
, "timeout=%u\ntimeoutfactor=%u\n", TIMEOUT
, timeoutfactor
);
382 #ifdef EXPECTED_STATUS
383 fprintf (f
, "exit=%u\n", EXPECTED_STATUS
);
385 #ifdef EXPECTED_SIGNAL
386 switch (EXPECTED_SIGNAL
)
389 # define init_sig(signo, name, text) \
390 case signo: fprintf (f, "signal=%s\n", name); break;
391 # include <siglist.h>
396 if (temp_name_list
!= NULL
)
398 struct temp_name_list
*n
;
399 fprintf (f
, "temp_files=(\n");
400 for (n
= temp_name_list
;
402 n
= (struct temp_name_list
*) n
->q
.q_forw
)
403 fprintf (f
, " '%s'\n", n
->name
);
411 /* If we are not expected to fork run the function immediately. */
413 return TEST_FUNCTION
;
415 /* Set up the test environment:
418 - fork and execute the function. */
423 /* This is the child. */
425 /* Try to avoid dumping core. */
426 struct rlimit core_limit
;
427 core_limit
.rlim_cur
= 0;
428 core_limit
.rlim_max
= 0;
429 setrlimit (RLIMIT_CORE
, &core_limit
);
432 /* We put the test process in its own pgrp so that if it bogusly
433 generates any job control signals, they won't hit the whole build. */
434 if (setpgid (0, 0) != 0)
435 printf ("Failed to set the process group ID: %m\n");
437 /* Execute the test function and exit with the return value. */
438 exit (TEST_FUNCTION
);
442 printf ("Cannot fork test program: %m\n");
447 signal (SIGALRM
, signal_handler
);
448 alarm (TIMEOUT
* timeoutfactor
);
450 /* Make sure we clean up if the wrapper gets interrupted. */
451 signal (SIGINT
, signal_handler
);
453 /* Wait for the regular termination. */
454 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
457 printf ("Waiting for test program failed: %m\n");
462 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
463 (long int) pid
, (long int) termpid
);
467 /* Process terminated normaly without timeout etc. */
468 if (WIFEXITED (status
))
470 #ifndef EXPECTED_STATUS
471 # ifndef EXPECTED_SIGNAL
472 /* Simply exit with the return value of the test. */
473 return WEXITSTATUS (status
);
475 printf ("Expected signal '%s' from child, got none\n",
476 strsignal (EXPECTED_SIGNAL
));
480 if (WEXITSTATUS (status
) != EXPECTED_STATUS
)
482 printf ("Expected status %d, got %d\n",
483 EXPECTED_STATUS
, WEXITSTATUS (status
));
490 /* Process was killed by timer or other signal. */
493 #ifndef EXPECTED_SIGNAL
494 printf ("Didn't expect signal from child: got `%s'\n",
495 strsignal (WTERMSIG (status
)));
498 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
500 printf ("Incorrect signal from child: got `%s', need `%s'\n",
501 strsignal (WTERMSIG (status
)),
502 strsignal (EXPECTED_SIGNAL
));