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