• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*	$NetBSD: trap.c,v 1.31 2005/01/11 19:38:57 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. 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.31 2005/01/11 19:38:57 christos 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 #include "var.h"
62 
63 static const char *sys_signame[NSIG] = {
64 	"Unused",
65 	"HUP",      "INT",    "QUIT",    "ILL",
66 	"TRAP",     "ABRT",   "BUS",     "FPE",
67 	"KILL",     "USR1",   "SEGV",    "USR2",
68 	"PIPE",     "ALRM",   "TERM",
69 	"Unknown",
70 	"CHLD",
71 	"CONT",     "STOP",   "TSTP",    "TTIN",
72 	"TTOU",     "URG",    "XCPU",    "XFSZ",
73 	"VTALRM",   "PROF",   "WINCH",   "IO",
74 	"PWR",      "SYS"
75 };
76 
77 /*
78  * Sigmode records the current value of the signal handlers for the various
79  * modes.  A value of zero means that the current handler is not known.
80  * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
81  */
82 
83 #define S_DFL 1			/* default signal handling (SIG_DFL) */
84 #define S_CATCH 2		/* signal is caught */
85 #define S_IGN 3			/* signal is ignored (SIG_IGN) */
86 #define S_HARD_IGN 4		/* signal is ignored permenantly */
87 #define S_RESET 5		/* temporary - to reset a hard ignored sig */
88 
89 
90 char *trap[NSIG+1];		/* trap handler commands */
91 MKINIT char sigmode[NSIG];	/* current value of signal */
92 char gotsig[NSIG];		/* indicates specified signal received */
93 int pendingsigs;		/* indicates some signal received */
94 
95 static int getsigaction(int, sig_t *);
96 
97 /*
98  * return the signal number described by `p' (as a number or a name)
99  * or -1 if it isn't one
100  */
101 
102 static int
signame_to_signum(const char * p)103 signame_to_signum(const char *p)
104 {
105 	int i;
106 
107 	if (is_number(p))
108 		return number(p);
109 
110 	if (strcasecmp(p, "exit") == 0 )
111 		return 0;
112 
113 	if (strncasecmp(p, "sig", 3) == 0)
114 		p += 3;
115 
116 	for (i = 0; i < NSIG; ++i)
117 		if (strcasecmp (p, sys_signame[i]) == 0)
118 			return i;
119 	return -1;
120 }
121 
122 /*
123  * Print a list of valid signal names
124  */
125 static void
printsignals(void)126 printsignals(void)
127 {
128 	int n;
129 
130 	out1str("EXIT ");
131 
132 	for (n = 1; n < NSIG; n++) {
133 		out1fmt("%s", sys_signame[n]);
134 		if ((n == NSIG/2) ||  n == (NSIG - 1))
135 			out1str("\n");
136 		else
137 			out1c(' ');
138 	}
139 }
140 
141 /*
142  * The trap builtin.
143  */
144 
145 int
trapcmd(int argc,char ** argv)146 trapcmd(int argc, char **argv)
147 {
148 	char *action;
149 	char **ap;
150 	int signo;
151 
152 	if (argc <= 1) {
153 		for (signo = 0 ; signo <= NSIG ; signo++)
154 			if (trap[signo] != NULL) {
155 				out1fmt("trap -- ");
156 				print_quoted(trap[signo]);
157 				out1fmt(" %s\n",
158 				    (signo) ? sys_signame[signo] : "EXIT");
159 			}
160 		return 0;
161 	}
162 	ap = argv + 1;
163 
164 	action = NULL;
165 
166 	if (strcmp(*ap, "--") == 0)
167 		if (*++ap == NULL)
168 			return 0;
169 
170 	if (signame_to_signum(*ap) == -1) {
171 		if ((*ap)[0] == '-') {
172 			if ((*ap)[1] == '\0')
173 				ap++;
174 			else if ((*ap)[1] == 'l' && (*ap)[2] == '\0') {
175 				printsignals();
176 				return 0;
177 			}
178 			else
179 				error("bad option %s\n", *ap);
180 		}
181 		else
182 			action = *ap++;
183 	}
184 
185 	while (*ap) {
186 		if (is_number(*ap))
187 			signo = number(*ap);
188 		else
189 			signo = signame_to_signum(*ap);
190 
191 		if (signo < 0 || signo > NSIG)
192 			error("%s: bad trap", *ap);
193 
194 		INTOFF;
195 		if (action)
196 			action = savestr(action);
197 
198 		if (trap[signo])
199 			ckfree(trap[signo]);
200 
201 		trap[signo] = action;
202 
203 		if (signo != 0)
204 			setsignal(signo, 0);
205 		INTON;
206 		ap++;
207 	}
208 	return 0;
209 }
210 
211 
212 
213 /*
214  * Clear traps on a fork or vfork.
215  * Takes one arg vfork, to tell it to not be destructive of
216  * the parents variables.
217  */
218 
219 void
clear_traps(int vforked)220 clear_traps(int vforked)
221 {
222 	char **tp;
223 
224 	for (tp = trap ; tp <= &trap[NSIG] ; tp++) {
225 		if (*tp && **tp) {	/* trap not NULL or SIG_IGN */
226 			INTOFF;
227 			if (!vforked) {
228 				ckfree(*tp);
229 				*tp = NULL;
230 			}
231 			if (tp != &trap[0])
232 				setsignal(tp - trap, vforked);
233 			INTON;
234 		}
235 	}
236 }
237 
238 
239 
240 /*
241  * Set the signal handler for the specified signal.  The routine figures
242  * out what it should be set to.
243  */
244 
245 long
setsignal(int signo,int vforked)246 setsignal(int signo, int vforked)
247 {
248 	int action;
249 	sig_t sigact = SIG_DFL;
250 	struct sigaction act, oact;
251 	char *t, tsig;
252 
253 	if ((t = trap[signo]) == NULL)
254 		action = S_DFL;
255 	else if (*t != '\0')
256 		action = S_CATCH;
257 	else
258 		action = S_IGN;
259 	if (rootshell && !vforked && action == S_DFL) {
260 		switch (signo) {
261 		case SIGINT:
262 			if (iflag || minusc || sflag == 0)
263 				action = S_CATCH;
264 			break;
265 		case SIGQUIT:
266 #ifdef DEBUG
267 			if (debug)
268 				break;
269 #endif
270 			/* FALLTHROUGH */
271 		case SIGTERM:
272 			if (iflag)
273 				action = S_IGN;
274 			break;
275 #if JOBS
276 		case SIGTSTP:
277 		case SIGTTOU:
278 			if (mflag)
279 				action = S_IGN;
280 			break;
281 #endif
282 		}
283 	}
284 
285 	t = &sigmode[signo - 1];
286 	tsig = *t;
287 	if (tsig == 0) {
288 		/*
289 		 * current setting unknown
290 		 */
291 		if (!getsigaction(signo, &sigact)) {
292 			/*
293 			 * Pretend it worked; maybe we should give a warning
294 			 * here, but other shells don't. We don't alter
295 			 * sigmode, so that we retry every time.
296 			 */
297 			return 0;
298 		}
299 		if (sigact == SIG_IGN) {
300 			if (mflag && (signo == SIGTSTP ||
301 			     signo == SIGTTIN || signo == SIGTTOU)) {
302 				tsig = S_IGN;	/* don't hard ignore these */
303 			} else
304 				tsig = S_HARD_IGN;
305 		} else {
306 			tsig = S_RESET;	/* force to be set */
307 		}
308 	}
309 	if (tsig == S_HARD_IGN || tsig == action)
310 		return 0;
311 	switch (action) {
312 		case S_DFL:	sigact = SIG_DFL;	break;
313 		case S_CATCH:  	sigact = onsig;		break;
314 		case S_IGN:	sigact = SIG_IGN;	break;
315 	}
316 	if (!vforked)
317 		*t = action;
318     act.sa_handler = sigact;
319     sigemptyset(&act.sa_mask);
320     act.sa_flags = 0;
321 #ifdef SA_INTERRUPT
322     act.sa_flags |= SA_INTERRUPT;
323 #endif
324     if(sigaction(signo, &act, &oact) < 0)
325         return (long) SIG_ERR;
326     return (long) oact.sa_handler;
327 }
328 
329 /*
330  * Return the current setting for sig w/o changing it.
331  */
332 static int
getsigaction(int signo,sig_t * sigact)333 getsigaction(int signo, sig_t *sigact)
334 {
335 	struct sigaction sa;
336 
337 	if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
338 		return 0;
339 	*sigact = (sig_t) sa.sa_handler;
340 	return 1;
341 }
342 
343 /*
344  * Ignore a signal.
345  */
346 
347 void
ignoresig(int signo,int vforked)348 ignoresig(int signo, int vforked)
349 {
350 	if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN)
351 		bsd_signal(signo, SIG_IGN);
352 	if (!vforked)
353 		sigmode[signo - 1] = S_HARD_IGN;
354 }
355 
356 
357 #ifdef mkinit
358 INCLUDE <signal.h>
359 INCLUDE "trap.h"
360 
361 SHELLPROC {
362 	char *sm;
363 
364 	clear_traps(0);
365 	for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
366 		if (*sm == S_IGN)
367 			*sm = S_HARD_IGN;
368 	}
369 }
370 #endif
371 
372 
373 
374 /*
375  * Signal handler.
376  */
377 
378 void
onsig(int signo)379 onsig(int signo)
380 {
381 	bsd_signal(signo, onsig);
382 	if (signo == SIGINT && trap[SIGINT] == NULL) {
383 		onint();
384 		return;
385 	}
386 	gotsig[signo - 1] = 1;
387 	pendingsigs++;
388 }
389 
390 
391 
392 /*
393  * Called to execute a trap.  Perhaps we should avoid entering new trap
394  * handlers while we are executing a trap handler.
395  */
396 
397 void
dotrap(void)398 dotrap(void)
399 {
400 	int i;
401 	int savestatus;
402 
403 	for (;;) {
404 		for (i = 1 ; ; i++) {
405 			if (gotsig[i - 1])
406 				break;
407 			if (i >= NSIG)
408 				goto done;
409 		}
410 		gotsig[i - 1] = 0;
411 		savestatus=exitstatus;
412 		evalstring(trap[i], 0);
413 		exitstatus=savestatus;
414 	}
415 done:
416 	pendingsigs = 0;
417 }
418 
419 
420 
421 /*
422  * Controls whether the shell is interactive or not.
423  */
424 
425 
426 void
setinteractive(int on)427 setinteractive(int on)
428 {
429 	static int is_interactive;
430 
431 	if (on == is_interactive)
432 		return;
433 	setsignal(SIGINT, 0);
434 	setsignal(SIGQUIT, 0);
435 	setsignal(SIGTERM, 0);
436 	is_interactive = on;
437 }
438 
439 
440 
441 /*
442  * Called to exit the shell.
443  */
444 
445 void
exitshell(int status)446 exitshell(int status)
447 {
448 	struct jmploc loc1, loc2;
449 	char *p;
450 
451 	TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
452 	if (setjmp(loc1.loc)) {
453 		goto l1;
454 	}
455 	if (setjmp(loc2.loc)) {
456 		goto l2;
457 	}
458 	handler = &loc1;
459 	if ((p = trap[0]) != NULL && *p != '\0') {
460 		trap[0] = NULL;
461 		evalstring(p, 0);
462 	}
463 l1:   handler = &loc2;			/* probably unnecessary */
464 	flushall();
465 #if JOBS
466 	setjobctl(0);
467 #endif
468 l2:   _exit(status);
469 	/* NOTREACHED */
470 }
471