1 /* Skeleton for test programs.
2 Copyright (C) 1998, 2000, 2001, 2002, 2003 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <sys/resource.h>
31 #include <sys/param.h>
33 /* The test function is normally called `do_test' and it is called
34 with argc and argv as the arguments. We nevertheless provide the
35 possibility to overwrite this name. */
37 # define TEST_FUNCTION do_test (argc, argv)
40 #ifndef TEST_DATA_LIMIT
41 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
44 #define OPT_DIRECT 1000
45 #define OPT_TESTDIR 1001
47 static struct option options[] =
49 #ifdef CMDLINE_OPTIONS
52 { "direct", no_argument, NULL, OPT_DIRECT },
53 { "test-dir", required_argument, NULL, OPT_TESTDIR },
57 /* PID of the test itself. */
60 /* Directory to place temporary files in. */
61 static const char *test_dir;
63 /* List of temporary files. */
70 /* Add temporary files in list. */
72 __attribute__ ((unused))
73 add_temp_file (const char *name)
75 struct temp_name_list *newp
76 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
80 if (temp_name_list == NULL)
81 temp_name_list = (struct temp_name_list *) &newp->q;
83 insque (newp, temp_name_list);
87 /* Delete all temporary files. */
89 delete_temp_files (void)
91 while (temp_name_list != NULL)
93 remove (temp_name_list->name);
94 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
98 /* Create a temporary file. */
100 __attribute__ ((unused))
101 create_temp_file (const char *base, char **filename)
106 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
107 + sizeof ("XXXXXX"));
110 puts ("out of memory");
113 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
115 fd = mkstemp (fname);
118 printf ("cannot open temporary file '%s': %m\n", fname);
123 add_temp_file (fname);
124 if (filename != NULL)
130 /* Timeout handler. We kill the child and exit with an error. */
132 __attribute__ ((noreturn))
133 timeout_handler (int sig __attribute__ ((unused)))
141 /* Wait for it to terminate. */
142 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
143 if (killed != 0 && killed != pid)
145 perror ("Failed to killed test process");
149 #ifdef CLEANUP_HANDLER
153 /* If we expected this signal: good! */
154 #ifdef EXPECTED_SIGNAL
155 if (EXPECTED_SIGNAL == SIGALRM)
159 if (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
160 fputs ("Timed out: killed the child process\n", stderr);
161 else if (WIFSTOPPED (status))
162 fprintf (stderr, "Timed out: the child process was %s\n",
163 strsignal (WSTOPSIG (status)));
164 else if (WIFSIGNALED (status))
165 fprintf (stderr, "Timed out: the child process got signal %s\n",
166 strsignal (WTERMSIG (status)));
168 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
169 WEXITSTATUS (status));
171 /* Exit with an error. */
175 /* We provide the entry point here. */
177 main (int argc, char *argv[])
179 int direct = 0; /* Directly call the test function? */
184 #ifdef STDOUT_UNBUFFERED
185 setbuf (stdout, NULL);
188 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
199 #ifdef CMDLINE_PROCESS
204 /* Set TMPDIR to specified test directory. */
205 if (test_dir != NULL)
207 setenv ("TMPDIR", test_dir, 1);
209 if (chdir (test_dir) < 0)
217 test_dir = getenv ("TMPDIR");
218 if (test_dir == NULL || test_dir[0] == '\0')
222 /* Make sure we see all message, even those on stdout. */
223 setvbuf (stdout, NULL, _IONBF, 0);
225 /* make sure temporary files are deleted. */
226 atexit (delete_temp_files);
228 /* Correct for the possible parameters. */
229 argv[optind - 1] = argv[0];
233 /* Call the initializing function, if one is available. */
235 PREPARE (argc, argv);
238 /* If we are not expected to fork run the function immediately. */
240 return TEST_FUNCTION;
242 /* Set up the test environment:
245 - fork and execute the function. */
250 /* This is the child. */
252 /* Try to avoid dumping core. */
253 struct rlimit core_limit;
254 core_limit.rlim_cur = 0;
255 core_limit.rlim_max = 0;
256 setrlimit (RLIMIT_CORE, &core_limit);
260 /* Try to avoid eating all memory if a test leaks. */
261 struct rlimit data_limit;
262 if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
264 if (TEST_DATA_LIMIT == RLIM_INFINITY)
265 data_limit.rlim_cur = data_limit.rlim_max;
266 else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
267 data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
268 data_limit.rlim_max);
269 if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
270 perror ("setrlimit: RLIMIT_DATA");
273 perror ("getrlimit: RLIMIT_DATA");
276 /* We put the test process in its own pgrp so that if it bogusly
277 generates any job control signals, they won't hit the whole build. */
280 /* Execute the test function and exit with the return value. */
281 exit (TEST_FUNCTION);
285 perror ("Cannot fork test program");
291 /* Default timeout is two seconds. */
294 signal (SIGALRM, timeout_handler);
297 /* Wait for the regular termination. */
298 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
301 printf ("Waiting for test program failed: %m\n");
306 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
307 (long int) pid, (long int) termpid);
311 #ifndef EXPECTED_SIGNAL
312 /* We don't expect any signal. */
313 # define EXPECTED_SIGNAL 0
315 if (WTERMSIG (status) != EXPECTED_SIGNAL)
317 if (EXPECTED_SIGNAL != 0)
319 if (WTERMSIG (status) == 0)
321 "Expected signal '%s' from child, got none\n",
322 strsignal (EXPECTED_SIGNAL));
325 "Incorrect signal from child: got `%s', need `%s'\n",
326 strsignal (WTERMSIG (status)),
327 strsignal (EXPECTED_SIGNAL));
330 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
331 strsignal (WTERMSIG (status)));
335 /* Simply exit with the return value of the test. */
336 return WEXITSTATUS (status);