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