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