trap.c revision 1.23 1 /* $NetBSD: trap.c,v 1.23 2000/05/13 20:50:15 elric 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.23 2000/05/13 20:50:15 elric 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 extern char nullstr[1]; /* null string */
81
82 char *trap[NSIG+1]; /* trap handler commands */
83 MKINIT char sigmode[NSIG]; /* current value of signal */
84 char gotsig[NSIG]; /* indicates specified signal received */
85 int pendingsigs; /* indicates some signal received */
86
87 static int getsigaction __P((int, sig_t *));
88
89 /*
90 * The trap builtin.
91 */
92
93 int
94 trapcmd(argc, argv)
95 int argc;
96 char **argv;
97 {
98 char *action;
99 char **ap;
100 int signo;
101
102 if (argc <= 1) {
103 for (signo = 0 ; signo <= NSIG ; signo++) {
104 if (trap[signo] != NULL)
105 out1fmt("%d: %s\n", signo, trap[signo]);
106 }
107 return 0;
108 }
109 ap = argv + 1;
110 if (is_number(*ap))
111 action = NULL;
112 else
113 action = *ap++;
114 while (*ap) {
115 if ((signo = number(*ap)) < 0 || signo > NSIG)
116 error("%s: bad trap", *ap);
117 INTOFF;
118 if (action)
119 action = savestr(action);
120 if (trap[signo])
121 ckfree(trap[signo]);
122 trap[signo] = action;
123 if (signo != 0)
124 setsignal(signo, 0);
125 INTON;
126 ap++;
127 }
128 return 0;
129 }
130
131
132
133 /*
134 * Clear traps on a fork or vfork.
135 * Takes one arg vfork, to tell it to not be destructive of
136 * the parents variables.
137 */
138
139 void
140 clear_traps(vforked)
141 int vforked;
142 {
143 char **tp;
144
145 for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
146 if (*tp && **tp) { /* trap not NULL or SIG_IGN */
147 INTOFF;
148 if (!vforked) {
149 ckfree(*tp);
150 *tp = NULL;
151 }
152 if (tp != &trap[0])
153 setsignal(tp - trap, vforked);
154 INTON;
155 }
156 }
157 }
158
159
160
161 /*
162 * Set the signal handler for the specified signal. The routine figures
163 * out what it should be set to.
164 */
165
166 long
167 setsignal(signo, vforked)
168 int signo;
169 int vforked;
170 {
171 int action;
172 sig_t sigact = SIG_DFL;
173 char *t, tsig;
174
175 if ((t = trap[signo]) == NULL)
176 action = S_DFL;
177 else if (*t != '\0')
178 action = S_CATCH;
179 else
180 action = S_IGN;
181 if (rootshell && !vforked && action == S_DFL) {
182 switch (signo) {
183 case SIGINT:
184 if (iflag || minusc || sflag == 0)
185 action = S_CATCH;
186 break;
187 case SIGQUIT:
188 #ifdef DEBUG
189 {
190 extern int debug;
191
192 if (debug)
193 break;
194 }
195 #endif
196 /* FALLTHROUGH */
197 case SIGTERM:
198 if (iflag)
199 action = S_IGN;
200 break;
201 #if JOBS
202 case SIGTSTP:
203 case SIGTTOU:
204 if (mflag)
205 action = S_IGN;
206 break;
207 #endif
208 }
209 }
210
211 t = &sigmode[signo - 1];
212 tsig = *t;
213 if (tsig == 0) {
214 /*
215 * current setting unknown
216 */
217 if (!getsigaction(signo, &sigact)) {
218 /*
219 * Pretend it worked; maybe we should give a warning
220 * here, but other shells don't. We don't alter
221 * sigmode, so that we retry every time.
222 */
223 return 0;
224 }
225 if (sigact == SIG_IGN) {
226 if (mflag && (signo == SIGTSTP ||
227 signo == SIGTTIN || signo == SIGTTOU)) {
228 tsig = S_IGN; /* don't hard ignore these */
229 } else
230 tsig = S_HARD_IGN;
231 } else {
232 tsig = S_RESET; /* force to be set */
233 }
234 }
235 if (tsig == S_HARD_IGN || tsig == action)
236 return 0;
237 switch (action) {
238 case S_DFL: sigact = SIG_DFL; break;
239 case S_CATCH: sigact = onsig; break;
240 case S_IGN: sigact = SIG_IGN; break;
241 }
242 if (!vforked)
243 *t = action;
244 siginterrupt(signo, 1);
245 return (long)signal(signo, sigact);
246 }
247
248 /*
249 * Return the current setting for sig w/o changing it.
250 */
251 static int
252 getsigaction(signo, sigact)
253 int signo;
254 sig_t *sigact;
255 {
256 struct sigaction sa;
257
258 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
259 return 0;
260 *sigact = (sig_t) sa.sa_handler;
261 return 1;
262 }
263
264 /*
265 * Ignore a signal.
266 */
267
268 void
269 ignoresig(signo, vforked)
270 int signo;
271 int vforked;
272 {
273 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
274 signal(signo, SIG_IGN);
275 }
276 if (!vforked)
277 sigmode[signo - 1] = S_HARD_IGN;
278 }
279
280
281 #ifdef mkinit
282 INCLUDE <signal.h>
283 INCLUDE "trap.h"
284
285 SHELLPROC {
286 char *sm;
287
288 clear_traps(0);
289 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
290 if (*sm == S_IGN)
291 *sm = S_HARD_IGN;
292 }
293 }
294 #endif
295
296
297
298 /*
299 * Signal handler.
300 */
301
302 void
303 onsig(signo)
304 int signo;
305 {
306 signal(signo, onsig);
307 if (signo == SIGINT && trap[SIGINT] == NULL) {
308 onint();
309 return;
310 }
311 gotsig[signo - 1] = 1;
312 pendingsigs++;
313 }
314
315
316
317 /*
318 * Called to execute a trap. Perhaps we should avoid entering new trap
319 * handlers while we are executing a trap handler.
320 */
321
322 void
323 dotrap() {
324 int i;
325 int savestatus;
326
327 for (;;) {
328 for (i = 1 ; ; i++) {
329 if (gotsig[i - 1])
330 break;
331 if (i >= NSIG)
332 goto done;
333 }
334 gotsig[i - 1] = 0;
335 savestatus=exitstatus;
336 evalstring(trap[i], 0);
337 exitstatus=savestatus;
338 }
339 done:
340 pendingsigs = 0;
341 }
342
343
344
345 /*
346 * Controls whether the shell is interactive or not.
347 */
348
349
350 void
351 setinteractive(on)
352 int on;
353 {
354 static int is_interactive;
355
356 if (on == is_interactive)
357 return;
358 setsignal(SIGINT, 0);
359 setsignal(SIGQUIT, 0);
360 setsignal(SIGTERM, 0);
361 is_interactive = on;
362 }
363
364
365
366 /*
367 * Called to exit the shell.
368 */
369
370 void
371 exitshell(status)
372 int status;
373 {
374 struct jmploc loc1, loc2;
375 char *p;
376
377 TRACE(("exitshell(%d) pid=%d\n", status, getpid()));
378 if (setjmp(loc1.loc)) {
379 goto l1;
380 }
381 if (setjmp(loc2.loc)) {
382 goto l2;
383 }
384 handler = &loc1;
385 if ((p = trap[0]) != NULL && *p != '\0') {
386 trap[0] = NULL;
387 evalstring(p, 0);
388 }
389 l1: handler = &loc2; /* probably unnecessary */
390 flushall();
391 #if JOBS
392 setjobctl(0);
393 #endif
394 l2: _exit(status);
395 /* NOTREACHED */
396 }
397