Remove "face-lift" comment.
[bpt/guile.git] / libguile / scmsigs.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42
43 \f
44
45 #include <signal.h>
46 #include <errno.h>
47
48 #include "libguile/_scm.h"
49
50 #include "libguile/async.h"
51 #include "libguile/eval.h"
52 #include "libguile/root.h"
53 #include "libguile/vectors.h"
54
55 #include "libguile/validate.h"
56 #include "libguile/scmsigs.h"
57
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61
62 /* The thread system has its own sleep and usleep functions. */
63 #ifndef USE_THREADS
64
65 #if defined(MISSING_SLEEP_DECL)
66 int sleep ();
67 #endif
68
69 #if defined(HAVE_USLEEP) && defined(MISSING_USLEEP_DECL)
70 int usleep ();
71 #endif
72
73 #endif
74
75 #ifdef __MINGW32__
76 #include <windows.h>
77 #define alarm(sec) (0)
78 /* This weird comma expression is because Sleep is void under Windows. */
79 #define sleep(sec) (Sleep ((sec) * 1000), 0)
80 #define kill(pid, sig) raise (sig)
81 #endif
82
83 \f
84
85 /* SIGRETTYPE is the type that signal handlers return. See <signal.h> */
86
87 #ifdef RETSIGTYPE
88 # define SIGRETTYPE RETSIGTYPE
89 #else
90 # ifdef STDC_HEADERS
91 # define SIGRETTYPE void
92 # else
93 # define SIGRETTYPE int
94 # endif
95 #endif
96
97 \f
98
99 /* take_signal is installed as the C signal handler whenever a Scheme
100 handler is set. when a signal arrives, take_signal marks the corresponding
101 element of got_signal and marks signal_async. the thunk in signal_async
102 (sys_deliver_signals) will be run at the next opportunity, outside a
103 critical section. sys_deliver_signals runs each Scheme handler for
104 which got_signal is set. */
105
106 static SCM signal_async;
107
108 static char got_signal[NSIG];
109
110 /* a Scheme vector of handler procedures. */
111 static SCM *signal_handlers;
112
113 /* saves the original C handlers, when a new handler is installed.
114 set to SIG_ERR if the original handler is installed. */
115 #ifdef HAVE_SIGACTION
116 static struct sigaction orig_handlers[NSIG];
117 #else
118 static SIGRETTYPE (*orig_handlers[NSIG])(int);
119 #endif
120
121 static SIGRETTYPE
122 take_signal (int signum)
123 {
124 int saved_errno = errno;
125 SCM ignored;
126
127 if (!scm_ints_disabled)
128 {
129 /* For reasons of speed, the SCM_NEWCELL macro doesn't defer
130 interrupts. Instead, it first sets its argument to point to
131 the first cell in the list, and then advances the freelist
132 pointer to the next cell. Now, if this procedure is
133 interrupted, the only anomalous state possible is to have
134 both SCM_NEWCELL's argument and scm_freelist pointing to the
135 same cell. To deal with this case, we always throw away the
136 first cell in scm_freelist here.
137
138 At least, that's the theory. I'm not convinced that that's
139 the only anomalous path we need to worry about. */
140 SCM_NEWCELL (ignored);
141 }
142 got_signal[signum] = 1;
143 #if HAVE_SIGACTION
144 /* unblock the signal before the scheme handler gets to run, since
145 it may use longjmp to escape (i.e., throw an exception). */
146 {
147 sigset_t set;
148 sigemptyset (&set);
149 sigaddset (&set, signum);
150 sigprocmask (SIG_UNBLOCK, &set, NULL);
151 }
152 #endif
153 scm_system_async_mark (signal_async);
154 errno = saved_errno;
155 }
156
157 static SCM
158 sys_deliver_signals (void)
159 {
160 int i;
161
162 for (i = 0; i < NSIG; i++)
163 {
164 if (got_signal[i])
165 {
166 /* The flag is reset before calling the handler in case the
167 handler doesn't return. If the handler doesn't return
168 but leaves other signals flagged, they their handlers
169 will be applied some time later when the async is checked
170 again. It would probably be better to reset the flags
171 after doing a longjmp. */
172 got_signal[i] = 0;
173 #ifndef HAVE_SIGACTION
174 signal (i, take_signal);
175 #endif
176 scm_call_1 (SCM_VELTS (*signal_handlers)[i], SCM_MAKINUM (i));
177 }
178 }
179 return SCM_UNSPECIFIED;
180 }
181
182 /* user interface for installation of signal handlers. */
183 SCM_DEFINE (scm_sigaction, "sigaction", 1, 2, 0,
184 (SCM signum, SCM handler, SCM flags),
185 "Install or report the signal handler for a specified signal.\n\n"
186 "@var{signum} is the signal number, which can be specified using the value\n"
187 "of variables such as @code{SIGINT}.\n\n"
188 "If @var{action} is omitted, @code{sigaction} returns a pair: the\n"
189 "CAR is the current\n"
190 "signal hander, which will be either an integer with the value @code{SIG_DFL}\n"
191 "(default action) or @code{SIG_IGN} (ignore), or the Scheme procedure which\n"
192 "handles the signal, or @code{#f} if a non-Scheme procedure handles the\n"
193 "signal. The CDR contains the current @code{sigaction} flags for the handler.\n\n"
194 "If @var{action} is provided, it is installed as the new handler for\n"
195 "@var{signum}. @var{action} can be a Scheme procedure taking one\n"
196 "argument, or the value of @code{SIG_DFL} (default action) or\n"
197 "@code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler\n"
198 "was installed before @code{sigaction} was first used. Flags can\n"
199 "optionally be specified for the new handler (@code{SA_RESTART} will\n"
200 "always be added if it's available and the system is using restartable\n"
201 "system calls.) The return value is a pair with information about the\n"
202 "old handler as described above.\n\n"
203 "This interface does not provide access to the \"signal blocking\"\n"
204 "facility. Maybe this is not needed, since the thread support may\n"
205 "provide solutions to the problem of consistent access to data\n"
206 "structures.")
207 #define FUNC_NAME s_scm_sigaction
208 {
209 int csig;
210 #ifdef HAVE_SIGACTION
211 struct sigaction action;
212 struct sigaction old_action;
213 #else
214 SIGRETTYPE (* chandler) (int);
215 SIGRETTYPE (* old_chandler) (int);
216 #endif
217 int query_only = 0;
218 int save_handler = 0;
219 SCM *scheme_handlers = SCM_VELTS (*signal_handlers);
220 SCM old_handler;
221
222 SCM_VALIDATE_INUM_COPY (1,signum,csig);
223 #if defined(HAVE_SIGACTION)
224 #if defined(SA_RESTART) && defined(HAVE_RESTARTABLE_SYSCALLS)
225 /* don't allow SA_RESTART to be omitted if HAVE_RESTARTABLE_SYSCALLS
226 is defined, since libguile would be likely to produce spurious
227 EINTR errors. */
228 action.sa_flags = SA_RESTART;
229 #else
230 action.sa_flags = 0;
231 #endif
232 if (!SCM_UNBNDP (flags))
233 {
234 SCM_VALIDATE_INUM (3,flags);
235 action.sa_flags |= SCM_INUM (flags);
236 }
237 sigemptyset (&action.sa_mask);
238 #endif
239 SCM_DEFER_INTS;
240 old_handler = scheme_handlers[csig];
241 if (SCM_UNBNDP (handler))
242 query_only = 1;
243 else if (SCM_EQ_P (scm_integer_p (handler), SCM_BOOL_T))
244 {
245 if (SCM_NUM2LONG (2, handler) == (long) SIG_DFL
246 || SCM_NUM2LONG (2, handler) == (long) SIG_IGN)
247 {
248 #ifdef HAVE_SIGACTION
249 action.sa_handler = (SIGRETTYPE (*) (int)) SCM_INUM (handler);
250 #else
251 chandler = (SIGRETTYPE (*) (int)) SCM_INUM (handler);
252 #endif
253 scheme_handlers[csig] = SCM_BOOL_F;
254 }
255 else
256 SCM_OUT_OF_RANGE (2, handler);
257 }
258 else if (SCM_FALSEP (handler))
259 {
260 /* restore the default handler. */
261 #ifdef HAVE_SIGACTION
262 if (orig_handlers[csig].sa_handler == SIG_ERR)
263 query_only = 1;
264 else
265 {
266 action = orig_handlers[csig];
267 orig_handlers[csig].sa_handler = SIG_ERR;
268 scheme_handlers[csig] = SCM_BOOL_F;
269 }
270 #else
271 if (orig_handlers[csig] == SIG_ERR)
272 query_only = 1;
273 else
274 {
275 chandler = orig_handlers[csig];
276 orig_handlers[csig] = SIG_ERR;
277 scheme_handlers[csig] = SCM_BOOL_F;
278 }
279 #endif
280 }
281 else
282 {
283 SCM_VALIDATE_NIM (2,handler);
284 #ifdef HAVE_SIGACTION
285 action.sa_handler = take_signal;
286 if (orig_handlers[csig].sa_handler == SIG_ERR)
287 save_handler = 1;
288 #else
289 chandler = take_signal;
290 if (orig_handlers[csig] == SIG_ERR)
291 save_handler = 1;
292 #endif
293 scheme_handlers[csig] = handler;
294 }
295
296 /* XXX - Silently ignore setting handlers for `program error signals'
297 because they can't currently be handled by Scheme code.
298 */
299
300 switch (csig)
301 {
302 /* This list of program error signals is from the GNU Libc
303 Reference Manual */
304 case SIGFPE:
305 case SIGILL:
306 case SIGSEGV:
307 #ifdef SIGBUS
308 case SIGBUS:
309 #endif
310 case SIGABRT:
311 #if defined(SIGIOT) && (SIGIOT != SIGABRT)
312 case SIGIOT:
313 #endif
314 #ifdef SIGTRAP
315 case SIGTRAP:
316 #endif
317 #ifdef SIGEMT
318 case SIGEMT:
319 #endif
320 #ifdef SIGSYS
321 case SIGSYS:
322 #endif
323 query_only = 1;
324 }
325
326 #ifdef HAVE_SIGACTION
327 if (query_only)
328 {
329 if (sigaction (csig, 0, &old_action) == -1)
330 SCM_SYSERROR;
331 }
332 else
333 {
334 if (sigaction (csig, &action , &old_action) == -1)
335 SCM_SYSERROR;
336 if (save_handler)
337 orig_handlers[csig] = old_action;
338 }
339 if (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN)
340 old_handler = scm_long2num ((long) old_action.sa_handler);
341 SCM_ALLOW_INTS;
342 return scm_cons (old_handler, SCM_MAKINUM (old_action.sa_flags));
343 #else
344 if (query_only)
345 {
346 if ((old_chandler = signal (csig, SIG_IGN)) == SIG_ERR)
347 SCM_SYSERROR;
348 if (signal (csig, old_chandler) == SIG_ERR)
349 SCM_SYSERROR;
350 }
351 else
352 {
353 if ((old_chandler = signal (csig, chandler)) == SIG_ERR)
354 SCM_SYSERROR;
355 if (save_handler)
356 orig_handlers[csig] = old_chandler;
357 }
358 if (old_chandler == SIG_DFL || old_chandler == SIG_IGN)
359 old_handler = scm_long2num ((long) old_chandler);
360 SCM_ALLOW_INTS;
361 return scm_cons (old_handler, SCM_MAKINUM (0));
362 #endif
363 }
364 #undef FUNC_NAME
365
366 SCM_DEFINE (scm_restore_signals, "restore-signals", 0, 0, 0,
367 (void),
368 "Return all signal handlers to the values they had before any call to\n"
369 "@code{sigaction} was made. The return value is unspecified.")
370 #define FUNC_NAME s_scm_restore_signals
371 {
372 int i;
373 SCM *scheme_handlers = SCM_VELTS (*signal_handlers);
374
375 for (i = 0; i < NSIG; i++)
376 {
377 #ifdef HAVE_SIGACTION
378 if (orig_handlers[i].sa_handler != SIG_ERR)
379 {
380 if (sigaction (i, &orig_handlers[i], NULL) == -1)
381 SCM_SYSERROR;
382 orig_handlers[i].sa_handler = SIG_ERR;
383 scheme_handlers[i] = SCM_BOOL_F;
384 }
385 #else
386 if (orig_handlers[i] != SIG_ERR)
387 {
388 if (signal (i, orig_handlers[i]) == SIG_ERR)
389 SCM_SYSERROR;
390 orig_handlers[i] = SIG_ERR;
391 scheme_handlers[i] = SCM_BOOL_F;
392 }
393 #endif
394 }
395 return SCM_UNSPECIFIED;
396 }
397 #undef FUNC_NAME
398
399 SCM_DEFINE (scm_alarm, "alarm", 1, 0, 0,
400 (SCM i),
401 "Set a timer to raise a @code{SIGALRM} signal after the specified\n"
402 "number of seconds (an integer). It's advisable to install a signal\n"
403 "handler for\n"
404 "@code{SIGALRM} beforehand, since the default action is to terminate\n"
405 "the process.\n\n"
406 "The return value indicates the time remaining for the previous alarm,\n"
407 "if any. The new value replaces the previous alarm. If there was\n"
408 "no previous alarm, the return value is zero.")
409 #define FUNC_NAME s_scm_alarm
410 {
411 unsigned int j;
412 SCM_VALIDATE_INUM (1,i);
413 j = alarm (SCM_INUM (i));
414 return SCM_MAKINUM (j);
415 }
416 #undef FUNC_NAME
417
418 #ifdef HAVE_PAUSE
419 SCM_DEFINE (scm_pause, "pause", 0, 0, 0,
420 (),
421 "Pause the current process (thread?) until a signal arrives whose\n"
422 "action is to either terminate the current process or invoke a\n"
423 "handler procedure. The return value is unspecified.")
424 #define FUNC_NAME s_scm_pause
425 {
426 pause ();
427 return SCM_UNSPECIFIED;
428 }
429 #undef FUNC_NAME
430 #endif
431
432 SCM_DEFINE (scm_sleep, "sleep", 1, 0, 0,
433 (SCM i),
434 "Wait for the given number of seconds (an integer) or until a signal\n"
435 "arrives. The return value is zero if the time elapses or the number\n"
436 "of seconds remaining otherwise.")
437 #define FUNC_NAME s_scm_sleep
438 {
439 unsigned long j;
440 SCM_VALIDATE_INUM_MIN (1,i,0);
441 #ifdef USE_THREADS
442 j = scm_thread_sleep (SCM_INUM(i));
443 #else
444 j = sleep (SCM_INUM(i));
445 #endif
446 return scm_ulong2num (j);
447 }
448 #undef FUNC_NAME
449
450 #if defined(USE_THREADS) || defined(HAVE_USLEEP)
451 SCM_DEFINE (scm_usleep, "usleep", 1, 0, 0,
452 (SCM i),
453 "Sleep for I microseconds. @code{usleep} is not available on\n"
454 "all platforms.")
455 #define FUNC_NAME s_scm_usleep
456 {
457 SCM_VALIDATE_INUM_MIN (1,i,0);
458
459 #ifdef USE_THREADS
460 /* If we have threads, we use the thread system's sleep function. */
461 {
462 unsigned long j = scm_thread_usleep (SCM_INUM (i));
463 return scm_ulong2num (j);
464 }
465 #else
466 #ifdef USLEEP_RETURNS_VOID
467 usleep (SCM_INUM (i));
468 return SCM_INUM0;
469 #else
470 {
471 int j = usleep (SCM_INUM (i));
472 return SCM_MAKINUM (j);
473 }
474 #endif
475 #endif
476 }
477 #undef FUNC_NAME
478 #endif /* GUILE_ISELECT || HAVE_USLEEP */
479
480 SCM_DEFINE (scm_raise, "raise", 1, 0, 0,
481 (SCM sig),
482 "Sends a specified signal @var{sig} to the current process, where\n"
483 "@var{sig} is as described for the kill procedure.")
484 #define FUNC_NAME s_scm_raise
485 {
486 SCM_VALIDATE_INUM (1,sig);
487 SCM_DEFER_INTS;
488 if (kill (getpid (), (int) SCM_INUM (sig)) != 0)
489 SCM_SYSERROR;
490 SCM_ALLOW_INTS;
491 return SCM_UNSPECIFIED;
492 }
493 #undef FUNC_NAME
494
495 \f
496
497 void
498 scm_init_scmsigs ()
499 {
500 SCM thunk;
501 int i;
502
503 signal_handlers =
504 SCM_VARIABLE_LOC (scm_c_define ("signal-handlers",
505 scm_c_make_vector (NSIG, SCM_BOOL_F)));
506 /* XXX - use scm_c_make_gsubr here instead of `define'? */
507 thunk = scm_c_define_gsubr ("%deliver-signals", 0, 0, 0,
508 sys_deliver_signals);
509 signal_async = scm_system_async (thunk);
510
511 for (i = 0; i < NSIG; i++)
512 {
513 got_signal[i] = 0;
514 #ifdef HAVE_SIGACTION
515 orig_handlers[i].sa_handler = SIG_ERR;
516
517 #else
518 orig_handlers[i] = SIG_ERR;
519 #endif
520
521 #ifdef HAVE_RESTARTABLE_SYSCALLS
522 /* If HAVE_RESTARTABLE_SYSCALLS is defined, it's important that
523 signals really are restartable. don't rely on the same
524 run-time that configure got: reset the default for every signal.
525 */
526 #ifdef HAVE_SIGINTERRUPT
527 siginterrupt (i, 0);
528 #elif defined(SA_RESTART)
529 {
530 struct sigaction action;
531
532 sigaction (i, NULL, &action);
533 if (!(action.sa_flags & SA_RESTART))
534 {
535 action.sa_flags |= SA_RESTART;
536 sigaction (i, &action, NULL);
537 }
538 }
539 #endif
540 /* if neither siginterrupt nor SA_RESTART are available we may
541 as well assume that signals are always restartable. */
542 #endif
543 }
544
545 scm_c_define ("NSIG", scm_long2num (NSIG));
546 scm_c_define ("SIG_IGN", scm_long2num ((long) SIG_IGN));
547 scm_c_define ("SIG_DFL", scm_long2num ((long) SIG_DFL));
548 #ifdef SA_NOCLDSTOP
549 scm_c_define ("SA_NOCLDSTOP", scm_long2num (SA_NOCLDSTOP));
550 #endif
551 #ifdef SA_RESTART
552 scm_c_define ("SA_RESTART", scm_long2num (SA_RESTART));
553 #endif
554
555 #ifndef SCM_MAGIC_SNARFER
556 #include "libguile/scmsigs.x"
557 #endif
558 }
559
560
561 /*
562 Local Variables:
563 c-file-style: "gnu"
564 End:
565 */