ia64: sync with glibc headers
[uclibc-ng.git] / test / test-skeleton.c
blob4f268048521e2a15e570c8b930dbb4486b8e3929
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/>. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <getopt.h>
23 #include <malloc.h>
24 #include <search.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/resource.h>
31 #include <sys/wait.h>
32 #include <sys/param.h>
33 #include <time.h>
34 #include <features.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. */
39 #ifndef TEST_FUNCTION
40 # define TEST_FUNCTION do_test (argc, argv)
41 #endif
43 #ifndef TEST_DATA_LIMIT
44 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
45 #endif
47 /* PID of the test itself. */
48 static pid_t pid;
50 /* Directory to place temporary files in. */
51 static const char *test_dir;
53 /* List of temporary files. */
54 struct temp_name_list
56 struct qelem q;
57 char *name;
58 } *temp_name_list;
60 /* Add temporary files in list. */
61 static void
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)
70 newp->name = newname;
71 if (temp_name_list == NULL)
72 temp_name_list = (struct temp_name_list *) &newp->q;
73 else
74 insque (newp, temp_name_list);
76 else
77 free (newp);
80 /* Delete all temporary files. */
81 static void
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
99 *FILENAME. */
100 static int
101 __attribute__ ((unused))
102 create_temp_file (const char *base, char **filename)
104 char *fname;
105 int _fd;
107 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
108 + sizeof ("XXXXXX"));
109 if (fname == NULL)
111 puts ("out of memory");
112 return -1;
114 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
116 _fd = mkstemp (fname);
117 if (_fd == -1)
119 printf ("cannot open temporary file '%s': %s\n", fname, strerror(errno));
120 free (fname);
121 return -1;
124 add_temp_file (fname);
125 if (filename != NULL)
126 *filename = fname;
127 else
128 free (fname);
130 return _fd;
133 /* Timeout handler. We kill the child and exit with an error. */
134 static void
135 __attribute__ ((noreturn))
136 signal_handler (int sig __attribute__ ((unused)))
138 int killed = 0;
139 int status;
140 int i;
142 assert (pid > 1);
143 /* Kill the whole process group. */
144 kill (-pid, SIGKILL);
145 /* In case setpgid failed in the child, kill it individually too. */
146 kill (pid, SIGKILL);
148 /* Wait for it to terminate. */
149 for (i = 0; i < 5; ++i)
151 #ifdef __UCLIBC_HAS_REALTIME__
152 struct timespec ts;
153 #endif
154 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
155 if (killed != 0)
156 break;
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
161 finally died. */
162 #ifdef __UCLIBC_HAS_REALTIME__
163 ts.tv_sec = 0;
164 ts.tv_nsec = 100000000;
165 nanosleep (&ts, NULL);
166 #else
167 /* No nanosleep, just sleep 1s instead of 0.1s */
168 sleep(1);
169 #endif
171 if (killed != 0 && killed != pid)
173 perror ("Failed to kill test process");
174 exit (1);
177 #ifdef CLEANUP_HANDLER
178 CLEANUP_HANDLER;
179 #endif
181 if (sig == SIGINT)
183 signal (sig, SIG_DFL);
184 raise (sig);
187 /* If we expected this signal: good! */
188 #ifdef EXPECTED_SIGNAL
189 if (EXPECTED_SIGNAL == SIGALRM)
190 exit (0);
191 #endif
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)));
201 else
202 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
203 WEXITSTATUS (status));
205 /* Exit with an error. */
206 exit (1);
209 #ifdef __XXX_HANDLE_CTRL_C
210 static void
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 */
219 #endif
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? */
227 #else
228 int direct = 1;
229 #endif
230 int status;
231 int opt;
232 unsigned int timeoutfactor = 1;
233 pid_t termpid;
234 char *envstr_timeoutfactor;
236 /* Make uses of freed and uninitialized memory known. */
237 #ifdef __MALLOC_STANDARD__
238 #ifndef M_PERTURB
239 # define M_PERTURB -6
240 #endif
241 mallopt (M_PERTURB, 42);
242 #endif
244 #ifdef STDOUT_UNBUFFERED
245 setbuf (stdout, NULL);
246 #endif
248 # ifndef CMDLINE_OPTIONS
249 # define CMDLINE_OPTIONS ""
250 # endif
251 while ((opt = getopt (argc, argv, "+dt:" CMDLINE_OPTIONS)) >= 0)
252 switch (opt)
254 case '?':
255 exit (1);
256 case 'd':
257 direct = 1;
258 break;
259 case 't':
260 test_dir = optarg;
261 break;
262 #ifdef CMDLINE_PROCESS
263 CMDLINE_PROCESS
264 #endif
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)
287 perror ("chdir");
288 exit (1);
291 else
293 test_dir = getenv ("TMPDIR");
294 if (test_dir == NULL || test_dir[0] == '\0')
295 test_dir = "/tmp";
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];
306 argv += optind - 1;
307 argc -= optind - 1;
309 /* Call the initializing function, if one is available. */
310 #ifdef PREPARE
311 PREPARE (argc, argv);
312 #endif
314 /* If we are not expected to fork run the function immediately. */
315 if (direct)
316 return TEST_FUNCTION;
318 /* Set up the test environment:
319 - prevent core dumps
320 - set up the timer
321 - fork and execute the function. */
323 #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
324 pid = fork ();
325 if (pid == 0)
327 /* This is the child. */
328 #ifdef RLIMIT_DATA
329 struct rlimit data_limit;
330 #endif
331 #ifdef RLIMIT_CORE
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);
337 #endif
339 #ifdef RLIMIT_DATA
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");
351 else
352 perror ("getrlimit: RLIMIT_DATA");
353 #endif
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);
363 else if (pid < 0)
364 #endif
366 perror ("Cannot fork test program");
367 exit (1);
370 #ifdef __XXX_HANDLE_CTRL_C
371 signal (SIGTERM, handler_killpid);
372 signal (SIGINT, handler_killpid);
373 signal (SIGQUIT, handler_killpid);
374 #endif
376 /* Set timeout. */
377 #ifndef TIMEOUT
378 /* Default timeout is two seconds. */
379 # define TIMEOUT 2
380 #endif
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));
389 if (termpid == -1)
391 printf ("Waiting for test program failed: %s\n", strerror(errno));
392 exit (1);
394 if (termpid != pid)
396 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
397 (long int) pid, (long int) termpid);
398 exit (1);
401 #ifndef EXPECTED_SIGNAL
402 /* We don't expect any signal. */
403 # define EXPECTED_SIGNAL 0
404 #endif
405 if (WTERMSIG (status) != EXPECTED_SIGNAL)
407 if (EXPECTED_SIGNAL != 0)
409 if (WTERMSIG (status) == 0)
410 fprintf (stderr,
411 "Expected signal '%s' from child, got none\n",
412 strsignal (EXPECTED_SIGNAL));
413 else
414 fprintf (stderr,
415 "Incorrect signal from child: got `%s', need `%s'\n",
416 strsignal (WTERMSIG (status)),
417 strsignal (EXPECTED_SIGNAL));
419 else
420 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
421 strsignal (WTERMSIG (status)));
422 exit (1);
425 /* Simply exit with the return value of the test. */
426 #ifndef EXPECTED_STATUS
427 return WEXITSTATUS (status);
428 #else
429 if (WEXITSTATUS (status) != EXPECTED_STATUS)
431 fprintf (stderr, "Expected status %d, got %d\n",
432 EXPECTED_STATUS, WEXITSTATUS (status));
433 exit (1);
436 return 0;
437 #endif