| 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 | /*
|
|---|
| 83 | * return the signal number described by `p' (as a number or a name)
|
|---|
| 84 | * or -1 if it isn't one
|
|---|
| 85 | */
|
|---|
| 86 |
|
|---|
| 87 | static int
|
|---|
| 88 | signame_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 | */
|
|---|
| 110 | static void
|
|---|
| 111 | printsignals(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 |
|
|---|
| 130 | int
|
|---|
| 131 | trapcmd(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, 0);
|
|---|
| 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 |
|
|---|
| 204 | void
|
|---|
| 205 | clear_traps(shinstance *psh, int vforked)
|
|---|
| 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 | if (!vforked) {
|
|---|
| 213 | ckfree(psh, *tp);
|
|---|
| 214 | *tp = NULL;
|
|---|
| 215 | }
|
|---|
| 216 | if (tp != &psh->trap[0])
|
|---|
| 217 | setsignal(psh, (int)(tp - psh->trap), vforked);
|
|---|
| 218 | INTON;
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 | /*
|
|---|
| 226 | * Set the signal handler for the specified signal. The routine figures
|
|---|
| 227 | * out what it should be set to.
|
|---|
| 228 | */
|
|---|
| 229 |
|
|---|
| 230 | void
|
|---|
| 231 | setsignal(shinstance *psh, int signo, int vforked)
|
|---|
| 232 | {
|
|---|
| 233 | int action;
|
|---|
| 234 | shsig_t sigact = SH_SIG_DFL;
|
|---|
| 235 | char *t, tsig;
|
|---|
| 236 |
|
|---|
| 237 | if ((t = psh->trap[signo]) == NULL)
|
|---|
| 238 | action = S_DFL;
|
|---|
| 239 | else if (*t != '\0')
|
|---|
| 240 | action = S_CATCH;
|
|---|
| 241 | else
|
|---|
| 242 | action = S_IGN;
|
|---|
| 243 | if (psh->rootshell && !vforked && action == S_DFL) {
|
|---|
| 244 | switch (signo) {
|
|---|
| 245 | case SIGINT:
|
|---|
| 246 | if (iflag(psh) || psh->minusc || sflag(psh) == 0)
|
|---|
| 247 | action = S_CATCH;
|
|---|
| 248 | break;
|
|---|
| 249 | case SIGQUIT:
|
|---|
| 250 | #ifdef DEBUG
|
|---|
| 251 | if (debug(psh))
|
|---|
| 252 | break;
|
|---|
| 253 | #endif
|
|---|
| 254 | /* FALLTHROUGH */
|
|---|
| 255 | case SIGTERM:
|
|---|
| 256 | if (iflag(psh))
|
|---|
| 257 | action = S_IGN;
|
|---|
| 258 | break;
|
|---|
| 259 | #if JOBS
|
|---|
| 260 | case SIGTSTP:
|
|---|
| 261 | case SIGTTOU:
|
|---|
| 262 | if (mflag(psh))
|
|---|
| 263 | action = S_IGN;
|
|---|
| 264 | break;
|
|---|
| 265 | #endif
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | t = &psh->sigmode[signo - 1];
|
|---|
| 270 | tsig = *t;
|
|---|
| 271 | if (tsig == 0) {
|
|---|
| 272 | /*
|
|---|
| 273 | * current setting unknown
|
|---|
| 274 | */
|
|---|
| 275 | if (!getsigaction(psh, signo, &sigact)) {
|
|---|
| 276 | /*
|
|---|
| 277 | * Pretend it worked; maybe we should give a warning
|
|---|
| 278 | * here, but other shells don't. We don't alter
|
|---|
| 279 | * sigmode, so that we retry every time.
|
|---|
| 280 | */
|
|---|
| 281 | return;
|
|---|
| 282 | }
|
|---|
| 283 | if (sigact == SH_SIG_IGN) {
|
|---|
| 284 | if (mflag(psh) && (signo == SIGTSTP ||
|
|---|
| 285 | signo == SIGTTIN || signo == SIGTTOU)) {
|
|---|
| 286 | tsig = S_IGN; /* don't hard ignore these */
|
|---|
| 287 | } else
|
|---|
| 288 | tsig = S_HARD_IGN;
|
|---|
| 289 | } else {
|
|---|
| 290 | tsig = S_RESET; /* force to be set */
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 | if (tsig == S_HARD_IGN || tsig == action)
|
|---|
| 294 | return;
|
|---|
| 295 | switch (action) {
|
|---|
| 296 | case S_DFL: sigact = SH_SIG_DFL; break;
|
|---|
| 297 | case S_CATCH: sigact = onsig; break;
|
|---|
| 298 | case S_IGN: sigact = SH_SIG_IGN; break;
|
|---|
| 299 | }
|
|---|
| 300 | if (!vforked)
|
|---|
| 301 | *t = action;
|
|---|
| 302 | sh_siginterrupt(psh, signo, 1);
|
|---|
| 303 | sh_signal(psh, signo, sigact);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | /*
|
|---|
| 307 | * Return the current setting for sig w/o changing it.
|
|---|
| 308 | */
|
|---|
| 309 | static int
|
|---|
| 310 | getsigaction(shinstance *psh, int signo, shsig_t *sigact)
|
|---|
| 311 | {
|
|---|
| 312 | struct shsigaction sa;
|
|---|
| 313 |
|
|---|
| 314 | if (sh_sigaction(psh, signo, NULL, &sa) == -1)
|
|---|
| 315 | return 0;
|
|---|
| 316 | *sigact = (shsig_t)sa.sh_handler;
|
|---|
| 317 | return 1;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /*
|
|---|
| 321 | * Ignore a signal.
|
|---|
| 322 | */
|
|---|
| 323 |
|
|---|
| 324 | void
|
|---|
| 325 | ignoresig(shinstance *psh, int signo, int vforked)
|
|---|
| 326 | {
|
|---|
| 327 | if (psh->sigmode[signo - 1] != S_IGN && psh->sigmode[signo - 1] != S_HARD_IGN) {
|
|---|
| 328 | sh_signal(psh, signo, SH_SIG_IGN);
|
|---|
| 329 | }
|
|---|
| 330 | if (!vforked)
|
|---|
| 331 | psh->sigmode[signo - 1] = S_HARD_IGN;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 | #ifdef mkinit
|
|---|
| 336 | INCLUDE <signal.h>
|
|---|
| 337 | INCLUDE "trap.h"
|
|---|
| 338 |
|
|---|
| 339 | SHELLPROC {
|
|---|
| 340 | char *sm;
|
|---|
| 341 |
|
|---|
| 342 | clear_traps(psh, 0);
|
|---|
| 343 | for (sm = psh->sigmode ; sm < psh->sigmode + NSIG ; sm++) {
|
|---|
| 344 | if (*sm == S_IGN)
|
|---|
| 345 | *sm = S_HARD_IGN;
|
|---|
| 346 | }
|
|---|
| 347 | }
|
|---|
| 348 | #endif
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 | /*
|
|---|
| 353 | * Signal handler.
|
|---|
| 354 | */
|
|---|
| 355 |
|
|---|
| 356 | void
|
|---|
| 357 | onsig(shinstance *psh, int signo)
|
|---|
| 358 | {
|
|---|
| 359 | sh_signal(psh, signo, onsig);
|
|---|
| 360 | if (signo == SIGINT && psh->trap[SIGINT] == NULL) {
|
|---|
| 361 | onint(psh);
|
|---|
| 362 | return;
|
|---|
| 363 | }
|
|---|
| 364 | psh->gotsig[signo - 1] = 1;
|
|---|
| 365 | psh->pendingsigs++;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 | /*
|
|---|
| 371 | * Called to execute a trap. Perhaps we should avoid entering new trap
|
|---|
| 372 | * handlers while we are executing a trap handler.
|
|---|
| 373 | */
|
|---|
| 374 |
|
|---|
| 375 | void
|
|---|
| 376 | dotrap(shinstance *psh)
|
|---|
| 377 | {
|
|---|
| 378 | int i;
|
|---|
| 379 | int savestatus;
|
|---|
| 380 |
|
|---|
| 381 | for (;;) {
|
|---|
| 382 | for (i = 1 ; ; i++) {
|
|---|
| 383 | if (psh->gotsig[i - 1])
|
|---|
| 384 | break;
|
|---|
| 385 | if (i >= NSIG)
|
|---|
| 386 | goto done;
|
|---|
| 387 | }
|
|---|
| 388 | psh->gotsig[i - 1] = 0;
|
|---|
| 389 | savestatus=psh->exitstatus;
|
|---|
| 390 | evalstring(psh, psh->trap[i], 0);
|
|---|
| 391 | psh->exitstatus=savestatus;
|
|---|
| 392 | }
|
|---|
| 393 | done:
|
|---|
| 394 | psh->pendingsigs = 0;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 | /*
|
|---|
| 400 | * Controls whether the shell is interactive or not.
|
|---|
| 401 | */
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 | void
|
|---|
| 405 | setinteractive(shinstance *psh, int on)
|
|---|
| 406 | {
|
|---|
| 407 | static int is_interactive;
|
|---|
| 408 |
|
|---|
| 409 | if (on == is_interactive)
|
|---|
| 410 | return;
|
|---|
| 411 | setsignal(psh, SIGINT, 0);
|
|---|
| 412 | setsignal(psh, SIGQUIT, 0);
|
|---|
| 413 | setsignal(psh, SIGTERM, 0);
|
|---|
| 414 | is_interactive = on;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 | /*
|
|---|
| 420 | * Called to exit the shell.
|
|---|
| 421 | */
|
|---|
| 422 |
|
|---|
| 423 | SH_NORETURN_1 void
|
|---|
| 424 | exitshell(shinstance *psh, int status)
|
|---|
| 425 | {
|
|---|
| 426 | struct jmploc loc1, loc2;
|
|---|
| 427 | char *p;
|
|---|
| 428 |
|
|---|
| 429 | TRACE((psh, "pid %d, exitshell(%d)\n", sh_getpid(psh), status));
|
|---|
| 430 | if (setjmp(loc1.loc)) {
|
|---|
| 431 | goto l1;
|
|---|
| 432 | }
|
|---|
| 433 | if (setjmp(loc2.loc)) {
|
|---|
| 434 | goto l2;
|
|---|
| 435 | }
|
|---|
| 436 | psh->handler = &loc1;
|
|---|
| 437 | if ((p = psh->trap[0]) != NULL && *p != '\0') {
|
|---|
| 438 | psh->trap[0] = NULL;
|
|---|
| 439 | evalstring(psh, p, 0);
|
|---|
| 440 | }
|
|---|
| 441 | l1: psh->handler = &loc2; /* probably unnecessary */
|
|---|
| 442 | output_flushall(psh);
|
|---|
| 443 | #if JOBS
|
|---|
| 444 | setjobctl(psh, 0);
|
|---|
| 445 | #endif
|
|---|
| 446 | l2: sh__exit(psh, status);
|
|---|
| 447 | /* NOTREACHED */
|
|---|
| 448 | }
|
|---|