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 qxe_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
332 alarm (unsigned int howlong)
334 struct itimerval old_it, new_it;
336 /* If alarm() gets called when polling isn't disabled, it can mess
337 up the periodic timer. */
338 assert (async_timer_suppress_count > 0);
340 new_it.it_value.tv_sec = howlong;
341 new_it.it_value.tv_usec = 0;
342 new_it.it_interval.tv_sec = 0;
343 new_it.it_interval.tv_usec = 0;
344 qxe_setitimer (ITIMER_REAL, &new_it, &old_it);
346 /* Never return zero if there was a timer outstanding. */
347 return old_it.it_value.tv_sec + (old_it.it_value.tv_usec > 0 ? 1 : 0);
351 qxe_setitimer (int kind, const struct itimerval *itnew,
352 struct itimerval *itold)
354 #if defined (WIN32_NATIVE) || defined (CYGWIN)
355 /* setitimer() does not exist on native MS Windows, and appears broken
356 on Cygwin. See win32.c. */
357 return mswindows_setitimer (kind, itnew, itold);
359 return setitimer (kind, itnew, itold);
363 #endif /* HAVE_SETITIMER */
366 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, 0, 0, 0, /*
367 Return non-nil if XEmacs is waiting for input from the user.
368 This is intended for use by asynchronous timeout callbacks and by
369 asynchronous process output filters and sentinels (not yet implemented
370 in XEmacs). It will always be nil if XEmacs is not inside of
371 an asynchronous timeout or process callback.
375 return waiting_for_user_input_p ? Qt : Qnil;
379 /**********************************************************************/
380 /* Control-G checking */
381 /**********************************************************************/
383 /* Set this for debugging, to have a way to get out */
384 int stop_character; /* #### not currently implemented */
386 /* This routine is called in response to a SIGINT or SIGQUIT.
387 On TTY's, one of these two signals will get generated in response
388 to C-g. (When running under X, C-g is handled using the SIGIO
389 handler, which sets a flag telling the QUIT macro to scan the
390 unread events for a ^G.)
392 Otherwise it sets the Lisp variable quit-flag not-nil.
393 This causes eval to throw, when it gets a chance.
394 If quit-flag is already non-nil, it stops the job right away. */
397 interrupt_signal (int sig)
399 /* This function can call lisp */
400 /* #### we should NOT be calling lisp from a signal handler, boys
402 /* Must preserve main program's value of errno. */
403 int old_errno = errno;
405 EMACS_REESTABLISH_SIGNAL (sig, interrupt_signal);
407 /* with the macroized error-checking stuff, the garbage below
408 may mess things up because XCONSOLE() and such can use and
409 change global vars. */
410 #if ! (defined (ERROR_CHECK_TYPECHECK) && defined (MACROIZE_ERROR_CHECKING))
411 if (sigint_happened && CONSOLEP (Vcontrolling_terminal) &&
412 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)) &&
417 reset_initial_console ();
418 EMACS_UNBLOCK_SIGNAL (sig);
419 #ifdef SIGTSTP /* Support possible in later USG versions */
421 * On systems which can suspend the current process and return to the original
422 * shell, this command causes the user to end up back at the shell.
423 * The "Auto-save" and "Abort" questions are not asked until
424 * the user elects to return to emacs, at which point he can save the current
425 * job and either dump core or continue.
429 /* Perhaps should really fork an inferior shell?
430 But that would not provide any way to get back
431 to the original shell, ever. */
432 stdout_out ("No support for stopping a process on this operating system;\n");
433 stdout_out ("you can continue or abort.\n");
434 #endif /* not SIGTSTP */
435 stdout_out ("Auto-save? (y or n) ");
436 if (((c = getc (stdin)) & ~040) == 'Y')
437 Fdo_auto_save (Qnil, Qnil);
440 stdout_out ("Abort (and dump core)? (y or n) ");
441 if (((c = getc (stdin)) & ~040) == 'Y')
445 stdout_out ("Continuing...\n");
446 reinit_initial_console ();
447 MARK_FRAME_CHANGED (XFRAME (DEVICE_SELECTED_FRAME
448 (XDEVICE (CONSOLE_SELECTED_DEVICE
450 (Vcontrolling_terminal))))));
453 #endif /* ! (defined (ERROR_CHECKING) && defined (MACROIZE_ERROR_CHECKING)) */
455 /* Else request quit when it's safe */
458 #ifdef HAVE_UNIXOID_EVENT_LOOP
459 signal_fake_event ();
467 restore_dont_check_for_quit (Lisp_Object val)
469 dont_check_for_quit = XINT (val);
474 begin_dont_check_for_quit (void)
476 specbind (Qinhibit_quit, Qt);
477 record_unwind_protect (restore_dont_check_for_quit,
478 make_int (dont_check_for_quit));
479 dont_check_for_quit = 1;
482 /* The effect of this function is to set Vquit_flag if the user pressed
483 ^G and discard the ^G, so as to not notice the same ^G again. */
487 /* dont_check_for_quit is set in two circumstances:
489 (1) when we are in the process of changing the window
490 configuration. The frame might be in an inconsistent state,
491 which will cause assertion failures if we check for QUIT.
493 (2) when we are reading events, and want to read the C-g
494 as an event. The normal check for quit will discard the C-g,
497 #### C-g is still often read as quit, e.g. if you type C-x C-g
498 (the C-g happens during the sit-for in maybe_echo_keys(); even
499 if we attempt to inhibit quit here, there is still a check
500 later on for QUIT. To fix this properly requires a fairly
501 substantial overhaul of the quit-checking code, which is
502 probably not worth it.)
504 We should *not* conditionalize on Vinhibit_quit, or
505 critical-quit (Control-Shift-G) won't work right. */
507 if (dont_check_for_quit)
510 if (quit_check_signal_happened)
512 quit_check_signal_happened = 0;
513 event_stream_quit_p ();
521 check_what_happened (void) /* called from QUIT when
522 something_happened gets set */
524 something_happened = 0;
528 handle_alarm_going_off ();
530 return check_quit ();
536 init_poll_for_quit (void)
538 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
539 /* Check for C-g every 1/4 of a second.
541 #### This is just a guess. Some investigation will have to be
542 done to see what the best value is. The best value is the
543 smallest possible value that doesn't cause a significant amount
544 of running time to be spent in C-g checking. */
545 if (!poll_for_quit_id)
547 event_stream_generate_wakeup (NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
548 NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
550 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
554 reset_poll_for_quit (void)
556 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
557 if (poll_for_quit_id)
559 event_stream_disable_wakeup (poll_for_quit_id, 1);
560 poll_for_quit_id = 0;
562 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
565 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
568 init_poll_for_sigchld (void)
570 /* Check for terminated processes every 1/4 of a second.
572 #### This is just a guess. Some investigation will have to be
573 done to see what the best value is. The best value is the
574 smallest possible value that doesn't cause a significant amount
575 of running time to be spent in process-termination checking.
577 poll_for_sigchld_id =
578 event_stream_generate_wakeup (NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
579 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
583 #endif /* not SIGCHLD */
588 input_available_signal (int signo)
590 something_happened = 1; /* tell QUIT to wake up */
591 quit_check_signal_happened = 1;
592 quit_check_signal_tick_count++;
593 EMACS_REESTABLISH_SIGNAL (signo, input_available_signal);
600 /**********************************************************************/
601 /* Enabling/disabling signals */
602 /**********************************************************************/
604 static int interrupts_initted;
607 stop_interrupts (void)
609 if (!interrupts_initted)
611 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
614 stop_async_timeouts ();
618 start_interrupts (void)
620 if (!interrupts_initted)
622 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
625 start_async_timeouts ();
628 /* Cheesy but workable implementation of sleep() that doesn't
629 interfere with our periodic timers. */
632 emacs_sleep (int secs)
640 /************************************************************************/
642 /************************************************************************/
644 /* If we've been nohup'ed, keep it that way.
645 This allows `nohup xemacs &' to work.
646 More generally, if a normally fatal signal has been redirected
647 to SIG_IGN by our invocation environment, trust the environment.
648 This keeps xemacs from being killed by a SIGQUIT intended for a
649 different process after having been backgrounded under a
650 non-job-control shell! */
652 handle_signal_if_fatal (int signo)
654 if (signal (signo, fatal_error_signal) == SIG_IGN)
655 signal (signo, SIG_IGN);
659 init_signals_very_early (void)
661 /* Catch all signals that would kill us.
662 Don't catch these signals in batch mode if not initialized.
663 On some machines, this sets static data that would make
664 signal fail to work right when the dumped Emacs is run. */
665 if (noninteractive && !initialized)
668 handle_signal_if_fatal (SIGILL); /* ANSI */
669 handle_signal_if_fatal (SIGABRT); /* ANSI */
670 handle_signal_if_fatal (SIGFPE); /* ANSI */
671 handle_signal_if_fatal (SIGSEGV); /* ANSI */
672 handle_signal_if_fatal (SIGTERM); /* ANSI */
676 handle_signal_if_fatal (SIGHUP); /* POSIX */
679 handle_signal_if_fatal (SIGQUIT); /* POSIX */
682 handle_signal_if_fatal (SIGTRAP); /* POSIX */
685 handle_signal_if_fatal (SIGUSR1); /* POSIX */
688 handle_signal_if_fatal (SIGUSR2); /* POSIX */
691 handle_signal_if_fatal (SIGPIPE); /* POSIX */
694 /* This will get reset later, once we're
695 capable of handling it properly. */
696 handle_signal_if_fatal (SIGALRM); /* POSIX */
701 handle_signal_if_fatal (SIGBUS); /* XPG5 */
704 handle_signal_if_fatal (SIGSYS); /* XPG5 */
707 handle_signal_if_fatal (SIGXCPU); /* XPG5 */
710 handle_signal_if_fatal (SIGXFSZ); /* XPG5 */
713 handle_signal_if_fatal (SIGVTALRM); /* XPG5 */
716 /* Messes up the REAL profiler */
717 /* handle_signal_if_fatal (SIGPROF); */ /* XPG5 */
722 handle_signal_if_fatal (SIGHWE);
725 handle_signal_if_fatal (SIGPRE);
728 handle_signal_if_fatal (SIGORE);
731 handle_signal_if_fatal (SIGUME);
734 handle_signal_if_fatal (SIGDLK);
737 handle_signal_if_fatal (SIGCPULIM);
740 handle_signal_if_fatal (SIGIOT);
743 handle_signal_if_fatal (SIGEMT);
746 handle_signal_if_fatal (SIGLOST);
748 #ifdef SIGSTKFLT /* coprocessor stack fault under Linux */
749 handle_signal_if_fatal (SIGSTKFLT);
751 #ifdef SIGUNUSED /* exists under Linux, and will kill process! */
752 handle_signal_if_fatal (SIGUNUSED);
756 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
758 handle_signal_if_fatal (SIGIOINT);
760 handle_signal_if_fatal (SIGGRANT);
761 handle_signal_if_fatal (SIGRETRACT);
762 handle_signal_if_fatal (SIGSOUND);
763 handle_signal_if_fatal (SIGMSG);
767 /* This just means available memory is getting low. */
768 signal (SIGDANGER, memory_warning_signal);
773 syms_of_signal (void)
775 DEFSUBR (Fwaiting_for_user_input_p);
779 init_interrupts_late (void)
783 signal (SIGINT, interrupt_signal);
785 /* On systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
786 and we can't tell which one it will give us. */
787 signal (SIGQUIT, interrupt_signal);
788 #endif /* HAVE_TERMIO */
789 init_async_timeouts ();
791 signal (SIGIO, input_available_signal);
792 # ifdef SIGPOLL /* XPG5 */
793 /* Some systems (e.g. Motorola SVR4) losingly have different
794 values for SIGIO and SIGPOLL, and send SIGPOLL instead of
795 SIGIO. On those same systems, an uncaught SIGPOLL kills the
797 signal (SIGPOLL, input_available_signal);
799 #elif !defined (DONT_POLL_FOR_QUIT)
800 init_poll_for_quit ();
804 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
805 init_poll_for_sigchld ();
808 EMACS_UNBLOCK_ALL_SIGNALS ();
810 interrupts_initted = 1;