| 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
|
|---|
| 37 | static 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 |
|
|---|
| 80 | static int getsigaction(shinstance *, int, shsig_t *);
|
|---|
| 81 |
|
|---|
| 82 | #ifndef SH_FORKED_MODE
|
|---|
| 83 | void
|
|---|
| 84 | subshellinittrap(shinstance *psh, shinstance *inherit)
|
|---|
| 85 | {
|
|---|
| 86 | /* The forkchild calls clear_traps(), we have to emulate that here. */
|
|---|
| 87 | unsigned i;
|
|---|
| 88 | memcpy(psh->sigmode, inherit->sigmode, sizeof(psh->sigmode));
|
|---|
| 89 | for (i = 0; i < K_ELEMENTS(inherit->trap); i++) {
|
|---|
| 90 | const char *src = inherit->trap[i];
|
|---|
| 91 | if (!src) {
|
|---|
| 92 | } else if (i > 0) {
|
|---|
| 93 | setsignal(psh, i);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | #endif
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | /*
|
|---|
| 101 | * return the signal number described by `p' (as a number or a name)
|
|---|
| 102 | * or -1 if it isn't one
|
|---|
| 103 | */
|
|---|
| 104 |
|
|---|
| 105 | static int
|
|---|
| 106 | signame_to_signum(shinstance *psh, const char *p)
|
|---|
| 107 | {
|
|---|
| 108 | int i;
|
|---|
| 109 |
|
|---|
| 110 | if (is_number(p))
|
|---|
| 111 | return number(psh, p);
|
|---|
| 112 |
|
|---|
| 113 | if (strcasecmp(p, "exit") == 0 )
|
|---|
| 114 | return 0;
|
|---|
| 115 |
|
|---|
| 116 | if (strncasecmp(p, "sig", 3) == 0)
|
|---|
| 117 | p += 3;
|
|---|
| 118 |
|
|---|
| 119 | for (i = 0; i < NSIG; ++i)
|
|---|
| 120 | if (strcasecmp(p, sys_signame[i]) == 0)
|
|---|
| 121 | return i;
|
|---|
| 122 | return -1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /*
|
|---|
| 126 | * Print a list of valid signal names
|
|---|
| 127 | */
|
|---|
| 128 | static void
|
|---|
| 129 | printsignals(shinstance *psh)
|
|---|
| 130 | {
|
|---|
| 131 | int n;
|
|---|
| 132 |
|
|---|
| 133 | out1str(psh, "EXIT ");
|
|---|
| 134 |
|
|---|
| 135 | for (n = 1; n < NSIG; n++) {
|
|---|
| 136 | out1fmt(psh, "%s", sys_signame[n]);
|
|---|
| 137 | if ((n == NSIG/2) || n == (NSIG - 1))
|
|---|
| 138 | out1str(psh, "\n");
|
|---|
| 139 | else
|
|---|
| 140 | out1c(psh, ' ');
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*
|
|---|
| 145 | * The trap builtin.
|
|---|
| 146 | */
|
|---|
| 147 |
|
|---|
| 148 | int
|
|---|
| 149 | trapcmd(shinstance *psh, int argc, char **argv)
|
|---|
| 150 | {
|
|---|
| 151 | char *action;
|
|---|
| 152 | char **ap;
|
|---|
| 153 | int signo;
|
|---|
| 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(psh, action);
|
|---|
| 200 |
|
|---|
| 201 | if (psh->trap[signo])
|
|---|
| 202 | ckfree(psh, psh->trap[signo]);
|
|---|
| 203 |
|
|---|
| 204 | psh->trap[signo] = action;
|
|---|
| 205 |
|
|---|
| 206 | if (signo != 0)
|
|---|
| 207 | setsignal(psh, signo);
|
|---|
| 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 |
|
|---|
| 222 | void
|
|---|
| 223 | clear_traps(shinstance *psh)
|
|---|
| 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 | ckfree(psh, *tp);
|
|---|
| 231 | *tp = NULL;
|
|---|
| 232 | if (tp != &psh->trap[0])
|
|---|
| 233 | setsignal(psh, (int)(tp - psh->trap));
|
|---|
| 234 | INTON;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | /*
|
|---|
| 242 | * Set the signal handler for the specified signal. The routine figures
|
|---|
| 243 | * out what it should be set to.
|
|---|
| 244 | */
|
|---|
| 245 |
|
|---|
| 246 | void
|
|---|
| 247 | setsignal(shinstance *psh, int signo)
|
|---|
| 248 | {
|
|---|
| 249 | int action;
|
|---|
| 250 | shsig_t sigact = SH_SIG_DFL;
|
|---|
| 251 | char *t, tsig;
|
|---|
| 252 |
|
|---|
| 253 | if ((t = psh->trap[signo]) == NULL)
|
|---|
| 254 | action = S_DFL;
|
|---|
| 255 | else if (*t != '\0')
|
|---|
| 256 | action = S_CATCH;
|
|---|
| 257 | else
|
|---|
| 258 | action = S_IGN;
|
|---|
| 259 | if (psh->rootshell && action == S_DFL) {
|
|---|
| 260 | switch (signo) {
|
|---|
| 261 | case SIGINT:
|
|---|
| 262 | if (iflag(psh) || psh->minusc || sflag(psh) == 0)
|
|---|
| 263 | action = S_CATCH;
|
|---|
| 264 | break;
|
|---|
| 265 | case SIGQUIT:
|
|---|
| 266 | #ifdef DEBUG
|
|---|
| 267 | if (debug(psh))
|
|---|
| 268 | break;
|
|---|
| 269 | #endif
|
|---|
| 270 | /* FALLTHROUGH */
|
|---|
| 271 | case SIGTERM:
|
|---|
| 272 | if (iflag(psh))
|
|---|
| 273 | action = S_IGN;
|
|---|
| 274 | break;
|
|---|
| 275 | #if JOBS
|
|---|
| 276 | case SIGTSTP:
|
|---|
| 277 | case SIGTTOU:
|
|---|
| 278 | if (mflag(psh))
|
|---|
| 279 | action = S_IGN;
|
|---|
| 280 | break;
|
|---|
| 281 | #endif
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | t = &psh->sigmode[signo - 1];
|
|---|
| 286 | tsig = *t;
|
|---|
| 287 | if (tsig == 0) {
|
|---|
| 288 | /*
|
|---|
| 289 | * current setting unknown
|
|---|
| 290 | */
|
|---|
| 291 | if (!getsigaction(psh, 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;
|
|---|
| 298 | }
|
|---|
| 299 | if (sigact == SH_SIG_IGN) {
|
|---|
| 300 | if (mflag(psh) && (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;
|
|---|
| 311 | switch (action) {
|
|---|
| 312 | case S_DFL: sigact = SH_SIG_DFL; break;
|
|---|
| 313 | case S_CATCH: sigact = onsig; break;
|
|---|
| 314 | case S_IGN: sigact = SH_SIG_IGN; break;
|
|---|
| 315 | }
|
|---|
| 316 | *t = action;
|
|---|
| 317 | sh_siginterrupt(psh, signo, 1);
|
|---|
| 318 | sh_signal(psh, signo, sigact);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /*
|
|---|
| 322 | * Return the current setting for sig w/o changing it.
|
|---|
| 323 | */
|
|---|
| 324 | static int
|
|---|
| 325 | getsigaction(shinstance *psh, int signo, shsig_t *sigact)
|
|---|
| 326 | {
|
|---|
| 327 | struct shsigaction sa;
|
|---|
| 328 |
|
|---|
| 329 | if (sh_sigaction(psh, signo, NULL, &sa) == -1)
|
|---|
| 330 | return 0;
|
|---|
| 331 | *sigact = (shsig_t)sa.sh_handler;
|
|---|
| 332 | return 1;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /*
|
|---|
| 336 | * Ignore a signal.
|
|---|
| 337 | */
|
|---|
| 338 |
|
|---|
| 339 | void
|
|---|
| 340 | ignoresig(shinstance *psh, int signo)
|
|---|
| 341 | {
|
|---|
| 342 | if (psh->sigmode[signo - 1] != S_IGN && psh->sigmode[signo - 1] != S_HARD_IGN) {
|
|---|
| 343 | sh_signal(psh, signo, SH_SIG_IGN);
|
|---|
| 344 | }
|
|---|
| 345 | psh->sigmode[signo - 1] = S_HARD_IGN;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 | #ifdef mkinit
|
|---|
| 350 | INCLUDE <signal.h>
|
|---|
| 351 | INCLUDE "trap.h"
|
|---|
| 352 |
|
|---|
| 353 | SHELLPROC {
|
|---|
| 354 | char *sm;
|
|---|
| 355 |
|
|---|
| 356 | clear_traps(psh);
|
|---|
| 357 | for (sm = psh->sigmode ; sm < psh->sigmode + NSIG ; sm++) {
|
|---|
| 358 | if (*sm == S_IGN)
|
|---|
| 359 | *sm = S_HARD_IGN;
|
|---|
| 360 | }
|
|---|
| 361 | }
|
|---|
| 362 | #endif
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 | /*
|
|---|
| 367 | * Signal handler.
|
|---|
| 368 | */
|
|---|
| 369 |
|
|---|
| 370 | void
|
|---|
| 371 | onsig(shinstance *psh, int signo)
|
|---|
| 372 | {
|
|---|
| 373 | sh_signal(psh, signo, onsig);
|
|---|
| 374 | if (signo == SIGINT && psh->trap[SIGINT] == NULL) {
|
|---|
| 375 | onint(psh);
|
|---|
| 376 | return;
|
|---|
| 377 | }
|
|---|
| 378 | psh->gotsig[signo - 1] = 1;
|
|---|
| 379 | psh->pendingsigs++;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 | /*
|
|---|
| 385 | * Called to execute a trap. Perhaps we should avoid entering new trap
|
|---|
| 386 | * handlers while we are executing a trap handler.
|
|---|
| 387 | */
|
|---|
| 388 |
|
|---|
| 389 | void
|
|---|
| 390 | dotrap(shinstance *psh)
|
|---|
| 391 | {
|
|---|
| 392 | int i;
|
|---|
| 393 | int savestatus;
|
|---|
| 394 |
|
|---|
| 395 | for (;;) {
|
|---|
| 396 | for (i = 1 ; ; i++) {
|
|---|
| 397 | if (psh->gotsig[i - 1])
|
|---|
| 398 | break;
|
|---|
| 399 | if (i >= NSIG)
|
|---|
| 400 | goto done;
|
|---|
| 401 | }
|
|---|
| 402 | psh->gotsig[i - 1] = 0;
|
|---|
| 403 | savestatus=psh->exitstatus;
|
|---|
| 404 | evalstring(psh, psh->trap[i], 0);
|
|---|
| 405 | psh->exitstatus=savestatus;
|
|---|
| 406 | }
|
|---|
| 407 | done:
|
|---|
| 408 | psh->pendingsigs = 0;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 | /*
|
|---|
| 414 | * Controls whether the shell is interactive or not.
|
|---|
| 415 | */
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 | void
|
|---|
| 419 | setinteractive(shinstance *psh, int on)
|
|---|
| 420 | {
|
|---|
| 421 | static int is_interactive;
|
|---|
| 422 |
|
|---|
| 423 | if (on == is_interactive)
|
|---|
| 424 | return;
|
|---|
| 425 | setsignal(psh, SIGINT);
|
|---|
| 426 | setsignal(psh, SIGQUIT);
|
|---|
| 427 | setsignal(psh, SIGTERM);
|
|---|
| 428 | is_interactive = on;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 | /*
|
|---|
| 434 | * Called to exit the shell.
|
|---|
| 435 | */
|
|---|
| 436 |
|
|---|
| 437 | int
|
|---|
| 438 | exitshell2(shinstance *psh, int status)
|
|---|
| 439 | {
|
|---|
| 440 | struct jmploc loc1, loc2;
|
|---|
| 441 | char *p;
|
|---|
| 442 |
|
|---|
| 443 | TRACE((psh, "pid %" SHPID_PRI ", exitshell(%d)\n", sh_getpid(psh), status));
|
|---|
| 444 | if (setjmp(loc1.loc)) {
|
|---|
| 445 | goto l1;
|
|---|
| 446 | }
|
|---|
| 447 | if (setjmp(loc2.loc)) {
|
|---|
| 448 | goto l2;
|
|---|
| 449 | }
|
|---|
| 450 | psh->handler = &loc1;
|
|---|
| 451 | if ((p = psh->trap[0]) != NULL && *p != '\0') {
|
|---|
| 452 | psh->trap[0] = NULL;
|
|---|
| 453 | evalstring(psh, p, 0);
|
|---|
| 454 | }
|
|---|
| 455 | l1:
|
|---|
| 456 | psh->handler = &loc2; /* probably unnecessary */
|
|---|
| 457 | output_flushall(psh);
|
|---|
| 458 | #if JOBS
|
|---|
| 459 | setjobctl(psh, 0);
|
|---|
| 460 | #endif
|
|---|
| 461 | l2:
|
|---|
| 462 | return status;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | SH_NORETURN_1 void
|
|---|
| 466 | exitshell(shinstance *psh, int status)
|
|---|
| 467 | {
|
|---|
| 468 | sh__exit(psh, exitshell2(psh, status));
|
|---|
| 469 | /* NOTREACHED */
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|