* sysdeps/m68k/Makefile (CFLAGS-.oS): Append -fPIC.
[glibc/pb-stable.git] / test-skeleton.c
blob24f8fb8eed5dc21c8fef8e5e7d097e87c8a23d3d
1 /* Skeleton for test programs.
2 Copyright (C) 1998, 2000, 2001, 2002 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
19 02111-1307 USA. */
21 #include <getopt.h>
22 #include <search.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/resource.h>
29 #include <sys/wait.h>
30 #include <sys/param.h>
32 /* The test function is normally called `do_test' and it is called
33 with argc and argv as the arguments. We nevertheless provide the
34 possibility to overwrite this name. */
35 #ifndef TEST_FUNCTION
36 # define TEST_FUNCTION do_test (argc, argv)
37 #endif
39 #ifndef TEST_DATA_LIMIT
40 # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
41 #endif
43 #define OPT_DIRECT 1000
44 #define OPT_TESTDIR 1001
46 static struct option options[] =
48 #ifdef CMDLINE_OPTIONS
49 CMDLINE_OPTIONS
50 #endif
51 { "direct", no_argument, NULL, OPT_DIRECT },
52 { "test-dir", required_argument, NULL, OPT_TESTDIR },
53 { NULL, 0, NULL, 0 }
56 /* PID of the test itself. */
57 static pid_t pid;
59 /* Directory to place temporary files in. */
60 static const char *test_dir;
62 /* List of temporary files. */
63 struct temp_name_list
65 struct qelem q;
66 const char *name;
67 } *temp_name_list;
69 /* Add temporary files in list. */
70 static void
71 __attribute__ ((unused))
72 add_temp_file (const char *name)
74 struct temp_name_list *newp
75 = (struct temp_name_list *) calloc (sizeof (*newp), 1);
76 if (newp != NULL)
78 newp->name = name;
79 if (temp_name_list == NULL)
80 temp_name_list = (struct temp_name_list *) &newp->q;
81 else
82 insque (newp, temp_name_list);
86 /* Delete all temporary files. */
87 static void
88 delete_temp_files (void)
90 while (temp_name_list != NULL)
92 remove (temp_name_list->name);
93 temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
97 /* Create a temporary file. */
98 static int
99 __attribute__ ((unused))
100 create_temp_file (const char *base, char **filename)
102 char *fname;
103 int fd;
105 fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
106 + sizeof ("XXXXXX"));
107 if (fname == NULL)
109 puts ("out of memory");
110 return -1;
112 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
114 fd = mkstemp (fname);
115 if (fd == -1)
117 printf ("cannot open temporary file '%s': %m\n", fname);
118 free (fname);
119 return -1;
122 add_temp_file (fname);
123 if (filename != NULL)
124 *filename = fname;
126 return fd;
129 /* Timeout handler. We kill the child and exit with an error. */
130 static void
131 __attribute__ ((noreturn))
132 timeout_handler (int sig __attribute__ ((unused)))
134 int killed;
135 int status;
137 /* Send signal. */
138 kill (pid, SIGKILL);
140 /* Wait for it to terminate. */
141 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
142 if (killed != 0 && killed != pid)
144 perror ("Failed to killed test process");
145 exit (1);
148 #ifdef CLEANUP_HANDLER
149 CLEANUP_HANDLER;
150 #endif
152 /* If we expected this signal: good! */
153 #ifdef EXPECTED_SIGNAL
154 if (EXPECTED_SIGNAL == SIGALRM)
155 exit (0);
156 #endif
158 if (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)
159 fputs ("Timed out: killed the child process\n", stderr);
160 else if (WIFSTOPPED (status))
161 fprintf (stderr, "Timed out: the child process was %s\n",
162 strsignal (WSTOPSIG (status)));
163 else if (WIFSIGNALED (status))
164 fprintf (stderr, "Timed out: the child process got signal %s\n",
165 strsignal (WTERMSIG (status)));
166 else
167 fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
168 WEXITSTATUS (status));
170 /* Exit with an error. */
171 exit (1);
174 /* We provide the entry point here. */
176 main (int argc, char *argv[])
178 int direct = 0; /* Directly call the test function? */
179 int status;
180 int opt;
181 pid_t termpid;
183 #ifdef STDOUT_UNBUFFERED
184 setbuf (stdout, NULL);
185 #endif
187 while ((opt = getopt_long (argc, argv, "", options, NULL)) != -1)
188 switch (opt)
190 case '?':
191 exit (1);
192 case OPT_DIRECT:
193 direct = 1;
194 break;
195 case OPT_TESTDIR:
196 test_dir = optarg;
197 break;
198 #ifdef CMDLINE_PROCESS
199 CMDLINE_PROCESS
200 #endif
203 /* Set TMPDIR to specified test directory. */
204 if (test_dir != NULL)
206 setenv ("TMPDIR", test_dir, 1);
208 if (chdir (test_dir) < 0)
210 perror ("chdir");
211 exit (1);
214 else
216 test_dir = getenv ("TMPDIR");
217 if (test_dir == NULL || test_dir[0] == '\0')
218 test_dir = "/tmp";
221 /* Make sure we see all message, even those on stdout. */
222 setvbuf (stdout, NULL, _IONBF, 0);
224 /* make sure temporary files are deleted. */
225 atexit (delete_temp_files);
227 /* Correct for the possible parameters. */
228 argv[optind - 1] = argv[0];
229 argv += optind - 1;
230 argc -= optind - 1;
232 /* Call the initializing function, if one is available. */
233 #ifdef PREPARE
234 PREPARE (argc, argv);
235 #endif
237 /* If we are not expected to fork run the function immediately. */
238 if (direct)
239 return TEST_FUNCTION;
241 /* Set up the test environment:
242 - prevent core dumps
243 - set up the timer
244 - fork and execute the function. */
246 pid = fork ();
247 if (pid == 0)
249 /* This is the child. */
250 #ifdef RLIMIT_CORE
251 /* Try to avoid dumping core. */
252 struct rlimit core_limit;
253 core_limit.rlim_cur = 0;
254 core_limit.rlim_max = 0;
255 setrlimit (RLIMIT_CORE, &core_limit);
256 #endif
258 #ifdef RLIMIT_DATA
259 /* Try to avoid eating all memory if a test leaks. */
260 struct rlimit data_limit;
261 if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
263 if (TEST_DATA_LIMIT == RLIM_INFINITY)
264 data_limit.rlim_cur = data_limit.rlim_max;
265 else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
266 data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
267 data_limit.rlim_max);
268 if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
269 perror ("setrlimit: RLIMIT_DATA");
271 else
272 perror ("getrlimit: RLIMIT_DATA");
273 #endif
275 /* We put the test process in its own pgrp so that if it bogusly
276 generates any job control signals, they won't hit the whole build. */
277 setpgid (0, 0);
279 /* Execute the test function and exit with the return value. */
280 exit (TEST_FUNCTION);
282 else if (pid < 0)
284 perror ("Cannot fork test program");
285 exit (1);
288 /* Set timeout. */
289 #ifndef TIMEOUT
290 /* Default timeout is two seconds. */
291 # define TIMEOUT 2
292 #endif
293 alarm (TIMEOUT);
294 signal (SIGALRM, timeout_handler);
296 /* Wait for the regular termination. */
297 termpid = waitpid (pid, &status, 0);
298 if (termpid == -1)
300 printf ("Waiting for test program failed: %m\n");
301 exit (1);
303 if (termpid != pid)
305 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
306 (long int) pid, (long int) termpid);
307 exit (1);
310 #ifndef EXPECTED_SIGNAL
311 /* We don't expect any signal. */
312 # define EXPECTED_SIGNAL 0
313 #endif
314 if (WTERMSIG (status) != EXPECTED_SIGNAL)
316 if (EXPECTED_SIGNAL != 0)
318 if (WTERMSIG (status) == 0)
319 fprintf (stderr,
320 "Expected signal '%s' from child, got none\n",
321 strsignal (EXPECTED_SIGNAL));
322 else
323 fprintf (stderr,
324 "Incorrect signal from child: got `%s', need `%s'\n",
325 strsignal (WTERMSIG (status)),
326 strsignal (EXPECTED_SIGNAL));
328 else
329 fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
330 strsignal (WTERMSIG (status)));
331 exit (1);
334 /* Simply exit with the return value of the test. */
335 return WEXITSTATUS (status);