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