1 /* Handling asynchronous signals.
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996 Ben Wing.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Not synched with FSF. Split out of keyboard.c. */
28 #include "events.h" /* for signal_fake_event() */
31 #include "syssignal.h"
36 /* Set to 1 when a quit-check signal (either a SIGIO interrupt or
37 the asynch. timeout for poll-for-quit) occurs. The QUITP
38 macro may look at this. */
39 volatile int quit_check_signal_happened;
41 /* Count of the number of times a quit-check signal has occurred.
42 Some stuff in event-Xt.c looks at this. */
43 volatile int quit_check_signal_tick_count;
45 /* Set to 1 when a SIGINT (or SIGQUIT) interrupt is processed.
46 maybe_read_quit_event() looks at this. */
47 volatile int sigint_happened;
49 /* Set to 1 when an asynch. timeout signal occurs. */
50 static volatile int alarm_happened;
52 /* This is used to synchronize setting the waiting_for_user_input_p
54 static volatile int alarm_happened_while_emacs_was_blocking;
56 /* See check_quit() for when this is set. */
57 int dont_check_for_quit;
59 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
63 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
64 int poll_for_sigchld_id;
67 /* This variable is used to communicate to a lisp
68 process-filter/sentinel/asynchronous callback (via the function
69 Fwaiting_for_user_input_p below) whether XEmacs was waiting for
70 user-input when that process-filter was called. */
71 static int waiting_for_user_input_p;
73 static int interrupts_slowed_down;
75 #define SLOWED_DOWN_INTERRUPTS_SECS 15
76 #define NORMAL_QUIT_CHECK_TIMEOUT_MSECS 250
77 #define NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS 250
79 /* Used so that signals can break out of system calls that aren't
80 naturally interruptible. */
82 JMP_BUF break_system_call_jump;
83 volatile int can_break_system_calls;
86 /**********************************************************************/
87 /* Asynchronous timeout functions */
88 /**********************************************************************/
90 /* The pending timers are stored in an ordered list, where the first timer
91 on the list is the first one to fire. Times recorded here are
93 static struct low_level_timeout *async_timer_queue;
95 /* Nonzero means async timers are temporarily suppressed. */
96 static int async_timer_suppress_count;
99 set_one_shot_timer (EMACS_TIME interval)
101 #ifdef HAVE_SETITIMER
103 it.it_value = interval;
104 EMACS_SET_SECS_USECS (it.it_interval, 0, 0);
105 setitimer (ITIMER_REAL, &it, 0);
108 EMACS_TIME_TO_INT (interval, secs);
114 reset_interval_timer (void)
118 /* Get the interval to set. If an interval is available,
119 make sure it's not zero (this is a valid return, but it will
120 cause the timer to get disabled, so convert it to a very short
122 if (get_low_level_timeout_interval (async_timer_queue, &interval))
124 if (EMACS_SECS (interval) == 0 && EMACS_USECS (interval) == 0)
125 EMACS_SET_USECS (interval, 1);
128 /* A time of 0 means "disable". */
129 EMACS_SET_SECS_USECS (interval, 0, 0);
131 set_one_shot_timer (interval);
135 event_stream_add_async_timeout (EMACS_TIME thyme)
137 int id = add_low_level_timeout (&async_timer_queue, thyme);
139 /* If this timeout is at the head of the queue, then we need to
140 set the timer right now for this timeout. Otherwise, things
141 are fine as-is; after the timers ahead of us are signalled,
142 the timer will be set for us. */
144 if (async_timer_queue->id == id)
145 reset_interval_timer ();
151 event_stream_remove_async_timeout (int id)
153 int first = (async_timer_queue && async_timer_queue->id == id);
154 remove_low_level_timeout (&async_timer_queue, id);
156 /* If we removed the timeout from the head of the queue, then
157 we need to reset the interval timer right now. */
159 reset_interval_timer ();
162 /* Handle an alarm once each second and read pending input
163 so as to handle a C-g if it comes in. */
166 alarm_signal (int signo)
168 if (interrupts_slowed_down)
170 something_happened = 1; /* tell QUIT to wake up */
171 /* we are in "slowed-down interrupts" mode; the only alarm
172 happening here is the slowed-down quit-check alarm, so
175 Do NOT set alarm_happened, because we don't want anyone
176 looking at the timeout queue. We didn't set it and
177 it needs to stay the way it is. */
178 quit_check_signal_happened = 1;
181 can_break_system_calls = 0;
183 /* can_break_system_calls is set when we want to break out of
184 non-interruptible system calls. */
185 if (can_break_system_calls)
187 /* reset the flag for safety and such. Do this *before*
188 unblocking or reestablishing the signal to avoid potential
190 can_break_system_calls = 0;
191 EMACS_UNBLOCK_SIGNAL (signo);
192 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
193 LONGJMP (break_system_call_jump, 0);
197 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
201 something_happened = 1; /* tell QUIT to wake up */
203 if (emacs_is_blocking)
204 alarm_happened_while_emacs_was_blocking = 1;
205 /* #### This is for QUITP. When it is run, it may not be the
206 place to do arbitrary stuff like run asynch. handlers, but
207 it needs to know whether the poll-for-quit asynch. timeout
208 went off. Rather than put the code in to compute this
209 specially, we just set this flag. Should fix this. */
210 quit_check_signal_happened = 1;
212 #ifdef HAVE_UNIXOID_EVENT_LOOP
213 signal_fake_event ();
216 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
221 init_async_timeouts (void)
223 signal (SIGALRM, alarm_signal);
224 async_timer_suppress_count = 0;
227 /* Turn off async timeouts. */
230 stop_async_timeouts (void)
232 if (async_timer_suppress_count == 0)
234 /* If timer was on, turn it off. */
236 EMACS_SET_SECS_USECS (thyme, 0, 0);
237 set_one_shot_timer (thyme);
239 async_timer_suppress_count++;
242 /* Turn on async timeouts again. */
245 start_async_timeouts (void)
247 assert (async_timer_suppress_count > 0);
248 async_timer_suppress_count--;
249 if (async_timer_suppress_count == 0)
251 /* Some callers turn off async timeouts and then use the alarm
252 for their own purposes; so reinitialize everything. */
253 signal (SIGALRM, alarm_signal);
254 reset_interval_timer ();
258 /* Some functions don't like being interrupted with SIGALRM or SIGIO.
259 Previously we were calling stop_interrupts() / start_interrupts(),
260 but then if the program hangs in one of those functions, e.g.
261 waiting for a connect(), we're really screwed. So instead we
262 just "slow them down". We do this by disabling all interrupts
263 and then installing a timer of length fairly large, like 5 or
264 10 secs. That way, any "legitimate" connections (which should
265 take a fairly short amount of time) go through OK, but we can
266 interrupt bogus ones. */
269 slow_down_interrupts (void)
273 /* We have to set the flag *before* setting the slowed-down timer,
274 to avoid a race condition -- if the signal occurs between the
275 call to set_one_shot_timer() and the setting of this flag,
276 alarm_happened will get set, which will be a Bad Thing if
277 there were no timeouts on the queue. */
278 interrupts_slowed_down++;
279 if (interrupts_slowed_down == 1)
282 EMACS_SET_SECS_USECS (thyme, SLOWED_DOWN_INTERRUPTS_SECS, 0);
283 set_one_shot_timer (thyme);
288 speed_up_interrupts (void)
290 if (interrupts_slowed_down > 0)
293 /* Change this flag AFTER fiddling with interrupts, for the same
294 race-condition reasons as above. */
295 interrupts_slowed_down--;
300 handle_alarm_going_off (void)
304 /* If asynch. timeouts are blocked, then don't do anything now,
305 but make this function get called again next QUIT.
307 #### This is a bit inefficient because there will be function call
308 overhead each time QUIT occurs. */
310 if (!NILP (Vinhibit_quit))
312 something_happened = 1;
317 interval_id = pop_low_level_timeout (&async_timer_queue, 0);
319 reset_interval_timer ();
320 if (alarm_happened_while_emacs_was_blocking)
322 alarm_happened_while_emacs_was_blocking = 0;
323 waiting_for_user_input_p = 1;
325 event_stream_deal_with_async_timeout (interval_id);
326 waiting_for_user_input_p = 0;
329 #ifdef HAVE_SETITIMER
331 alarm (unsigned int howlong)
333 struct itimerval old_it, new_it;
335 /* If alarm() gets called when polling isn't disabled, it can mess
336 up the periodic timer. */
337 assert (async_timer_suppress_count > 0);
339 new_it.it_value.tv_sec = howlong;
340 new_it.it_value.tv_usec = 0;
341 new_it.it_interval.tv_sec = 0;
342 new_it.it_interval.tv_usec = 0;
343 setitimer (ITIMER_REAL, &new_it, &old_it);
345 /* Never return zero if there was a timer outstanding. */
346 return old_it.it_value.tv_sec + (old_it.it_value.tv_usec > 0 ? 1 : 0);
350 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, 0, 0, 0, /*
351 Return non-nil if XEmacs is waiting for input from the user.
352 This is intended for use by asynchronous timeout callbacks and by
353 asynchronous process output filters and sentinels (not yet implemented
354 in XEmacs). It will always be nil if XEmacs is not inside of
355 an asynchronous timeout or process callback.
359 return waiting_for_user_input_p ? Qt : Qnil;
363 /**********************************************************************/
364 /* Control-G checking */
365 /**********************************************************************/
367 /* Set this for debugging, to have a way to get out */
368 int stop_character; /* #### not currently implemented */
370 /* This routine is called in response to a SIGINT or SIGQUIT.
371 On TTY's, one of these two signals will get generated in response
372 to C-g. (When running under X, C-g is handled using the SIGIO
373 handler, which sets a flag telling the QUIT macro to scan the
374 unread events for a ^G.)
376 Otherwise it sets the Lisp variable quit-flag not-nil.
377 This causes eval to throw, when it gets a chance.
378 If quit-flag is already non-nil, it stops the job right away. */
381 interrupt_signal (int sig)
383 /* This function can call lisp */
384 /* #### we should NOT be calling lisp from a signal handler, boys
386 /* Must preserve main program's value of errno. */
387 int old_errno = errno;
389 EMACS_REESTABLISH_SIGNAL (sig, interrupt_signal);
391 /* with the macroized error-checking stuff, the garbage below
392 may mess things up because XCONSOLE() and such can use and
393 change global vars. */
394 #if ! (defined (ERROR_CHECK_TYPECHECK) && defined (MACROIZE_ERROR_CHECKING))
395 if (sigint_happened && CONSOLEP (Vcontrolling_terminal) &&
396 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)) &&
401 reset_initial_console ();
402 EMACS_UNBLOCK_SIGNAL (sig);
403 #ifdef SIGTSTP /* Support possible in later USG versions */
405 * On systems which can suspend the current process and return to the original
406 * shell, this command causes the user to end up back at the shell.
407 * The "Auto-save" and "Abort" questions are not asked until
408 * the user elects to return to emacs, at which point he can save the current
409 * job and either dump core or continue.
413 /* Perhaps should really fork an inferior shell?
414 But that would not provide any way to get back
415 to the original shell, ever. */
416 stdout_out ("No support for stopping a process on this operating system;\n");
417 stdout_out ("you can continue or abort.\n");
418 #endif /* not SIGTSTP */
419 stdout_out ("Auto-save? (y or n) ");
420 if (((c = getc (stdin)) & ~040) == 'Y')
421 Fdo_auto_save (Qnil, Qnil);
424 stdout_out ("Abort (and dump core)? (y or n) ");
425 if (((c = getc (stdin)) & ~040) == 'Y')
429 stdout_out ("Continuing...\n");
430 reinit_initial_console ();
431 MARK_FRAME_CHANGED (XFRAME (DEVICE_SELECTED_FRAME
432 (XDEVICE (CONSOLE_SELECTED_DEVICE
434 (Vcontrolling_terminal))))));
437 #endif /* ! (defined (ERROR_CHECKING) && defined (MACROIZE_ERROR_CHECKING)) */
439 /* Else request quit when it's safe */
442 #ifdef HAVE_UNIXOID_EVENT_LOOP
443 signal_fake_event ();
451 restore_dont_check_for_quit (Lisp_Object val)
453 dont_check_for_quit = XINT (val);
458 begin_dont_check_for_quit (void)
460 specbind (Qinhibit_quit, Qt);
461 record_unwind_protect (restore_dont_check_for_quit,
462 make_int (dont_check_for_quit));
463 dont_check_for_quit = 1;
466 /* The effect of this function is to set Vquit_flag if the user pressed
467 ^G and discard the ^G, so as to not notice the same ^G again. */
471 /* dont_check_for_quit is set in two circumstances:
473 (1) when we are in the process of changing the window
474 configuration. The frame might be in an inconsistent state,
475 which will cause assertion failures if we check for QUIT.
477 (2) when we are reading events, and want to read the C-g
478 as an event. The normal check for quit will discard the C-g,
481 #### C-g is still often read as quit, e.g. if you type C-x C-g
482 (the C-g happens during the sit-for in maybe_echo_keys(); even
483 if we attempt to inhibit quit here, there is still a check
484 later on for QUIT. To fix this properly requires a fairly
485 substantial overhaul of the quit-checking code, which is
486 probably not worth it.)
488 We should *not* conditionalize on Vinhibit_quit, or
489 critical-quit (Control-Shift-G) won't work right. */
491 if (dont_check_for_quit)
494 if (quit_check_signal_happened)
496 quit_check_signal_happened = 0;
497 event_stream_quit_p ();
505 check_what_happened (void) /* called from QUIT when
506 something_happened gets set */
508 something_happened = 0;
512 handle_alarm_going_off ();
514 return check_quit ();
520 init_poll_for_quit (void)
522 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
523 /* Check for C-g every 1/4 of a second.
525 #### This is just a guess. Some investigation will have to be
526 done to see what the best value is. The best value is the
527 smallest possible value that doesn't cause a significant amount
528 of running time to be spent in C-g checking. */
529 if (!poll_for_quit_id)
531 event_stream_generate_wakeup (NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
532 NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
534 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
538 reset_poll_for_quit (void)
540 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
541 if (poll_for_quit_id)
543 event_stream_disable_wakeup (poll_for_quit_id, 1);
544 poll_for_quit_id = 0;
546 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
549 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
552 init_poll_for_sigchld (void)
554 /* Check for terminated processes every 1/4 of a second.
556 #### This is just a guess. Some investigation will have to be
557 done to see what the best value is. The best value is the
558 smallest possible value that doesn't cause a significant amount
559 of running time to be spent in process-termination checking.
561 poll_for_sigchld_id =
562 event_stream_generate_wakeup (NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
563 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
567 #endif /* not SIGCHLD */
572 input_available_signal (int signo)
574 something_happened = 1; /* tell QUIT to wake up */
575 quit_check_signal_happened = 1;
576 quit_check_signal_tick_count++;
577 EMACS_REESTABLISH_SIGNAL (signo, input_available_signal);
584 /**********************************************************************/
585 /* Enabling/disabling signals */
586 /**********************************************************************/
588 static int interrupts_initted;
591 stop_interrupts (void)
593 if (!interrupts_initted)
595 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
598 stop_async_timeouts ();
602 start_interrupts (void)
604 if (!interrupts_initted)
606 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
609 start_async_timeouts ();
612 /* Cheesy but workable implementation of sleep() that doesn't
613 interfere with our periodic timers. */
616 emacs_sleep (int secs)
624 /************************************************************************/
626 /************************************************************************/
628 /* If we've been nohup'ed, keep it that way.
629 This allows `nohup xemacs &' to work.
630 More generally, if a normally fatal signal has been redirected
631 to SIG_IGN by our invocation environment, trust the environment.
632 This keeps xemacs from being killed by a SIGQUIT intended for a
633 different process after having been backgrounded under a
634 non-job-control shell! */
636 handle_signal_if_fatal (int signo)
638 if (signal (signo, fatal_error_signal) == SIG_IGN)
639 signal (signo, SIG_IGN);
643 init_signals_very_early (void)
645 /* Catch all signals that would kill us.
646 Don't catch these signals in batch mode if not initialized.
647 On some machines, this sets static data that would make
648 signal fail to work right when the dumped Emacs is run. */
649 if (noninteractive && !initialized)
652 handle_signal_if_fatal (SIGILL); /* ANSI */
653 handle_signal_if_fatal (SIGABRT); /* ANSI */
654 handle_signal_if_fatal (SIGFPE); /* ANSI */
655 handle_signal_if_fatal (SIGSEGV); /* ANSI */
656 handle_signal_if_fatal (SIGTERM); /* ANSI */
660 handle_signal_if_fatal (SIGHUP); /* POSIX */
663 handle_signal_if_fatal (SIGQUIT); /* POSIX */
666 handle_signal_if_fatal (SIGTRAP); /* POSIX */
669 handle_signal_if_fatal (SIGUSR1); /* POSIX */
672 handle_signal_if_fatal (SIGUSR2); /* POSIX */
675 handle_signal_if_fatal (SIGPIPE); /* POSIX */
678 /* This will get reset later, once we're
679 capable of handling it properly. */
680 handle_signal_if_fatal (SIGALRM); /* POSIX */
685 handle_signal_if_fatal (SIGBUS); /* XPG5 */
688 handle_signal_if_fatal (SIGSYS); /* XPG5 */
691 handle_signal_if_fatal (SIGXCPU); /* XPG5 */
694 handle_signal_if_fatal (SIGXFSZ); /* XPG5 */
697 handle_signal_if_fatal (SIGVTALRM); /* XPG5 */
700 /* Messes up the REAL profiler */
701 /* handle_signal_if_fatal (SIGPROF); */ /* XPG5 */
706 handle_signal_if_fatal (SIGHWE);
709 handle_signal_if_fatal (SIGPRE);
712 handle_signal_if_fatal (SIGORE);
715 handle_signal_if_fatal (SIGUME);
718 handle_signal_if_fatal (SIGDLK);
721 handle_signal_if_fatal (SIGCPULIM);
724 handle_signal_if_fatal (SIGIOT);
727 handle_signal_if_fatal (SIGEMT);
730 handle_signal_if_fatal (SIGLOST);
732 #ifdef SIGSTKFLT /* coprocessor stack fault under Linux */
733 handle_signal_if_fatal (SIGSTKFLT);
735 #ifdef SIGUNUSED /* exists under Linux, and will kill process! */
736 handle_signal_if_fatal (SIGUNUSED);
740 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
742 handle_signal_if_fatal (SIGIOINT);
744 handle_signal_if_fatal (SIGGRANT);
745 handle_signal_if_fatal (SIGRETRACT);
746 handle_signal_if_fatal (SIGSOUND);
747 handle_signal_if_fatal (SIGMSG);
751 /* This just means available memory is getting low. */
752 signal (SIGDANGER, memory_warning_signal);
757 syms_of_signal (void)
759 DEFSUBR (Fwaiting_for_user_input_p);
763 init_interrupts_late (void)
767 signal (SIGINT, interrupt_signal);
769 /* On systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
770 and we can't tell which one it will give us. */
771 signal (SIGQUIT, interrupt_signal);
772 #endif /* HAVE_TERMIO */
773 init_async_timeouts ();
775 signal (SIGIO, input_available_signal);
776 # ifdef SIGPOLL /* XPG5 */
777 /* Some systems (e.g. Motorola SVR4) losingly have different
778 values for SIGIO and SIGPOLL, and send SIGPOLL instead of
779 SIGIO. On those same systems, an uncaught SIGPOLL kills the
781 signal (SIGPOLL, input_available_signal);
783 #elif !defined (DONT_POLL_FOR_QUIT)
784 init_poll_for_quit ();
788 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
789 init_poll_for_sigchld ();
792 EMACS_UNBLOCK_ALL_SIGNALS ();
794 interrupts_initted = 1;