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