forked from seehuhn/moon-buggy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.c
197 lines (164 loc) · 3.98 KB
/
signal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* signal.c - signal handling
*
* Copyright 1999, 2000, 2001 Jochen Voss. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include "moon-buggy.h"
#include "darray.h"
struct sig_info {
int signum;
volatile sig_atomic_t pending;
void (*handler) (int);
};
static struct {
struct sig_info *data;
int slots, used;
} sig_info_table;
static volatile sig_atomic_t signal_arrived;
static sigset_t full_set, old_sigset;
void
block_all (void)
/* Block all signals until `unblock' is called. */
{
sigprocmask (SIG_BLOCK, &full_set, &old_sigset);
}
void
unblock (void)
/* Undo the effect of `block_all'. */
{
sigprocmask (SIG_SETMASK, &old_sigset, NULL);
}
static void
install_signal (int signum, RETSIGTYPE (*handler) ())
/* Emulate the `signal' function via `sigaction'. */
{
struct sigaction action;
int ret;
action.sa_handler = handler;
sigemptyset (&action.sa_mask);
action.sa_flags = 0;
ret = sigaction (signum, &action, NULL);
assert (ret == 0);
}
static RETSIGTYPE
generic_handler (int signum)
/* Interrupt handlers shouldn't do much. So we just note that the
* signal arrived. */
{
int i;
signal_arrived = 1;
for (i=0; i<sig_info_table.used; ++i) {
if (sig_info_table.data[i].signum == signum) break;
}
assert (i<sig_info_table.used);
sig_info_table.data[i].pending = 1;
if (signum == SIGINT || signum == SIGHUP || signum == SIGTERM) {
/* Pressing `C-c' twice exits, even if the program hangs. */
install_signal (signum, SIG_DFL);
} else {
install_signal (signum, generic_handler);
}
}
static void
my_signal (int signum, void (*handler)(int), int ignore_test)
/* Install HANDLER as a new signal handler for signal SIGNUM. But if
* IGNORE_TEST is true and SIGNUM was ignored before, do nothing. */
{
struct sig_info *info;
if (ignore_test) {
struct sigaction action;
sigaction (signum, NULL, &action);
if (action.sa_handler == SIG_IGN) return;
}
DA_ADD_EMPTY (sig_info_table, struct sig_info, info);
info->signum = signum;
info->pending = 0;
info->handler = handler;
install_signal (signum, generic_handler);
}
/************************************************************
* signal handlers
*/
static void
termination_handler (int signum)
{
prepare_for_exit ();
fprintf (stderr, "GAME ABORTED (signal %d)\n", signum);
/* We did `signal (signum, SIG_DFL)' in `generic_handler'. */
raise (signum);
}
static void
tstp_handler (int signum)
{
clock_freeze ();
mode_signal (signum);
prepare_for_exit ();
install_signal (SIGTSTP, SIG_DFL);
raise (SIGTSTP);
}
static void
cont_handler (int signum)
{
install_signal (SIGTSTP, tstp_handler);
refresh ();
prepare_screen ();
mode_redraw ();
mode_signal (signum);
clock_thaw ();
}
static void
winch_handler (int signum)
{
clock_freeze ();
delwin (moon);
delwin (status);
delwin (message);
sleep (1);
endwin ();
refresh ();
allocate_windows ();
hide_cursor ();
mode_redraw ();
clock_thaw ();
}
/************************************************************
* The outside-visible entry point
*/
void
initialise_signals (void)
{
DA_INIT (sig_info_table, struct sig_info);
my_signal (SIGINT, termination_handler, 1);
my_signal (SIGHUP, termination_handler, 1);
my_signal (SIGTERM, termination_handler, 1);
my_signal (SIGCONT, cont_handler, 0);
my_signal (SIGTSTP, tstp_handler, 0);
#ifdef SIGWINCH
my_signal (SIGWINCH, winch_handler, 0);
#endif
sigfillset (&full_set);
}
int
handle_signals (void)
/* Execute signal actions, for all signals, which occured before.
* Return 1, if any action was taken. */
{
int res = 0;
while (signal_arrived) {
int i;
signal_arrived = 0;
res = 1;
for (i=0; i<sig_info_table.used; ++i) {
if (sig_info_table.data[i].pending) {
sig_info_table.data[i].pending = 0;
sig_info_table.data[i].handler (sig_info_table.data[i].signum);
}
}
}
return res;
}