tests: add signature checks
[gnulib.git] / tests / test-sigaction.c
1 /* Test of sigaction() function.
2    Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2008.  */
18
19 #include <config.h>
20
21 #include <signal.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (sigaction, int, (int, struct sigaction const *,
25                                   struct sigaction *));
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           signal (SIGABRT, SIG_DFL);                                         \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 #ifndef SA_NOCLDSTOP
44 # define SA_NOCLDSTOP 0
45 #endif
46 #ifndef SA_ONSTACK
47 # define SA_ONSTACK 0
48 #endif
49 #ifndef SA_SIGINFO
50 # define SA_SIGINFO 0
51 #endif
52 #ifndef SA_NOCLDWAIT
53 # define SA_NOCLDWAIT 0
54 #endif
55
56 /* Define a mask of flags required by POSIX.  Some implementations
57    provide other flags as extensions, such as SA_RESTORER, that we
58    must ignore in this test.  */
59 #define MASK_SA_FLAGS (SA_NOCLDSTOP | SA_ONSTACK | SA_RESETHAND | SA_RESTART \
60                        | SA_SIGINFO | SA_NOCLDWAIT | SA_NODEFER)
61
62 /* This test is unsafe in the presence of an asynchronous SIGABRT,
63    because we install a signal-handler that is intentionally not
64    async-safe.  Hopefully, this does not lead to too many reports of
65    false failures, since people don't generally use 'kill -s SIGABRT'
66    to end a runaway program.  */
67
68 static void
69 handler (int sig)
70 {
71   static int entry_count;
72   struct sigaction sa;
73   ASSERT (sig == SIGABRT);
74   ASSERT (sigaction (SIGABRT, NULL, &sa) == 0);
75   ASSERT ((sa.sa_flags & SA_SIGINFO) == 0);
76   switch (entry_count++)
77     {
78     case 0:
79       ASSERT ((sa.sa_flags & SA_RESETHAND) == 0);
80       ASSERT (sa.sa_handler == handler);
81       break;
82     case 1:
83       /* This assertion fails on glibc-2.3.6 systems with LinuxThreads,
84          when this program is linked with -lpthread, due to the sigaction()
85          override in libpthread.so.  */
86 #if !defined __GLIBC__
87       ASSERT (sa.sa_handler == SIG_DFL);
88 #endif
89       break;
90     default:
91       ASSERT (0);
92     }
93 }
94
95 int
96 main (void)
97 {
98   struct sigaction sa;
99   struct sigaction old_sa;
100   sa.sa_handler = handler;
101
102   sa.sa_flags = 0;
103   ASSERT (sigemptyset (&sa.sa_mask) == 0);
104   ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
105   ASSERT (raise (SIGABRT) == 0);
106
107   sa.sa_flags = SA_RESETHAND | SA_NODEFER;
108   ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
109   ASSERT ((old_sa.sa_flags & MASK_SA_FLAGS) == 0);
110   ASSERT (old_sa.sa_handler == handler);
111   ASSERT (raise (SIGABRT) == 0);
112
113   sa.sa_handler = SIG_DFL;
114   ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
115   ASSERT ((old_sa.sa_flags & SA_SIGINFO) == 0);
116 #if !defined __GLIBC__ /* see above */
117   ASSERT (old_sa.sa_handler == SIG_DFL);
118 #endif
119
120   sa.sa_handler = SIG_IGN;
121   ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
122   ASSERT (raise (SIGABRT) == 0);
123   ASSERT (sigaction (SIGABRT, NULL, &old_sa) == 0);
124   ASSERT (old_sa.sa_handler == SIG_IGN);
125   ASSERT (raise (SIGABRT) == 0);
126
127   return 0;
128 }