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

Last change on this file since 3435 was 3435, checked in by bird, 5 years ago

kash: Remove vfork code, we've never used it and we wont need it if we replace fork() with pthreads.

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