summaryrefslogtreecommitdiff
path: root/src/emacs.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2012-09-23 01:44:20 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2012-09-23 01:44:20 -0700
commit4d7e6e51dd4acecff466a28d958c50f34fc130b8 (patch)
tree5e340d48fb69f9a40a4304cc94db2006caefe51f /src/emacs.c
parent8f4635e97e1587c4026ec83fc1bd9f8954775915 (diff)
downloademacs-4d7e6e51dd4acecff466a28d958c50f34fc130b8.tar.gz
Simplify and avoid signal-handling races.
* nt/inc/ms-w32.h (emacs_raise): New macro. * src/alloc.c (die): * src/sysdep.c (emacs_abort) [HAVE_NTGUI]: Avoid recursive loop if there's a fatal error in the function itself. * src/atimer.c (pending_atimers): * src/blockinput.h: Don't include "atimer.h"; no longer needed. (interrupt_input_pending): Remove. All uses removed. pending_signals now counts both atimers and ordinary interrupts. This is less racy than having three separate pending-signal flags. (block_input, unblock_input, totally_unblock_input, unblock_input_to) (input_blocked_p): Rename from their upper-case counterparts BLOCK_INPUT, UNBLOCK_INPUT, TOTALLY_UNBLOCK_INPUT, UNBLOCK_INPUT_TO, INPUT_BLOCKED_P, and turn into functions. All uses changed. This makes it easier to access volatile variables more accurately. (BLOCK_INPUT_RESIGNAL): Remove. All uses replaced by unblock_input (). (input_blocked_p): Prefer this to 'interrupt_input_blocked', as that's more reliable if the code is buggy and sets interrupt_input_blocked to a negative value. All uses changed. * src/atimer.c (deliver_alarm_signal): Remove. No need to deliver this to the parent; any thread can handle this signal now. All uses replaced by underlying handler. * src/atimer.c (turn_on_atimers): * src/dispnew.c (handle_window_change_signal): * src/emacs.c (handle_danger_signal): * src/keyboard.c (kbd_buffer_get_event): Don't reestablish signal handler; not needed with sigaction. * src/blockinput.h (UNBLOCK_INPUT_TO, TOTALLY_UNBLOCK_INPUT) (UNBLOCK_INPUT_TO): Rework to avoid unnecessary accesses to volatile variables. (UNBLOCK_INPUT_TO): Now a function. (totally_unblock_input, unblock_input): New decls. * src/data.c (handle_arith_signal, deliver_arith_signal): Move to sysdep.c (init_data): Remove. Necessary stuff now done in init_signal. * src/emacs.c, src/xdisp.c: Include "atimer.h", since we invoke atimer functions. * src/emacs.c (handle_fatal_signal, deliver_fatal_signal): Move to sysdep.c. (fatal_error_code): Remove; no longer needed. (terminate_due_to_signal): Rename from fatal_error_backtrace, since it doesn't always backtrace. All uses changed. No need to reset signal to default, since sigaction and/or die does that for us now. Use emacs_raise (FOO), not kill (getpid (), FOO). (main): Check more-accurately whether we're dumping. Move fatal-error setup to sysdep.c * src/floatfns.c: Do not include "syssignal.h"; no longer needed. * src/gtkutil.c (xg_get_file_name, xg_get_font): Remove no-longer-needed signal-mask manipulation. * src/keyboard.c, src/process.c (POLL_FOR_INPUT): Don't depend on USE_ASYNC_EVENTS, a symbol that is never defined. * src/keyboard.c (read_avail_input): Remove. All uses replaced by gobble_input. (Ftop_level): Use TOTALLY_UNBLOCK_INPUT rather than open code. (kbd_buffer_store_event_hold, gobble_input): (record_asynch_buffer_change) [USABLE_SIGIO]: (store_user_signal_events): No need to mess with signal mask. (gobble_input): If blocking input and there are terminals, simply set pending_signals to 1 and return. All hooks changed to not worry about whether input is blocked. (process_pending_signals): Clear pending_signals before processing them, in case a signal comes in while we're processing. By convention callers now test pending_signals before calling us. (UNBLOCK_INPUT_TO, unblock_input, totally_unblock_input): New functions, to support changes to blockinput.h. (handle_input_available_signal): Now extern. (reinvoke_input_signal): Remove. All uses replaced by handle_async_input. (quit_count): Now volatile, since a signal handler uses it. (handle_interrupt): Now takes bool IN_SIGNAL_HANDLER as arg. All callers changed. Block SIGINT only if not already blocked. Clear sigmask reliably, even if Fsignal returns, which it can. Omit unnecessary accesses to volatile var. (quit_throw_to_read_char): No need to restore sigmask. * src/keyboard.c (gobble_input, handle_user_signal): * src/process.c (wait_reading_process_output): Call signal-handling code rather than killing ourselves. * src/lisp.h: Include <float.h>, for... (IEEE_FLOATING_POINT): New macro, moved here to avoid duplication. (pending_signals): Now volatile. (syms_of_data): Now const if IEEE floating point. (handle_input_available_signal) [USABLE_SIGIO]: (terminate_due_to_signal, record_child_status_change): New decls. * src/process.c (create_process): Avoid disaster if memory is exhausted while we're processing a vfork, by tightening the critical section around the vfork. (send_process_frame, process_sent_to, handle_pipe_signal) (deliver_pipe_signal): Remove. No longer needed, as Emacs now ignores SIGPIPE. (send_process): No need for setjmp/longjmp any more, since the SIGPIPE stuff is now gone. Instead, report an error if errno is EPIPE. (record_child_status_change): Now extern. PID and W are now args. Return void, not bool. All callers changed. * src/sysdep.c (wait_debugging) [(BSD_SYSTEM || HPUX) && !defined (__GNU__)]: Remove. All uses removed. This bug should be fixed now in a different way. (wait_for_termination_1): Use waitpid rather than sigsuspend, and record the child status change directly. This avoids the need to futz with the signal mask. (process_fatal_action): Move here from emacs.c. (emacs_sigaction_flags): New function, containing much of what used to be in emacs_sigaction_init. (emacs_sigaction_init): Use it. Block nonfatal system signals that are caught by emacs, to make races less likely. (deliver_process_signal): Rename from handle_on_main_thread. All uses changed. (BACKTRACE_LIMIT_MAX): Now at top level. (thread_backtrace_buffer, threadback_backtrace_pointers): New static vars. (deliver_thread_signal, deliver_fatal_thread_signal): New functions, for more-accurate delivery of thread-specific signals. (handle_fatal_signal, deliver_fatal_signal): Move here from emacs.c. (deliver_arith_signal): Handle in this thread, not in the main thread, since it's triggered by this thread. (maybe_fatal_sig): New function. (init_signals): New arg DUMPING so that we can be more accurate about whether we're dumping. Caller changed. Treat thread-specific signals differently from process-general signals. Block all signals while handling fatal error; that's safer. xsignal from SIGFPE only on non-IEEE hosts, treating it as fatal on IEEE hosts. When batch, ignore SIGHUP, SIGINT, SIGTERM if they were already ignored. Ignore SIGPIPE unless batch. (emacs_backtrace): Output backtrace for the appropriate thread, which is not necessarily the main thread. * src/syssignal.h: Include <stdbool.h>. (emacs_raise): New macro. * src/xterm.c (x_connection_signal): Remove; no longer needed now that we use sigaction. (x_connection_closed): No need to mess with sigmask now. (x_initialize): No need to reset SIGPIPE handler here, since init_signals does this for us now. Fixes: debbugs:12471
Diffstat (limited to 'src/emacs.c')
-rw-r--r--src/emacs.c168
1 files changed, 14 insertions, 154 deletions
diff --git a/src/emacs.c b/src/emacs.c
index 52f38925b32..eb83a0ae9a8 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -54,6 +54,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include "window.h"
#include "systty.h"
+#include "atimer.h"
#include "blockinput.h"
#include "syssignal.h"
#include "process.h"
@@ -269,9 +270,6 @@ Report bugs to bug-gnu-emacs@gnu.org. First, please see the Bugs\n\
section of the Emacs manual or the file BUGS.\n"
-/* Signal code for the fatal signal that was received. */
-static int fatal_error_code;
-
/* True if handling a fatal error already. */
bool fatal_error_in_progress;
@@ -282,28 +280,12 @@ static void *ns_pool;
-/* Handle bus errors, invalid instruction, etc. */
-static void
-handle_fatal_signal (int sig)
-{
- fatal_error_backtrace (sig, 10);
-}
-
-static void
-deliver_fatal_signal (int sig)
-{
- handle_on_main_thread (sig, handle_fatal_signal);
-}
-
/* Report a fatal error due to signal SIG, output a backtrace of at
most BACKTRACE_LIMIT lines, and exit. */
_Noreturn void
-fatal_error_backtrace (int sig, int backtrace_limit)
+terminate_due_to_signal (int sig, int backtrace_limit)
{
- fatal_error_code = sig;
- signal (sig, SIG_DFL);
-
- TOTALLY_UNBLOCK_INPUT;
+ totally_unblock_input ();
/* If fatal error occurs in code below, avoid infinite recursion. */
if (! fatal_error_in_progress)
@@ -318,19 +300,18 @@ fatal_error_backtrace (int sig, int backtrace_limit)
}
/* Signal the same code; this time it will really be fatal.
- Remember that since we're in a signal handler, the signal we're
- going to send is probably blocked, so we have to unblock it if we
- want to really receive it. */
+ Since we're in a signal handler, the signal is blocked, so we
+ have to unblock it if we want to really receive it. */
#ifndef MSDOS
{
sigset_t unblocked;
sigemptyset (&unblocked);
- sigaddset (&unblocked, fatal_error_code);
+ sigaddset (&unblocked, sig);
pthread_sigmask (SIG_UNBLOCK, &unblocked, 0);
}
#endif
- kill (getpid (), fatal_error_code);
+ emacs_raise (sig);
/* This shouldn't be executed, but it prevents a warning. */
exit (1);
@@ -339,15 +320,9 @@ fatal_error_backtrace (int sig, int backtrace_limit)
#ifdef SIGDANGER
/* Handler for SIGDANGER. */
-static void deliver_danger_signal (int);
-
static void
handle_danger_signal (int sig)
{
- struct sigaction action;
- emacs_sigaction_init (&action, deliver_danger_signal);
- sigaction (sig, &action, 0);
-
malloc_warning ("Operating system warns that virtual memory is running low.\n");
/* It might be unsafe to call do_auto_save now. */
@@ -357,7 +332,7 @@ handle_danger_signal (int sig)
static void
deliver_danger_signal (int sig)
{
- handle_on_main_thread (sig, handle_danger_signal);
+ deliver_process_signal (sig, handle_danger_signal);
}
#endif
@@ -680,6 +655,7 @@ main (int argc, char **argv)
#endif
char stack_bottom_variable;
bool do_initial_setlocale;
+ bool dumping;
int skip_args = 0;
#ifdef HAVE_SETRLIMIT
struct rlimit rlim;
@@ -691,7 +667,6 @@ main (int argc, char **argv)
char dname_arg2[80];
#endif
char *ch_to_dir;
- struct sigaction fatal_error_action;
#if GC_MARK_STACK
stack_base = &dummy;
@@ -777,12 +752,11 @@ main (int argc, char **argv)
exit (1);
}
+ dumping = !initialized && (strcmp (argv[argc - 1], "dump") == 0
+ || strcmp (argv[argc - 1], "bootstrap") == 0);
#ifdef HAVE_PERSONALITY_LINUX32
- if (!initialized
- && (strcmp (argv[argc-1], "dump") == 0
- || strcmp (argv[argc-1], "bootstrap") == 0)
- && ! getenv ("EMACS_HEAP_EXEC"))
+ if (dumping && ! getenv ("EMACS_HEAP_EXEC"))
{
static char heapexec[] = "EMACS_HEAP_EXEC=true";
/* Set this so we only do this once. */
@@ -1107,119 +1081,7 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
#endif
}
- init_signals ();
- emacs_sigaction_init (&fatal_error_action, deliver_fatal_signal);
-
- /* Don't catch SIGHUP if dumping. */
- if (1
-#ifndef CANNOT_DUMP
- && initialized
-#endif
- )
- {
- /* In --batch mode, don't catch SIGHUP if already ignored.
- That makes nohup work. */
- bool catch_SIGHUP = !noninteractive;
- if (!catch_SIGHUP)
- {
- struct sigaction old_action;
- sigaction (SIGHUP, 0, &old_action);
- catch_SIGHUP = old_action.sa_handler != SIG_IGN;
- }
- if (catch_SIGHUP)
- sigaction (SIGHUP, &fatal_error_action, 0);
- }
-
- if (
-#ifndef CANNOT_DUMP
- ! noninteractive || initialized
-#else
- 1
-#endif
- )
- {
- /* Don't catch these signals in batch mode if dumping.
- On some machines, this sets static data that would make
- signal fail to work right when the dumped Emacs is run. */
- sigaction (SIGQUIT, &fatal_error_action, 0);
- sigaction (SIGILL, &fatal_error_action, 0);
- sigaction (SIGTRAP, &fatal_error_action, 0);
-#ifdef SIGUSR1
- add_user_signal (SIGUSR1, "sigusr1");
-#endif
-#ifdef SIGUSR2
- add_user_signal (SIGUSR2, "sigusr2");
-#endif
-#ifdef SIGABRT
- sigaction (SIGABRT, &fatal_error_action, 0);
-#endif
-#ifdef SIGHWE
- sigaction (SIGHWE, &fatal_error_action, 0);
-#endif
-#ifdef SIGPRE
- sigaction (SIGPRE, &fatal_error_action, 0);
-#endif
-#ifdef SIGORE
- sigaction (SIGORE, &fatal_error_action, 0);
-#endif
-#ifdef SIGUME
- sigaction (SIGUME, &fatal_error_action, 0);
-#endif
-#ifdef SIGDLK
- sigaction (SIGDLK, &fatal_error_action, 0);
-#endif
-#ifdef SIGCPULIM
- sigaction (SIGCPULIM, &fatal_error_action, 0);
-#endif
-#ifdef SIGIOT
- /* This is missing on some systems - OS/2, for example. */
- sigaction (SIGIOT, &fatal_error_action, 0);
-#endif
-#ifdef SIGEMT
- sigaction (SIGEMT, &fatal_error_action, 0);
-#endif
- sigaction (SIGFPE, &fatal_error_action, 0);
-#ifdef SIGBUS
- sigaction (SIGBUS, &fatal_error_action, 0);
-#endif
- sigaction (SIGSEGV, &fatal_error_action, 0);
-#ifdef SIGSYS
- sigaction (SIGSYS, &fatal_error_action, 0);
-#endif
- /* May need special treatment on MS-Windows. See
- http://lists.gnu.org/archive/html/emacs-devel/2010-09/msg01062.html
- Please update the doc of kill-emacs, kill-emacs-hook, and
- NEWS if you change this.
- */
- if (noninteractive)
- sigaction (SIGINT, &fatal_error_action, 0);
- sigaction (SIGTERM, &fatal_error_action, 0);
-#ifdef SIGXCPU
- sigaction (SIGXCPU, &fatal_error_action, 0);
-#endif
-#ifdef SIGXFSZ
- sigaction (SIGXFSZ, &fatal_error_action, 0);
-#endif /* SIGXFSZ */
-
-#ifdef SIGDANGER
- /* This just means available memory is getting low. */
- {
- struct sigaction action;
- emacs_sigaction_init (&action, deliver_danger_signal);
- sigaction (SIGDANGER, &action, 0);
- }
-#endif
-
-#ifdef AIX
-/* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
- sigaction (SIGXCPU, &fatal_error_action, 0);
- sigaction (SIGIOINT, &fatal_error_action, 0);
- sigaction (SIGGRANT, &fatal_error_action, 0);
- sigaction (SIGRETRACT, &fatal_error_action, 0);
- sigaction (SIGSOUND, &fatal_error_action, 0);
- sigaction (SIGMSG, &fatal_error_action, 0);
-#endif /* AIX */
- }
+ init_signals (dumping);
noninteractive1 = noninteractive;
@@ -1281,7 +1143,6 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
}
init_eval ();
- init_data ();
init_atimer ();
running_asynch_code = 0;
init_random ();
@@ -1407,8 +1268,7 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
/* egetenv is a pretty low-level facility, which may get called in
many circumstances; it seems flimsy to put off initializing it
until calling init_callproc. Do not do it when dumping. */
- if (initialized || ((strcmp (argv[argc-1], "dump") != 0
- && strcmp (argv[argc-1], "bootstrap") != 0)))
+ if (! dumping)
set_initial_environment ();
/* AIX crashes are reported in system versions 3.2.3 and 3.2.4