1 /* Skeleton for test programs.
2 Copyright (C) 1998-2013 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/>. */
29 #include <sys/resource.h>
31 #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 static struct option options
[] =
50 #ifdef CMDLINE_OPTIONS
53 { "direct", no_argument
, NULL
, OPT_DIRECT
},
54 { "test-dir", required_argument
, NULL
, OPT_TESTDIR
},
58 /* PID of the test itself. */
61 /* Directory to place temporary files in. */
62 static const char *test_dir
;
64 /* List of temporary files. */
71 /* Add temporary files in list. */
73 __attribute__ ((unused
))
74 add_temp_file (const char *name
)
76 struct temp_name_list
*newp
77 = (struct temp_name_list
*) calloc (sizeof (*newp
), 1);
81 if (temp_name_list
== NULL
)
82 temp_name_list
= (struct temp_name_list
*) &newp
->q
;
84 insque (newp
, temp_name_list
);
88 /* Delete all temporary files. */
90 delete_temp_files (void)
92 while (temp_name_list
!= NULL
)
94 remove (temp_name_list
->name
);
95 temp_name_list
= (struct temp_name_list
*) temp_name_list
->q
.q_forw
;
99 /* Create a temporary file. */
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': %m\n", fname
);
124 add_temp_file (fname
);
125 if (filename
!= NULL
)
131 /* Timeout handler. We kill the child and exit with an error. */
133 __attribute__ ((noreturn
))
134 signal_handler (int sig
__attribute__ ((unused
)))
142 /* Wait for it to terminate. */
144 for (i
= 0; i
< 5; ++i
)
146 killed
= waitpid (pid
, &status
, WNOHANG
|WUNTRACED
);
150 /* Delay, give the system time to process the kill. If the
151 nanosleep() call return prematurely, all the better. We
152 won't restart it since this probably means the child process
156 ts
.tv_nsec
= 100000000;
157 nanosleep (&ts
, NULL
);
159 if (killed
!= 0 && killed
!= pid
)
161 perror ("Failed to kill test process");
165 #ifdef CLEANUP_HANDLER
171 signal (sig
, SIG_DFL
);
175 /* If we expected this signal: good! */
176 #ifdef EXPECTED_SIGNAL
177 if (EXPECTED_SIGNAL
== SIGALRM
)
181 if (killed
== 0 || (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGKILL
))
182 fputs ("Timed out: killed the child process\n", stderr
);
183 else if (WIFSTOPPED (status
))
184 fprintf (stderr
, "Timed out: the child process was %s\n",
185 strsignal (WSTOPSIG (status
)));
186 else if (WIFSIGNALED (status
))
187 fprintf (stderr
, "Timed out: the child process got signal %s\n",
188 strsignal (WTERMSIG (status
)));
190 fprintf (stderr
, "Timed out: killed the child process but it exited %d\n",
191 WEXITSTATUS (status
));
193 /* Exit with an error. */
197 /* We provide the entry point here. */
199 main (int argc
, char *argv
[])
201 int direct
= 0; /* Directly call the test function? */
204 unsigned int timeoutfactor
= 1;
207 /* Make uses of freed and uninitialized memory known. */
208 mallopt (M_PERTURB
, 42);
210 #ifdef STDOUT_UNBUFFERED
211 setbuf (stdout
, NULL
);
214 while ((opt
= getopt_long (argc
, argv
, "+", options
, NULL
)) != -1)
225 #ifdef CMDLINE_PROCESS
230 /* If set, read the test TIMEOUTFACTOR value from the environment.
231 This value is used to scale the default test timeout values. */
232 char *envstr_timeoutfactor
= getenv ("TIMEOUTFACTOR");
233 if (envstr_timeoutfactor
!= NULL
)
235 char *envstr_conv
= envstr_timeoutfactor
;
236 unsigned long int env_fact
;
238 env_fact
= strtoul (envstr_timeoutfactor
, &envstr_conv
, 0);
239 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_timeoutfactor
)
240 timeoutfactor
= MAX (env_fact
, 1);
243 /* Set TMPDIR to specified test directory. */
244 if (test_dir
!= NULL
)
246 setenv ("TMPDIR", test_dir
, 1);
248 if (chdir (test_dir
) < 0)
256 test_dir
= getenv ("TMPDIR");
257 if (test_dir
== NULL
|| test_dir
[0] == '\0')
261 /* Make sure we see all message, even those on stdout. */
262 setvbuf (stdout
, NULL
, _IONBF
, 0);
264 /* make sure temporary files are deleted. */
265 atexit (delete_temp_files
);
267 /* Correct for the possible parameters. */
268 argv
[optind
- 1] = argv
[0];
272 /* Call the initializing function, if one is available. */
274 PREPARE (argc
, argv
);
277 /* If we are not expected to fork run the function immediately. */
279 return TEST_FUNCTION
;
281 /* Set up the test environment:
284 - fork and execute the function. */
289 /* This is the child. */
291 /* Try to avoid dumping core. */
292 struct rlimit core_limit
;
293 core_limit
.rlim_cur
= 0;
294 core_limit
.rlim_max
= 0;
295 setrlimit (RLIMIT_CORE
, &core_limit
);
299 /* Try to avoid eating all memory if a test leaks. */
300 struct rlimit data_limit
;
301 if (getrlimit (RLIMIT_DATA
, &data_limit
) == 0)
303 if (TEST_DATA_LIMIT
== RLIM_INFINITY
)
304 data_limit
.rlim_cur
= data_limit
.rlim_max
;
305 else if (data_limit
.rlim_cur
> (rlim_t
) TEST_DATA_LIMIT
)
306 data_limit
.rlim_cur
= MIN ((rlim_t
) TEST_DATA_LIMIT
,
307 data_limit
.rlim_max
);
308 if (setrlimit (RLIMIT_DATA
, &data_limit
) < 0)
309 perror ("setrlimit: RLIMIT_DATA");
312 perror ("getrlimit: RLIMIT_DATA");
315 /* We put the test process in its own pgrp so that if it bogusly
316 generates any job control signals, they won't hit the whole build. */
319 /* Execute the test function and exit with the return value. */
320 exit (TEST_FUNCTION
);
324 perror ("Cannot fork test program");
330 /* Default timeout is two seconds. */
333 signal (SIGALRM
, signal_handler
);
334 alarm (TIMEOUT
* timeoutfactor
);
336 /* Make sure we clean up if the wrapper gets interrupted. */
337 signal (SIGINT
, signal_handler
);
339 /* Wait for the regular termination. */
340 termpid
= TEMP_FAILURE_RETRY (waitpid (pid
, &status
, 0));
343 printf ("Waiting for test program failed: %m\n");
348 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
349 (long int) pid
, (long int) termpid
);
353 #ifndef EXPECTED_SIGNAL
354 /* We don't expect any signal. */
355 # define EXPECTED_SIGNAL 0
357 if (WTERMSIG (status
) != EXPECTED_SIGNAL
)
359 if (EXPECTED_SIGNAL
!= 0)
361 if (WTERMSIG (status
) == 0)
363 "Expected signal '%s' from child, got none\n",
364 strsignal (EXPECTED_SIGNAL
));
367 "Incorrect signal from child: got `%s', need `%s'\n",
368 strsignal (WTERMSIG (status
)),
369 strsignal (EXPECTED_SIGNAL
));
372 fprintf (stderr
, "Didn't expect signal from child: got `%s'\n",
373 strsignal (WTERMSIG (status
)));
377 /* Simply exit with the return value of the test. */
378 #ifndef EXPECTED_STATUS
379 return WEXITSTATUS (status
);
381 if (WEXITSTATUS (status
) != EXPECTED_STATUS
)
383 fprintf (stderr
, "Expected status %d, got %d\n",
384 EXPECTED_STATUS
, WEXITSTATUS (status
));