illumos-port-bash/trap.c

750 lines
20 KiB
C
Raw Normal View History

1996-08-27 02:22:31 +08:00
/* trap.c -- Not the trap command, but useful functions for manipulating
those objects. The trap command is in builtins/trap.def. */
/* Copyright (C) 1987, 1991 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any later
version.
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
1996-12-24 01:02:34 +08:00
#include "config.h"
1997-09-23 04:22:27 +08:00
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
1996-08-27 02:22:31 +08:00
#include "bashtypes.h"
1997-06-05 22:59:13 +08:00
#include "bashansi.h"
1996-08-27 02:22:31 +08:00
1998-04-18 03:52:44 +08:00
#include <stdio.h>
#include "trap.h"
1996-08-27 02:22:31 +08:00
#include "shell.h"
#include "signames.h"
1996-12-24 01:02:34 +08:00
#include "builtins/common.h"
1996-08-27 02:22:31 +08:00
/* Flags which describe the current handling state of a signal. */
#define SIG_INHERITED 0x0 /* Value inherited from parent. */
#define SIG_TRAPPED 0x1 /* Currently trapped. */
#define SIG_HARD_IGNORE 0x2 /* Signal was ignored on shell entry. */
#define SIG_SPECIAL 0x4 /* Treat this signal specially. */
#define SIG_NO_TRAP 0x8 /* Signal cannot be trapped. */
1996-12-24 01:02:34 +08:00
#define SIG_INPROGRESS 0x10 /* Signal handler currently executing. */
#define SIG_CHANGED 0x20 /* Trap value changed in trap handler. */
#define SIG_IGNORED 0x40 /* The signal is currently being ignored. */
1996-08-27 02:22:31 +08:00
/* An array of such flags, one for each signal, describing what the
1996-12-24 01:02:34 +08:00
shell will do with a signal. DEBUG_TRAP == NSIG; some code below
assumes this. */
static int sigmodes[NSIG+1];
1996-08-27 02:22:31 +08:00
static void change_signal (), restore_signal ();
/* Variables used here but defined in other files. */
extern int interactive_shell, interactive;
extern int interrupt_immediately;
extern int last_command_exit_value;
1997-06-05 22:59:13 +08:00
extern int line_number;
1996-08-27 02:22:31 +08:00
/* The list of things to do originally, before we started trapping. */
SigHandler *original_signals[NSIG];
/* For each signal, a slot for a string, which is a command to be
executed when that signal is recieved. The slot can also contain
DEFAULT_SIG, which means do whatever you were going to do before
you were so rudely interrupted, or IGNORE_SIG, which says ignore
this signal. */
1996-12-24 01:02:34 +08:00
char *trap_list[NSIG+1];
1996-08-27 02:22:31 +08:00
/* A bitmap of signals received for which we have trap handlers. */
int pending_traps[NSIG];
1996-12-24 01:02:34 +08:00
/* Set to the number of the signal we're running the trap for + 1.
Used in execute_cmd.c and builtins/common.c to clean up when
parse_and_execute does not return normally after executing the
trap command (e.g., when `return' is executed in the trap command). */
int running_trap;
1997-06-05 22:59:13 +08:00
/* The value of line_number when the trap started executing, since
parse_and_execute resets it to 1 and the trap command might want
it. */
int trap_line_number;
1996-08-27 02:22:31 +08:00
/* A value which can never be the target of a trap handler. */
#define IMPOSSIBLE_TRAP_HANDLER (SigHandler *)initialize_traps
void
initialize_traps ()
{
register int i;
1996-12-24 01:02:34 +08:00
trap_list[EXIT_TRAP] = trap_list[DEBUG_TRAP] = (char *)NULL;
sigmodes[EXIT_TRAP] = sigmodes[DEBUG_TRAP] = SIG_INHERITED;
original_signals[EXIT_TRAP] = IMPOSSIBLE_TRAP_HANDLER;
1996-08-27 02:22:31 +08:00
for (i = 1; i < NSIG; i++)
{
pending_traps[i] = 0;
trap_list[i] = (char *)DEFAULT_SIG;
sigmodes[i] = SIG_INHERITED;
original_signals[i] = IMPOSSIBLE_TRAP_HANDLER;
}
/* Show which signals are treated specially by the shell. */
#if defined (SIGCHLD)
1996-12-24 01:02:34 +08:00
original_signals[SIGCHLD] =
(SigHandler *) set_signal_handler (SIGCHLD, SIG_DFL);
1996-08-27 02:22:31 +08:00
set_signal_handler (SIGCHLD, original_signals[SIGCHLD]);
sigmodes[SIGCHLD] |= (SIG_SPECIAL | SIG_NO_TRAP);
#endif /* SIGCHLD */
original_signals[SIGINT] =
(SigHandler *) set_signal_handler (SIGINT, SIG_DFL);
set_signal_handler (SIGINT, original_signals[SIGINT]);
sigmodes[SIGINT] |= SIG_SPECIAL;
1999-02-20 01:11:39 +08:00
#if defined (__BEOS__)
/* BeOS sets SIGINT to SIG_IGN! */
original_signals[SIGINT] = SIG_DFL;
#endif
1996-08-27 02:22:31 +08:00
original_signals[SIGQUIT] =
(SigHandler *) set_signal_handler (SIGQUIT, SIG_DFL);
set_signal_handler (SIGQUIT, original_signals[SIGQUIT]);
sigmodes[SIGQUIT] |= SIG_SPECIAL;
if (interactive)
{
1996-12-24 01:02:34 +08:00
original_signals[SIGTERM] =
(SigHandler *)set_signal_handler (SIGTERM, SIG_DFL);
1996-08-27 02:22:31 +08:00
set_signal_handler (SIGTERM, original_signals[SIGTERM]);
sigmodes[SIGTERM] |= SIG_SPECIAL;
}
}
/* Return the print name of this signal. */
char *
signal_name (sig)
int sig;
{
1998-04-18 03:52:44 +08:00
char *ret;
/* on cygwin32, signal_names[sig] could be null */
ret = (sig > NSIG || sig < 0) ? "bad signal number" : signal_names[sig];
if (ret == NULL)
ret = "unrecognized signal number";
return ret;
1996-08-27 02:22:31 +08:00
}
/* Turn a string into a signal number, or a number into
a signal number. If STRING is "2", "SIGINT", or "INT",
then (int)2 is returned. Return NO_SIG if STRING doesn't
contain a valid signal descriptor. */
int
decode_signal (string)
char *string;
{
1996-12-24 01:02:34 +08:00
long sig;
1996-08-27 02:22:31 +08:00
1996-12-24 01:02:34 +08:00
if (legal_number (string, &sig))
return ((sig >= 0 && sig <= NSIG) ? (int)sig : NO_SIG);
1996-08-27 02:22:31 +08:00
1997-09-23 04:22:27 +08:00
/* A leading `SIG' may be omitted. */
1996-12-24 01:02:34 +08:00
for (sig = 0; sig <= NSIG; sig++)
1999-02-20 01:11:39 +08:00
{
if (signal_names[sig] == 0 || signal_names[sig][0] == '\0')
continue;
if (strcasecmp (string, signal_names[sig]) == 0 ||
(STREQN (signal_names[sig], "SIG", 3) &&
strcasecmp (string, &(signal_names[sig])[3]) == 0))
return ((int)sig);
}
1996-08-27 02:22:31 +08:00
return (NO_SIG);
}
/* Non-zero when we catch a trapped signal. */
1996-12-24 01:02:34 +08:00
static int catch_flag;
1996-08-27 02:22:31 +08:00
void
run_pending_traps ()
{
register int sig;
int old_exit_value;
if (catch_flag == 0) /* simple optimization */
return;
catch_flag = 0;
/* Preserve $? when running trap. */
old_exit_value = last_command_exit_value;
for (sig = 1; sig < NSIG; sig++)
{
/* XXX this could be made into a counter by using
while (pending_traps[sig]--) instead of the if statement. */
if (pending_traps[sig])
{
1996-12-24 01:02:34 +08:00
#if defined (HAVE_POSIX_SIGNALS)
1996-08-27 02:22:31 +08:00
sigset_t set, oset;
sigemptyset (&set);
sigemptyset (&oset);
sigaddset (&set, sig);
sigprocmask (SIG_BLOCK, &set, &oset);
#else
# if defined (HAVE_BSD_SIGNALS)
int oldmask = sigblock (sigmask (sig));
# endif
1996-12-24 01:02:34 +08:00
#endif /* HAVE_POSIX_SIGNALS */
1996-08-27 02:22:31 +08:00
if (sig == SIGINT)
{
run_interrupt_trap ();
1996-12-24 01:02:34 +08:00
CLRINTERRUPT;
1996-08-27 02:22:31 +08:00
}
else
1997-06-05 22:59:13 +08:00
parse_and_execute (savestring (trap_list[sig]), "trap", SEVAL_NONINT|SEVAL_NOHIST);
1996-08-27 02:22:31 +08:00
pending_traps[sig] = 0;
1996-12-24 01:02:34 +08:00
#if defined (HAVE_POSIX_SIGNALS)
1996-08-27 02:22:31 +08:00
sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
#else
# if defined (HAVE_BSD_SIGNALS)
sigsetmask (oldmask);
# endif
#endif /* POSIX_VERSION */
}
}
last_command_exit_value = old_exit_value;
}
sighandler
trap_handler (sig)
int sig;
{
if ((sig >= NSIG) ||
(trap_list[sig] == (char *)DEFAULT_SIG) ||
(trap_list[sig] == (char *)IGNORE_SIG))
1996-12-24 01:02:34 +08:00
programming_error ("trap_handler: bad signal %d", sig);
1996-08-27 02:22:31 +08:00
else
{
1996-12-24 01:02:34 +08:00
#if defined (MUST_REINSTALL_SIGHANDLERS)
1996-08-27 02:22:31 +08:00
set_signal_handler (sig, trap_handler);
1996-12-24 01:02:34 +08:00
#endif /* MUST_REINSTALL_SIGHANDLERS */
1996-08-27 02:22:31 +08:00
catch_flag = 1;
pending_traps[sig]++;
if (interrupt_immediately)
run_pending_traps ();
}
1996-12-24 01:02:34 +08:00
SIGRETURN (0);
1996-08-27 02:22:31 +08:00
}
#if defined (JOB_CONTROL) && defined (SIGCHLD)
1998-04-18 03:52:44 +08:00
#ifdef INCLUDE_UNUSED
1996-08-27 02:22:31 +08:00
/* Make COMMAND_STRING be executed when SIGCHLD is caught. */
void
set_sigchld_trap (command_string)
char *command_string;
{
set_signal (SIGCHLD, command_string);
}
1998-04-18 03:52:44 +08:00
#endif
1996-08-27 02:22:31 +08:00
/* Make COMMAND_STRING be executed when SIGCHLD is caught iff the current
SIGCHLD trap handler is DEFAULT_SIG. */
void
maybe_set_sigchld_trap (command_string)
char *command_string;
{
if ((sigmodes[SIGCHLD] & SIG_TRAPPED) == 0)
set_signal (SIGCHLD, command_string);
}
#endif /* JOB_CONTROL && SIGCHLD */
1996-12-24 01:02:34 +08:00
void
set_debug_trap (command)
1996-08-27 02:22:31 +08:00
char *command;
{
1996-12-24 01:02:34 +08:00
set_signal (DEBUG_TRAP, command);
}
1996-08-27 02:22:31 +08:00
1998-04-18 03:52:44 +08:00
#ifdef INCLUDE_UNUSED
1996-12-24 01:02:34 +08:00
void
set_sigint_trap (command)
char *command;
{
1996-08-27 02:22:31 +08:00
set_signal (SIGINT, command);
}
1998-04-18 03:52:44 +08:00
#endif
1996-08-27 02:22:31 +08:00
/* Reset the SIGINT handler so that subshells that are doing `shellsy'
things, like waiting for command substitution or executing commands
in explicit subshells ( ( cmd ) ), can catch interrupts properly. */
SigHandler *
set_sigint_handler ()
{
if (sigmodes[SIGINT] & SIG_HARD_IGNORE)
return ((SigHandler *)SIG_IGN);
else if (sigmodes[SIGINT] & SIG_IGNORED)
1996-12-24 01:02:34 +08:00
return ((SigHandler *)set_signal_handler (SIGINT, SIG_IGN)); /* XXX */
1996-08-27 02:22:31 +08:00
else if (sigmodes[SIGINT] & SIG_TRAPPED)
return ((SigHandler *)set_signal_handler (SIGINT, trap_handler));
/* The signal is not trapped, so set the handler to the shell's special
interrupt handler. */
else if (interactive) /* XXX - was interactive_shell */
return (set_signal_handler (SIGINT, sigint_sighandler));
else
return (set_signal_handler (SIGINT, termination_unwind_protect));
}
1997-09-23 04:22:27 +08:00
/* Return the correct handler for signal SIG according to the values in
sigmodes[SIG]. */
SigHandler *
trap_to_sighandler (sig)
int sig;
{
if (sigmodes[sig] & (SIG_IGNORED|SIG_HARD_IGNORE))
return (SIG_IGN);
else if (sigmodes[sig] & SIG_TRAPPED)
return (trap_handler);
else
return (SIG_DFL);
}
1996-08-27 02:22:31 +08:00
/* Set SIG to call STRING as a command. */
void
set_signal (sig, string)
int sig;
char *string;
{
1996-12-24 01:02:34 +08:00
if (sig == DEBUG_TRAP || sig == EXIT_TRAP)
{
change_signal (sig, savestring (string));
1997-06-05 22:59:13 +08:00
if (sig == EXIT_TRAP && interactive == 0)
initialize_terminating_signals ();
1996-12-24 01:02:34 +08:00
return;
}
1996-08-27 02:22:31 +08:00
/* A signal ignored on entry to the shell cannot be trapped or reset, but
no error is reported when attempting to do so. -- Posix.2 */
if (sigmodes[sig] & SIG_HARD_IGNORE)
return;
/* Make sure we have original_signals[sig] if the signal has not yet
been trapped. */
if ((sigmodes[sig] & SIG_TRAPPED) == 0)
{
/* If we aren't sure of the original value, check it. */
if (original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER)
{
original_signals[sig] = (SigHandler *)set_signal_handler (sig, SIG_DFL);
set_signal_handler (sig, original_signals[sig]);
}
/* Signals ignored on entry to the shell cannot be trapped or reset. */
if (original_signals[sig] == SIG_IGN)
{
sigmodes[sig] |= SIG_HARD_IGNORE;
return;
}
}
/* Only change the system signal handler if SIG_NO_TRAP is not set.
The trap command string is changed in either case. The shell signal
handlers for SIGINT and SIGCHLD run the user specified traps in an
environment in which it is safe to do so. */
if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
{
set_signal_handler (sig, SIG_IGN);
change_signal (sig, savestring (string));
set_signal_handler (sig, trap_handler);
}
else
change_signal (sig, savestring (string));
}
static void
free_trap_command (sig)
int sig;
{
if ((sigmodes[sig] & SIG_TRAPPED) && trap_list[sig] &&
(trap_list[sig] != (char *)IGNORE_SIG) &&
(trap_list[sig] != (char *)DEFAULT_SIG) &&
(trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER))
free (trap_list[sig]);
}
1996-12-24 01:02:34 +08:00
1996-08-27 02:22:31 +08:00
/* If SIG has a string assigned to it, get rid of it. Then give it
VALUE. */
static void
change_signal (sig, value)
int sig;
char *value;
{
1997-06-05 22:59:13 +08:00
if ((sigmodes[sig] & SIG_INPROGRESS) == 0)
free_trap_command (sig);
1996-08-27 02:22:31 +08:00
trap_list[sig] = value;
sigmodes[sig] |= SIG_TRAPPED;
if (value == (char *)IGNORE_SIG)
sigmodes[sig] |= SIG_IGNORED;
else
sigmodes[sig] &= ~SIG_IGNORED;
if (sigmodes[sig] & SIG_INPROGRESS)
sigmodes[sig] |= SIG_CHANGED;
}
#define GET_ORIGINAL_SIGNAL(sig) \
if (sig && sig < NSIG && original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER) \
get_original_signal (sig)
static void
get_original_signal (sig)
int sig;
{
/* If we aren't sure the of the original value, then get it. */
if (original_signals[sig] == (SigHandler *)IMPOSSIBLE_TRAP_HANDLER)
{
original_signals[sig] =
(SigHandler *) set_signal_handler (sig, SIG_DFL);
set_signal_handler (sig, original_signals[sig]);
/* Signals ignored on entry to the shell cannot be trapped. */
if (original_signals[sig] == SIG_IGN)
sigmodes[sig] |= SIG_HARD_IGNORE;
}
}
/* Restore the default action for SIG; i.e., the action the shell
would have taken before you used the trap command. This is called
from trap_builtin (), which takes care to restore the handlers for
the signals the shell treats specially. */
void
restore_default_signal (sig)
int sig;
{
1996-12-24 01:02:34 +08:00
if (sig == DEBUG_TRAP || sig == EXIT_TRAP)
1996-08-27 02:22:31 +08:00
{
1997-06-05 22:59:13 +08:00
if ((sig != DEBUG_TRAP) || (sigmodes[sig] & SIG_INPROGRESS) == 0)
free_trap_command (sig);
1996-08-27 02:22:31 +08:00
trap_list[sig] = (char *)NULL;
sigmodes[sig] &= ~SIG_TRAPPED;
1997-06-05 22:59:13 +08:00
if (sigmodes[sig] & SIG_INPROGRESS)
sigmodes[sig] |= SIG_CHANGED;
1996-08-27 02:22:31 +08:00
return;
}
GET_ORIGINAL_SIGNAL (sig);
/* A signal ignored on entry to the shell cannot be trapped or reset, but
no error is reported when attempting to do so. Thanks Posix.2. */
if (sigmodes[sig] & SIG_HARD_IGNORE)
return;
/* If we aren't trapping this signal, don't bother doing anything else. */
1996-12-24 01:02:34 +08:00
if ((sigmodes[sig] & SIG_TRAPPED) == 0)
1996-08-27 02:22:31 +08:00
return;
/* Only change the signal handler for SIG if it allows it. */
1996-12-24 01:02:34 +08:00
if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
1996-08-27 02:22:31 +08:00
set_signal_handler (sig, original_signals[sig]);
/* Change the trap command in either case. */
change_signal (sig, (char *)DEFAULT_SIG);
/* Mark the signal as no longer trapped. */
sigmodes[sig] &= ~SIG_TRAPPED;
}
/* Make this signal be ignored. */
void
ignore_signal (sig)
int sig;
{
1996-12-24 01:02:34 +08:00
if ((sig == EXIT_TRAP || sig == DEBUG_TRAP) && ((sigmodes[sig] & SIG_IGNORED) == 0))
{
change_signal (sig, (char *)IGNORE_SIG);
return;
}
1996-08-27 02:22:31 +08:00
GET_ORIGINAL_SIGNAL (sig);
/* A signal ignored on entry to the shell cannot be trapped or reset.
1996-12-24 01:02:34 +08:00
No error is reported when the user attempts to do so. */
1996-08-27 02:22:31 +08:00
if (sigmodes[sig] & SIG_HARD_IGNORE)
return;
/* If already trapped and ignored, no change necessary. */
1996-12-24 01:02:34 +08:00
if (sigmodes[sig] & SIG_IGNORED)
1996-08-27 02:22:31 +08:00
return;
/* Only change the signal handler for SIG if it allows it. */
1996-12-24 01:02:34 +08:00
if ((sigmodes[sig] & SIG_NO_TRAP) == 0)
1996-08-27 02:22:31 +08:00
set_signal_handler (sig, SIG_IGN);
/* Change the trap command in either case. */
change_signal (sig, (char *)IGNORE_SIG);
}
/* Handle the calling of "trap 0". The only sticky situation is when
the command to be executed includes an "exit". This is why we have
to provide our own place for top_level to jump to. */
int
run_exit_trap ()
{
1996-12-24 01:02:34 +08:00
char *trap_command;
int code, old_exit_value;
1996-08-27 02:22:31 +08:00
old_exit_value = last_command_exit_value;
1996-12-24 01:02:34 +08:00
/* Run the trap only if signal 0 is trapped and not ignored, and we are not
currently running in the trap handler (call to exit in the list of
commands given to trap 0). */
if ((sigmodes[EXIT_TRAP] & SIG_TRAPPED) &&
(sigmodes[EXIT_TRAP] & (SIG_IGNORED|SIG_INPROGRESS)) == 0)
1996-08-27 02:22:31 +08:00
{
1996-12-24 01:02:34 +08:00
trap_command = savestring (trap_list[EXIT_TRAP]);
sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED;
sigmodes[EXIT_TRAP] |= SIG_INPROGRESS;
1996-08-27 02:22:31 +08:00
code = setjmp (top_level);
if (code == 0)
1998-04-18 03:52:44 +08:00
{
reset_parser ();
parse_and_execute (trap_command, "exit trap", SEVAL_NONINT|SEVAL_NOHIST);
}
1996-08-27 02:22:31 +08:00
else if (code == EXITPROG)
1996-12-24 01:02:34 +08:00
return (last_command_exit_value);
1996-08-27 02:22:31 +08:00
else
return (old_exit_value);
}
return (old_exit_value);
}
1996-12-24 01:02:34 +08:00
void
run_trap_cleanup (sig)
int sig;
{
sigmodes[sig] &= ~(SIG_INPROGRESS|SIG_CHANGED);
}
/* Run a trap command for SIG. SIG is one of the signals the shell treats
specially. */
1996-08-27 02:22:31 +08:00
static void
1996-12-24 01:02:34 +08:00
_run_trap_internal (sig, tag)
1996-08-27 02:22:31 +08:00
int sig;
1996-12-24 01:02:34 +08:00
char *tag;
1996-08-27 02:22:31 +08:00
{
1996-12-24 01:02:34 +08:00
char *trap_command, *old_trap;
1997-06-05 22:59:13 +08:00
int old_exit_value, old_line_number;
1996-12-24 01:02:34 +08:00
/* Run the trap only if SIG is trapped and not ignored, and we are not
currently executing in the trap handler. */
if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0) &&
(trap_list[sig] != (char *)IMPOSSIBLE_TRAP_HANDLER) &&
((sigmodes[sig] & SIG_INPROGRESS) == 0))
{
old_trap = trap_list[sig];
sigmodes[sig] |= SIG_INPROGRESS;
sigmodes[sig] &= ~SIG_CHANGED; /* just to be sure */
trap_command = savestring (old_trap);
running_trap = sig + 1;
old_exit_value = last_command_exit_value;
1997-06-05 22:59:13 +08:00
/* Need to copy the value of line_number because parse_and_execute
resets it to 1, and the trap command might want it. */
trap_line_number = line_number;
parse_and_execute (trap_command, tag, SEVAL_NONINT|SEVAL_NOHIST);
1996-12-24 01:02:34 +08:00
last_command_exit_value = old_exit_value;
running_trap = 0;
sigmodes[sig] &= ~SIG_INPROGRESS;
if (sigmodes[sig] & SIG_CHANGED)
{
free (old_trap);
sigmodes[sig] &= ~SIG_CHANGED;
}
}
}
void
run_debug_trap ()
{
if ((sigmodes[DEBUG_TRAP] & SIG_TRAPPED) && (sigmodes[DEBUG_TRAP] & SIG_INPROGRESS) == 0)
_run_trap_internal (DEBUG_TRAP, "debug trap");
}
/* Run a trap set on SIGINT. This is called from throw_to_top_level (), and
declared here to localize the trap functions. */
void
run_interrupt_trap ()
{
_run_trap_internal (SIGINT, "interrupt trap");
1996-08-27 02:22:31 +08:00
}
1998-04-18 03:52:44 +08:00
#ifdef INCLUDE_UNUSED
1996-08-27 02:22:31 +08:00
/* Free all the allocated strings in the list of traps and reset the trap
values to the default. */
void
free_trap_strings ()
{
register int i;
1996-12-24 01:02:34 +08:00
for (i = 0; i < NSIG+1; i++)
1996-08-27 02:22:31 +08:00
{
free_trap_command (i);
trap_list[i] = (char *)DEFAULT_SIG;
sigmodes[i] &= ~SIG_TRAPPED;
}
1996-12-24 01:02:34 +08:00
trap_list[DEBUG_TRAP] = trap_list[EXIT_TRAP] = (char *)NULL;
1996-08-27 02:22:31 +08:00
}
1998-04-18 03:52:44 +08:00
#endif
1996-08-27 02:22:31 +08:00
/* Reset the handler for SIG to the original value. */
static void
reset_signal (sig)
int sig;
{
set_signal_handler (sig, original_signals[sig]);
}
1996-12-24 01:02:34 +08:00
/* Set the handler signal SIG to the original and free any trap
command associated with it. */
static void
restore_signal (sig)
int sig;
1996-08-27 02:22:31 +08:00
{
1996-12-24 01:02:34 +08:00
set_signal_handler (sig, original_signals[sig]);
change_signal (sig, (char *)DEFAULT_SIG);
sigmodes[sig] &= ~SIG_TRAPPED;
1996-08-27 02:22:31 +08:00
}
1996-12-24 01:02:34 +08:00
static void
reset_or_restore_signal_handlers (reset)
VFunction *reset;
1996-08-27 02:22:31 +08:00
{
register int i;
1996-12-24 01:02:34 +08:00
/* Take care of the exit trap first */
if (sigmodes[EXIT_TRAP] & SIG_TRAPPED)
1996-08-27 02:22:31 +08:00
{
1996-12-24 01:02:34 +08:00
free_trap_command (EXIT_TRAP);
trap_list[EXIT_TRAP] = (char *)NULL;
sigmodes[EXIT_TRAP] &= ~SIG_TRAPPED;
1996-08-27 02:22:31 +08:00
}
for (i = 1; i < NSIG; i++)
{
1996-12-24 01:02:34 +08:00
if (sigmodes[i] & SIG_TRAPPED)
1996-08-27 02:22:31 +08:00
{
if (trap_list[i] == (char *)IGNORE_SIG)
set_signal_handler (i, SIG_IGN);
else
1996-12-24 01:02:34 +08:00
(*reset) (i);
1996-08-27 02:22:31 +08:00
}
1996-12-24 01:02:34 +08:00
else if (sigmodes[i] & SIG_SPECIAL)
(*reset) (i);
1996-08-27 02:22:31 +08:00
}
}
void
1996-12-24 01:02:34 +08:00
reset_signal_handlers ()
1996-08-27 02:22:31 +08:00
{
1996-12-24 01:02:34 +08:00
reset_or_restore_signal_handlers (reset_signal);
}
1996-08-27 02:22:31 +08:00
1996-12-24 01:02:34 +08:00
/* Reset all trapped signals to their original values. Signals set to be
ignored with trap '' SIGNAL should be ignored, so we make sure that they
are. Called by child processes after they are forked. */
void
restore_original_signals ()
{
reset_or_restore_signal_handlers (restore_signal);
1996-08-27 02:22:31 +08:00
}
/* If a trap handler exists for signal SIG, then call it; otherwise just
return failure. */
int
maybe_call_trap_handler (sig)
int sig;
{
/* Call the trap handler for SIG if the signal is trapped and not ignored. */
1996-12-24 01:02:34 +08:00
if ((sigmodes[sig] & SIG_TRAPPED) && ((sigmodes[sig] & SIG_IGNORED) == 0))
1996-08-27 02:22:31 +08:00
{
switch (sig)
{
case SIGINT:
run_interrupt_trap ();
break;
1996-12-24 01:02:34 +08:00
case EXIT_TRAP:
1996-08-27 02:22:31 +08:00
run_exit_trap ();
break;
1996-12-24 01:02:34 +08:00
case DEBUG_TRAP:
run_debug_trap ();
break;
1996-08-27 02:22:31 +08:00
default:
trap_handler (sig);
break;
}
return (1);
}
else
return (0);
}
int
signal_is_trapped (sig)
int sig;
{
return (sigmodes[sig] & SIG_TRAPPED);
}
int
signal_is_special (sig)
int sig;
{
return (sigmodes[sig] & SIG_SPECIAL);
}
int
signal_is_ignored (sig)
int sig;
{
return (sigmodes[sig] & SIG_IGNORED);
}
void
set_signal_ignored (sig)
int sig;
{
sigmodes[sig] |= SIG_HARD_IGNORE;
1996-12-24 01:02:34 +08:00
original_signals[sig] = SIG_IGN;
1996-08-27 02:22:31 +08:00
}