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 SXEmacs
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with: Not synched with FSF. Split out of keyboard.c. */
26 #include "ui/console.h"
27 #include "events/events.h" /* for signal_fake_event() */
30 #include "syssignal.h"
35 /* Set to 1 when a quit-check signal (either a SIGIO interrupt or
36 the asynch. timeout for poll-for-quit) occurs. The QUITP
37 macro may look at this. */
38 volatile int quit_check_signal_happened;
40 /* Count of the number of times a quit-check signal has occurred.
41 Some stuff in event-Xt.c looks at this. */
42 volatile int quit_check_signal_tick_count;
44 /* Set to 1 when a SIGINT (or SIGQUIT) interrupt is processed.
45 maybe_read_quit_event() looks at this. */
46 volatile int sigint_happened;
48 /* Set to 1 when an asynch. timeout signal occurs. */
49 static volatile int alarm_happened;
51 /* This is used to synchronize setting the waiting_for_user_input_p
53 static volatile int alarm_happened_while_emacs_was_blocking;
55 /* See check_quit() for when this is set. */
56 int dont_check_for_quit;
58 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
62 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
63 int poll_for_sigchld_id;
66 /* This variable is used to communicate to a lisp
67 process-filter/sentinel/asynchronous callback (via the function
68 Fwaiting_for_user_input_p below) whether SXEmacs was waiting for
69 user-input when that process-filter was called. */
70 static int waiting_for_user_input_p;
72 static int interrupts_slowed_down;
74 #define SLOWED_DOWN_INTERRUPTS_SECS 15
75 #define NORMAL_QUIT_CHECK_TIMEOUT_MSECS 250
76 #define NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS 250
78 /* Used so that signals can break out of system calls that aren't
79 naturally interruptible. */
81 JMP_BUF break_system_call_jump;
82 volatile int can_break_system_calls;
84 /**********************************************************************/
85 /* Asynchronous timeout functions */
86 /**********************************************************************/
88 /* The pending timers are stored in an ordered list, where the first timer
89 on the list is the first one to fire. Times recorded here are
91 static struct low_level_timeout *async_timer_queue;
93 /* Nonzero means async timers are temporarily suppressed. */
94 static int async_timer_suppress_count;
96 static void set_one_shot_timer(EMACS_TIME interval)
100 it.it_value = interval;
101 EMACS_SET_SECS_USECS(it.it_interval, 0, 0);
102 qxe_setitimer(ITIMER_REAL, &it, 0);
105 EMACS_TIME_TO_INT(interval, secs);
110 static void reset_interval_timer(void)
114 /* Get the interval to set. If an interval is available,
115 make sure it's not zero (this is a valid return, but it will
116 cause the timer to get disabled, so convert it to a very short
118 if (get_low_level_timeout_interval(async_timer_queue, &interval)) {
119 if (EMACS_SECS(interval) == 0 && EMACS_USECS(interval) == 0)
120 EMACS_SET_USECS(interval, 1);
122 /* A time of 0 means "disable". */
123 EMACS_SET_SECS_USECS(interval, 0, 0);
125 set_one_shot_timer(interval);
128 int event_stream_add_async_timeout(EMACS_TIME thyme)
130 int id = add_low_level_timeout(&async_timer_queue, thyme);
132 /* If this timeout is at the head of the queue, then we need to
133 set the timer right now for this timeout. Otherwise, things
134 are fine as-is; after the timers ahead of us are signalled,
135 the timer will be set for us. */
137 if (async_timer_queue->id == id)
138 reset_interval_timer();
143 void event_stream_remove_async_timeout(int id)
145 int first = (async_timer_queue && async_timer_queue->id == id);
146 remove_low_level_timeout(&async_timer_queue, id);
148 /* If we removed the timeout from the head of the queue, then
149 we need to reset the interval timer right now. */
151 reset_interval_timer();
154 /* Handle an alarm once each second and read pending input
155 so as to handle a C-g if it comes in. */
157 static SIGTYPE alarm_signal(int signo)
159 if (interrupts_slowed_down) {
160 something_happened = 1; /* tell QUIT to wake up */
161 /* we are in "slowed-down interrupts" mode; the only alarm
162 happening here is the slowed-down quit-check alarm, so
165 Do NOT set alarm_happened, because we don't want anyone
166 looking at the timeout queue. We didn't set it and
167 it needs to stay the way it is. */
168 quit_check_signal_happened = 1;
170 /* can_break_system_calls is set when we want to break out of
171 non-interruptible system calls. */
172 if (can_break_system_calls) {
173 /* reset the flag for safety and such. Do this *before*
174 unblocking or reestablishing the signal to avoid potential
176 can_break_system_calls = 0;
177 EMACS_UNBLOCK_SIGNAL(signo);
178 EMACS_REESTABLISH_SIGNAL(signo, alarm_signal);
179 LONGJMP(break_system_call_jump, 0);
182 EMACS_REESTABLISH_SIGNAL(signo, alarm_signal);
186 something_happened = 1; /* tell QUIT to wake up */
188 if (emacs_is_blocking)
189 alarm_happened_while_emacs_was_blocking = 1;
190 /* #### This is for QUITP. When it is run, it may not be the
191 place to do arbitrary stuff like run asynch. handlers, but
192 it needs to know whether the poll-for-quit asynch. timeout
193 went off. Rather than put the code in to compute this
194 specially, we just set this flag. Should fix this. */
195 quit_check_signal_happened = 1;
197 #ifdef HAVE_UNIXOID_EVENT_LOOP
201 EMACS_REESTABLISH_SIGNAL(signo, alarm_signal);
205 static void init_async_timeouts(void)
207 signal(SIGALRM, alarm_signal);
208 async_timer_suppress_count = 0;
211 /* Turn off async timeouts. */
213 extern void stop_async_timeouts(void)
215 if (async_timer_suppress_count == 0) {
216 /* If timer was on, turn it off. */
218 EMACS_SET_SECS_USECS(thyme, 0, 0);
219 set_one_shot_timer(thyme);
221 async_timer_suppress_count++;
224 /* Turn on async timeouts again. */
226 extern void start_async_timeouts(void)
228 assert(async_timer_suppress_count > 0);
229 async_timer_suppress_count--;
230 if (async_timer_suppress_count == 0) {
231 /* Some callers turn off async timeouts and then use the alarm
232 for their own purposes; so reinitialize everything. */
233 signal(SIGALRM, alarm_signal);
234 reset_interval_timer();
238 /* Some functions don't like being interrupted with SIGALRM or SIGIO.
239 Previously we were calling stop_interrupts() / start_interrupts(),
240 but then if the program hangs in one of those functions, e.g.
241 waiting for a connect(), we're really screwed. So instead we
242 just "slow them down". We do this by disabling all interrupts
243 and then installing a timer of length fairly large, like 5 or
244 10 secs. That way, any "legitimate" connections (which should
245 take a fairly short amount of time) go through OK, but we can
246 interrupt bogus ones. */
248 void slow_down_interrupts(void)
252 /* We have to set the flag *before* setting the slowed-down timer,
253 to avoid a race condition -- if the signal occurs between the
254 call to set_one_shot_timer() and the setting of this flag,
255 alarm_happened will get set, which will be a Bad Thing if
256 there were no timeouts on the queue. */
257 interrupts_slowed_down++;
258 if (interrupts_slowed_down == 1) {
260 EMACS_SET_SECS_USECS(thyme, SLOWED_DOWN_INTERRUPTS_SECS, 0);
261 set_one_shot_timer(thyme);
265 void speed_up_interrupts(void)
267 if (interrupts_slowed_down > 0) {
269 /* Change this flag AFTER fiddling with interrupts, for the same
270 race-condition reasons as above. */
271 interrupts_slowed_down--;
275 static void handle_alarm_going_off(void)
279 /* If asynch. timeouts are blocked, then don't do anything now,
280 but make this function get called again next QUIT.
282 #### This is a bit inefficient because there will be function call
283 overhead each time QUIT occurs. */
285 if (!NILP(Vinhibit_quit)) {
286 something_happened = 1;
291 interval_id = pop_low_level_timeout(&async_timer_queue, 0);
293 reset_interval_timer();
294 if (alarm_happened_while_emacs_was_blocking) {
295 alarm_happened_while_emacs_was_blocking = 0;
296 waiting_for_user_input_p = 1;
298 event_stream_deal_with_async_timeout(interval_id);
299 waiting_for_user_input_p = 0;
302 #ifdef HAVE_SETITIMER
304 unsigned int alarm(unsigned int howlong)
306 struct itimerval old_it, new_it;
308 /* If alarm() gets called when polling isn't disabled, it can mess
309 up the periodic timer. */
310 assert(async_timer_suppress_count > 0);
312 new_it.it_value.tv_sec = howlong;
313 new_it.it_value.tv_usec = 0;
314 new_it.it_interval.tv_sec = 0;
315 new_it.it_interval.tv_usec = 0;
316 qxe_setitimer(ITIMER_REAL, &new_it, &old_it);
318 /* Never return zero if there was a timer outstanding. */
319 return old_it.it_value.tv_sec + (old_it.it_value.tv_usec > 0 ? 1 : 0);
323 qxe_setitimer(int kind, const struct itimerval *itnew, struct itimerval *itold)
325 return setitimer(kind, itnew, itold);
328 #endif /* HAVE_SETITIMER */
330 DEFUN("waiting-for-user-input-p", Fwaiting_for_user_input_p, 0, 0, 0, /*
331 Return non-nil if SXEmacs is waiting for input from the user.
332 This is intended for use by asynchronous timeout callbacks and by
333 asynchronous process output filters and sentinels (not yet implemented
334 in SXEmacs). It will always be nil if SXEmacs is not inside of
335 an asynchronous timeout or process callback.
339 return waiting_for_user_input_p ? Qt : Qnil;
342 /**********************************************************************/
343 /* Control-G checking */
344 /**********************************************************************/
346 /* Set this for debugging, to have a way to get out */
347 int stop_character; /* #### not currently implemented */
349 /* This routine is called in response to a SIGINT or SIGQUIT.
350 On TTY's, one of these two signals will get generated in response
351 to C-g. (When running under X, C-g is handled using the SIGIO
352 handler, which sets a flag telling the QUIT macro to scan the
353 unread events for a ^G.)
355 Otherwise it sets the Lisp variable quit-flag not-nil.
356 This causes eval to throw, when it gets a chance.
357 If quit-flag is already non-nil, it stops the job right away. */
359 static SIGTYPE interrupt_signal(int sig)
361 /* This function can call lisp */
362 /* #### we should NOT be calling lisp from a signal handler, boys
364 /* Must preserve main program's value of errno. */
365 int old_errno = errno;
367 EMACS_REESTABLISH_SIGNAL(sig, interrupt_signal);
369 /* with the macroized error-checking stuff, the garbage below
370 may mess things up because XCONSOLE() and such can use and
371 change global vars. */
372 #if ! (defined (ERROR_CHECK_TYPECHECK) && defined (MACROIZE_ERROR_CHECKING))
373 if (sigint_happened && CONSOLEP(Vcontrolling_terminal) &&
374 CONSOLE_LIVE_P(XCONSOLE(Vcontrolling_terminal)) &&
375 !emacs_is_blocking) {
378 reset_initial_console();
379 EMACS_UNBLOCK_SIGNAL(sig);
380 #ifdef SIGTSTP /* Support possible in later USG versions */
382 * On systems which can suspend the current process and return to the original
383 * shell, this command causes the user to end up back at the shell.
384 * The "Auto-save" and "Abort" questions are not asked until
385 * the user elects to return to emacs, at which point he can save the current
386 * job and either dump core or continue.
390 /* Perhaps should really fork an inferior shell?
391 But that would not provide any way to get back
392 to the original shell, ever. */
394 ("No support for stopping a process on this operating system;\n");
395 stdout_out("you can continue or abort.\n");
396 #endif /* not SIGTSTP */
397 stdout_out("Auto-save? (y or n) ");
398 if (((c = getc(stdin)) & ~040) == 'Y')
399 Fdo_auto_save(Qnil, Qnil);
402 stdout_out("Abort (and dump core)? (y or n) ");
403 if (((c = getc(stdin)) & ~040) == 'Y')
407 stdout_out("Continuing...\n");
408 reinit_initial_console();
409 MARK_FRAME_CHANGED(XFRAME(DEVICE_SELECTED_FRAME
410 (XDEVICE(CONSOLE_SELECTED_DEVICE
412 (Vcontrolling_terminal))))));
414 #endif /* ! (defined (ERROR_CHECKING) && defined (MACROIZE_ERROR_CHECKING)) */
416 /* Else request quit when it's safe */
419 #ifdef HAVE_UNIXOID_EVENT_LOOP
427 static Lisp_Object restore_dont_check_for_quit(Lisp_Object val)
429 dont_check_for_quit = XINT(val);
433 void begin_dont_check_for_quit(void)
435 specbind(Qinhibit_quit, Qt);
436 record_unwind_protect(restore_dont_check_for_quit,
437 make_int(dont_check_for_quit));
438 dont_check_for_quit = 1;
441 /* The effect of this function is to set Vquit_flag if the user pressed
442 ^G and discard the ^G, so as to not notice the same ^G again. */
445 /* dont_check_for_quit is set in two circumstances:
447 (1) when we are in the process of changing the window
448 configuration. The frame might be in an inconsistent state,
449 which will cause assertion failures if we check for QUIT.
451 (2) when we are reading events, and want to read the C-g
452 as an event. The normal check for quit will discard the C-g,
455 #### C-g is still often read as quit, e.g. if you type C-x C-g
456 (the C-g happens during the sit-for in maybe_echo_keys(); even
457 if we attempt to inhibit quit here, there is still a check
458 later on for QUIT. To fix this properly requires a fairly
459 substantial overhaul of the quit-checking code, which is
460 probably not worth it.)
462 We should *not* conditionalize on Vinhibit_quit, or
463 critical-quit (Control-Shift-G) won't work right. */
465 if (dont_check_for_quit)
468 if (quit_check_signal_happened) {
469 quit_check_signal_happened = 0;
470 event_stream_quit_p();
476 int check_what_happened(void)
477 { /* called from QUIT when
478 something_happened gets set */
479 something_happened = 0;
480 if (alarm_happened) {
482 handle_alarm_going_off();
487 void init_poll_for_quit(void)
489 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
490 /* Check for C-g every 1/4 of a second.
492 #### This is just a guess. Some investigation will have to be
493 done to see what the best value is. The best value is the
494 smallest possible value that doesn't cause a significant amount
495 of running time to be spent in C-g checking. */
496 if (!poll_for_quit_id)
498 event_stream_generate_wakeup
499 (NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
500 NORMAL_QUIT_CHECK_TIMEOUT_MSECS, Qnil, Qnil, 1);
501 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
504 void reset_poll_for_quit(void)
506 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
507 if (poll_for_quit_id) {
508 event_stream_disable_wakeup(poll_for_quit_id, 1);
509 poll_for_quit_id = 0;
511 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
514 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
516 static void init_poll_for_sigchld(void)
518 /* Check for terminated processes every 1/4 of a second.
520 #### This is just a guess. Some investigation will have to be
521 done to see what the best value is. The best value is the
522 smallest possible value that doesn't cause a significant amount
523 of running time to be spent in process-termination checking.
525 poll_for_sigchld_id =
526 event_stream_generate_wakeup(NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
527 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
531 #endif /* not SIGCHLD */
535 static void input_available_signal(int signo)
537 something_happened = 1; /* tell QUIT to wake up */
538 quit_check_signal_happened = 1;
539 quit_check_signal_tick_count++;
540 EMACS_REESTABLISH_SIGNAL(signo, input_available_signal);
546 /**********************************************************************/
547 /* Enabling/disabling signals */
548 /**********************************************************************/
550 static int interrupts_initted;
552 void stop_interrupts(void)
554 if (!interrupts_initted)
556 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
559 stop_async_timeouts();
562 void start_interrupts(void)
564 if (!interrupts_initted)
566 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
569 start_async_timeouts();
572 /* Cheesy but workable implementation of sleep() that doesn't
573 interfere with our periodic timers. */
575 void emacs_sleep(int secs)
582 /************************************************************************/
584 /************************************************************************/
586 /* If we've been nohup'ed, keep it that way.
587 This allows `nohup sxemacs &' to work.
588 More generally, if a normally fatal signal has been redirected
589 to SIG_IGN by our invocation environment, trust the environment.
590 This keeps sxemacs from being killed by a SIGQUIT intended for a
591 different process after having been backgrounded under a
592 non-job-control shell! */
593 static void handle_signal_if_fatal(int signo)
596 if (signal(signo, fatal_error_signal) == SIG_IGN) {
597 signal(signo, SIG_IGN);
602 void init_signals_very_early(void)
604 /* Catch all signals that would kill us.
605 Don't catch these signals in batch mode if not initialized.
606 On some machines, this sets static data that would make
607 signal fail to work right when the dumped Emacs is run. */
608 if (noninteractive && !initialized)
611 handle_signal_if_fatal(SIGILL); /* ANSI */
612 handle_signal_if_fatal(SIGABRT); /* ANSI */
613 handle_signal_if_fatal(SIGFPE); /* ANSI */
614 handle_signal_if_fatal(SIGSEGV); /* ANSI */
615 handle_signal_if_fatal(SIGTERM); /* ANSI */
618 handle_signal_if_fatal(SIGHUP); /* POSIX */
621 handle_signal_if_fatal(SIGQUIT); /* POSIX */
624 handle_signal_if_fatal(SIGTRAP); /* POSIX */
627 handle_signal_if_fatal(SIGUSR1); /* POSIX */
630 handle_signal_if_fatal(SIGUSR2); /* POSIX */
633 handle_signal_if_fatal(SIGPIPE); /* POSIX */
636 /* This will get reset later, once we're
637 capable of handling it properly. */
638 handle_signal_if_fatal(SIGALRM); /* POSIX */
642 handle_signal_if_fatal(SIGBUS); /* XPG5 */
645 handle_signal_if_fatal(SIGSYS); /* XPG5 */
648 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
649 handle_signal_if_fatal(SIGXCPU); /* XPG5 */
653 handle_signal_if_fatal(SIGXFSZ); /* XPG5 */
656 handle_signal_if_fatal(SIGVTALRM); /* XPG5 */
659 /* Messes up the REAL profiler */
660 /* handle_signal_if_fatal (SIGPROF); *//* XPG5 */
664 handle_signal_if_fatal(SIGHWE);
667 handle_signal_if_fatal(SIGPRE);
670 handle_signal_if_fatal(SIGORE);
673 handle_signal_if_fatal(SIGUME);
676 handle_signal_if_fatal(SIGDLK);
679 handle_signal_if_fatal(SIGCPULIM);
682 handle_signal_if_fatal(SIGIOT);
685 handle_signal_if_fatal(SIGEMT);
688 # if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
689 handle_signal_if_fatal(SIGLOST);
692 #ifdef SIGSTKFLT /* coprocessor stack fault under Linux */
693 handle_signal_if_fatal(SIGSTKFLT);
695 #ifdef SIGUNUSED /* exists under Linux, and will kill process! */
696 handle_signal_if_fatal(SIGUNUSED);
700 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
702 handle_signal_if_fatal(SIGIOINT);
704 handle_signal_if_fatal(SIGGRANT);
705 handle_signal_if_fatal(SIGRETRACT);
706 handle_signal_if_fatal(SIGSOUND);
707 handle_signal_if_fatal(SIGMSG);
711 /* This just means available memory is getting low. */
712 signal(SIGDANGER, memory_warning_signal);
716 void syms_of_signal(void)
718 DEFSUBR(Fwaiting_for_user_input_p);
721 void init_interrupts_late(void)
723 if (!noninteractive) {
724 signal(SIGINT, interrupt_signal);
726 /* On systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
727 and we can't tell which one it will give us. */
728 signal(SIGQUIT, interrupt_signal);
729 #endif /* HAVE_TERMIO */
730 init_async_timeouts();
732 signal(SIGIO, input_available_signal);
733 # ifdef SIGPOLL /* XPG5 */
734 /* Some systems (e.g. Motorola SVR4) losingly have different
735 values for SIGIO and SIGPOLL, and send SIGPOLL instead of
736 SIGIO. On those same systems, an uncaught SIGPOLL kills the
738 signal(SIGPOLL, input_available_signal);
740 #elif !defined (DONT_POLL_FOR_QUIT)
741 init_poll_for_quit();
744 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
745 init_poll_for_sigchld();
748 EMACS_UNBLOCK_ALL_SIGNALS();
750 interrupts_initted = 1;