source: trunk/src/kash/trap.c@ 1208

Last change on this file since 1208 was 1208, checked in by bird, 18 years ago

trap.c ++.

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