trap.c revision 1.4 1 1.4 wiz /* $NetBSD: trap.c,v 1.4 2001/09/16 16:34:23 wiz Exp $ */
2 1.2 tls
3 1.1 jtc /*
4 1.1 jtc * signal handling
5 1.1 jtc */
6 1.1 jtc
7 1.1 jtc /* Kludge to avoid bogus re-declaration of sigtraps[] error on AIX 3.2.5 */
8 1.1 jtc #define FROM_TRAP_C
9 1.1 jtc #include "sh.h"
10 1.1 jtc
11 1.1 jtc /* Table is indexed by signal number
12 1.1 jtc *
13 1.1 jtc * The script siglist.sh generates siglist.out, which is a sorted, complete
14 1.1 jtc * list of signals
15 1.1 jtc */
16 1.1 jtc Trap sigtraps[SIGNALS+1] = {
17 1.1 jtc { SIGEXIT_, "EXIT", "Signal 0" },
18 1.1 jtc #include "siglist.out" /* generated by siglist.sh */
19 1.1 jtc { SIGERR_, "ERR", "Error handler" },
20 1.1 jtc };
21 1.1 jtc
22 1.1 jtc static struct sigaction Sigact_ign, Sigact_trap;
23 1.1 jtc
24 1.1 jtc void
25 1.1 jtc inittraps()
26 1.1 jtc {
27 1.1 jtc #ifdef HAVE_SYS_SIGLIST
28 1.1 jtc # ifndef SYS_SIGLIST_DECLARED
29 1.1 jtc extern char *sys_siglist[];
30 1.1 jtc # endif
31 1.1 jtc int i;
32 1.1 jtc
33 1.1 jtc /* Use system description, if available, for unknown signals... */
34 1.1 jtc for (i = 0; i < NSIG; i++)
35 1.3 hubertf if (!sigtraps[i].name && sys_siglist[i] && sys_siglist[i][0])
36 1.1 jtc sigtraps[i].mess = sys_siglist[i];
37 1.1 jtc #endif /* HAVE_SYS_SIGLIST */
38 1.1 jtc
39 1.1 jtc sigemptyset(&Sigact_ign.sa_mask);
40 1.1 jtc Sigact_ign.sa_flags = KSH_SA_FLAGS;
41 1.1 jtc Sigact_ign.sa_handler = SIG_IGN;
42 1.1 jtc Sigact_trap = Sigact_ign;
43 1.1 jtc Sigact_trap.sa_handler = trapsig;
44 1.1 jtc
45 1.1 jtc sigtraps[SIGINT].flags |= TF_DFL_INTR | TF_TTY_INTR;
46 1.1 jtc sigtraps[SIGQUIT].flags |= TF_DFL_INTR | TF_TTY_INTR;
47 1.1 jtc sigtraps[SIGTERM].flags |= TF_DFL_INTR;/* not fatal for interactive */
48 1.1 jtc sigtraps[SIGHUP].flags |= TF_FATAL;
49 1.1 jtc sigtraps[SIGCHLD].flags |= TF_SHELL_USES;
50 1.1 jtc
51 1.1 jtc /* these are always caught so we can clean up any temproary files. */
52 1.1 jtc setsig(&sigtraps[SIGINT], trapsig, SS_RESTORE_ORIG);
53 1.1 jtc setsig(&sigtraps[SIGQUIT], trapsig, SS_RESTORE_ORIG);
54 1.1 jtc setsig(&sigtraps[SIGTERM], trapsig, SS_RESTORE_ORIG);
55 1.1 jtc setsig(&sigtraps[SIGHUP], trapsig, SS_RESTORE_ORIG);
56 1.1 jtc }
57 1.1 jtc
58 1.1 jtc #ifdef KSH
59 1.1 jtc static RETSIGTYPE alarm_catcher ARGS((int sig));
60 1.1 jtc
61 1.1 jtc void
62 1.1 jtc alarm_init()
63 1.1 jtc {
64 1.1 jtc sigtraps[SIGALRM].flags |= TF_SHELL_USES;
65 1.1 jtc setsig(&sigtraps[SIGALRM], alarm_catcher,
66 1.1 jtc SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
67 1.1 jtc }
68 1.1 jtc
69 1.1 jtc static RETSIGTYPE
70 1.1 jtc alarm_catcher(sig)
71 1.1 jtc int sig;
72 1.1 jtc {
73 1.1 jtc if (ksh_tmout_state == TMOUT_READING) {
74 1.1 jtc int left = alarm(0);
75 1.1 jtc
76 1.1 jtc if (left == 0) {
77 1.1 jtc ksh_tmout_state = TMOUT_LEAVING;
78 1.1 jtc intrsig = 1;
79 1.1 jtc } else
80 1.1 jtc alarm(left);
81 1.1 jtc }
82 1.1 jtc return RETSIGVAL;
83 1.1 jtc }
84 1.1 jtc #endif /* KSH */
85 1.1 jtc
86 1.1 jtc Trap *
87 1.3 hubertf gettrap(name, igncase)
88 1.1 jtc const char *name;
89 1.3 hubertf int igncase;
90 1.1 jtc {
91 1.1 jtc int i;
92 1.1 jtc register Trap *p;
93 1.1 jtc
94 1.1 jtc if (digit(*name)) {
95 1.1 jtc int n;
96 1.1 jtc
97 1.1 jtc if (getn(name, &n) && 0 <= n && n < SIGNALS)
98 1.1 jtc return &sigtraps[n];
99 1.1 jtc return NULL;
100 1.1 jtc }
101 1.1 jtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++)
102 1.3 hubertf if (p->name && (igncase ? strcasecmp(p->name, name) == 0
103 1.3 hubertf : strcmp(p->name, name) == 0))
104 1.1 jtc return p;
105 1.1 jtc return NULL;
106 1.1 jtc }
107 1.1 jtc
108 1.1 jtc /*
109 1.1 jtc * trap signal handler
110 1.1 jtc */
111 1.1 jtc RETSIGTYPE
112 1.1 jtc trapsig(i)
113 1.1 jtc int i;
114 1.1 jtc {
115 1.1 jtc Trap *p = &sigtraps[i];
116 1.1 jtc
117 1.1 jtc trap = p->set = 1;
118 1.1 jtc if (p->flags & TF_DFL_INTR)
119 1.1 jtc intrsig = 1;
120 1.1 jtc if ((p->flags & TF_FATAL) && !p->trap) {
121 1.1 jtc fatal_trap = 1;
122 1.1 jtc intrsig = 1;
123 1.1 jtc }
124 1.1 jtc if (p->shtrap)
125 1.1 jtc (*p->shtrap)(i);
126 1.1 jtc #ifdef V7_SIGNALS
127 1.1 jtc if (sigtraps[i].cursig == trapsig) /* this for SIGCHLD,SIGALRM */
128 1.1 jtc sigaction(i, &Sigact_trap, (struct sigaction *) 0);
129 1.1 jtc #endif /* V7_SIGNALS */
130 1.1 jtc return RETSIGVAL;
131 1.1 jtc }
132 1.1 jtc
133 1.1 jtc /* called when we want to allow the user to ^C out of something - won't
134 1.1 jtc * work if user has trapped SIGINT.
135 1.1 jtc */
136 1.1 jtc void
137 1.1 jtc intrcheck()
138 1.1 jtc {
139 1.1 jtc if (intrsig)
140 1.1 jtc runtraps(TF_DFL_INTR|TF_FATAL);
141 1.1 jtc }
142 1.1 jtc
143 1.1 jtc /* called after EINTR to check if a signal with normally causes process
144 1.1 jtc * termination has been received.
145 1.1 jtc */
146 1.1 jtc int
147 1.1 jtc fatal_trap_check()
148 1.1 jtc {
149 1.1 jtc int i;
150 1.1 jtc Trap *p;
151 1.1 jtc
152 1.1 jtc /* todo: should check if signal is fatal, not the TF_DFL_INTR flag */
153 1.1 jtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++)
154 1.1 jtc if (p->set && (p->flags & (TF_DFL_INTR|TF_FATAL)))
155 1.1 jtc /* return value is used as an exit code */
156 1.1 jtc return 128 + p->signal;
157 1.1 jtc return 0;
158 1.1 jtc }
159 1.1 jtc
160 1.1 jtc /* Returns the signal number of any pending traps: ie, a signal which has
161 1.4 wiz * occurred for which a trap has been set or for which the TF_DFL_INTR flag
162 1.1 jtc * is set.
163 1.1 jtc */
164 1.1 jtc int
165 1.1 jtc trap_pending()
166 1.1 jtc {
167 1.1 jtc int i;
168 1.1 jtc Trap *p;
169 1.1 jtc
170 1.1 jtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++)
171 1.1 jtc if (p->set && ((p->trap && p->trap[0])
172 1.1 jtc || ((p->flags & (TF_DFL_INTR|TF_FATAL))
173 1.1 jtc && !p->trap)))
174 1.1 jtc return p->signal;
175 1.1 jtc return 0;
176 1.1 jtc }
177 1.1 jtc
178 1.1 jtc /*
179 1.1 jtc * run any pending traps. If intr is set, only run traps that
180 1.1 jtc * can interrupt commands.
181 1.1 jtc */
182 1.1 jtc void
183 1.1 jtc runtraps(flag)
184 1.1 jtc int flag;
185 1.1 jtc {
186 1.1 jtc int i;
187 1.1 jtc register Trap *p;
188 1.1 jtc
189 1.1 jtc #ifdef KSH
190 1.1 jtc if (ksh_tmout_state == TMOUT_LEAVING) {
191 1.1 jtc ksh_tmout_state = TMOUT_EXECUTING;
192 1.1 jtc warningf(FALSE, "timed out waiting for input");
193 1.1 jtc unwind(LEXIT);
194 1.1 jtc } else
195 1.1 jtc /* XXX: this means the alarm will have no effect if a trap
196 1.1 jtc * is caught after the alarm() was started...not good.
197 1.1 jtc */
198 1.1 jtc ksh_tmout_state = TMOUT_EXECUTING;
199 1.1 jtc #endif /* KSH */
200 1.1 jtc if (!flag)
201 1.1 jtc trap = 0;
202 1.1 jtc if (flag & TF_DFL_INTR)
203 1.1 jtc intrsig = 0;
204 1.1 jtc if (flag & TF_FATAL)
205 1.1 jtc fatal_trap = 0;
206 1.1 jtc for (p = sigtraps, i = SIGNALS+1; --i >= 0; p++)
207 1.1 jtc if (p->set && (!flag
208 1.1 jtc || ((p->flags & flag) && p->trap == (char *) 0)))
209 1.1 jtc runtrap(p);
210 1.1 jtc }
211 1.1 jtc
212 1.1 jtc void
213 1.1 jtc runtrap(p)
214 1.1 jtc Trap *p;
215 1.1 jtc {
216 1.1 jtc int i = p->signal;
217 1.1 jtc char *trapstr = p->trap;
218 1.1 jtc int oexstat;
219 1.1 jtc int UNINITIALIZED(old_changed);
220 1.1 jtc
221 1.1 jtc p->set = 0;
222 1.1 jtc if (trapstr == (char *) 0) { /* SIG_DFL */
223 1.1 jtc if (p->flags & TF_FATAL) {
224 1.1 jtc /* eg, SIGHUP */
225 1.1 jtc exstat = 128 + i;
226 1.1 jtc unwind(LLEAVE);
227 1.1 jtc }
228 1.1 jtc if (p->flags & TF_DFL_INTR) {
229 1.1 jtc /* eg, SIGINT, SIGQUIT, SIGTERM, etc. */
230 1.1 jtc exstat = 128 + i;
231 1.1 jtc unwind(LINTR);
232 1.1 jtc }
233 1.1 jtc return;
234 1.1 jtc }
235 1.1 jtc if (trapstr[0] == '\0') /* SIG_IGN */
236 1.1 jtc return;
237 1.1 jtc if (i == SIGEXIT_ || i == SIGERR_) { /* avoid recursion on these */
238 1.1 jtc old_changed = p->flags & TF_CHANGED;
239 1.1 jtc p->flags &= ~TF_CHANGED;
240 1.1 jtc p->trap = (char *) 0;
241 1.1 jtc }
242 1.1 jtc oexstat = exstat;
243 1.3 hubertf /* Note: trapstr is fully parsed before anything is executed, thus
244 1.3 hubertf * no problem with afree(p->trap) in settrap() while still in use.
245 1.3 hubertf */
246 1.1 jtc command(trapstr);
247 1.1 jtc exstat = oexstat;
248 1.1 jtc if (i == SIGEXIT_ || i == SIGERR_) {
249 1.1 jtc if (p->flags & TF_CHANGED)
250 1.1 jtc /* don't clear TF_CHANGED */
251 1.1 jtc afree(trapstr, APERM);
252 1.1 jtc else
253 1.1 jtc p->trap = trapstr;
254 1.1 jtc p->flags |= old_changed;
255 1.1 jtc }
256 1.1 jtc }
257 1.1 jtc
258 1.1 jtc /* clear pending traps and reset user's trap handlers; used after fork(2) */
259 1.1 jtc void
260 1.1 jtc cleartraps()
261 1.1 jtc {
262 1.1 jtc int i;
263 1.1 jtc Trap *p;
264 1.1 jtc
265 1.1 jtc trap = 0;
266 1.1 jtc intrsig = 0;
267 1.1 jtc fatal_trap = 0;
268 1.1 jtc for (i = SIGNALS+1, p = sigtraps; --i >= 0; p++) {
269 1.1 jtc p->set = 0;
270 1.1 jtc if ((p->flags & TF_USER_SET) && (p->trap && p->trap[0]))
271 1.1 jtc settrap(p, (char *) 0);
272 1.1 jtc }
273 1.1 jtc }
274 1.1 jtc
275 1.1 jtc /* restore signals just before an exec(2) */
276 1.1 jtc void
277 1.1 jtc restoresigs()
278 1.1 jtc {
279 1.1 jtc int i;
280 1.1 jtc Trap *p;
281 1.1 jtc
282 1.1 jtc for (i = SIGNALS+1, p = sigtraps; --i >= 0; p++)
283 1.1 jtc if (p->flags & (TF_EXEC_IGN|TF_EXEC_DFL))
284 1.1 jtc setsig(p, (p->flags & TF_EXEC_IGN) ? SIG_IGN : SIG_DFL,
285 1.1 jtc SS_RESTORE_CURR|SS_FORCE);
286 1.1 jtc }
287 1.1 jtc
288 1.1 jtc void
289 1.1 jtc settrap(p, s)
290 1.1 jtc Trap *p;
291 1.1 jtc char *s;
292 1.1 jtc {
293 1.1 jtc handler_t f;
294 1.1 jtc
295 1.1 jtc if (p->trap)
296 1.1 jtc afree(p->trap, APERM);
297 1.1 jtc p->trap = str_save(s, APERM); /* handles s == 0 */
298 1.1 jtc p->flags |= TF_CHANGED;
299 1.1 jtc f = !s ? SIG_DFL : s[0] ? trapsig : SIG_IGN;
300 1.1 jtc
301 1.1 jtc p->flags |= TF_USER_SET;
302 1.1 jtc if ((p->flags & (TF_DFL_INTR|TF_FATAL)) && f == SIG_DFL)
303 1.1 jtc f = trapsig;
304 1.1 jtc else if (p->flags & TF_SHELL_USES) {
305 1.1 jtc if (!(p->flags & TF_ORIG_IGN) || Flag(FTALKING)) {
306 1.1 jtc /* do what user wants at exec time */
307 1.1 jtc p->flags &= ~(TF_EXEC_IGN|TF_EXEC_DFL);
308 1.1 jtc if (f == SIG_IGN)
309 1.1 jtc p->flags |= TF_EXEC_IGN;
310 1.1 jtc else
311 1.1 jtc p->flags |= TF_EXEC_DFL;
312 1.1 jtc }
313 1.1 jtc /* assumes handler already set to what shell wants it
314 1.1 jtc * (normally trapsig, but could be j_sigchld() or SIG_IGN)
315 1.1 jtc */
316 1.1 jtc return;
317 1.1 jtc }
318 1.1 jtc
319 1.1 jtc /* todo: should we let user know signal is ignored? how? */
320 1.1 jtc setsig(p, f, SS_RESTORE_CURR|SS_USER);
321 1.1 jtc }
322 1.1 jtc
323 1.1 jtc /* Called by c_print() when writing to a co-process to ensure SIGPIPE won't
324 1.1 jtc * kill shell (unless user catches it and exits)
325 1.1 jtc */
326 1.1 jtc int
327 1.1 jtc block_pipe()
328 1.1 jtc {
329 1.1 jtc int restore_dfl = 0;
330 1.1 jtc Trap *p = &sigtraps[SIGPIPE];
331 1.1 jtc
332 1.1 jtc if (!(p->flags & (TF_ORIG_IGN|TF_ORIG_DFL))) {
333 1.1 jtc setsig(p, SIG_IGN, SS_RESTORE_CURR);
334 1.1 jtc if (p->flags & TF_ORIG_DFL)
335 1.1 jtc restore_dfl = 1;
336 1.1 jtc } else if (p->cursig == SIG_DFL) {
337 1.1 jtc setsig(p, SIG_IGN, SS_RESTORE_CURR);
338 1.1 jtc restore_dfl = 1; /* restore to SIG_DFL */
339 1.1 jtc }
340 1.1 jtc return restore_dfl;
341 1.1 jtc }
342 1.1 jtc
343 1.1 jtc /* Called by c_print() to undo whatever block_pipe() did */
344 1.1 jtc void
345 1.1 jtc restore_pipe(restore_dfl)
346 1.1 jtc int restore_dfl;
347 1.1 jtc {
348 1.1 jtc if (restore_dfl)
349 1.1 jtc setsig(&sigtraps[SIGPIPE], SIG_DFL, SS_RESTORE_CURR);
350 1.1 jtc }
351 1.1 jtc
352 1.1 jtc /* Set action for a signal. Action may not be set if original
353 1.1 jtc * action was SIG_IGN, depending on the value of flags and
354 1.1 jtc * FTALKING.
355 1.1 jtc */
356 1.1 jtc int
357 1.1 jtc setsig(p, f, flags)
358 1.1 jtc Trap *p;
359 1.1 jtc handler_t f;
360 1.1 jtc int flags;
361 1.1 jtc {
362 1.1 jtc struct sigaction sigact;
363 1.1 jtc
364 1.1 jtc if (p->signal == SIGEXIT_ || p->signal == SIGERR_)
365 1.1 jtc return 1;
366 1.1 jtc
367 1.1 jtc /* First time setting this signal? If so, get and note the current
368 1.1 jtc * setting.
369 1.1 jtc */
370 1.1 jtc if (!(p->flags & (TF_ORIG_IGN|TF_ORIG_DFL))) {
371 1.1 jtc sigaction(p->signal, &Sigact_ign, &sigact);
372 1.1 jtc p->flags |= sigact.sa_handler == SIG_IGN ?
373 1.1 jtc TF_ORIG_IGN : TF_ORIG_DFL;
374 1.1 jtc p->cursig = SIG_IGN;
375 1.1 jtc }
376 1.1 jtc
377 1.1 jtc /* Generally, an ignored signal stays ignored, except if
378 1.1 jtc * - the user of an interactive shell wants to change it
379 1.1 jtc * - the shell wants for force a change
380 1.1 jtc */
381 1.1 jtc if ((p->flags & TF_ORIG_IGN) && !(flags & SS_FORCE)
382 1.1 jtc && (!(flags & SS_USER) || !Flag(FTALKING)))
383 1.1 jtc return 0;
384 1.1 jtc
385 1.1 jtc setexecsig(p, flags & SS_RESTORE_MASK);
386 1.1 jtc
387 1.1 jtc /* This is here 'cause there should be a way of clearing shtraps, but
388 1.1 jtc * don't know if this is a sane way of doing it. At the moment,
389 1.1 jtc * all users of shtrap are lifetime users (SIGCHLD, SIGALRM, SIGWINCH).
390 1.1 jtc */
391 1.1 jtc if (!(flags & SS_USER))
392 1.1 jtc p->shtrap = (handler_t) 0;
393 1.1 jtc if (flags & SS_SHTRAP) {
394 1.1 jtc p->shtrap = f;
395 1.1 jtc f = trapsig;
396 1.1 jtc }
397 1.1 jtc
398 1.1 jtc if (p->cursig != f) {
399 1.1 jtc p->cursig = f;
400 1.1 jtc sigemptyset(&sigact.sa_mask);
401 1.1 jtc sigact.sa_flags = KSH_SA_FLAGS;
402 1.1 jtc sigact.sa_handler = f;
403 1.1 jtc sigaction(p->signal, &sigact, (struct sigaction *) 0);
404 1.1 jtc }
405 1.1 jtc
406 1.1 jtc return 1;
407 1.1 jtc }
408 1.1 jtc
409 1.1 jtc /* control what signal is set to before an exec() */
410 1.1 jtc void
411 1.1 jtc setexecsig(p, restore)
412 1.1 jtc Trap *p;
413 1.1 jtc int restore;
414 1.1 jtc {
415 1.1 jtc /* XXX debugging */
416 1.1 jtc if (!(p->flags & (TF_ORIG_IGN|TF_ORIG_DFL)))
417 1.1 jtc internal_errorf(1, "setexecsig: unset signal %d(%s)",
418 1.1 jtc p->signal, p->name);
419 1.1 jtc
420 1.1 jtc /* restore original value for exec'd kids */
421 1.1 jtc p->flags &= ~(TF_EXEC_IGN|TF_EXEC_DFL);
422 1.1 jtc switch (restore & SS_RESTORE_MASK) {
423 1.1 jtc case SS_RESTORE_CURR: /* leave things as they currently are */
424 1.1 jtc break;
425 1.1 jtc case SS_RESTORE_ORIG:
426 1.1 jtc p->flags |= p->flags & TF_ORIG_IGN ? TF_EXEC_IGN : TF_EXEC_DFL;
427 1.1 jtc break;
428 1.1 jtc case SS_RESTORE_DFL:
429 1.1 jtc p->flags |= TF_EXEC_DFL;
430 1.1 jtc break;
431 1.1 jtc case SS_RESTORE_IGN:
432 1.1 jtc p->flags |= TF_EXEC_IGN;
433 1.1 jtc break;
434 1.1 jtc }
435 1.1 jtc }
436