VirtualBox

source: kBuild/trunk/src/ash/trap.c@ 630

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

Made it build on linux.

  • Property svn:eol-style set to native
File size: 8.9 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#include <sys/cdefs.h>
36#ifndef lint
37#if 0
38static 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#ifndef HAVE_SYS_SIGNAME
64extern void init_sys_signame(void);
65extern char sys_signame[NSIG][16];
66#endif
67
68/*
69 * Sigmode records the current value of the signal handlers for the various
70 * modes. A value of zero means that the current handler is not known.
71 * S_HARD_IGN indicates that the signal was ignored on entry to the shell,
72 */
73
74#define S_DFL 1 /* default signal handling (SIG_DFL) */
75#define S_CATCH 2 /* signal is caught */
76#define S_IGN 3 /* signal is ignored (SIG_IGN) */
77#define S_HARD_IGN 4 /* signal is ignored permenantly */
78#define S_RESET 5 /* temporary - to reset a hard ignored sig */
79
80
81char *trap[NSIG+1]; /* trap handler commands */
82MKINIT char sigmode[NSIG]; /* current value of signal */
83char gotsig[NSIG]; /* indicates specified signal received */
84int pendingsigs; /* indicates some signal received */
85
86static int getsigaction(int, sig_t *);
87
88/*
89 * return the signal number described by `p' (as a number or a name)
90 * or -1 if it isn't one
91 */
92
93static int
94signame_to_signum(const char *p)
95{
96 int i;
97
98 if (is_number(p))
99 return number(p);
100
101 if (strcasecmp(p, "exit") == 0 )
102 return 0;
103
104 if (strncasecmp(p, "sig", 3) == 0)
105 p += 3;
106
107#ifndef HAVE_SYS_SIGNAME
108 init_sys_signame();
109#endif
110 for (i = 0; i < NSIG; ++i)
111 if (strcasecmp (p, sys_signame[i]) == 0)
112 return i;
113 return -1;
114}
115
116/*
117 * Print a list of valid signal names
118 */
119static void
120printsignals(void)
121{
122 int n;
123
124 out1str("EXIT ");
125#ifndef HAVE_SYS_SIGNAME
126 init_sys_signame();
127#endif
128
129 for (n = 1; n < NSIG; n++) {
130 out1fmt("%s", sys_signame[n]);
131 if ((n == NSIG/2) || n == (NSIG - 1))
132 out1str("\n");
133 else
134 out1c(' ');
135 }
136}
137
138/*
139 * The trap builtin.
140 */
141
142int
143trapcmd(int argc, char **argv)
144{
145 char *action;
146 char **ap;
147 int signo;
148#ifndef HAVE_SYS_SIGNAME
149 init_sys_signame();
150#endif
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
219void
220clear_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
245long
246setsignal(int signo, int vforked)
247{
248 int action;
249 sig_t sigact = SIG_DFL;
250 char *t, tsig;
251
252 if ((t = trap[signo]) == NULL)
253 action = S_DFL;
254 else if (*t != '\0')
255 action = S_CATCH;
256 else
257 action = S_IGN;
258 if (rootshell && !vforked && action == S_DFL) {
259 switch (signo) {
260 case SIGINT:
261 if (iflag || minusc || sflag == 0)
262 action = S_CATCH;
263 break;
264 case SIGQUIT:
265#ifdef DEBUG
266 if (debug)
267 break;
268#endif
269 /* FALLTHROUGH */
270 case SIGTERM:
271 if (iflag)
272 action = S_IGN;
273 break;
274#if JOBS
275 case SIGTSTP:
276 case SIGTTOU:
277 if (mflag)
278 action = S_IGN;
279 break;
280#endif
281 }
282 }
283
284 t = &sigmode[signo - 1];
285 tsig = *t;
286 if (tsig == 0) {
287 /*
288 * current setting unknown
289 */
290 if (!getsigaction(signo, &sigact)) {
291 /*
292 * Pretend it worked; maybe we should give a warning
293 * here, but other shells don't. We don't alter
294 * sigmode, so that we retry every time.
295 */
296 return 0;
297 }
298 if (sigact == SIG_IGN) {
299 if (mflag && (signo == SIGTSTP ||
300 signo == SIGTTIN || signo == SIGTTOU)) {
301 tsig = S_IGN; /* don't hard ignore these */
302 } else
303 tsig = S_HARD_IGN;
304 } else {
305 tsig = S_RESET; /* force to be set */
306 }
307 }
308 if (tsig == S_HARD_IGN || tsig == action)
309 return 0;
310 switch (action) {
311 case S_DFL: sigact = SIG_DFL; break;
312 case S_CATCH: sigact = onsig; break;
313 case S_IGN: sigact = SIG_IGN; break;
314 }
315 if (!vforked)
316 *t = action;
317 siginterrupt(signo, 1);
318 return (long)signal(signo, sigact);
319}
320
321/*
322 * Return the current setting for sig w/o changing it.
323 */
324static int
325getsigaction(int signo, sig_t *sigact)
326{
327 struct sigaction sa;
328
329 if (sigaction(signo, (struct sigaction *)0, &sa) == -1)
330 return 0;
331 *sigact = (sig_t) sa.sa_handler;
332 return 1;
333}
334
335/*
336 * Ignore a signal.
337 */
338
339void
340ignoresig(int signo, int vforked)
341{
342 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
343 signal(signo, SIG_IGN);
344 }
345 if (!vforked)
346 sigmode[signo - 1] = S_HARD_IGN;
347}
348
349
350#ifdef mkinit
351INCLUDE <signal.h>
352INCLUDE "trap.h"
353
354SHELLPROC {
355 char *sm;
356
357 clear_traps(0);
358 for (sm = sigmode ; sm < sigmode + NSIG ; sm++) {
359 if (*sm == S_IGN)
360 *sm = S_HARD_IGN;
361 }
362}
363#endif
364
365
366
367/*
368 * Signal handler.
369 */
370
371void
372onsig(int signo)
373{
374 signal(signo, onsig);
375 if (signo == SIGINT && trap[SIGINT] == NULL) {
376 onint();
377 return;
378 }
379 gotsig[signo - 1] = 1;
380 pendingsigs++;
381}
382
383
384
385/*
386 * Called to execute a trap. Perhaps we should avoid entering new trap
387 * handlers while we are executing a trap handler.
388 */
389
390void
391dotrap(void)
392{
393 int i;
394 int savestatus;
395
396 for (;;) {
397 for (i = 1 ; ; i++) {
398 if (gotsig[i - 1])
399 break;
400 if (i >= NSIG)
401 goto done;
402 }
403 gotsig[i - 1] = 0;
404 savestatus=exitstatus;
405 evalstring(trap[i], 0);
406 exitstatus=savestatus;
407 }
408done:
409 pendingsigs = 0;
410}
411
412
413
414/*
415 * Controls whether the shell is interactive or not.
416 */
417
418
419void
420setinteractive(int on)
421{
422 static int is_interactive;
423
424 if (on == is_interactive)
425 return;
426 setsignal(SIGINT, 0);
427 setsignal(SIGQUIT, 0);
428 setsignal(SIGTERM, 0);
429 is_interactive = on;
430}
431
432
433
434/*
435 * Called to exit the shell.
436 */
437
438void
439exitshell(int status)
440{
441 struct jmploc loc1, loc2;
442 char *p;
443
444 TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
445 if (setjmp(loc1.loc)) {
446 goto l1;
447 }
448 if (setjmp(loc2.loc)) {
449 goto l2;
450 }
451 handler = &loc1;
452 if ((p = trap[0]) != NULL && *p != '\0') {
453 trap[0] = NULL;
454 evalstring(p, 0);
455 }
456l1: handler = &loc2; /* probably unnecessary */
457 output_flushall();
458#if JOBS
459 setjobctl(0);
460#endif
461l2: _exit(status);
462 /* NOTREACHED */
463}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette