-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap.c
297 lines (240 loc) · 4.52 KB
/
trap.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
#include <signal.h>
#include "shellio.h"
#include "sh.h"
/*
* Interrupt flags.
*/
static int intflag = 0;
static int realint = 0;
static int norecur = 0;
/*
* Trap storage area.
*/
typedef struct {
int t_cnt; /* Signal received */
void (*t_set)(); /* Current action */
void (*t_def)(); /* Default action */
char *t_act; /* Action to take */
} TRAP;
TRAP trap [NSIG + 1];
/*
* Return a character-string name for a signal.
*/
char *
signame (signo)
int signo;
{
switch (signo) {
/*
* Construct the case: labels from a general system.
*/
#define SIGNAL(name,short,desc) case name: return desc;
#include "signal.tab"
#undef SIGNAL
default:
return NULL;
}
}
void
deftrp (sig, def)
int sig;
int (*def)();
{
sigset (sig, def);
trap [sig].t_cnt = 0;
trap [sig].t_set = def;
trap [sig].t_def = def;
if (trap [sig].t_act != NULL) {
sfree (trap [sig].t_act);
trap [sig].t_act = NULL;
}
}
/*
* Set default traps for various contexts.
*/
void
dflttrp (context)
int context;
{
int sig;
switch (context) {
case IRDY: /* Trap initialisation */
for (sig = 0; sig <= NSIG; sig += 1)
deftrp (sig, sigset (sig, SIG_IGN));
if (iflag) {
sigset (SIGINT, sigintr);
sigset (SIGTERM, SIG_IGN);
}
sigset (SIGQUIT, SIG_IGN);
break;
case IBACK: /* Background initialisation */
deftrp (SIGINT, SIG_IGN);
deftrp (SIGQUIT, SIG_IGN);
break;
case IDONE: /* Done wait () */
sigset (SIGINT, iflag ? sigintr : trap [SIGINT].t_set);
break;
case IBUILTIN: /* Builtin command initialisation */
case IWAITINT: /* Parent, waiting interactively */
if (iflag)
sigset (SIGINT, sigintr);
sigset (SIGQUIT, SIG_IGN);
break;
case IPARENT: /* parent, before fork () */
case IWAIT: /* Parent, waiting for child */
sigset (SIGINT, SIG_HOLD);
sigset (SIGQUIT, SIG_IGN);
break;
case IFORK: /* Sub-shell initialization */
sigset (SIGINT, trap [SIGINT].t_set);
sigset (SIGTERM, trap [SIGTERM].t_set);
break;
case ICMD: /* Command execution initialisation */
sigset (SIGQUIT, trap [SIGQUIT].t_set);
break;
case IPIPE: /* In child about to take LHS of | command */
break;
}
}
/*
* Show the contents of traps.
*/
telltrp ()
{
int sig;
void (*act)();
char *what;
if ((what = trap [0].t_act) != NULL) {
cmd_outstr ("0 = exit : ");
cmd_outstr (what);
cmd_endl ();
}
for (sig = 1; sig <= NSIG ; sig += 1) {
act = trap [sig].t_set;
if (act == SIG_IGN)
what = "ignored";
else if (act == sigintr)
what = trap [sig].t_act;
else
what = NULL;
if (what) {
cmd_outint (sig);
cmd_outstr (" = ");
if (signame (sig) != NULL) {
cmd_outstr (signame (sig));
cmd_outstr (" : ");
}
cmd_outstr (what);
cmd_endl ();
}
}
return 0;
}
/*
* Set a trap.
*/
int
setstrp (sig, actp)
int sig;
char * actp;
{
int spc;
if (sig < 0 || sig > NSIG) {
if (cmderr_begin ("trap")) {
cmderr_outstr ("trap number out of range: ");
cmderr_outint (sig);
cmderr_endl ();
}
return 1;
}
spc = (iflag && (sig == SIGINT || sig == SIGTERM)) || sig == SIGQUIT;
if (trap [sig].t_act != NULL)
sfree (trap [sig].t_act);
trap [sig].t_act = NULL;
if (actp == NULL) {
if (! spc)
sigset (sig, trap [sig].t_def);
trap [sig].t_set = trap [sig].t_def;
} else if (* actp == 0) {
if (! spc)
sigset (sig, SIG_IGN);
trap [sig].t_set = SIG_IGN;
} else if (trap [sig].t_def == SIG_IGN) {
;
} else {
trap [sig].t_act = duplstr (actp, 1);
if (! spc)
sigset (sig, sigintr);
trap [sig].t_set = sigintr;
}
return 0;
}
/*
* Execute a trap action.
*/
/*
* Interrupt routine.
*/
void
sigintr (sig)
unsigned int sig;
{
intflag += 1;
if (sig <= NSIG)
trap [sig].t_cnt += 1;
if (sig == SIGINT) {
noeflag += 1;
realint += 1;
}
}
/*
* Recover from an interrupt.
*/
int
recover (context)
{
int sig;
char * actp;
if (! intflag || norecur)
return 1;
norecur ++;
switch (context) {
case IPROF:
exit (-1);
case IRDY:
case ICMD:
break;
case ILEX:
case IEVAL:
norecur = 0;
return ! (realint && iflag);
}
for (sig = 0; sig <= NSIG; sig += 1) {
if (trap [sig].t_cnt) {
trap [sig].t_cnt = 0;
if ((actp = trap [sig].t_act) != NULL) {
/*
* NIGEL: Take steps to preserve the exit code.
*/
int oslret = slret;
#if 0
trap [sig].t_act = NULL;
session (SARGS, actp);
trap [sig].t_act = actp;
setstrp (sig, NULL);
#else
session (SARGS, actp);
slret = oslret;
#endif
}
}
}
intflag = 0;
norecur = 0;
if (realint) {
noeflag = 0;
realint = 0;
reset (iflag ? RINT : RINTDIE);
}
return 1;
}