]>
Commit | Line | Data |
---|---|---|
80a18298 | 1 | /* Skeleton for test programs. |
854278df | 2 | Copyright (C) 1998,2000-2004, 2005 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
80a18298 UD |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
80a18298 UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
80a18298 | 15 | |
41bdb6e2 AJ |
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 | |
19 | 02111-1307 USA. */ | |
80a18298 | 20 | |
e1d8e1b7 | 21 | #include <errno.h> |
80a18298 | 22 | #include <getopt.h> |
854278df | 23 | #include <malloc.h> |
b9337b6a | 24 | #include <search.h> |
80a18298 UD |
25 | #include <signal.h> |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
4380ef5e | 28 | #include <string.h> |
80a18298 UD |
29 | #include <unistd.h> |
30 | #include <sys/resource.h> | |
31 | #include <sys/wait.h> | |
63b11dd1 | 32 | #include <sys/param.h> |
fa4a36fd | 33 | #include <time.h> |
80a18298 UD |
34 | |
35 | /* The test function is normally called `do_test' and it is called | |
36 | with argc and argv as the arguments. We nevertheless provide the | |
37 | possibility to overwrite this name. */ | |
38 | #ifndef TEST_FUNCTION | |
39 | # define TEST_FUNCTION do_test (argc, argv) | |
40 | #endif | |
41 | ||
63b11dd1 RM |
42 | #ifndef TEST_DATA_LIMIT |
43 | # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */ | |
44 | #endif | |
80a18298 UD |
45 | |
46 | #define OPT_DIRECT 1000 | |
47 | #define OPT_TESTDIR 1001 | |
48 | ||
49 | static struct option options[] = | |
50 | { | |
51 | #ifdef CMDLINE_OPTIONS | |
52 | CMDLINE_OPTIONS | |
53 | #endif | |
54 | { "direct", no_argument, NULL, OPT_DIRECT }, | |
55 | { "test-dir", required_argument, NULL, OPT_TESTDIR }, | |
56 | { NULL, 0, NULL, 0 } | |
57 | }; | |
58 | ||
59 | /* PID of the test itself. */ | |
608c5afd | 60 | static pid_t pid; |
80a18298 UD |
61 | |
62 | /* Directory to place temporary files in. */ | |
63 | static const char *test_dir; | |
64 | ||
b9337b6a | 65 | /* List of temporary files. */ |
51eecc4a | 66 | struct temp_name_list |
b9337b6a UD |
67 | { |
68 | struct qelem q; | |
69 | const char *name; | |
51eecc4a | 70 | } *temp_name_list; |
b9337b6a UD |
71 | |
72 | /* Add temporary files in list. */ | |
9c0592ab | 73 | static void |
5cd6f8f7 | 74 | __attribute__ ((unused)) |
b9337b6a UD |
75 | add_temp_file (const char *name) |
76 | { | |
51eecc4a AJ |
77 | struct temp_name_list *newp |
78 | = (struct temp_name_list *) calloc (sizeof (*newp), 1); | |
b9337b6a UD |
79 | if (newp != NULL) |
80 | { | |
81 | newp->name = name; | |
51eecc4a AJ |
82 | if (temp_name_list == NULL) |
83 | temp_name_list = (struct temp_name_list *) &newp->q; | |
b9337b6a | 84 | else |
51eecc4a | 85 | insque (newp, temp_name_list); |
b9337b6a UD |
86 | } |
87 | } | |
88 | ||
89 | /* Delete all temporary files. */ | |
9c0592ab | 90 | static void |
b9337b6a UD |
91 | delete_temp_files (void) |
92 | { | |
51eecc4a | 93 | while (temp_name_list != NULL) |
b9337b6a | 94 | { |
51eecc4a AJ |
95 | remove (temp_name_list->name); |
96 | temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw; | |
b9337b6a UD |
97 | } |
98 | } | |
99 | ||
10e62564 UD |
100 | /* Create a temporary file. */ |
101 | static int | |
102 | __attribute__ ((unused)) | |
103 | create_temp_file (const char *base, char **filename) | |
104 | { | |
105 | char *fname; | |
106 | int fd; | |
107 | ||
108 | fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base) | |
109 | + sizeof ("XXXXXX")); | |
110 | if (fname == NULL) | |
111 | { | |
112 | puts ("out of memory"); | |
113 | return -1; | |
114 | } | |
115 | strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX"); | |
116 | ||
117 | fd = mkstemp (fname); | |
118 | if (fd == -1) | |
119 | { | |
120 | printf ("cannot open temporary file '%s': %m\n", fname); | |
121 | free (fname); | |
122 | return -1; | |
123 | } | |
124 | ||
125 | add_temp_file (fname); | |
126 | if (filename != NULL) | |
127 | *filename = fname; | |
128 | ||
129 | return fd; | |
130 | } | |
131 | ||
80a18298 | 132 | /* Timeout handler. We kill the child and exit with an error. */ |
9c0592ab | 133 | static void |
8c0b7170 | 134 | __attribute__ ((noreturn)) |
80a18298 UD |
135 | timeout_handler (int sig __attribute__ ((unused))) |
136 | { | |
137 | int killed; | |
b79e3737 | 138 | int status; |
80a18298 UD |
139 | |
140 | /* Send signal. */ | |
141 | kill (pid, SIGKILL); | |
142 | ||
143 | /* Wait for it to terminate. */ | |
6d0e6e84 UD |
144 | int i; |
145 | for (i = 0; i < 5; ++i) | |
146 | { | |
147 | killed = waitpid (pid, &status, WNOHANG|WUNTRACED); | |
148 | if (killed != 0) | |
149 | break; | |
150 | ||
151 | /* Delay, give the system time to process the kill. If the | |
152 | nanosleep() call return prematurely, all the better. We | |
153 | won't restart it since this probably means the child process | |
154 | finally died. */ | |
a334319f | 155 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 }; |
6d0e6e84 UD |
156 | nanosleep (&ts, NULL); |
157 | } | |
80a18298 UD |
158 | if (killed != 0 && killed != pid) |
159 | { | |
520ec963 | 160 | perror ("Failed to kill test process"); |
80a18298 UD |
161 | exit (1); |
162 | } | |
163 | ||
164 | #ifdef CLEANUP_HANDLER | |
165 | CLEANUP_HANDLER; | |
166 | #endif | |
167 | ||
4614167a UD |
168 | /* If we expected this signal: good! */ |
169 | #ifdef EXPECTED_SIGNAL | |
170 | if (EXPECTED_SIGNAL == SIGALRM) | |
171 | exit (0); | |
172 | #endif | |
173 | ||
6d0e6e84 | 174 | if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)) |
b79e3737 RM |
175 | fputs ("Timed out: killed the child process\n", stderr); |
176 | else if (WIFSTOPPED (status)) | |
177 | fprintf (stderr, "Timed out: the child process was %s\n", | |
178 | strsignal (WSTOPSIG (status))); | |
179 | else if (WIFSIGNALED (status)) | |
180 | fprintf (stderr, "Timed out: the child process got signal %s\n", | |
181 | strsignal (WTERMSIG (status))); | |
182 | else | |
183 | fprintf (stderr, "Timed out: killed the child process but it exited %d\n", | |
184 | WEXITSTATUS (status)); | |
80a18298 UD |
185 | |
186 | /* Exit with an error. */ | |
187 | exit (1); | |
188 | } | |
189 | ||
190 | /* We provide the entry point here. */ | |
191 | int | |
192 | main (int argc, char *argv[]) | |
193 | { | |
194 | int direct = 0; /* Directly call the test function? */ | |
195 | int status; | |
196 | int opt; | |
dc391246 | 197 | unsigned int timeoutfactor = 1; |
608c5afd | 198 | pid_t termpid; |
80a18298 | 199 | |
854278df UD |
200 | /* Make uses of freed and uninitialized memory known. */ |
201 | mallopt (M_PERTURB, 42); | |
202 | ||
e7c036b3 UD |
203 | #ifdef STDOUT_UNBUFFERED |
204 | setbuf (stdout, NULL); | |
205 | #endif | |
206 | ||
6f1acb30 | 207 | while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1) |
80a18298 UD |
208 | switch (opt) |
209 | { | |
210 | case '?': | |
211 | exit (1); | |
212 | case OPT_DIRECT: | |
213 | direct = 1; | |
214 | break; | |
215 | case OPT_TESTDIR: | |
216 | test_dir = optarg; | |
217 | break; | |
218 | #ifdef CMDLINE_PROCESS | |
219 | CMDLINE_PROCESS | |
220 | #endif | |
221 | } | |
222 | ||
dc391246 UD |
223 | /* If set, read the test TIMEOUTFACTOR value from the environment. |
224 | This value is used to scale the default test timeout values. */ | |
225 | char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR"); | |
226 | if (envstr_timeoutfactor != NULL) | |
227 | { | |
228 | char *envstr_conv = envstr_timeoutfactor; | |
229 | unsigned long int env_fact; | |
230 | ||
231 | env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0); | |
232 | if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor) | |
233 | timeoutfactor = MAX (env_fact, 1); | |
234 | } | |
235 | ||
80a18298 UD |
236 | /* Set TMPDIR to specified test directory. */ |
237 | if (test_dir != NULL) | |
238 | { | |
239 | setenv ("TMPDIR", test_dir, 1); | |
240 | ||
241 | if (chdir (test_dir) < 0) | |
242 | { | |
243 | perror ("chdir"); | |
244 | exit (1); | |
245 | } | |
246 | } | |
b9337b6a UD |
247 | else |
248 | { | |
249 | test_dir = getenv ("TMPDIR"); | |
250 | if (test_dir == NULL || test_dir[0] == '\0') | |
251 | test_dir = "/tmp"; | |
252 | } | |
80a18298 | 253 | |
6b9c2e67 UD |
254 | /* Make sure we see all message, even those on stdout. */ |
255 | setvbuf (stdout, NULL, _IONBF, 0); | |
256 | ||
310b3460 UD |
257 | /* make sure temporary files are deleted. */ |
258 | atexit (delete_temp_files); | |
259 | ||
726b7b0f | 260 | /* Correct for the possible parameters. */ |
55033a44 | 261 | argv[optind - 1] = argv[0]; |
726b7b0f UD |
262 | argv += optind - 1; |
263 | argc -= optind - 1; | |
264 | ||
310b3460 UD |
265 | /* Call the initializing function, if one is available. */ |
266 | #ifdef PREPARE | |
267 | PREPARE (argc, argv); | |
268 | #endif | |
269 | ||
80a18298 UD |
270 | /* If we are not expected to fork run the function immediately. */ |
271 | if (direct) | |
272 | return TEST_FUNCTION; | |
273 | ||
274 | /* Set up the test environment: | |
275 | - prevent core dumps | |
276 | - set up the timer | |
277 | - fork and execute the function. */ | |
278 | ||
279 | pid = fork (); | |
280 | if (pid == 0) | |
281 | { | |
282 | /* This is the child. */ | |
283 | #ifdef RLIMIT_CORE | |
284 | /* Try to avoid dumping core. */ | |
285 | struct rlimit core_limit; | |
286 | core_limit.rlim_cur = 0; | |
287 | core_limit.rlim_max = 0; | |
288 | setrlimit (RLIMIT_CORE, &core_limit); | |
289 | #endif | |
290 | ||
63b11dd1 RM |
291 | #ifdef RLIMIT_DATA |
292 | /* Try to avoid eating all memory if a test leaks. */ | |
293 | struct rlimit data_limit; | |
294 | if (getrlimit (RLIMIT_DATA, &data_limit) == 0) | |
295 | { | |
296 | if (TEST_DATA_LIMIT == RLIM_INFINITY) | |
297 | data_limit.rlim_cur = data_limit.rlim_max; | |
298 | else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT) | |
299 | data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT, | |
300 | data_limit.rlim_max); | |
301 | if (setrlimit (RLIMIT_DATA, &data_limit) < 0) | |
302 | perror ("setrlimit: RLIMIT_DATA"); | |
303 | } | |
304 | else | |
305 | perror ("getrlimit: RLIMIT_DATA"); | |
306 | #endif | |
307 | ||
b79e3737 RM |
308 | /* We put the test process in its own pgrp so that if it bogusly |
309 | generates any job control signals, they won't hit the whole build. */ | |
310 | setpgid (0, 0); | |
311 | ||
80a18298 UD |
312 | /* Execute the test function and exit with the return value. */ |
313 | exit (TEST_FUNCTION); | |
314 | } | |
315 | else if (pid < 0) | |
316 | { | |
317 | perror ("Cannot fork test program"); | |
318 | exit (1); | |
319 | } | |
320 | ||
321 | /* Set timeout. */ | |
322 | #ifndef TIMEOUT | |
323 | /* Default timeout is two seconds. */ | |
324 | # define TIMEOUT 2 | |
325 | #endif | |
80a18298 | 326 | signal (SIGALRM, timeout_handler); |
dc391246 | 327 | alarm (TIMEOUT * timeoutfactor); |
80a18298 UD |
328 | |
329 | /* Wait for the regular termination. */ | |
53854476 | 330 | termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)); |
608c5afd | 331 | if (termpid == -1) |
80a18298 | 332 | { |
608c5afd UD |
333 | printf ("Waiting for test program failed: %m\n"); |
334 | exit (1); | |
335 | } | |
336 | if (termpid != pid) | |
337 | { | |
338 | printf ("Oops, wrong test program terminated: expected %ld, got %ld\n", | |
339 | (long int) pid, (long int) termpid); | |
80a18298 UD |
340 | exit (1); |
341 | } | |
342 | ||
343 | #ifndef EXPECTED_SIGNAL | |
344 | /* We don't expect any signal. */ | |
345 | # define EXPECTED_SIGNAL 0 | |
346 | #endif | |
789b13c4 | 347 | if (WTERMSIG (status) != EXPECTED_SIGNAL) |
80a18298 | 348 | { |
b9337b6a | 349 | if (EXPECTED_SIGNAL != 0) |
3fc65a77 UD |
350 | { |
351 | if (WTERMSIG (status) == 0) | |
352 | fprintf (stderr, | |
353 | "Expected signal '%s' from child, got none\n", | |
354 | strsignal (EXPECTED_SIGNAL)); | |
355 | else | |
356 | fprintf (stderr, | |
357 | "Incorrect signal from child: got `%s', need `%s'\n", | |
358 | strsignal (WTERMSIG (status)), | |
359 | strsignal (EXPECTED_SIGNAL)); | |
360 | } | |
b9337b6a | 361 | else |
6b9c2e67 | 362 | fprintf (stderr, "Didn't expect signal from child: got `%s'\n", |
b9337b6a | 363 | strsignal (WTERMSIG (status))); |
80a18298 UD |
364 | exit (1); |
365 | } | |
366 | ||
367 | /* Simply exit with the return value of the test. */ | |
ede0f73a | 368 | #ifndef EXPECTED_STATUS |
80a18298 | 369 | return WEXITSTATUS (status); |
ede0f73a UD |
370 | #else |
371 | if (WEXITSTATUS (status) != EXPECTED_STATUS) | |
372 | { | |
373 | fprintf (stderr, "Expected status %d, got %d\n", | |
374 | EXPECTED_STATUS, WEXITSTATUS (status)); | |
375 | exit (1); | |
376 | } | |
377 | ||
378 | return 0; | |
379 | #endif | |
80a18298 | 380 | } |