forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.c
320 lines (280 loc) · 6.58 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* signal.c -- signal handling ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "sigmsgs.h"
typedef void (*Sighandler)(int);
Boolean sigint_newline = TRUE;
jmp_buf slowlabel;
Atomic slow = FALSE;
Atomic interrupted = FALSE;
static Atomic sigcount;
static Atomic caught[NSIG];
static Sigeffect sigeffect[NSIG];
#if HAVE_SIGACTION
#ifndef SA_NOCLDSTOP
#define SA_NOCLDSTOP 0
#endif
#ifndef SA_NOCLDWAIT
#define SA_NOCLDWAIT 0
#endif
#ifndef SA_INTERRUPT /* for sunos */
#define SA_INTERRUPT 0
#endif
#endif
/*
* name<->signal mappings
*/
extern int signumber(const char *name) {
int i;
char *suffix;
if (!hasprefix(name, "sig"))
return -1;
for (i = 0; i < nsignals; i++)
if (streq(signals[i].name, name))
return signals[i].sig;
i = strtol(name + 3, &suffix, 10);
if (0 < i && i < NSIG && (suffix == NULL || *suffix == '\0'))
return i;
return -1;
}
extern char *signame(int sig) {
int i;
for (i = 0; i < nsignals; i++)
if (signals[i].sig == sig)
return (char *) signals[i].name;
return str("sig%d", sig);
}
extern char *sigmessage(int sig) {
int i;
for (i = 0; i < nsignals; i++)
if (signals[i].sig == sig)
return (char *) signals[i].msg;
return str("unknown signal %d", sig);
}
/*
* the signal handler
*/
/* catcher -- catch (and defer) a signal from the kernel */
static void catcher(int sig) {
#if SYSV_SIGNALS /* only do this for unreliable signals */
signal(sig, catcher);
#endif
if (hasforked)
/* exit unconditionally on a signal in a child process */
exit(1);
if (caught[sig] == 0) {
caught[sig] = TRUE;
++sigcount;
}
interrupted = TRUE;
if (slow)
longjmp(slowlabel, 1);
}
/*
* setting and getting signal effects
*/
static Sighandler setsignal(int sig, Sighandler handler) {
#if HAVE_SIGACTION
struct sigaction nsa, osa;
sigemptyset(&nsa.sa_mask);
nsa.sa_handler = handler;
nsa.sa_flags = SA_INTERRUPT;
if (sigaction(sig, &nsa, &osa) == -1)
return SIG_ERR;
return osa.sa_handler;
#else /* !HAVE_SIGACTION */
#ifdef SIGCLD
if (sig == SIGCLD && handler != SIG_DFL)
return SIG_ERR;
#endif
return signal(sig, handler);
#endif /* !HAVE_SIGACTION */
}
extern Sigeffect esignal(int sig, Sigeffect effect) {
Sigeffect old;
assert(0 < sig && sig <= NSIG);
old = sigeffect[sig];
if (effect != sig_nochange && effect != old) {
switch (effect) {
case sig_ignore:
if (setsignal(sig, SIG_IGN) == SIG_ERR) {
eprint("$&setsignals: cannot ignore %s\n", signame(sig));
return old;
}
break;
case sig_special:
if (sig != SIGINT) {
eprint("$&setsignals: special handler not defined for %s\n", signame(sig));
return old;
}
FALLTHROUGH;
case sig_catch:
case sig_noop:
if (setsignal(sig, catcher) == SIG_ERR) {
eprint("$&setsignals: cannot catch %s\n", signame(sig));
return old;
}
break;
case sig_default:
setsignal(sig, SIG_DFL);
break;
default:
NOTREACHED;
}
sigeffect[sig] = effect;
}
return old;
}
extern void setsigeffects(const Sigeffect effects[]) {
int sig;
for (sig = 1; sig < NSIG; sig++)
esignal(sig, effects[sig]);
}
extern void getsigeffects(Sigeffect effects[]) {
memcpy(effects, sigeffect, sizeof sigeffect);
}
/*
* initialization
*/
extern void initsignals(Boolean interactive, Boolean allowdumps) {
int sig;
Push settor;
for (sig = 0; sig < nsignals; sig++)
if (signals[sig].sig < 1 || NSIG <= signals[sig].sig)
panic(
"initsignals: bad signal in sigmsgs.c: %s (see mksignal)",
signals[sig].name
);
for (sig = 1; sig < NSIG; sig++) {
Sighandler h;
#if HAVE_SIGACTION
struct sigaction sa;
sigaction(sig, NULL, &sa);
h = sa.sa_handler;
if (h == SIG_IGN)
sigeffect[sig] = sig_ignore;
#else /* !HAVE_SIGACTION */
h = signal(sig, SIG_DFL);
if (h == SIG_IGN) {
setsignal(sig, SIG_IGN);
sigeffect[sig] = sig_ignore;
}
#endif /* !HAVE_SIGACTION */
else if (h == SIG_DFL || h == SIG_ERR)
sigeffect[sig] = sig_default;
else
panic(
"initsignals: bad incoming signal value for %s: %x",
signame(sig), h
);
}
if (interactive || sigeffect[SIGINT] == sig_default)
esignal(SIGINT, sig_special);
if (!allowdumps) {
if (interactive)
esignal(SIGTERM, sig_noop);
if (interactive || sigeffect[SIGQUIT] == sig_default)
esignal(SIGQUIT, sig_noop);
}
/* here's the end-run around set-signals */
varpush(&settor, "set-signals", NULL);
vardef("signals", NULL, mksiglist());
varpop(&settor);
}
extern void setsigdefaults(void) {
int sig;
for (sig = 1; sig < NSIG; sig++) {
Sigeffect e = sigeffect[sig];
if (e == sig_catch || e == sig_noop || e == sig_special)
esignal(sig, sig_default);
}
}
/*
* utility functions
*/
extern Boolean issilentsignal(List *e) {
return (termeq(e->term, "signal"))
&& e->next != NULL
&& termeq(e->next->term, "sigint");
}
extern List *mksiglist(void) {
int sig = NSIG;
Sigeffect effects[NSIG];
getsigeffects(effects);
Ref(List *, lp, NULL);
while (--sig > 0) {
int prefix;
switch (effects[sig]) {
default: panic("mksiglist: bad sigeffects for %s: %d", signame(sig), effects[sig]);
case sig_default: continue;
case sig_catch: prefix = '\0'; break;
case sig_ignore: prefix = '-'; break;
case sig_noop: prefix = '/'; break;
case sig_special: prefix = '.'; break;
}
Ref(char *, name, signame(sig));
if (prefix != '\0')
name = str("%c%s", prefix, name);
Ref(Term *, term, mkstr(name));
lp = mklist(term, lp);
RefEnd2(term, name);
}
RefReturn(lp);
}
/*
* signal delivery
*/
static int blocked = 0;
/* blocksignals -- turn off delivery of signals as exceptions */
extern void blocksignals(void) {
assert(blocked >= 0);
++blocked;
}
/* unblocksignals -- turn on delivery of signals as exceptions */
extern void unblocksignals(void) {
assert(blocked > 0);
--blocked;
}
/* sigchk -- throw the signal as an exception */
extern void sigchk(void) {
int sig;
if (sigcount == 0 || blocked)
return;
if (hasforked)
/* exit unconditionally on a signal in a child process */
exit(1);
for (sig = 0;; sig++) {
if (caught[sig] != 0) {
--sigcount;
caught[sig] = 0;
break;
}
if (sig >= NSIG) {
sigcount = 0;
return;
}
}
resetparser();
Ref(List *, e,
mklist(mkstr("signal"), mklist(mkstr(signame(sig)), NULL)));
switch (sigeffect[sig]) {
case sig_catch:
while (gcisblocked())
gcenable();
throw(e);
case sig_special:
assert(sig == SIGINT);
/* this is the newline you see when you hit ^C while typing a command */
if (sigint_newline)
eprint("\n");
sigint_newline = TRUE;
while (gcisblocked())
gcenable();
throw(e);
case sig_noop:
break;
default:
/* panic("sigchk: caught %L with sigeffect %d", e, " ", sigeffect[sig]); */
break;
}
RefEnd(e);
}