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