]> git.ipfire.org Git - thirdparty/glibc.git/blame - test-skeleton.c
Make common fdim implementation generic.
[thirdparty/glibc.git] / test-skeleton.c
CommitLineData
80a18298 1/* Skeleton for test programs.
f7a9f785 2 Copyright (C) 1998-2016 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 47
b0b88abc 48#ifndef TIMEOUT
a28605b2
MF
49 /* Default timeout is twenty seconds. Tests should normally complete faster
50 than this, but if they don't, that's abnormal (a bug) anyways. */
51# define TIMEOUT 20
b0b88abc
RM
52#endif
53
80a18298
UD
54#define OPT_DIRECT 1000
55#define OPT_TESTDIR 1001
56
57static struct option options[] =
58{
59#ifdef CMDLINE_OPTIONS
60 CMDLINE_OPTIONS
61#endif
62 { "direct", no_argument, NULL, OPT_DIRECT },
63 { "test-dir", required_argument, NULL, OPT_TESTDIR },
64 { NULL, 0, NULL, 0 }
65};
66
67/* PID of the test itself. */
608c5afd 68static pid_t pid;
80a18298
UD
69
70/* Directory to place temporary files in. */
71static const char *test_dir;
72
530bb2bf
PP
73static void
74oom_error (const char *fn, size_t size)
75{
76 printf ("%s: unable to allocate %zu bytes: %m\n", fn, size);
77 exit (1);
78}
79
80/* Allocate N bytes of memory dynamically, with error checking. */
9d52cb01 81__attribute__ ((unused))
530bb2bf 82static void *
530bb2bf
PP
83xmalloc (size_t n)
84{
85 void *p;
86
87 p = malloc (n);
88 if (p == NULL)
89 oom_error ("malloc", n);
90 return p;
91}
92
93/* Allocate memory for N elements of S bytes, with error checking. */
9d52cb01 94__attribute__ ((unused))
530bb2bf 95static void *
530bb2bf
PP
96xcalloc (size_t n, size_t s)
97{
98 void *p;
99
100 p = calloc (n, s);
101 if (p == NULL)
102 oom_error ("calloc", n * s);
103 return p;
104}
105
106/* Change the size of an allocated block of memory P to N bytes,
107 with error checking. */
9d52cb01 108__attribute__ ((unused))
530bb2bf 109static void *
530bb2bf
PP
110xrealloc (void *p, size_t n)
111{
64ba1731
FW
112 void *result = realloc (p, n);
113 if (result == NULL && (n > 0 || p == NULL))
530bb2bf 114 oom_error ("realloc", n);
64ba1731 115 return result;
530bb2bf
PP
116}
117
14699b6e
FW
118/* Write a message to standard output. Can be used in signal
119 handlers. */
120static void
121__attribute__ ((unused))
122write_message (const char *message)
123{
124 ssize_t unused __attribute__ ((unused));
125 unused = write (STDOUT_FILENO, message, strlen (message));
126}
127
b9337b6a 128/* List of temporary files. */
51eecc4a 129struct temp_name_list
b9337b6a
UD
130{
131 struct qelem q;
cc8dcf96 132 char *name;
51eecc4a 133} *temp_name_list;
b9337b6a
UD
134
135/* Add temporary files in list. */
9c0592ab 136static void
5cd6f8f7 137__attribute__ ((unused))
b9337b6a
UD
138add_temp_file (const char *name)
139{
51eecc4a 140 struct temp_name_list *newp
530bb2bf 141 = (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
cc8dcf96 142 char *newname = strdup (name);
530bb2bf 143 if (newname != NULL)
b9337b6a 144 {
cc8dcf96 145 newp->name = newname;
51eecc4a
AJ
146 if (temp_name_list == NULL)
147 temp_name_list = (struct temp_name_list *) &newp->q;
b9337b6a 148 else
51eecc4a 149 insque (newp, temp_name_list);
b9337b6a 150 }
cc8dcf96
FW
151 else
152 free (newp);
b9337b6a
UD
153}
154
155/* Delete all temporary files. */
9c0592ab 156static void
b9337b6a
UD
157delete_temp_files (void)
158{
51eecc4a 159 while (temp_name_list != NULL)
b9337b6a 160 {
51eecc4a 161 remove (temp_name_list->name);
cc8dcf96
FW
162 free (temp_name_list->name);
163
164 struct temp_name_list *next
165 = (struct temp_name_list *) temp_name_list->q.q_forw;
166 free (temp_name_list);
167 temp_name_list = next;
b9337b6a
UD
168 }
169}
170
cc8dcf96
FW
171/* Create a temporary file. Return the opened file descriptor on
172 success, or -1 on failure. Write the file name to *FILENAME if
173 FILENAME is not NULL. In this case, the caller is expected to free
174 *FILENAME. */
10e62564
UD
175static int
176__attribute__ ((unused))
177create_temp_file (const char *base, char **filename)
178{
179 char *fname;
180 int fd;
181
530bb2bf
PP
182 fname = (char *) xmalloc (strlen (test_dir) + 1 + strlen (base)
183 + sizeof ("XXXXXX"));
10e62564
UD
184 strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
185
186 fd = mkstemp (fname);
187 if (fd == -1)
188 {
189 printf ("cannot open temporary file '%s': %m\n", fname);
190 free (fname);
191 return -1;
192 }
193
194 add_temp_file (fname);
195 if (filename != NULL)
196 *filename = fname;
cc8dcf96
FW
197 else
198 free (fname);
10e62564
UD
199
200 return fd;
201}
202
80a18298 203/* Timeout handler. We kill the child and exit with an error. */
9c0592ab 204static void
8c0b7170 205__attribute__ ((noreturn))
85fda49b 206signal_handler (int sig __attribute__ ((unused)))
80a18298
UD
207{
208 int killed;
b79e3737 209 int status;
80a18298 210
d7a05d07
MR
211 assert (pid > 1);
212 /* Kill the whole process group. */
213 kill (-pid, SIGKILL);
214 /* In case setpgid failed in the child, kill it individually too. */
80a18298
UD
215 kill (pid, SIGKILL);
216
217 /* Wait for it to terminate. */
6d0e6e84
UD
218 int i;
219 for (i = 0; i < 5; ++i)
220 {
221 killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
222 if (killed != 0)
223 break;
224
225 /* Delay, give the system time to process the kill. If the
226 nanosleep() call return prematurely, all the better. We
227 won't restart it since this probably means the child process
228 finally died. */
33192609
UD
229 struct timespec ts;
230 ts.tv_sec = 0;
231 ts.tv_nsec = 100000000;
6d0e6e84
UD
232 nanosleep (&ts, NULL);
233 }
80a18298
UD
234 if (killed != 0 && killed != pid)
235 {
c5c13355 236 printf ("Failed to kill test process: %m\n");
80a18298
UD
237 exit (1);
238 }
239
240#ifdef CLEANUP_HANDLER
241 CLEANUP_HANDLER;
242#endif
243
85fda49b
UD
244 if (sig == SIGINT)
245 {
246 signal (sig, SIG_DFL);
247 raise (sig);
248 }
249
4614167a
UD
250 /* If we expected this signal: good! */
251#ifdef EXPECTED_SIGNAL
252 if (EXPECTED_SIGNAL == SIGALRM)
253 exit (0);
254#endif
255
6d0e6e84 256 if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
c5c13355 257 puts ("Timed out: killed the child process");
b79e3737 258 else if (WIFSTOPPED (status))
c5c13355
WN
259 printf ("Timed out: the child process was %s\n",
260 strsignal (WSTOPSIG (status)));
b79e3737 261 else if (WIFSIGNALED (status))
c5c13355
WN
262 printf ("Timed out: the child process got signal %s\n",
263 strsignal (WTERMSIG (status)));
b79e3737 264 else
c5c13355
WN
265 printf ("Timed out: killed the child process but it exited %d\n",
266 WEXITSTATUS (status));
80a18298
UD
267
268 /* Exit with an error. */
269 exit (1);
270}
271
02242448
TMQMF
272/* Avoid all the buffer overflow messages on stderr. */
273static void
274__attribute__ ((unused))
275ignore_stderr (void)
276{
277 int fd = open (_PATH_DEVNULL, O_WRONLY);
278 if (fd == -1)
279 close (STDERR_FILENO);
280 else
281 {
282 dup2 (fd, STDERR_FILENO);
283 close (fd);
284 }
285 setenv ("LIBC_FATAL_STDERR_", "1", 1);
286}
287
c5bb8e23
MF
288/* Set fortification error handler. Used when tests want to verify that bad
289 code is caught by the library. */
290static void
291__attribute__ ((unused))
292set_fortify_handler (void (*handler) (int sig))
293{
294 struct sigaction sa;
295
296 sa.sa_handler = handler;
297 sa.sa_flags = 0;
298 sigemptyset (&sa.sa_mask);
299
300 sigaction (SIGABRT, &sa, NULL);
02242448 301 ignore_stderr ();
c5bb8e23
MF
302}
303
496405af
MF
304/* Show people how to run the program. */
305static void
306usage (void)
307{
308 size_t i;
309
310 printf ("Usage: %s [options]\n"
311 "\n"
312 "Environment Variables:\n"
313 " TIMEOUTFACTOR An integer used to scale the timeout\n"
314 " TMPDIR Where to place temporary files\n"
315 "\n",
316 program_invocation_short_name);
317 printf ("Options:\n");
318 for (i = 0; options[i].name; ++i)
319 {
320 int indent;
321
322 indent = printf (" --%s", options[i].name);
323 if (options[i].has_arg == required_argument)
324 indent += printf (" <arg>");
325 printf ("%*s", 25 - indent, "");
326 switch (options[i].val)
327 {
328 case OPT_DIRECT:
329 printf ("Run the test directly (instead of forking & monitoring)");
330 break;
331 case OPT_TESTDIR:
332 printf ("Override the TMPDIR env var");
333 break;
334 }
335 printf ("\n");
336 }
337}
338
80a18298
UD
339/* We provide the entry point here. */
340int
341main (int argc, char *argv[])
342{
343 int direct = 0; /* Directly call the test function? */
344 int status;
345 int opt;
dc391246 346 unsigned int timeoutfactor = 1;
608c5afd 347 pid_t termpid;
80a18298 348
f690b569 349#ifndef TEST_NO_MALLOPT
854278df
UD
350 /* Make uses of freed and uninitialized memory known. */
351 mallopt (M_PERTURB, 42);
f690b569 352#endif
854278df 353
e7c036b3
UD
354#ifdef STDOUT_UNBUFFERED
355 setbuf (stdout, NULL);
356#endif
357
6f1acb30 358 while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
80a18298
UD
359 switch (opt)
360 {
361 case '?':
496405af 362 usage ();
80a18298
UD
363 exit (1);
364 case OPT_DIRECT:
365 direct = 1;
366 break;
367 case OPT_TESTDIR:
368 test_dir = optarg;
369 break;
370#ifdef CMDLINE_PROCESS
371 CMDLINE_PROCESS
372#endif
373 }
374
dc391246
UD
375 /* If set, read the test TIMEOUTFACTOR value from the environment.
376 This value is used to scale the default test timeout values. */
377 char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
378 if (envstr_timeoutfactor != NULL)
379 {
380 char *envstr_conv = envstr_timeoutfactor;
381 unsigned long int env_fact;
382
383 env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
384 if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
385 timeoutfactor = MAX (env_fact, 1);
386 }
387
80a18298
UD
388 /* Set TMPDIR to specified test directory. */
389 if (test_dir != NULL)
390 {
391 setenv ("TMPDIR", test_dir, 1);
392
393 if (chdir (test_dir) < 0)
394 {
c5c13355 395 printf ("chdir: %m\n");
80a18298
UD
396 exit (1);
397 }
398 }
b9337b6a
UD
399 else
400 {
401 test_dir = getenv ("TMPDIR");
402 if (test_dir == NULL || test_dir[0] == '\0')
403 test_dir = "/tmp";
404 }
80a18298 405
6b9c2e67
UD
406 /* Make sure we see all message, even those on stdout. */
407 setvbuf (stdout, NULL, _IONBF, 0);
408
b0b88abc 409 /* Make sure temporary files are deleted. */
310b3460
UD
410 atexit (delete_temp_files);
411
726b7b0f 412 /* Correct for the possible parameters. */
55033a44 413 argv[optind - 1] = argv[0];
726b7b0f
UD
414 argv += optind - 1;
415 argc -= optind - 1;
416
310b3460
UD
417 /* Call the initializing function, if one is available. */
418#ifdef PREPARE
419 PREPARE (argc, argv);
420#endif
421
b0b88abc
RM
422 const char *envstr_direct = getenv ("TEST_DIRECT");
423 if (envstr_direct != NULL)
424 {
425 FILE *f = fopen (envstr_direct, "w");
426 if (f == NULL)
427 {
428 printf ("cannot open TEST_DIRECT output file '%s': %m\n",
429 envstr_direct);
430 exit (1);
431 }
432
433 fprintf (f, "timeout=%u\ntimeoutfactor=%u\n", TIMEOUT, timeoutfactor);
434#ifdef EXPECTED_STATUS
435 fprintf (f, "exit=%u\n", EXPECTED_STATUS);
436#endif
437#ifdef EXPECTED_SIGNAL
438 switch (EXPECTED_SIGNAL)
439 {
440 default: abort ();
441# define init_sig(signo, name, text) \
442 case signo: fprintf (f, "signal=%s\n", name); break;
443# include <siglist.h>
444# undef init_sig
445 }
446#endif
447
448 if (temp_name_list != NULL)
449 {
330fadfc 450 struct temp_name_list *n;
b0b88abc 451 fprintf (f, "temp_files=(\n");
330fadfc 452 for (n = temp_name_list;
b0b88abc
RM
453 n != NULL;
454 n = (struct temp_name_list *) n->q.q_forw)
455 fprintf (f, " '%s'\n", n->name);
456 fprintf (f, ")\n");
457 }
458
459 fclose (f);
460 direct = 1;
461 }
462
80a18298
UD
463 /* If we are not expected to fork run the function immediately. */
464 if (direct)
465 return TEST_FUNCTION;
466
467 /* Set up the test environment:
468 - prevent core dumps
469 - set up the timer
470 - fork and execute the function. */
471
472 pid = fork ();
473 if (pid == 0)
474 {
475 /* This is the child. */
476#ifdef RLIMIT_CORE
477 /* Try to avoid dumping core. */
478 struct rlimit core_limit;
479 core_limit.rlim_cur = 0;
480 core_limit.rlim_max = 0;
481 setrlimit (RLIMIT_CORE, &core_limit);
482#endif
63b11dd1 483
b79e3737
RM
484 /* We put the test process in its own pgrp so that if it bogusly
485 generates any job control signals, they won't hit the whole build. */
d7a05d07
MR
486 if (setpgid (0, 0) != 0)
487 printf ("Failed to set the process group ID: %m\n");
b79e3737 488
80a18298
UD
489 /* Execute the test function and exit with the return value. */
490 exit (TEST_FUNCTION);
491 }
492 else if (pid < 0)
493 {
c5c13355 494 printf ("Cannot fork test program: %m\n");
80a18298
UD
495 exit (1);
496 }
497
498 /* Set timeout. */
85fda49b 499 signal (SIGALRM, signal_handler);
dc391246 500 alarm (TIMEOUT * timeoutfactor);
80a18298 501
85fda49b
UD
502 /* Make sure we clean up if the wrapper gets interrupted. */
503 signal (SIGINT, signal_handler);
504
80a18298 505 /* Wait for the regular termination. */
53854476 506 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
608c5afd 507 if (termpid == -1)
80a18298 508 {
608c5afd
UD
509 printf ("Waiting for test program failed: %m\n");
510 exit (1);
511 }
512 if (termpid != pid)
513 {
514 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
515 (long int) pid, (long int) termpid);
80a18298
UD
516 exit (1);
517 }
518
fffa1cf8
LH
519 /* Process terminated normaly without timeout etc. */
520 if (WIFEXITED (status))
80a18298 521 {
ede0f73a 522#ifndef EXPECTED_STATUS
fffa1cf8
LH
523# ifndef EXPECTED_SIGNAL
524 /* Simply exit with the return value of the test. */
525 return WEXITSTATUS (status);
526# else
527 printf ("Expected signal '%s' from child, got none\n",
528 strsignal (EXPECTED_SIGNAL));
529 exit (1);
530# endif
ede0f73a 531#else
fffa1cf8
LH
532 if (WEXITSTATUS (status) != EXPECTED_STATUS)
533 {
534 printf ("Expected status %d, got %d\n",
535 EXPECTED_STATUS, WEXITSTATUS (status));
536 exit (1);
537 }
538
539 return 0;
540#endif
541 }
542 /* Process was killed by timer or other signal. */
543 else
ede0f73a 544 {
fffa1cf8
LH
545#ifndef EXPECTED_SIGNAL
546 printf ("Didn't expect signal from child: got `%s'\n",
547 strsignal (WTERMSIG (status)));
ede0f73a 548 exit (1);
fffa1cf8
LH
549#else
550 if (WTERMSIG (status) != EXPECTED_SIGNAL)
551 {
552 printf ("Incorrect signal from child: got `%s', need `%s'\n",
553 strsignal (WTERMSIG (status)),
554 strsignal (EXPECTED_SIGNAL));
555 exit (1);
556 }
ede0f73a 557
fffa1cf8 558 return 0;
ede0f73a 559#endif
fffa1cf8 560 }
80a18298 561}
7e625f7e
FW
562
563/* The following functionality is only available if <pthread.h> was
564 included before this file. */
565#ifdef _PTHREAD_H
566
567/* Call pthread_sigmask with error checking. */
568static void
569xpthread_sigmask (int how, const sigset_t *set, sigset_t *oldset)
570{
571 if (pthread_sigmask (how, set, oldset) != 0)
572 {
573 write_message ("error: pthread_setmask failed\n");
574 _exit (1);
575 }
576}
577
578/* Call pthread_mutex_lock with error checking. */
579__attribute__ ((unused))
580static void
581xpthread_mutex_lock (pthread_mutex_t *mutex)
582{
583 int ret = pthread_mutex_lock (mutex);
584 if (ret != 0)
585 {
586 errno = ret;
587 printf ("error: pthread_mutex_lock: %m\n");
588 exit (1);
589 }
590}
591
592/* Call pthread_spin_lock with error checking. */
593__attribute__ ((unused))
594static void
595xpthread_spin_lock (pthread_spinlock_t *lock)
596{
597 int ret = pthread_spin_lock (lock);
598 if (ret != 0)
599 {
600 errno = ret;
601 printf ("error: pthread_spin_lock: %m\n");
602 exit (1);
603 }
604}
605
606/* Call pthread_cond_wait with error checking. */
607__attribute__ ((unused))
608static void
609xpthread_cond_wait (pthread_cond_t * cond,
610 pthread_mutex_t * mutex)
611{
612 int ret = pthread_cond_wait (cond, mutex);
613 if (ret != 0)
614 {
615 errno = ret;
616 printf ("error: pthread_cond_wait: %m\n");
617 exit (1);
618 }
619}
620
621/* Call pthread_barrier_wait with error checking. */
622__attribute__ ((unused))
623static int
624xpthread_barrier_wait (pthread_barrier_t *barrier)
625{
626 int ret = pthread_barrier_wait (barrier);
627 if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
628 {
629 errno = ret;
630 printf ("error: pthread_barrier_wait: %m\n");
631 exit (1);
632 }
633 return ret;
634}
635
636/* Call pthread_create with error checking. */
637static pthread_t
638xpthread_create (pthread_attr_t *attr,
639 void *(*thread_func) (void *), void *closure)
640{
641 pthread_t thr;
642 int ret = pthread_create (&thr, attr, thread_func, closure);
643 if (ret != 0)
644 {
645 errno = ret;
646 printf ("error: pthread_create: %m\n");
647 exit (1);
648 }
649 return thr;
650}
651
652/* Call pthread_detach with error checking. */
653static void
654xpthread_detach (pthread_t thr)
655{
656 int ret = pthread_detach (thr);
657 if (ret != 0)
658 {
659 errno = ret;
660 printf ("error: pthread_detach: %m\n");
661 exit (1);
662 }
663}
664
665/* Call pthread_join with error checking. */
666__attribute__ ((unused))
667static void *
668xpthread_join (pthread_t thr)
669{
670 void *result;
671 int ret = pthread_join (thr, &result);
672 if (ret != 0)
673 {
674 errno = ret;
675 printf ("error: pthread_join: %m\n");
676 exit (1);
677 }
678 return result;
679}
680
681/* Used to implement the delayed_exit function defined below. */
682static void *
683delayed_exit_thread (void *seconds_as_ptr)
684{
685 int seconds = (uintptr_t) seconds_as_ptr;
686 struct timespec delay = { seconds, 0 };
21e79af4 687 struct timespec remaining = { 0 };
7e625f7e
FW
688 if (nanosleep (&delay, &remaining) != 0)
689 {
690 printf ("error: nanosleep: %m\n");
691 _exit (1);
692 }
693 /* Exit the process sucessfully. */
694 exit (0);
695 return NULL;
696}
697
698/* Exit (with status 0) after SECONDS have elapsed, from a helper
699 thread. The process is terminated with the exit function, so
700 atexit handlers are executed. */
701__attribute__ ((unused))
702static void
703delayed_exit (int seconds)
704{
705 /* Create the new thread with all signals blocked. */
706 sigset_t all_blocked;
707 sigfillset (&all_blocked);
708 sigset_t old_set;
709 xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set);
710 /* Create a detached thread. */
711 pthread_t thr = xpthread_create
712 (NULL, delayed_exit_thread, (void *) (uintptr_t) seconds);
713 xpthread_detach (thr);
714 /* Restore the original signal mask. */
715 xpthread_sigmask (SIG_SETMASK, &old_set, NULL);
716}
717
718#endif /* _PTHREAD_H */