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