New module sigaction, for mingw.
[pspp] / tests / test-sigaction.c
1 /* Test of sigaction() function.
2    Copyright (C) 2008 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_SIGINFO
40 # define SA_SIGINFO 0
41 #endif
42
43 /* This test is unsafe in the presence of an asynchronous SIGABRT,
44    because we install a signal-handler that is intentionally not
45    async-safe.  Hopefully, this does not lead to too many reports of
46    false failures, since people don't generally use 'kill -s SIGABRT'
47    to end a runaway program.  */
48
49 static void
50 handler (int sig)
51 {
52   static int entry_count;
53   struct sigaction sa;
54   ASSERT (sig == SIGABRT);
55   ASSERT (sigaction (SIGABRT, NULL, &sa) == 0);
56   ASSERT ((sa.sa_flags & SA_SIGINFO) == 0);
57   switch (entry_count++)
58     {
59     case 0:
60       ASSERT ((sa.sa_flags & SA_RESETHAND) == 0);
61       ASSERT (sa.sa_handler == handler);
62       break;
63     case 1:
64       ASSERT (sa.sa_handler == SIG_DFL);
65       break;
66     default:
67       ASSERT (0);
68     }
69 }
70
71 int
72 main (int argc, char *argv[])
73 {
74   struct sigaction sa;
75   struct sigaction old_sa;
76   sa.sa_handler = handler;
77   sa.sa_flags = 0;
78   ASSERT (sigemptyset (&sa.sa_mask) == 0);
79   ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
80   ASSERT (raise (SIGABRT) == 0);
81   sa.sa_flags = SA_RESETHAND | SA_NODEFER;
82   ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
83   ASSERT (old_sa.sa_flags == 0);
84   ASSERT (old_sa.sa_handler == handler);
85   ASSERT (raise (SIGABRT) == 0);
86   sa.sa_handler = SIG_DFL;
87   ASSERT (sigaction (SIGABRT, &sa, &old_sa) == 0);
88   ASSERT ((old_sa.sa_flags & SA_SIGINFO) == 0);
89   ASSERT (old_sa.sa_handler == SIG_DFL);
90   sa.sa_handler = SIG_IGN;
91   ASSERT (sigaction (SIGABRT, &sa, NULL) == 0);
92   ASSERT (raise (SIGABRT) == 0);
93   ASSERT (sigaction (SIGABRT, NULL, &old_sa) == 0);
94   ASSERT (old_sa.sa_handler == SIG_IGN);
95   ASSERT (raise (SIGABRT) == 0);
96   return 0;
97 }