xref: /netbsd/bin/sh/trap.c (revision c4a72b64)
1 /*	$NetBSD: trap.c,v 1.28 2002/11/24 22:35:43 christos 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.28 2002/11/24 22:35:43 christos 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(int, sig_t *);
86 
87 /*
88  * return the signal number described by `p' (as a number or a name)
89  * or -1 if it isn't one
90  */
91 
92 static int
93 signame_to_signum(const char *p)
94 {
95 	int i;
96 
97 	if (is_number(p))
98 		return number(p);
99 
100 	if (strcasecmp(p, "exit") == 0 )
101 		return 0;
102 
103 	if (strncasecmp(p, "sig", 3) == 0)
104 		p += 3;
105 
106 	for (i = 0; i < NSIG; ++i)
107 		if (strcasecmp (p, sys_signame[i]) == 0)
108 			return i;
109 	return -1;
110 }
111 
112 /*
113  * Print a list of valid signal names
114  */
115 static void
116 printsignals(void)
117 {
118 	int n;
119 
120 	out1str("EXIT ");
121 
122 	for (n = 1; n < NSIG; n++) {
123 		out1fmt("%s", sys_signame[n]);
124 		if ((n == NSIG/2) ||  n == (NSIG - 1))
125 			out1str("\n");
126 		else
127 			out1c(' ');
128 	}
129 }
130 
131 /*
132  * The trap builtin.
133  */
134 
135 int
136 trapcmd(int argc, char **argv)
137 {
138 	char *action;
139 	char **ap;
140 	int signo;
141 
142 	if (argc <= 1) {
143 		for (signo = 0 ; signo <= NSIG ; signo++) {
144 			if (trap[signo] != NULL)
145 				out1fmt("trap -- '%s' %s\n", trap[signo],
146 				    (signo) ? sys_signame[signo] : "EXIT");
147 		}
148 		return 0;
149 	}
150 	ap = argv + 1;
151 
152 	action = NULL;
153 
154 	if (strcmp(*ap, "--") == 0)
155 		if (*++ap == NULL)
156 			return 0;
157 
158 	if (signame_to_signum(*ap) == -1) {
159 		if ((*ap)[0] =='-') {
160 			if ((*ap)[1] == NULL)
161 				ap++;
162 			else if ((*ap)[1] == 'l' && (*ap)[2] == NULL) {
163 				printsignals();
164 				return 0;
165 			}
166 			else
167 				error("bad option %s\n", *ap);
168 		}
169 		else
170 			action = *ap++;
171 	}
172 
173 	while (*ap) {
174 		if (is_number(*ap))
175 			signo = number(*ap);
176 		else
177 			signo = signame_to_signum(*ap);
178 
179 		if (signo < 0 || signo > NSIG)
180 			error("%s: bad trap", *ap);
181 
182 		INTOFF;
183 		if (action)
184 			action = savestr(action);
185 
186 		if (trap[signo])
187 			ckfree(trap[signo]);
188 
189 		trap[signo] = action;
190 
191 		if (signo != 0)
192 			setsignal(signo, 0);
193 		INTON;
194 		ap++;
195 	}
196 	return 0;
197 }
198 
199 
200 
201 /*
202  * Clear traps on a fork or vfork.
203  * Takes one arg vfork, to tell it to not be destructive of
204  * the parents variables.
205  */
206 
207 void
208 clear_traps(int vforked)
209 {
210 	char **tp;
211 
212 	for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
213 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
214 			INTOFF;
215 			if (!vforked) {
216 				ckfree(*tp);
217 				*tp = NULL;
218 			}
219 			if (tp != &trap[0])
220 				setsignal(tp - trap, vforked);
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(int signo, int vforked)
235 {
236 	int action;
237 	sig_t sigact = SIG_DFL;
238 	char *t, tsig;
239 
240 	if ((t = trap[signo]) == NULL)
241 		action = S_DFL;
242 	else if (*t != '\0')
243 		action = S_CATCH;
244 	else
245 		action = S_IGN;
246 	if (rootshell && !vforked && action == S_DFL) {
247 		switch (signo) {
248 		case SIGINT:
249 			if (iflag || minusc || sflag == 0)
250 				action = S_CATCH;
251 			break;
252 		case SIGQUIT:
253 #ifdef DEBUG
254 			if (debug)
255 				break;
256 #endif
257 			/* FALLTHROUGH */
258 		case SIGTERM:
259 			if (iflag)
260 				action = S_IGN;
261 			break;
262 #if JOBS
263 		case SIGTSTP:
264 		case SIGTTOU:
265 			if (mflag)
266 				action = S_IGN;
267 			break;
268 #endif
269 		}
270 	}
271 
272 	t = &sigmode[signo - 1];
273 	tsig = *t;
274 	if (tsig == 0) {
275 		/*
276 		 * current setting unknown
277 		 */
278 		if (!getsigaction(signo, &sigact)) {
279 			/*
280 			 * Pretend it worked; maybe we should give a warning
281 			 * here, but other shells don't. We don't alter
282 			 * sigmode, so that we retry every time.
283 			 */
284 			return 0;
285 		}
286 		if (sigact == SIG_IGN) {
287 			if (mflag && (signo == SIGTSTP ||
288 			     signo == SIGTTIN || signo == SIGTTOU)) {
289 				tsig = S_IGN;	/* don't hard ignore these */
290 			} else
291 				tsig = S_HARD_IGN;
292 		} else {
293 			tsig = S_RESET;	/* force to be set */
294 		}
295 	}
296 	if (tsig == S_HARD_IGN || tsig == action)
297 		return 0;
298 	switch (action) {
299 		case S_DFL:	sigact = SIG_DFL;	break;
300 		case S_CATCH:  	sigact = onsig;		break;
301 		case S_IGN:	sigact = SIG_IGN;	break;
302 	}
303 	if (!vforked)
304 		*t = action;
305 	siginterrupt(signo, 1);
306 	return (long)signal(signo, sigact);
307 }
308 
309 /*
310  * Return the current setting for sig w/o changing it.
311  */
312 static int
313 getsigaction(int signo, sig_t *sigact)
314 {
315 	struct sigaction sa;
316 
317 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
318 		return 0;
319 	*sigact = (sig_t) sa.sa_handler;
320 	return 1;
321 }
322 
323 /*
324  * Ignore a signal.
325  */
326 
327 void
328 ignoresig(int signo, int vforked)
329 {
330 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
331 		signal(signo, SIG_IGN);
332 	}
333 	if (!vforked)
334 		sigmode[signo - 1] = S_HARD_IGN;
335 }
336 
337 
338 #ifdef mkinit
339 INCLUDE <signal.h>
340 INCLUDE "trap.h"
341 
342 SHELLPROC {
343 	char *sm;
344 
345 	clear_traps(0);
346 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
347 		if (*sm == S_IGN)
348 			*sm = S_HARD_IGN;
349 	}
350 }
351 #endif
352 
353 
354 
355 /*
356  * Signal handler.
357  */
358 
359 void
360 onsig(int signo)
361 {
362 	signal(signo, onsig);
363 	if (signo == SIGINT && trap[SIGINT] == NULL) {
364 		onint();
365 		return;
366 	}
367 	gotsig[signo - 1] = 1;
368 	pendingsigs++;
369 }
370 
371 
372 
373 /*
374  * Called to execute a trap.  Perhaps we should avoid entering new trap
375  * handlers while we are executing a trap handler.
376  */
377 
378 void
379 dotrap(void)
380 {
381 	int i;
382 	int savestatus;
383 
384 	for (;;) {
385 		for (i = 1 ; ; i++) {
386 			if (gotsig[i - 1])
387 				break;
388 			if (i >= NSIG)
389 				goto done;
390 		}
391 		gotsig[i - 1] = 0;
392 		savestatus=exitstatus;
393 		evalstring(trap[i], 0);
394 		exitstatus=savestatus;
395 	}
396 done:
397 	pendingsigs = 0;
398 }
399 
400 
401 
402 /*
403  * Controls whether the shell is interactive or not.
404  */
405 
406 
407 void
408 setinteractive(int on)
409 {
410 	static int is_interactive;
411 
412 	if (on == is_interactive)
413 		return;
414 	setsignal(SIGINT, 0);
415 	setsignal(SIGQUIT, 0);
416 	setsignal(SIGTERM, 0);
417 	is_interactive = on;
418 }
419 
420 
421 
422 /*
423  * Called to exit the shell.
424  */
425 
426 void
427 exitshell(int status)
428 {
429 	struct jmploc loc1, loc2;
430 	char *p;
431 
432 	TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
433 	if (setjmp(loc1.loc)) {
434 		goto l1;
435 	}
436 	if (setjmp(loc2.loc)) {
437 		goto l2;
438 	}
439 	handler = &loc1;
440 	if ((p = trap[0]) != NULL && *p != '\0') {
441 		trap[0] = NULL;
442 		evalstring(p, 0);
443 	}
444 l1:   handler = &loc2;			/* probably unnecessary */
445 	flushall();
446 #if JOBS
447 	setjobctl(0);
448 #endif
449 l2:   _exit(status);
450 	/* NOTREACHED */
451 }
452