Use spaces for indentation, not tabs.
[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 <stdio.h>
24 #include <stdlib.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           fflush (stderr);                                                   \
33           signal (SIGABRT, SIG_DFL);                                         \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 #ifndef SA_NOCLDSTOP
40 # define SA_NOCLDSTOP 0
41 #endif
42 #ifndef SA_ONSTACK
43 # define SA_ONSTACK 0
44 #endif
45 #ifndef SA_SIGINFO
46 # define SA_SIGINFO 0
47 #endif
48 #ifndef SA_NOCLDWAIT
49 # define SA_NOCLDWAIT 0
50 #endif
51
52 /* Define a mask of flags required by POSIX.  Some implementations
53    provide other flags as extensions, such as SA_RESTORER, that we
54    must ignore in this test.  */
55 #define MASK_SA_FLAGS (SA_NOCLDSTOP | SA_ONSTACK | SA_RESETHAND | SA_RESTART \
56                        | SA_SIGINFO | SA_NOCLDWAIT | SA_NODEFER)
57
58 /* This test is unsafe in the presence of an asynchronous SIGABRT,
59    because we install a signal-handler that is intentionally not
60    async-safe.  Hopefully, this does not lead to too many reports of
61    false failures, since people don't generally use 'kill -s SIGABRT'
62    to end a runaway program.  */
63
64 static void
65 handler (int sig)
66 {
67   static int entry_count;
68   struct sigaction sa;
69   ASSERT (sig == SIGABRT);
70   ASSERT (sigaction (SIGABRT, NULL, &sa) == 0);
71   ASSERT ((sa.sa_flags & SA_SIGINFO) == 0);
72   switch (entry_count++)
73     {
74     case 0:
75       ASSERT ((sa.sa_flags & SA_RESETHAND) == 0);
76       ASSERT (sa.sa_handler == handler);
77       break;
78     case 1:
79       /* This assertion fails on glibc-2.3.6 systems with LinuxThreads,
80          when this program is linked with -lpthread, due to the sigaction()
81          override in libpthread.so.  */
82 #if !defined __GLIBC__
83       ASSERT (sa.sa_handler == SIG_DFL);
84 #endif
85       break;
86     default:
87       ASSERT (0);
88     }
89 }
90
91 int
92 main (void)
93 {
94   struct sigaction sa;
95   struct sigaction old_sa;
96   sa.sa_handler = handler;
97
98   sa.sa_flags = 0;
99   ASSERT (sigemptyset (&sa.sa_mask) == 0);
100   ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
101   ASSERT (raise (SIGABRT) == 0);
102
103   sa.sa_flags = SA_RESETHAND | SA_NODEFER;
104   ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
105   ASSERT ((old_sa.sa_flags & MASK_SA_FLAGS) == 0);
106   ASSERT (old_sa.sa_handler == handler);
107   ASSERT (raise (SIGABRT) == 0);
108
109   sa.sa_handler = SIG_DFL;
110   ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
111   ASSERT ((old_sa.sa_flags & SA_SIGINFO) == 0);
112 #if !defined __GLIBC__ /* see above */
113   ASSERT (old_sa.sa_handler == SIG_DFL);
114 #endif
115
116   sa.sa_handler = SIG_IGN;
117   ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
118   ASSERT (raise (SIGABRT) == 0);
119   ASSERT (sigaction (SIGABRT, NULL, &old_sa) == 0);
120   ASSERT (old_sa.sa_handler == SIG_IGN);
121   ASSERT (raise (SIGABRT) == 0);
122
123   return 0;
124 }