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