VirtualBox

source: kBuild/trunk/src/kash/trap.c@ 2629

Last change on this file since 2629 was 2298, checked in by bird, 16 years ago

kash: no return indicators that works for both gcc and msc (not pretty, but wtf).

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

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