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