trap.c revision 1.26 1 /* $NetBSD: trap.c,v 1.26 2001/03/18 04:04:23 wulf Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)trap.c 8.5 (Berkeley) 6/5/95";
43 #else
44 __RCSID("$NetBSD: trap.c,v 1.26 2001/03/18 04:04:23 wulf Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <signal.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51
52 #include "shell.h"
53 #include "main.h"
54 #include "nodes.h" /* for other headers */
55 #include "eval.h"
56 #include "jobs.h"
57 #include "show.h"
58 #include "options.h"
59 #include "syntax.h"
60 #include "output.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "trap.h"
64 #include "mystring.h"
65
66
67 /*
68 * Sigmode records the current value of the signal handlers for the various
69 * modes. A value of zero means that the current handler is not known.
70 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
71 */
72
73 #define S_DFL 1 /* default signal handling (SIG_DFL) */
74 #define S_CATCH 2 /* signal is caught */
75 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
76 #define S_HARD_IGN 4 /* signal is ignored permenantly */
77 #define S_RESET 5 /* temporary - to reset a hard ignored sig */
78
79
80 char *trap[NSIG+1]; /* trap handler commands */
81 MKINIT char sigmode[NSIG]; /* current value of signal */
82 char gotsig[NSIG]; /* indicates specified signal received */
83 int pendingsigs; /* indicates some signal received */
84
85 static int getsigaction __P((int, sig_t *));
86 static int signame_to_signum __P((const char *));
87 void printsignals __P((void));
88
89 /*
90 * return the signal number described by `p' (as a number or a name)
91 * or -1 if it isn't one
92 */
93
94 static int
95 signame_to_signum(p)
96 const char *p;
97 {
98 int i;
99
100 if (is_number(p))
101 return number(p);
102
103 if (strcasecmp(p, "exit") == 0 )
104 return 0;
105
106 if (strncasecmp(p, "sig", 3) == 0)
107 p += 3;
108
109 for (i = 0; i < NSIG; ++i)
110 if (strcasecmp (p, sys_signame[i]) == 0)
111 return i;
112 return -1;
113 }
114
115 /*
116 * Print a list of valid signal names
117 */
118 void
119 printsignals(void)
120 {
121 int n;
122
123 out1str("EXIT ");
124
125 for (n = 1; n < NSIG; n++) {
126 out1fmt("%s", sys_signame[n]);
127 if ((n == NSIG/2) || n == (NSIG - 1))
128 out1str("\n");
129 else
130 out1c(' ');
131 }
132 }
133
134 /*
135 * The trap builtin.
136 */
137
138 int
139 trapcmd(argc, argv)
140 int argc;
141 char **argv;
142 {
143 char *action;
144 char **ap;
145 int signo;
146
147 if (argc <= 1) {
148 for (signo = 0 ; signo <= NSIG ; signo++) {
149 if (trap[signo] != NULL)
150 out1fmt("trap -- '%s' %s\n", trap[signo],
151 (signo) ? sys_signame[signo] : "EXIT");
152 }
153 return 0;
154 }
155 ap = argv + 1;
156
157 action = NULL;
158
159 if (strcmp(*ap, "--") == 0)
160 if (*++ap == NULL)
161 return 0;
162
163 if (signame_to_signum(*ap) == -1) {
164 if ((*ap)[0] =='-') {
165 if ((*ap)[1] == NULL)
166 ap++;
167 else if ((*ap)[1] == 'l' && (*ap)[2] == NULL) {
168 printsignals();
169 return 0;
170 }
171 else
172 error("bad option %s\n", *ap);
173 }
174 else
175 action = *ap++;
176 }
177
178 while (*ap) {
179 if (is_number(*ap))
180 signo = number(*ap);
181 else
182 signo = signame_to_signum(*ap);
183
184 if (signo < 0 || signo > NSIG)
185 error("%s: bad trap", *ap);
186
187 INTOFF;
188 if (action)
189 action = savestr(action);
190
191 if (trap[signo])
192 ckfree(trap[signo]);
193
194 trap[signo] = action;
195
196 if (signo != 0)
197 setsignal(signo);
198 INTON;
199 ap++;
200 }
201 return 0;
202 }
203
204
205
206 /*
207 * Clear traps on a fork.
208 */
209
210 void
211 clear_traps() {
212 char **tp;
213
214 for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
215 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
216 INTOFF;
217 ckfree(*tp);
218 *tp = NULL;
219 if (tp != &trap[0])
220 setsignal(tp - trap);
221 INTON;
222 }
223 }
224 }
225
226
227
228 /*
229 * Set the signal handler for the specified signal. The routine figures
230 * out what it should be set to.
231 */
232
233 long
234 setsignal(signo)
235 int signo;
236 {
237 int action;
238 sig_t sigact = SIG_DFL;
239 char *t;
240
241 if ((t = trap[signo]) == NULL)
242 action = S_DFL;
243 else if (*t != '\0')
244 action = S_CATCH;
245 else
246 action = S_IGN;
247 if (rootshell && action == S_DFL) {
248 switch (signo) {
249 case SIGINT:
250 if (iflag || minusc || sflag == 0)
251 action = S_CATCH;
252 break;
253 case SIGQUIT:
254 #ifdef DEBUG
255 {
256 extern int debug;
257
258 if (debug)
259 break;
260 }
261 #endif
262 /* FALLTHROUGH */
263 case SIGTERM:
264 if (iflag)
265 action = S_IGN;
266 break;
267 #if JOBS
268 case SIGTSTP:
269 case SIGTTOU:
270 if (mflag)
271 action = S_IGN;
272 break;
273 #endif
274 }
275 }
276
277 t = &sigmode[signo - 1];
278 if (*t == 0) {
279 /*
280 * current setting unknown
281 */
282 if (!getsigaction(signo, &sigact)) {
283 /*
284 * Pretend it worked; maybe we should give a warning
285 * here, but other shells don't. We don't alter
286 * sigmode, so that we retry every time.
287 */
288 return 0;
289 }
290 if (sigact == SIG_IGN) {
291 if (mflag && (signo == SIGTSTP ||
292 signo == SIGTTIN || signo == SIGTTOU)) {
293 *t = S_IGN; /* don't hard ignore these */
294 } else
295 *t = S_HARD_IGN;
296 } else {
297 *t = S_RESET; /* force to be set */
298 }
299 }
300 if (*t == S_HARD_IGN || *t == action)
301 return 0;
302 switch (action) {
303 case S_DFL: sigact = SIG_DFL; break;
304 case S_CATCH: sigact = onsig; break;
305 case S_IGN: sigact = SIG_IGN; break;
306 }
307 *t = action;
308 siginterrupt(signo, 1);
309 return (long)signal(signo, sigact);
310 }
311
312 /*
313 * Return the current setting for sig w/o changing it.
314 */
315 static int
316 getsigaction(signo, sigact)
317 int signo;
318 sig_t *sigact;
319 {
320 struct sigaction sa;
321
322 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
323 return 0;
324 *sigact = (sig_t) sa.sa_handler;
325 return 1;
326 }
327
328 /*
329 * Ignore a signal.
330 */
331
332 void
333 ignoresig(signo)
334 int signo;
335 {
336 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
337 signal(signo, SIG_IGN);
338 }
339 sigmode[signo - 1] = S_HARD_IGN;
340 }
341
342
343 #ifdef mkinit
344 INCLUDE <signal.h>
345 INCLUDE "trap.h"
346
347 SHELLPROC {
348 char *sm;
349
350 clear_traps();
351 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
352 if (*sm == S_IGN)
353 *sm = S_HARD_IGN;
354 }
355 }
356 #endif
357
358
359
360 /*
361 * Signal handler.
362 */
363
364 void
365 onsig(signo)
366 int signo;
367 {
368 signal(signo, onsig);
369 if (signo == SIGINT && trap[SIGINT] == NULL) {
370 onint();
371 return;
372 }
373 gotsig[signo - 1] = 1;
374 pendingsigs++;
375 }
376
377
378
379 /*
380 * Called to execute a trap. Perhaps we should avoid entering new trap
381 * handlers while we are executing a trap handler.
382 */
383
384 void
385 dotrap() {
386 int i;
387 int savestatus;
388
389 for (;;) {
390 for (i = 1 ; ; i++) {
391 if (gotsig[i - 1])
392 break;
393 if (i >= NSIG)
394 goto done;
395 }
396 gotsig[i - 1] = 0;
397 savestatus=exitstatus;
398 evalstring(trap[i], 0);
399 exitstatus=savestatus;
400 }
401 done:
402 pendingsigs = 0;
403 }
404
405
406
407 /*
408 * Controls whether the shell is interactive or not.
409 */
410
411
412 void
413 setinteractive(on)
414 int on;
415 {
416 static int is_interactive;
417
418 if (on == is_interactive)
419 return;
420 setsignal(SIGINT);
421 setsignal(SIGQUIT);
422 setsignal(SIGTERM);
423 is_interactive = on;
424 }
425
426
427
428 /*
429 * Called to exit the shell.
430 */
431
432 void
433 exitshell(status)
434 int status;
435 {
436 struct jmploc loc1, loc2;
437 char *p;
438
439 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
440 if (setjmp(loc1.loc)) {
441 goto l1;
442 }
443 if (setjmp(loc2.loc)) {
444 goto l2;
445 }
446 handler = &loc1;
447 if ((p = trap[0]) != NULL && *p != '\0') {
448 trap[0] = NULL;
449 evalstring(p, 0);
450 }
451 l1: handler = &loc2; /* probably unnecessary */
452 flushall();
453 #if JOBS
454 setjobctl(0);
455 #endif
456 l2: _exit(status);
457 /* NOTREACHED */
458 }
459