428
|
1 /* Handling asynchronous signals.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
593
|
3 Copyright (C) 1995, 1996, 2001 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
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
|
|
10 later version.
|
|
11
|
|
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
|
|
15 for more details.
|
|
16
|
|
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. */
|
|
21
|
|
22 /* Synched up with: Not synched with FSF. Split out of keyboard.c. */
|
|
23
|
|
24 #include <config.h>
|
|
25 #include "lisp.h"
|
|
26
|
|
27 #include "console.h"
|
|
28 #include "events.h" /* for signal_fake_event() */
|
|
29 #include "frame.h"
|
593
|
30 #include "process.h"
|
611
|
31
|
428
|
32 #include "sysdep.h"
|
611
|
33 #include "sysfile.h"
|
428
|
34 #include "syssignal.h"
|
|
35 #include "systime.h"
|
|
36
|
|
37 /* Set to 1 when a quit-check signal (either a SIGIO interrupt or
|
|
38 the asynch. timeout for poll-for-quit) occurs. The QUITP
|
|
39 macro may look at this. */
|
|
40 volatile int quit_check_signal_happened;
|
|
41
|
|
42 /* Count of the number of times a quit-check signal has occurred.
|
|
43 Some stuff in event-Xt.c looks at this. */
|
|
44 volatile int quit_check_signal_tick_count;
|
|
45
|
|
46 /* Set to 1 when a SIGINT (or SIGQUIT) interrupt is processed.
|
|
47 maybe_read_quit_event() looks at this. */
|
|
48 volatile int sigint_happened;
|
|
49
|
|
50 /* Set to 1 when an asynch. timeout signal occurs. */
|
593
|
51 static volatile int async_timeout_happened;
|
|
52
|
|
53 /* Set to 1 when a multiple of SLOWED_DOWN_INTERRUPTS_SECS elapses,
|
|
54 after slow_down_interrupts() is called. */
|
|
55 static volatile int slowed_interrupt_timeout_happened;
|
428
|
56
|
|
57 /* This is used to synchronize setting the waiting_for_user_input_p
|
|
58 flag. */
|
593
|
59 static volatile int async_timeout_happened_while_emacs_was_blocking;
|
428
|
60
|
|
61 /* See check_quit() for when this is set. */
|
|
62 int dont_check_for_quit;
|
|
63
|
593
|
64 static int poll_for_quit_id;
|
|
65 static int poll_for_sigchld_id;
|
428
|
66
|
|
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;
|
|
72
|
|
73 static int interrupts_slowed_down;
|
|
74
|
|
75 #define SLOWED_DOWN_INTERRUPTS_SECS 15
|
|
76 #define NORMAL_QUIT_CHECK_TIMEOUT_MSECS 250
|
|
77 #define NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS 250
|
|
78
|
|
79 /* Used so that signals can break out of system calls that aren't
|
|
80 naturally interruptible. */
|
|
81
|
|
82 JMP_BUF break_system_call_jump;
|
|
83 volatile int can_break_system_calls;
|
|
84
|
593
|
85 static SIGTYPE alarm_signal (int signo);
|
|
86
|
|
87
|
428
|
88
|
|
89 /**********************************************************************/
|
|
90 /* Asynchronous timeout functions */
|
|
91 /**********************************************************************/
|
|
92
|
593
|
93 /* See the comment in event-stream.c, under major heading "Timeouts",
|
|
94 for the difference between low-level (one-shot) and high-level
|
|
95 (periodic/resignaling) timeouts. */
|
|
96
|
428
|
97 /* The pending timers are stored in an ordered list, where the first timer
|
|
98 on the list is the first one to fire. Times recorded here are
|
|
99 absolute. */
|
|
100 static struct low_level_timeout *async_timer_queue;
|
|
101
|
|
102 /* Nonzero means async timers are temporarily suppressed. */
|
|
103 static int async_timer_suppress_count;
|
|
104
|
|
105 static void
|
|
106 set_one_shot_timer (EMACS_TIME interval)
|
|
107 {
|
|
108 #ifdef HAVE_SETITIMER
|
|
109 struct itimerval it;
|
|
110 it.it_value = interval;
|
|
111 EMACS_SET_SECS_USECS (it.it_interval, 0, 0);
|
611
|
112 qxe_setitimer (ITIMER_REAL, &it, 0);
|
428
|
113 #else
|
|
114 int secs;
|
|
115 EMACS_TIME_TO_INT (interval, secs);
|
|
116 alarm (secs);
|
|
117 #endif
|
|
118 }
|
|
119
|
|
120 static void
|
|
121 reset_interval_timer (void)
|
|
122 {
|
|
123 EMACS_TIME interval;
|
|
124
|
|
125 /* Get the interval to set. If an interval is available,
|
|
126 make sure it's not zero (this is a valid return, but it will
|
|
127 cause the timer to get disabled, so convert it to a very short
|
|
128 time). */
|
|
129 if (get_low_level_timeout_interval (async_timer_queue, &interval))
|
|
130 {
|
|
131 if (EMACS_SECS (interval) == 0 && EMACS_USECS (interval) == 0)
|
|
132 EMACS_SET_USECS (interval, 1);
|
|
133 }
|
|
134 else
|
|
135 /* A time of 0 means "disable". */
|
|
136 EMACS_SET_SECS_USECS (interval, 0, 0);
|
|
137
|
|
138 set_one_shot_timer (interval);
|
|
139 }
|
|
140
|
|
141
|
|
142 static void
|
|
143 init_async_timeouts (void)
|
|
144 {
|
613
|
145 set_timeout_signal (SIGALRM, alarm_signal);
|
428
|
146 async_timer_suppress_count = 0;
|
|
147 }
|
|
148
|
|
149 /* Turn off async timeouts. */
|
|
150
|
|
151 static void
|
|
152 stop_async_timeouts (void)
|
|
153 {
|
|
154 if (async_timer_suppress_count == 0)
|
|
155 {
|
|
156 /* If timer was on, turn it off. */
|
|
157 EMACS_TIME thyme;
|
|
158 EMACS_SET_SECS_USECS (thyme, 0, 0);
|
|
159 set_one_shot_timer (thyme);
|
|
160 }
|
|
161 async_timer_suppress_count++;
|
|
162 }
|
|
163
|
|
164 /* Turn on async timeouts again. */
|
|
165
|
|
166 static void
|
|
167 start_async_timeouts (void)
|
|
168 {
|
|
169 assert (async_timer_suppress_count > 0);
|
|
170 async_timer_suppress_count--;
|
|
171 if (async_timer_suppress_count == 0)
|
|
172 {
|
|
173 /* Some callers turn off async timeouts and then use the alarm
|
|
174 for their own purposes; so reinitialize everything. */
|
613
|
175 set_timeout_signal (SIGALRM, alarm_signal);
|
428
|
176 reset_interval_timer ();
|
|
177 }
|
|
178 }
|
|
179
|
593
|
180 static void
|
|
181 handle_async_timeout_signal (void)
|
428
|
182 {
|
593
|
183 int interval_id;
|
|
184 int wakeup_id;
|
|
185 Lisp_Object fun, arg;
|
|
186
|
|
187 /* No checks for Vinhibit_quit here or anywhere else in this file!!!
|
|
188 Otherwise critical quit will not work right.
|
|
189 The only check for Vinhibit_quit is in QUIT itself. */
|
|
190 interval_id = pop_low_level_timeout (&async_timer_queue, 0);
|
|
191
|
|
192 reset_interval_timer ();
|
|
193 if (async_timeout_happened_while_emacs_was_blocking)
|
|
194 {
|
|
195 async_timeout_happened_while_emacs_was_blocking = 0;
|
|
196 waiting_for_user_input_p = 1;
|
|
197 }
|
|
198
|
|
199 wakeup_id = event_stream_resignal_wakeup (interval_id, 1, &fun, &arg);
|
428
|
200
|
593
|
201 if (wakeup_id == poll_for_quit_id)
|
|
202 {
|
|
203 quit_check_signal_happened = 1;
|
|
204 quit_check_signal_tick_count++;
|
|
205 }
|
|
206 else if (wakeup_id == poll_for_sigchld_id)
|
428
|
207 {
|
593
|
208 kick_status_notify ();
|
428
|
209 }
|
593
|
210 else
|
|
211 /* call1 GC-protects its arguments */
|
|
212 call1_trapping_errors ("Error in asynchronous timeout callback",
|
|
213 fun, arg);
|
|
214
|
|
215 waiting_for_user_input_p = 0;
|
|
216 }
|
|
217
|
|
218 /* The following two functions are the external interface onto
|
|
219 creating/deleting asynchronous interval timeouts, and are
|
|
220 called by event-stream.c. We call back to event-stream.c using
|
|
221 event_stream_resignal_wakeup(), when an interval goes off. */
|
|
222
|
|
223 int
|
|
224 signal_add_async_interval_timeout (EMACS_TIME thyme)
|
|
225 {
|
|
226 int id = add_low_level_timeout (&async_timer_queue, thyme);
|
|
227
|
|
228 /* If this timeout is at the head of the queue, then we need to
|
|
229 set the timer right now for this timeout. Otherwise, things
|
|
230 are fine as-is; after the timers ahead of us are signalled,
|
|
231 the timer will be set for us. */
|
|
232
|
|
233 if (async_timer_queue->id == id)
|
|
234 reset_interval_timer ();
|
|
235
|
|
236 return id;
|
428
|
237 }
|
|
238
|
|
239 void
|
593
|
240 signal_remove_async_interval_timeout (int id)
|
428
|
241 {
|
593
|
242 int first = (async_timer_queue && async_timer_queue->id == id);
|
|
243 remove_low_level_timeout (&async_timer_queue, id);
|
|
244
|
|
245 /* If we removed the timeout from the head of the queue, then
|
|
246 we need to reset the interval timer right now. */
|
|
247 if (first)
|
|
248 reset_interval_timer ();
|
428
|
249 }
|
|
250
|
593
|
251 /* If alarm() gets called when polling isn't disabled, it will mess up
|
|
252 the asynchronous timeouts, and then C-g checking won't work again.
|
|
253 Some libraries call alarm() directly, so we override the standard
|
|
254 library's alarm() and abort() if the caller of the library function
|
|
255 didn't wrap in stop_interrupts()/start_interrupts().
|
428
|
256
|
593
|
257 NOTE: We could potentially avoid the need to wrap by adding a
|
|
258 one-shot timeout to simulate the alarm(), smashing our signal
|
|
259 handler back into place, and calling the library function when the
|
|
260 alarm goes off. But do we want to? We're not going to gain the
|
|
261 ability to C-g out of library functions this way (unless we forcibly
|
|
262 longjmp() out of a signal handler, which is likely to lead to a
|
|
263 crash). --ben */
|
428
|
264
|
|
265 #ifdef HAVE_SETITIMER
|
611
|
266
|
428
|
267 unsigned int
|
|
268 alarm (unsigned int howlong)
|
|
269 {
|
|
270 struct itimerval old_it, new_it;
|
|
271
|
|
272 assert (async_timer_suppress_count > 0);
|
|
273
|
|
274 new_it.it_value.tv_sec = howlong;
|
|
275 new_it.it_value.tv_usec = 0;
|
|
276 new_it.it_interval.tv_sec = 0;
|
|
277 new_it.it_interval.tv_usec = 0;
|
611
|
278 qxe_setitimer (ITIMER_REAL, &new_it, &old_it);
|
428
|
279
|
|
280 /* Never return zero if there was a timer outstanding. */
|
|
281 return old_it.it_value.tv_sec + (old_it.it_value.tv_usec > 0 ? 1 : 0);
|
|
282 }
|
611
|
283
|
|
284 int
|
|
285 qxe_setitimer (int kind, const struct itimerval *itnew,
|
|
286 struct itimerval *itold)
|
|
287 {
|
|
288 #if defined (WIN32_NATIVE) || defined (CYGWIN)
|
|
289 /* setitimer() does not exist on native MS Windows, and appears broken
|
617
|
290 on Cygwin. See win32.c.
|
|
291
|
|
292 We are emulating the Unix98 setitimer() function, as found in its
|
|
293 incarnations on modern versions of Unix. Note however that in
|
|
294 the win32.c version, ITNEW and ITOLD must be equal if both are
|
|
295 non-zero, due to limitations in the underlying multimedia-timer
|
|
296 API. */
|
611
|
297 return mswindows_setitimer (kind, itnew, itold);
|
|
298 #else
|
617
|
299 /* YUCK! glibc defines setitimer's first argument as
|
|
300 enum __itimer_which, not int, which causes compile errors if
|
|
301 we call setitimer() in the obvious way. */
|
|
302 switch (kind)
|
|
303 {
|
|
304 case ITIMER_REAL: return setitimer (ITIMER_REAL, itnew, itold);
|
|
305 case ITIMER_VIRTUAL: return setitimer (ITIMER_VIRTUAL, itnew, itold);
|
|
306 case ITIMER_PROF: return setitimer (ITIMER_PROF, itnew, itold);
|
|
307 default: abort (); return 0;
|
|
308 }
|
428
|
309 #endif
|
611
|
310 }
|
|
311
|
|
312 #endif /* HAVE_SETITIMER */
|
|
313
|
613
|
314 signal_handler_t
|
|
315 set_timeout_signal (int signal_number, signal_handler_t action)
|
|
316 {
|
|
317 #ifdef CYGWIN_BROKEN_SIGNALS
|
|
318 return mswindows_sigset (signal_number, action);
|
|
319 #else
|
|
320 return EMACS_SIGNAL (signal_number, action);
|
|
321 #endif
|
|
322 }
|
428
|
323
|
|
324 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, 0, 0, 0, /*
|
|
325 Return non-nil if XEmacs is waiting for input from the user.
|
|
326 This is intended for use by asynchronous timeout callbacks and by
|
|
327 asynchronous process output filters and sentinels (not yet implemented
|
|
328 in XEmacs). It will always be nil if XEmacs is not inside of
|
|
329 an asynchronous timeout or process callback.
|
|
330 */
|
|
331 ())
|
|
332 {
|
|
333 return waiting_for_user_input_p ? Qt : Qnil;
|
|
334 }
|
|
335
|
|
336
|
|
337 /**********************************************************************/
|
593
|
338 /* Enabling/disabling signals */
|
|
339 /**********************************************************************/
|
|
340
|
|
341 static int interrupts_initted;
|
|
342
|
|
343 void
|
|
344 stop_interrupts (void)
|
|
345 {
|
|
346 if (!interrupts_initted)
|
|
347 return;
|
|
348 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
349 unrequest_sigio ();
|
|
350 #endif
|
|
351 stop_async_timeouts ();
|
|
352 }
|
|
353
|
|
354 void
|
|
355 start_interrupts (void)
|
|
356 {
|
|
357 if (!interrupts_initted)
|
|
358 return;
|
|
359 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
360 request_sigio ();
|
|
361 #endif
|
|
362 start_async_timeouts ();
|
|
363 }
|
|
364
|
|
365
|
|
366 static void
|
|
367 establish_slow_interrupt_timer (void)
|
|
368 {
|
|
369 EMACS_TIME thyme;
|
|
370
|
|
371 EMACS_SET_SECS_USECS (thyme, SLOWED_DOWN_INTERRUPTS_SECS, 0);
|
|
372 set_one_shot_timer (thyme);
|
|
373 }
|
|
374
|
|
375 /* Some functions don't like being interrupted with SIGALRM or SIGIO.
|
|
376 Previously we were calling stop_interrupts() / start_interrupts(),
|
|
377 but then if the program hangs in one of those functions, e.g.
|
|
378 waiting for a connect(), we're really screwed. So instead we
|
|
379 just "slow them down". We do this by disabling all interrupts
|
|
380 and then installing a timer of length fairly large, like 5 or
|
|
381 10 secs. That way, any "legitimate" connections (which should
|
|
382 take a fairly short amount of time) go through OK, but we can
|
|
383 interrupt bogus ones. */
|
|
384
|
|
385 void
|
|
386 slow_down_interrupts (void)
|
|
387 {
|
|
388 /* We have to set the flag *before* setting the slowed-down timer,
|
|
389 to avoid a race condition -- if the signal occurs between the
|
|
390 call to set_one_shot_timer() and the setting of this flag,
|
|
391 async_timeout_happened will get set, which will be a Bad Thing if
|
|
392 there were no timeouts on the queue. */
|
|
393 interrupts_slowed_down++;
|
|
394 if (interrupts_slowed_down == 1)
|
|
395 {
|
|
396 stop_interrupts ();
|
|
397 establish_slow_interrupt_timer ();
|
|
398 }
|
|
399 }
|
|
400
|
|
401 void
|
|
402 speed_up_interrupts (void)
|
|
403 {
|
|
404 if (interrupts_slowed_down > 0)
|
|
405 {
|
|
406 start_interrupts ();
|
|
407 /* Change this flag AFTER fiddling with interrupts, for the same
|
|
408 race-condition reasons as above. */
|
|
409 interrupts_slowed_down--;
|
|
410 }
|
|
411 }
|
|
412
|
|
413 /* Cheesy but workable implementation of sleep() that doesn't
|
|
414 interfere with our periodic timers. */
|
|
415
|
|
416 void
|
|
417 emacs_sleep (int secs)
|
|
418 {
|
|
419 stop_interrupts ();
|
|
420 sleep (secs);
|
|
421 start_interrupts ();
|
|
422 }
|
|
423
|
|
424
|
|
425 /**********************************************************************/
|
|
426 /* The mechanism that drives it all */
|
428
|
427 /**********************************************************************/
|
|
428
|
593
|
429 /* called from QUIT when something_happened gets set (as a result of
|
|
430 a signal) */
|
|
431
|
|
432 int
|
|
433 check_what_happened (void)
|
|
434 {
|
|
435 something_happened = 0;
|
|
436 if (async_timeout_happened)
|
|
437 {
|
|
438 async_timeout_happened = 0;
|
|
439 handle_async_timeout_signal ();
|
|
440 }
|
|
441 if (slowed_interrupt_timeout_happened)
|
|
442 {
|
|
443 slowed_interrupt_timeout_happened = 0;
|
|
444 establish_slow_interrupt_timer ();
|
|
445 }
|
|
446
|
|
447 return check_quit ();
|
|
448 }
|
|
449
|
|
450 #ifdef SIGIO
|
|
451
|
|
452 /* Signal handler for SIGIO. */
|
|
453
|
|
454 static void
|
|
455 input_available_signal (int signo)
|
|
456 {
|
|
457 something_happened = 1; /* tell QUIT to wake up */
|
|
458 quit_check_signal_happened = 1;
|
|
459 quit_check_signal_tick_count++;
|
|
460 EMACS_REESTABLISH_SIGNAL (signo, input_available_signal);
|
|
461 SIGRETURN;
|
|
462 }
|
|
463
|
|
464 #endif /* SIGIO */
|
|
465
|
|
466 /* Actual signal handler for SIGALRM. Called when:
|
|
467
|
|
468 -- asynchronous timeouts (added with `add-async-timeout') go off
|
|
469
|
|
470 -- when the poll-for-quit timer (used for C-g handling; more or
|
|
471 less when SIGIO is unavailable or BROKEN_SIGIO is defined) or
|
|
472 poll-for-sigchld timer (used when BROKEN_SIGCHLD is defined) go
|
|
473 off. The latter two timers, if set, normally go off every 1/4
|
|
474 of a second -- see NORMAL_QUIT_CHECK_TIMEOUT_MSECS and
|
|
475 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS. (Both of these timers are
|
|
476 treated like other asynchronous timeouts, but special-cased
|
|
477 in handle_async_timeout_signal().)
|
|
478
|
|
479 -- we called slow_down_interrupts() and SLOWED_DOWN_INTERRUPTS_SECS
|
|
480 (or a multiple of it) has elapsed.
|
|
481
|
|
482 Note that under Windows, we have no working setitimer(), so we
|
|
483 simulate it using the multimedia timeout functions,
|
|
484 e.g. timeSetEvent(). See setitimer() in nt.c.
|
|
485
|
|
486 Note also that we don't actually *do* anything here (except in the
|
|
487 case of can_break_system_calls). Instead, we just set various
|
|
488 flags; next time QUIT is called, the flags will cause
|
|
489 check_what_happened() to be called, at which point we do everything
|
|
490 indicated by the flags.
|
|
491 */
|
|
492
|
|
493 static SIGTYPE
|
|
494 alarm_signal (int signo)
|
|
495 {
|
|
496 something_happened = 1; /* tell QUIT to wake up and call
|
|
497 check_what_happened() */
|
|
498
|
|
499 if (interrupts_slowed_down)
|
|
500 {
|
|
501 /* we are in "slowed-down interrupts" mode; the only alarm
|
|
502 happening here is the slowed-down quit-check alarm, so
|
|
503 we set this flag.
|
|
504
|
|
505 Do NOT set async_timeout_happened, because we don't want
|
|
506 anyone looking at the timeout queue -- async timeouts
|
|
507 are disabled. */
|
|
508 quit_check_signal_happened = 1;
|
|
509 quit_check_signal_tick_count++;
|
|
510 /* make sure we establish the slow timer again. */
|
|
511 slowed_interrupt_timeout_happened = 1;
|
|
512
|
|
513 /* can_break_system_calls is set when we want to break out of
|
|
514 non-interruptible system calls. */
|
|
515 if (can_break_system_calls)
|
|
516 {
|
|
517 /* reset the flag for safety and such. Do this *before*
|
|
518 unblocking or reestablishing the signal to avoid potential
|
|
519 race conditions. */
|
|
520 can_break_system_calls = 0;
|
|
521 #ifndef WIN32_NATIVE
|
|
522 /* #### I didn't add this WIN32_NATIVE check. I'm not sure
|
|
523 why it's here. But then again, someone needs to review
|
|
524 this can_break_system_calls stuff and see if it still
|
|
525 makes sense. --ben */
|
|
526 EMACS_UNBLOCK_SIGNAL (signo);
|
|
527 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
|
|
528 LONGJMP (break_system_call_jump, 0);
|
|
529 #endif
|
|
530 }
|
|
531 }
|
|
532 else
|
|
533 {
|
|
534 async_timeout_happened = 1;
|
|
535 if (emacs_is_blocking)
|
|
536 async_timeout_happened_while_emacs_was_blocking = 1;
|
|
537 /* #### This is for QUITP. When it is run, it may not be the
|
|
538 place to do arbitrary stuff like run asynch. handlers, but
|
|
539 it needs to know whether the poll-for-quit asynch. timeout
|
|
540 went off. Rather than put the code in to compute this
|
|
541 specially, we just set this flag. Should fix this. */
|
|
542 quit_check_signal_happened = 1;
|
|
543
|
|
544 #ifdef HAVE_UNIXOID_EVENT_LOOP
|
|
545 signal_fake_event ();
|
|
546 #endif
|
|
547 }
|
|
548
|
|
549 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
|
|
550 SIGRETURN;
|
|
551 }
|
|
552
|
428
|
553 /* Set this for debugging, to have a way to get out */
|
|
554 int stop_character; /* #### not currently implemented */
|
|
555
|
593
|
556 /* Signal handler for SIGINT and SIGQUIT. On TTY's, one of these two
|
|
557 signals will get generated in response to C-g. (When running under
|
|
558 X, C-g is handled using the SIGIO handler, which sets a flag
|
|
559 telling the QUIT macro to scan the unread events for a ^G.)
|
|
560 */
|
428
|
561
|
|
562 static SIGTYPE
|
|
563 interrupt_signal (int sig)
|
|
564 {
|
|
565 /* This function can call lisp */
|
|
566 /* #### we should NOT be calling lisp from a signal handler, boys
|
|
567 and girls */
|
|
568 /* Must preserve main program's value of errno. */
|
|
569 int old_errno = errno;
|
|
570
|
|
571 EMACS_REESTABLISH_SIGNAL (sig, interrupt_signal);
|
|
572
|
|
573 if (sigint_happened && CONSOLEP (Vcontrolling_terminal) &&
|
|
574 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)) &&
|
|
575 !emacs_is_blocking)
|
|
576 {
|
593
|
577 /* #### this is inherited from GNU Emacs. Do we really want this?
|
|
578 --ben */
|
428
|
579 char c;
|
|
580 fflush (stdout);
|
|
581 reset_initial_console ();
|
|
582 EMACS_UNBLOCK_SIGNAL (sig);
|
|
583 #ifdef SIGTSTP /* Support possible in later USG versions */
|
|
584 /*
|
|
585 * On systems which can suspend the current process and return to the original
|
|
586 * shell, this command causes the user to end up back at the shell.
|
|
587 * The "Auto-save" and "Abort" questions are not asked until
|
|
588 * the user elects to return to emacs, at which point he can save the current
|
|
589 * job and either dump core or continue.
|
|
590 */
|
|
591 sys_suspend ();
|
|
592 #else
|
|
593 /* Perhaps should really fork an inferior shell?
|
|
594 But that would not provide any way to get back
|
|
595 to the original shell, ever. */
|
|
596 stdout_out ("No support for stopping a process on this operating system;\n");
|
|
597 stdout_out ("you can continue or abort.\n");
|
|
598 #endif /* not SIGTSTP */
|
|
599 stdout_out ("Auto-save? (y or n) ");
|
|
600 if (((c = getc (stdin)) & ~040) == 'Y')
|
|
601 Fdo_auto_save (Qnil, Qnil);
|
|
602 while (c != '\n')
|
|
603 c = getc (stdin);
|
|
604 stdout_out ("Abort (and dump core)? (y or n) ");
|
|
605 if (((c = getc (stdin)) & ~040) == 'Y')
|
|
606 abort ();
|
|
607 while (c != '\n')
|
|
608 c = getc (stdin);
|
|
609 stdout_out ("Continuing...\n");
|
|
610 reinit_initial_console ();
|
|
611 MARK_FRAME_CHANGED (XFRAME (DEVICE_SELECTED_FRAME
|
|
612 (XDEVICE (CONSOLE_SELECTED_DEVICE
|
|
613 (XCONSOLE
|
|
614 (Vcontrolling_terminal))))));
|
|
615 }
|
|
616 else
|
|
617 {
|
|
618 /* Else request quit when it's safe */
|
|
619 Vquit_flag = Qt;
|
|
620 sigint_happened = 1;
|
|
621 #ifdef HAVE_UNIXOID_EVENT_LOOP
|
|
622 signal_fake_event ();
|
|
623 #endif
|
|
624 }
|
|
625 errno = old_errno;
|
|
626 SIGRETURN;
|
|
627 }
|
|
628
|
593
|
629
|
|
630 /**********************************************************************/
|
|
631 /* Control-G checking */
|
|
632 /**********************************************************************/
|
|
633
|
428
|
634 static Lisp_Object
|
|
635 restore_dont_check_for_quit (Lisp_Object val)
|
|
636 {
|
|
637 dont_check_for_quit = XINT (val);
|
|
638 return Qnil;
|
|
639 }
|
|
640
|
|
641 void
|
|
642 begin_dont_check_for_quit (void)
|
|
643 {
|
|
644 specbind (Qinhibit_quit, Qt);
|
|
645 record_unwind_protect (restore_dont_check_for_quit,
|
|
646 make_int (dont_check_for_quit));
|
|
647 dont_check_for_quit = 1;
|
|
648 }
|
|
649
|
|
650 /* The effect of this function is to set Vquit_flag if the user pressed
|
|
651 ^G and discard the ^G, so as to not notice the same ^G again. */
|
|
652 int
|
|
653 check_quit (void)
|
|
654 {
|
|
655 /* dont_check_for_quit is set in two circumstances:
|
|
656
|
|
657 (1) when we are in the process of changing the window
|
|
658 configuration. The frame might be in an inconsistent state,
|
|
659 which will cause assertion failures if we check for QUIT.
|
|
660
|
|
661 (2) when we are reading events, and want to read the C-g
|
|
662 as an event. The normal check for quit will discard the C-g,
|
|
663 which would be bad.
|
|
664
|
|
665 #### C-g is still often read as quit, e.g. if you type C-x C-g
|
|
666 (the C-g happens during the sit-for in maybe_echo_keys(); even
|
|
667 if we attempt to inhibit quit here, there is still a check
|
|
668 later on for QUIT. To fix this properly requires a fairly
|
|
669 substantial overhaul of the quit-checking code, which is
|
|
670 probably not worth it.)
|
|
671
|
|
672 We should *not* conditionalize on Vinhibit_quit, or
|
|
673 critical-quit (Control-Shift-G) won't work right. */
|
|
674
|
|
675 if (dont_check_for_quit)
|
|
676 return 0;
|
|
677
|
|
678 if (quit_check_signal_happened)
|
|
679 {
|
|
680 quit_check_signal_happened = 0;
|
|
681 event_stream_quit_p ();
|
|
682 return 1;
|
|
683 }
|
|
684 else
|
|
685 return 0;
|
|
686 }
|
|
687
|
|
688
|
|
689
|
|
690 void
|
|
691 init_poll_for_quit (void)
|
|
692 {
|
|
693 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
|
|
694 /* Check for C-g every 1/4 of a second.
|
|
695
|
|
696 #### This is just a guess. Some investigation will have to be
|
|
697 done to see what the best value is. The best value is the
|
|
698 smallest possible value that doesn't cause a significant amount
|
|
699 of running time to be spent in C-g checking. */
|
|
700 if (!poll_for_quit_id)
|
|
701 poll_for_quit_id =
|
|
702 event_stream_generate_wakeup (NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
|
|
703 NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
|
|
704 Qnil, Qnil, 1);
|
|
705 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
|
|
706 }
|
|
707
|
593
|
708 #if 0 /* not used anywhere */
|
|
709
|
428
|
710 void
|
|
711 reset_poll_for_quit (void)
|
|
712 {
|
|
713 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
|
|
714 if (poll_for_quit_id)
|
|
715 {
|
|
716 event_stream_disable_wakeup (poll_for_quit_id, 1);
|
|
717 poll_for_quit_id = 0;
|
|
718 }
|
|
719 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
|
|
720 }
|
|
721
|
593
|
722 #endif /* 0 */
|
|
723
|
428
|
724 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
|
|
725
|
|
726 static void
|
|
727 init_poll_for_sigchld (void)
|
|
728 {
|
|
729 /* Check for terminated processes every 1/4 of a second.
|
|
730
|
|
731 #### This is just a guess. Some investigation will have to be
|
|
732 done to see what the best value is. The best value is the
|
|
733 smallest possible value that doesn't cause a significant amount
|
|
734 of running time to be spent in process-termination checking.
|
|
735 */
|
|
736 poll_for_sigchld_id =
|
|
737 event_stream_generate_wakeup (NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
|
|
738 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
|
|
739 Qnil, Qnil, 1);
|
|
740 }
|
|
741
|
|
742 #endif /* not SIGCHLD */
|
|
743
|
|
744
|
|
745 /************************************************************************/
|
|
746 /* initialization */
|
|
747 /************************************************************************/
|
|
748
|
|
749 /* If we've been nohup'ed, keep it that way.
|
|
750 This allows `nohup xemacs &' to work.
|
|
751 More generally, if a normally fatal signal has been redirected
|
|
752 to SIG_IGN by our invocation environment, trust the environment.
|
|
753 This keeps xemacs from being killed by a SIGQUIT intended for a
|
|
754 different process after having been backgrounded under a
|
|
755 non-job-control shell! */
|
|
756 static void
|
|
757 handle_signal_if_fatal (int signo)
|
|
758 {
|
613
|
759 if (EMACS_SIGNAL (signo, fatal_error_signal) == SIG_IGN)
|
|
760 EMACS_SIGNAL (signo, SIG_IGN);
|
428
|
761 }
|
|
762
|
|
763 void
|
|
764 init_signals_very_early (void)
|
|
765 {
|
|
766 /* Catch all signals that would kill us.
|
|
767 Don't catch these signals in batch mode if not initialized.
|
|
768 On some machines, this sets static data that would make
|
|
769 signal fail to work right when the dumped Emacs is run. */
|
|
770 if (noninteractive && !initialized)
|
|
771 return;
|
|
772
|
|
773 handle_signal_if_fatal (SIGILL); /* ANSI */
|
|
774 handle_signal_if_fatal (SIGABRT); /* ANSI */
|
|
775 handle_signal_if_fatal (SIGFPE); /* ANSI */
|
|
776 handle_signal_if_fatal (SIGSEGV); /* ANSI */
|
|
777 handle_signal_if_fatal (SIGTERM); /* ANSI */
|
|
778
|
|
779
|
|
780 #ifdef SIGHUP
|
|
781 handle_signal_if_fatal (SIGHUP); /* POSIX */
|
|
782 #endif
|
|
783 #ifdef SIGQUIT
|
|
784 handle_signal_if_fatal (SIGQUIT); /* POSIX */
|
|
785 #endif
|
|
786 #ifdef SIGTRAP
|
|
787 handle_signal_if_fatal (SIGTRAP); /* POSIX */
|
|
788 #endif
|
|
789 #ifdef SIGUSR1
|
|
790 handle_signal_if_fatal (SIGUSR1); /* POSIX */
|
|
791 #endif
|
|
792 #ifdef SIGUSR2
|
|
793 handle_signal_if_fatal (SIGUSR2); /* POSIX */
|
|
794 #endif
|
|
795 #ifdef SIGPIPE
|
|
796 handle_signal_if_fatal (SIGPIPE); /* POSIX */
|
|
797 #endif
|
|
798 #ifdef SIGALRM
|
|
799 /* This will get reset later, once we're
|
|
800 capable of handling it properly. */
|
|
801 handle_signal_if_fatal (SIGALRM); /* POSIX */
|
|
802 #endif
|
|
803
|
|
804
|
|
805 #ifdef SIGBUS
|
|
806 handle_signal_if_fatal (SIGBUS); /* XPG5 */
|
|
807 #endif
|
|
808 #ifdef SIGSYS
|
|
809 handle_signal_if_fatal (SIGSYS); /* XPG5 */
|
|
810 #endif
|
|
811 #ifdef SIGXCPU
|
|
812 handle_signal_if_fatal (SIGXCPU); /* XPG5 */
|
|
813 #endif
|
|
814 #ifdef SIGXFSZ
|
|
815 handle_signal_if_fatal (SIGXFSZ); /* XPG5 */
|
|
816 #endif
|
|
817 #ifdef SIGVTALRM
|
|
818 handle_signal_if_fatal (SIGVTALRM); /* XPG5 */
|
|
819 #endif
|
|
820 #ifdef SIGPROF
|
|
821 /* Messes up the REAL profiler */
|
|
822 /* handle_signal_if_fatal (SIGPROF); */ /* XPG5 */
|
|
823 #endif
|
|
824
|
|
825
|
|
826 #ifdef SIGHWE
|
|
827 handle_signal_if_fatal (SIGHWE);
|
|
828 #endif
|
|
829 #ifdef SIGPRE
|
|
830 handle_signal_if_fatal (SIGPRE);
|
|
831 #endif
|
|
832 #ifdef SIGORE
|
|
833 handle_signal_if_fatal (SIGORE);
|
|
834 #endif
|
|
835 #ifdef SIGUME
|
|
836 handle_signal_if_fatal (SIGUME);
|
|
837 #endif
|
|
838 #ifdef SIGDLK
|
|
839 handle_signal_if_fatal (SIGDLK);
|
|
840 #endif
|
|
841 #ifdef SIGCPULIM
|
|
842 handle_signal_if_fatal (SIGCPULIM);
|
|
843 #endif
|
|
844 #ifdef SIGIOT
|
|
845 handle_signal_if_fatal (SIGIOT);
|
|
846 #endif
|
|
847 #ifdef SIGEMT
|
|
848 handle_signal_if_fatal (SIGEMT);
|
|
849 #endif
|
|
850 #ifdef SIGLOST
|
|
851 handle_signal_if_fatal (SIGLOST);
|
|
852 #endif
|
|
853 #ifdef SIGSTKFLT /* coprocessor stack fault under Linux */
|
|
854 handle_signal_if_fatal (SIGSTKFLT);
|
|
855 #endif
|
|
856 #ifdef SIGUNUSED /* exists under Linux, and will kill process! */
|
|
857 handle_signal_if_fatal (SIGUNUSED);
|
|
858 #endif
|
|
859
|
|
860 #ifdef AIX
|
|
861 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
|
|
862 #ifndef _I386
|
|
863 handle_signal_if_fatal (SIGIOINT);
|
|
864 #endif
|
|
865 handle_signal_if_fatal (SIGGRANT);
|
|
866 handle_signal_if_fatal (SIGRETRACT);
|
|
867 handle_signal_if_fatal (SIGSOUND);
|
|
868 handle_signal_if_fatal (SIGMSG);
|
|
869 #endif /* AIX */
|
|
870
|
|
871 #ifdef SIGDANGER
|
|
872 /* This just means available memory is getting low. */
|
613
|
873 EMACS_SIGNAL (SIGDANGER, memory_warning_signal);
|
428
|
874 #endif
|
|
875 }
|
|
876
|
|
877 void
|
|
878 syms_of_signal (void)
|
|
879 {
|
|
880 DEFSUBR (Fwaiting_for_user_input_p);
|
|
881 }
|
|
882
|
|
883 void
|
|
884 init_interrupts_late (void)
|
|
885 {
|
|
886 if (!noninteractive)
|
|
887 {
|
613
|
888 EMACS_SIGNAL (SIGINT, interrupt_signal);
|
428
|
889 #ifdef HAVE_TERMIO
|
|
890 /* On systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
|
|
891 and we can't tell which one it will give us. */
|
613
|
892 EMACS_SIGNAL (SIGQUIT, interrupt_signal);
|
428
|
893 #endif /* HAVE_TERMIO */
|
|
894 init_async_timeouts ();
|
|
895 #ifdef SIGIO
|
613
|
896 EMACS_SIGNAL (SIGIO, input_available_signal);
|
428
|
897 # ifdef SIGPOLL /* XPG5 */
|
|
898 /* Some systems (e.g. Motorola SVR4) losingly have different
|
|
899 values for SIGIO and SIGPOLL, and send SIGPOLL instead of
|
|
900 SIGIO. On those same systems, an uncaught SIGPOLL kills the
|
|
901 process. */
|
613
|
902 EMACS_SIGNAL (SIGPOLL, input_available_signal);
|
428
|
903 # endif
|
|
904 #elif !defined (DONT_POLL_FOR_QUIT)
|
|
905 init_poll_for_quit ();
|
|
906 #endif
|
|
907 }
|
|
908
|
|
909 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
|
|
910 init_poll_for_sigchld ();
|
|
911 #endif
|
|
912
|
|
913 EMACS_UNBLOCK_ALL_SIGNALS ();
|
|
914
|
|
915 interrupts_initted = 1;
|
|
916 }
|
|
917
|