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