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