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
|
|
290 on Cygwin. See win32.c. */
|
|
291 return mswindows_setitimer (kind, itnew, itold);
|
|
292 #else
|
|
293 return setitimer (kind, itnew, itold);
|
428
|
294 #endif
|
611
|
295 }
|
|
296
|
|
297 #endif /* HAVE_SETITIMER */
|
|
298
|
613
|
299 signal_handler_t
|
|
300 set_timeout_signal (int signal_number, signal_handler_t action)
|
|
301 {
|
|
302 #ifdef CYGWIN_BROKEN_SIGNALS
|
|
303 return mswindows_sigset (signal_number, action);
|
|
304 #else
|
|
305 return EMACS_SIGNAL (signal_number, action);
|
|
306 #endif
|
|
307 }
|
428
|
308
|
|
309 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, 0, 0, 0, /*
|
|
310 Return non-nil if XEmacs is waiting for input from the user.
|
|
311 This is intended for use by asynchronous timeout callbacks and by
|
|
312 asynchronous process output filters and sentinels (not yet implemented
|
|
313 in XEmacs). It will always be nil if XEmacs is not inside of
|
|
314 an asynchronous timeout or process callback.
|
|
315 */
|
|
316 ())
|
|
317 {
|
|
318 return waiting_for_user_input_p ? Qt : Qnil;
|
|
319 }
|
|
320
|
|
321
|
|
322 /**********************************************************************/
|
593
|
323 /* Enabling/disabling signals */
|
|
324 /**********************************************************************/
|
|
325
|
|
326 static int interrupts_initted;
|
|
327
|
|
328 void
|
|
329 stop_interrupts (void)
|
|
330 {
|
|
331 if (!interrupts_initted)
|
|
332 return;
|
|
333 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
334 unrequest_sigio ();
|
|
335 #endif
|
|
336 stop_async_timeouts ();
|
|
337 }
|
|
338
|
|
339 void
|
|
340 start_interrupts (void)
|
|
341 {
|
|
342 if (!interrupts_initted)
|
|
343 return;
|
|
344 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
345 request_sigio ();
|
|
346 #endif
|
|
347 start_async_timeouts ();
|
|
348 }
|
|
349
|
|
350
|
|
351 static void
|
|
352 establish_slow_interrupt_timer (void)
|
|
353 {
|
|
354 EMACS_TIME thyme;
|
|
355
|
|
356 EMACS_SET_SECS_USECS (thyme, SLOWED_DOWN_INTERRUPTS_SECS, 0);
|
|
357 set_one_shot_timer (thyme);
|
|
358 }
|
|
359
|
|
360 /* Some functions don't like being interrupted with SIGALRM or SIGIO.
|
|
361 Previously we were calling stop_interrupts() / start_interrupts(),
|
|
362 but then if the program hangs in one of those functions, e.g.
|
|
363 waiting for a connect(), we're really screwed. So instead we
|
|
364 just "slow them down". We do this by disabling all interrupts
|
|
365 and then installing a timer of length fairly large, like 5 or
|
|
366 10 secs. That way, any "legitimate" connections (which should
|
|
367 take a fairly short amount of time) go through OK, but we can
|
|
368 interrupt bogus ones. */
|
|
369
|
|
370 void
|
|
371 slow_down_interrupts (void)
|
|
372 {
|
|
373 /* We have to set the flag *before* setting the slowed-down timer,
|
|
374 to avoid a race condition -- if the signal occurs between the
|
|
375 call to set_one_shot_timer() and the setting of this flag,
|
|
376 async_timeout_happened will get set, which will be a Bad Thing if
|
|
377 there were no timeouts on the queue. */
|
|
378 interrupts_slowed_down++;
|
|
379 if (interrupts_slowed_down == 1)
|
|
380 {
|
|
381 stop_interrupts ();
|
|
382 establish_slow_interrupt_timer ();
|
|
383 }
|
|
384 }
|
|
385
|
|
386 void
|
|
387 speed_up_interrupts (void)
|
|
388 {
|
|
389 if (interrupts_slowed_down > 0)
|
|
390 {
|
|
391 start_interrupts ();
|
|
392 /* Change this flag AFTER fiddling with interrupts, for the same
|
|
393 race-condition reasons as above. */
|
|
394 interrupts_slowed_down--;
|
|
395 }
|
|
396 }
|
|
397
|
|
398 /* Cheesy but workable implementation of sleep() that doesn't
|
|
399 interfere with our periodic timers. */
|
|
400
|
|
401 void
|
|
402 emacs_sleep (int secs)
|
|
403 {
|
|
404 stop_interrupts ();
|
|
405 sleep (secs);
|
|
406 start_interrupts ();
|
|
407 }
|
|
408
|
|
409
|
|
410 /**********************************************************************/
|
|
411 /* The mechanism that drives it all */
|
428
|
412 /**********************************************************************/
|
|
413
|
593
|
414 /* called from QUIT when something_happened gets set (as a result of
|
|
415 a signal) */
|
|
416
|
|
417 int
|
|
418 check_what_happened (void)
|
|
419 {
|
|
420 something_happened = 0;
|
|
421 if (async_timeout_happened)
|
|
422 {
|
|
423 async_timeout_happened = 0;
|
|
424 handle_async_timeout_signal ();
|
|
425 }
|
|
426 if (slowed_interrupt_timeout_happened)
|
|
427 {
|
|
428 slowed_interrupt_timeout_happened = 0;
|
|
429 establish_slow_interrupt_timer ();
|
|
430 }
|
|
431
|
|
432 return check_quit ();
|
|
433 }
|
|
434
|
|
435 #ifdef SIGIO
|
|
436
|
|
437 /* Signal handler for SIGIO. */
|
|
438
|
|
439 static void
|
|
440 input_available_signal (int signo)
|
|
441 {
|
|
442 something_happened = 1; /* tell QUIT to wake up */
|
|
443 quit_check_signal_happened = 1;
|
|
444 quit_check_signal_tick_count++;
|
|
445 EMACS_REESTABLISH_SIGNAL (signo, input_available_signal);
|
|
446 SIGRETURN;
|
|
447 }
|
|
448
|
|
449 #endif /* SIGIO */
|
|
450
|
|
451 /* Actual signal handler for SIGALRM. Called when:
|
|
452
|
|
453 -- asynchronous timeouts (added with `add-async-timeout') go off
|
|
454
|
|
455 -- when the poll-for-quit timer (used for C-g handling; more or
|
|
456 less when SIGIO is unavailable or BROKEN_SIGIO is defined) or
|
|
457 poll-for-sigchld timer (used when BROKEN_SIGCHLD is defined) go
|
|
458 off. The latter two timers, if set, normally go off every 1/4
|
|
459 of a second -- see NORMAL_QUIT_CHECK_TIMEOUT_MSECS and
|
|
460 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS. (Both of these timers are
|
|
461 treated like other asynchronous timeouts, but special-cased
|
|
462 in handle_async_timeout_signal().)
|
|
463
|
|
464 -- we called slow_down_interrupts() and SLOWED_DOWN_INTERRUPTS_SECS
|
|
465 (or a multiple of it) has elapsed.
|
|
466
|
|
467 Note that under Windows, we have no working setitimer(), so we
|
|
468 simulate it using the multimedia timeout functions,
|
|
469 e.g. timeSetEvent(). See setitimer() in nt.c.
|
|
470
|
|
471 Note also that we don't actually *do* anything here (except in the
|
|
472 case of can_break_system_calls). Instead, we just set various
|
|
473 flags; next time QUIT is called, the flags will cause
|
|
474 check_what_happened() to be called, at which point we do everything
|
|
475 indicated by the flags.
|
|
476 */
|
|
477
|
|
478 static SIGTYPE
|
|
479 alarm_signal (int signo)
|
|
480 {
|
|
481 something_happened = 1; /* tell QUIT to wake up and call
|
|
482 check_what_happened() */
|
|
483
|
|
484 if (interrupts_slowed_down)
|
|
485 {
|
|
486 /* we are in "slowed-down interrupts" mode; the only alarm
|
|
487 happening here is the slowed-down quit-check alarm, so
|
|
488 we set this flag.
|
|
489
|
|
490 Do NOT set async_timeout_happened, because we don't want
|
|
491 anyone looking at the timeout queue -- async timeouts
|
|
492 are disabled. */
|
|
493 quit_check_signal_happened = 1;
|
|
494 quit_check_signal_tick_count++;
|
|
495 /* make sure we establish the slow timer again. */
|
|
496 slowed_interrupt_timeout_happened = 1;
|
|
497
|
|
498 /* can_break_system_calls is set when we want to break out of
|
|
499 non-interruptible system calls. */
|
|
500 if (can_break_system_calls)
|
|
501 {
|
|
502 /* reset the flag for safety and such. Do this *before*
|
|
503 unblocking or reestablishing the signal to avoid potential
|
|
504 race conditions. */
|
|
505 can_break_system_calls = 0;
|
|
506 #ifndef WIN32_NATIVE
|
|
507 /* #### I didn't add this WIN32_NATIVE check. I'm not sure
|
|
508 why it's here. But then again, someone needs to review
|
|
509 this can_break_system_calls stuff and see if it still
|
|
510 makes sense. --ben */
|
|
511 EMACS_UNBLOCK_SIGNAL (signo);
|
|
512 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
|
|
513 LONGJMP (break_system_call_jump, 0);
|
|
514 #endif
|
|
515 }
|
|
516 }
|
|
517 else
|
|
518 {
|
|
519 async_timeout_happened = 1;
|
|
520 if (emacs_is_blocking)
|
|
521 async_timeout_happened_while_emacs_was_blocking = 1;
|
|
522 /* #### This is for QUITP. When it is run, it may not be the
|
|
523 place to do arbitrary stuff like run asynch. handlers, but
|
|
524 it needs to know whether the poll-for-quit asynch. timeout
|
|
525 went off. Rather than put the code in to compute this
|
|
526 specially, we just set this flag. Should fix this. */
|
|
527 quit_check_signal_happened = 1;
|
|
528
|
|
529 #ifdef HAVE_UNIXOID_EVENT_LOOP
|
|
530 signal_fake_event ();
|
|
531 #endif
|
|
532 }
|
|
533
|
|
534 EMACS_REESTABLISH_SIGNAL (signo, alarm_signal);
|
|
535 SIGRETURN;
|
|
536 }
|
|
537
|
428
|
538 /* Set this for debugging, to have a way to get out */
|
|
539 int stop_character; /* #### not currently implemented */
|
|
540
|
593
|
541 /* Signal handler for SIGINT and SIGQUIT. On TTY's, one of these two
|
|
542 signals will get generated in response to C-g. (When running under
|
|
543 X, C-g is handled using the SIGIO handler, which sets a flag
|
|
544 telling the QUIT macro to scan the unread events for a ^G.)
|
|
545 */
|
428
|
546
|
|
547 static SIGTYPE
|
|
548 interrupt_signal (int sig)
|
|
549 {
|
|
550 /* This function can call lisp */
|
|
551 /* #### we should NOT be calling lisp from a signal handler, boys
|
|
552 and girls */
|
|
553 /* Must preserve main program's value of errno. */
|
|
554 int old_errno = errno;
|
|
555
|
|
556 EMACS_REESTABLISH_SIGNAL (sig, interrupt_signal);
|
|
557
|
|
558 if (sigint_happened && CONSOLEP (Vcontrolling_terminal) &&
|
|
559 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)) &&
|
|
560 !emacs_is_blocking)
|
|
561 {
|
593
|
562 /* #### this is inherited from GNU Emacs. Do we really want this?
|
|
563 --ben */
|
428
|
564 char c;
|
|
565 fflush (stdout);
|
|
566 reset_initial_console ();
|
|
567 EMACS_UNBLOCK_SIGNAL (sig);
|
|
568 #ifdef SIGTSTP /* Support possible in later USG versions */
|
|
569 /*
|
|
570 * On systems which can suspend the current process and return to the original
|
|
571 * shell, this command causes the user to end up back at the shell.
|
|
572 * The "Auto-save" and "Abort" questions are not asked until
|
|
573 * the user elects to return to emacs, at which point he can save the current
|
|
574 * job and either dump core or continue.
|
|
575 */
|
|
576 sys_suspend ();
|
|
577 #else
|
|
578 /* Perhaps should really fork an inferior shell?
|
|
579 But that would not provide any way to get back
|
|
580 to the original shell, ever. */
|
|
581 stdout_out ("No support for stopping a process on this operating system;\n");
|
|
582 stdout_out ("you can continue or abort.\n");
|
|
583 #endif /* not SIGTSTP */
|
|
584 stdout_out ("Auto-save? (y or n) ");
|
|
585 if (((c = getc (stdin)) & ~040) == 'Y')
|
|
586 Fdo_auto_save (Qnil, Qnil);
|
|
587 while (c != '\n')
|
|
588 c = getc (stdin);
|
|
589 stdout_out ("Abort (and dump core)? (y or n) ");
|
|
590 if (((c = getc (stdin)) & ~040) == 'Y')
|
|
591 abort ();
|
|
592 while (c != '\n')
|
|
593 c = getc (stdin);
|
|
594 stdout_out ("Continuing...\n");
|
|
595 reinit_initial_console ();
|
|
596 MARK_FRAME_CHANGED (XFRAME (DEVICE_SELECTED_FRAME
|
|
597 (XDEVICE (CONSOLE_SELECTED_DEVICE
|
|
598 (XCONSOLE
|
|
599 (Vcontrolling_terminal))))));
|
|
600 }
|
|
601 else
|
|
602 {
|
|
603 /* Else request quit when it's safe */
|
|
604 Vquit_flag = Qt;
|
|
605 sigint_happened = 1;
|
|
606 #ifdef HAVE_UNIXOID_EVENT_LOOP
|
|
607 signal_fake_event ();
|
|
608 #endif
|
|
609 }
|
|
610 errno = old_errno;
|
|
611 SIGRETURN;
|
|
612 }
|
|
613
|
593
|
614
|
|
615 /**********************************************************************/
|
|
616 /* Control-G checking */
|
|
617 /**********************************************************************/
|
|
618
|
428
|
619 static Lisp_Object
|
|
620 restore_dont_check_for_quit (Lisp_Object val)
|
|
621 {
|
|
622 dont_check_for_quit = XINT (val);
|
|
623 return Qnil;
|
|
624 }
|
|
625
|
|
626 void
|
|
627 begin_dont_check_for_quit (void)
|
|
628 {
|
|
629 specbind (Qinhibit_quit, Qt);
|
|
630 record_unwind_protect (restore_dont_check_for_quit,
|
|
631 make_int (dont_check_for_quit));
|
|
632 dont_check_for_quit = 1;
|
|
633 }
|
|
634
|
|
635 /* The effect of this function is to set Vquit_flag if the user pressed
|
|
636 ^G and discard the ^G, so as to not notice the same ^G again. */
|
|
637 int
|
|
638 check_quit (void)
|
|
639 {
|
|
640 /* dont_check_for_quit is set in two circumstances:
|
|
641
|
|
642 (1) when we are in the process of changing the window
|
|
643 configuration. The frame might be in an inconsistent state,
|
|
644 which will cause assertion failures if we check for QUIT.
|
|
645
|
|
646 (2) when we are reading events, and want to read the C-g
|
|
647 as an event. The normal check for quit will discard the C-g,
|
|
648 which would be bad.
|
|
649
|
|
650 #### C-g is still often read as quit, e.g. if you type C-x C-g
|
|
651 (the C-g happens during the sit-for in maybe_echo_keys(); even
|
|
652 if we attempt to inhibit quit here, there is still a check
|
|
653 later on for QUIT. To fix this properly requires a fairly
|
|
654 substantial overhaul of the quit-checking code, which is
|
|
655 probably not worth it.)
|
|
656
|
|
657 We should *not* conditionalize on Vinhibit_quit, or
|
|
658 critical-quit (Control-Shift-G) won't work right. */
|
|
659
|
|
660 if (dont_check_for_quit)
|
|
661 return 0;
|
|
662
|
|
663 if (quit_check_signal_happened)
|
|
664 {
|
|
665 quit_check_signal_happened = 0;
|
|
666 event_stream_quit_p ();
|
|
667 return 1;
|
|
668 }
|
|
669 else
|
|
670 return 0;
|
|
671 }
|
|
672
|
|
673
|
|
674
|
|
675 void
|
|
676 init_poll_for_quit (void)
|
|
677 {
|
|
678 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
|
|
679 /* Check for C-g every 1/4 of a second.
|
|
680
|
|
681 #### This is just a guess. Some investigation will have to be
|
|
682 done to see what the best value is. The best value is the
|
|
683 smallest possible value that doesn't cause a significant amount
|
|
684 of running time to be spent in C-g checking. */
|
|
685 if (!poll_for_quit_id)
|
|
686 poll_for_quit_id =
|
|
687 event_stream_generate_wakeup (NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
|
|
688 NORMAL_QUIT_CHECK_TIMEOUT_MSECS,
|
|
689 Qnil, Qnil, 1);
|
|
690 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
|
|
691 }
|
|
692
|
593
|
693 #if 0 /* not used anywhere */
|
|
694
|
428
|
695 void
|
|
696 reset_poll_for_quit (void)
|
|
697 {
|
|
698 #if !defined (SIGIO) && !defined (DONT_POLL_FOR_QUIT)
|
|
699 if (poll_for_quit_id)
|
|
700 {
|
|
701 event_stream_disable_wakeup (poll_for_quit_id, 1);
|
|
702 poll_for_quit_id = 0;
|
|
703 }
|
|
704 #endif /* not SIGIO and not DONT_POLL_FOR_QUIT */
|
|
705 }
|
|
706
|
593
|
707 #endif /* 0 */
|
|
708
|
428
|
709 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
|
|
710
|
|
711 static void
|
|
712 init_poll_for_sigchld (void)
|
|
713 {
|
|
714 /* Check for terminated processes every 1/4 of a second.
|
|
715
|
|
716 #### This is just a guess. Some investigation will have to be
|
|
717 done to see what the best value is. The best value is the
|
|
718 smallest possible value that doesn't cause a significant amount
|
|
719 of running time to be spent in process-termination checking.
|
|
720 */
|
|
721 poll_for_sigchld_id =
|
|
722 event_stream_generate_wakeup (NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
|
|
723 NORMAL_SIGCHLD_CHECK_TIMEOUT_MSECS,
|
|
724 Qnil, Qnil, 1);
|
|
725 }
|
|
726
|
|
727 #endif /* not SIGCHLD */
|
|
728
|
|
729
|
|
730 /************************************************************************/
|
|
731 /* initialization */
|
|
732 /************************************************************************/
|
|
733
|
|
734 /* If we've been nohup'ed, keep it that way.
|
|
735 This allows `nohup xemacs &' to work.
|
|
736 More generally, if a normally fatal signal has been redirected
|
|
737 to SIG_IGN by our invocation environment, trust the environment.
|
|
738 This keeps xemacs from being killed by a SIGQUIT intended for a
|
|
739 different process after having been backgrounded under a
|
|
740 non-job-control shell! */
|
|
741 static void
|
|
742 handle_signal_if_fatal (int signo)
|
|
743 {
|
613
|
744 if (EMACS_SIGNAL (signo, fatal_error_signal) == SIG_IGN)
|
|
745 EMACS_SIGNAL (signo, SIG_IGN);
|
428
|
746 }
|
|
747
|
|
748 void
|
|
749 init_signals_very_early (void)
|
|
750 {
|
|
751 /* Catch all signals that would kill us.
|
|
752 Don't catch these signals in batch mode if not initialized.
|
|
753 On some machines, this sets static data that would make
|
|
754 signal fail to work right when the dumped Emacs is run. */
|
|
755 if (noninteractive && !initialized)
|
|
756 return;
|
|
757
|
|
758 handle_signal_if_fatal (SIGILL); /* ANSI */
|
|
759 handle_signal_if_fatal (SIGABRT); /* ANSI */
|
|
760 handle_signal_if_fatal (SIGFPE); /* ANSI */
|
|
761 handle_signal_if_fatal (SIGSEGV); /* ANSI */
|
|
762 handle_signal_if_fatal (SIGTERM); /* ANSI */
|
|
763
|
|
764
|
|
765 #ifdef SIGHUP
|
|
766 handle_signal_if_fatal (SIGHUP); /* POSIX */
|
|
767 #endif
|
|
768 #ifdef SIGQUIT
|
|
769 handle_signal_if_fatal (SIGQUIT); /* POSIX */
|
|
770 #endif
|
|
771 #ifdef SIGTRAP
|
|
772 handle_signal_if_fatal (SIGTRAP); /* POSIX */
|
|
773 #endif
|
|
774 #ifdef SIGUSR1
|
|
775 handle_signal_if_fatal (SIGUSR1); /* POSIX */
|
|
776 #endif
|
|
777 #ifdef SIGUSR2
|
|
778 handle_signal_if_fatal (SIGUSR2); /* POSIX */
|
|
779 #endif
|
|
780 #ifdef SIGPIPE
|
|
781 handle_signal_if_fatal (SIGPIPE); /* POSIX */
|
|
782 #endif
|
|
783 #ifdef SIGALRM
|
|
784 /* This will get reset later, once we're
|
|
785 capable of handling it properly. */
|
|
786 handle_signal_if_fatal (SIGALRM); /* POSIX */
|
|
787 #endif
|
|
788
|
|
789
|
|
790 #ifdef SIGBUS
|
|
791 handle_signal_if_fatal (SIGBUS); /* XPG5 */
|
|
792 #endif
|
|
793 #ifdef SIGSYS
|
|
794 handle_signal_if_fatal (SIGSYS); /* XPG5 */
|
|
795 #endif
|
|
796 #ifdef SIGXCPU
|
|
797 handle_signal_if_fatal (SIGXCPU); /* XPG5 */
|
|
798 #endif
|
|
799 #ifdef SIGXFSZ
|
|
800 handle_signal_if_fatal (SIGXFSZ); /* XPG5 */
|
|
801 #endif
|
|
802 #ifdef SIGVTALRM
|
|
803 handle_signal_if_fatal (SIGVTALRM); /* XPG5 */
|
|
804 #endif
|
|
805 #ifdef SIGPROF
|
|
806 /* Messes up the REAL profiler */
|
|
807 /* handle_signal_if_fatal (SIGPROF); */ /* XPG5 */
|
|
808 #endif
|
|
809
|
|
810
|
|
811 #ifdef SIGHWE
|
|
812 handle_signal_if_fatal (SIGHWE);
|
|
813 #endif
|
|
814 #ifdef SIGPRE
|
|
815 handle_signal_if_fatal (SIGPRE);
|
|
816 #endif
|
|
817 #ifdef SIGORE
|
|
818 handle_signal_if_fatal (SIGORE);
|
|
819 #endif
|
|
820 #ifdef SIGUME
|
|
821 handle_signal_if_fatal (SIGUME);
|
|
822 #endif
|
|
823 #ifdef SIGDLK
|
|
824 handle_signal_if_fatal (SIGDLK);
|
|
825 #endif
|
|
826 #ifdef SIGCPULIM
|
|
827 handle_signal_if_fatal (SIGCPULIM);
|
|
828 #endif
|
|
829 #ifdef SIGIOT
|
|
830 handle_signal_if_fatal (SIGIOT);
|
|
831 #endif
|
|
832 #ifdef SIGEMT
|
|
833 handle_signal_if_fatal (SIGEMT);
|
|
834 #endif
|
|
835 #ifdef SIGLOST
|
|
836 handle_signal_if_fatal (SIGLOST);
|
|
837 #endif
|
|
838 #ifdef SIGSTKFLT /* coprocessor stack fault under Linux */
|
|
839 handle_signal_if_fatal (SIGSTKFLT);
|
|
840 #endif
|
|
841 #ifdef SIGUNUSED /* exists under Linux, and will kill process! */
|
|
842 handle_signal_if_fatal (SIGUNUSED);
|
|
843 #endif
|
|
844
|
|
845 #ifdef AIX
|
|
846 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
|
|
847 #ifndef _I386
|
|
848 handle_signal_if_fatal (SIGIOINT);
|
|
849 #endif
|
|
850 handle_signal_if_fatal (SIGGRANT);
|
|
851 handle_signal_if_fatal (SIGRETRACT);
|
|
852 handle_signal_if_fatal (SIGSOUND);
|
|
853 handle_signal_if_fatal (SIGMSG);
|
|
854 #endif /* AIX */
|
|
855
|
|
856 #ifdef SIGDANGER
|
|
857 /* This just means available memory is getting low. */
|
613
|
858 EMACS_SIGNAL (SIGDANGER, memory_warning_signal);
|
428
|
859 #endif
|
|
860 }
|
|
861
|
|
862 void
|
|
863 syms_of_signal (void)
|
|
864 {
|
|
865 DEFSUBR (Fwaiting_for_user_input_p);
|
|
866 }
|
|
867
|
|
868 void
|
|
869 init_interrupts_late (void)
|
|
870 {
|
|
871 if (!noninteractive)
|
|
872 {
|
613
|
873 EMACS_SIGNAL (SIGINT, interrupt_signal);
|
428
|
874 #ifdef HAVE_TERMIO
|
|
875 /* On systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
|
|
876 and we can't tell which one it will give us. */
|
613
|
877 EMACS_SIGNAL (SIGQUIT, interrupt_signal);
|
428
|
878 #endif /* HAVE_TERMIO */
|
|
879 init_async_timeouts ();
|
|
880 #ifdef SIGIO
|
613
|
881 EMACS_SIGNAL (SIGIO, input_available_signal);
|
428
|
882 # ifdef SIGPOLL /* XPG5 */
|
|
883 /* Some systems (e.g. Motorola SVR4) losingly have different
|
|
884 values for SIGIO and SIGPOLL, and send SIGPOLL instead of
|
|
885 SIGIO. On those same systems, an uncaught SIGPOLL kills the
|
|
886 process. */
|
613
|
887 EMACS_SIGNAL (SIGPOLL, input_available_signal);
|
428
|
888 # endif
|
|
889 #elif !defined (DONT_POLL_FOR_QUIT)
|
|
890 init_poll_for_quit ();
|
|
891 #endif
|
|
892 }
|
|
893
|
|
894 #if defined(HAVE_UNIX_PROCESSES) && !defined(SIGCHLD)
|
|
895 init_poll_for_sigchld ();
|
|
896 #endif
|
|
897
|
|
898 EMACS_UNBLOCK_ALL_SIGNALS ();
|
|
899
|
|
900 interrupts_initted = 1;
|
|
901 }
|
|
902
|