(scm_sigaction_for_thread): Store original handler in signal_handlers,
[bpt/guile.git] / libguile / scmsigs.c
CommitLineData
2fbc8609 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 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 104/* take_signal is installed as the C signal handler whenever a Scheme
2fbc8609
MV
105 handler is set. when a signal arrives, take_signal will queue the
106 Scheme handler procedure for its thread. */
0f2d19dd 107
0f2d19dd 108
2fbc8609
MV
109/* Scheme vectors with information about a signal. signal_handlers
110 contains the handler procedure or #f, signal_handler_cells contains
111 preallocated cells for queuing the handler in take_signal since we
112 can't allocate during signal delivery, signal_handler_threads
113 points to the thread that a signal should be delivered to.
114*/
e1a191a8 115static SCM *signal_handlers;
2fbc8609
MV
116static SCM signal_handler_cells;
117static SCM signal_handler_threads;
0f2d19dd 118
e1a191a8
GH
119/* saves the original C handlers, when a new handler is installed.
120 set to SIG_ERR if the original handler is installed. */
121#ifdef HAVE_SIGACTION
122static struct sigaction orig_handlers[NSIG];
123#else
da6e81b6 124static SIGRETTYPE (*orig_handlers[NSIG])(int);
0f2d19dd
JB
125#endif
126
e1a191a8
GH
127static SIGRETTYPE
128take_signal (int signum)
129{
2fbc8609
MV
130 if (signum >= 0 && signum < NSIG)
131 {
132 SCM thread = SCM_VECTOR_REF (signal_handler_threads, signum);
133 scm_i_queue_async_cell (SCM_VECTOR_REF(signal_handler_cells, signum),
134 scm_i_thread_root (thread));
135 }
136#ifndef HAVE_SIGACTION
137 signal (signum, take_signal);
138#endif
e1a191a8 139}
0f2d19dd 140
2fbc8609
MV
141SCM
142scm_sigaction (SCM signum, SCM handler, SCM flags)
e1a191a8 143{
2fbc8609
MV
144 return scm_sigaction_for_thread (signum, handler, flags, SCM_UNDEFINED);
145}
e1a191a8 146
2fbc8609
MV
147static SCM
148close_1 (SCM proc, SCM arg)
149{
150 return scm_primitive_eval_x (scm_list_3 (scm_sym_lambda, SCM_EOL,
151 scm_list_2 (proc, arg)));
0f2d19dd
JB
152}
153
e1a191a8 154/* user interface for installation of signal handlers. */
2fbc8609
MV
155SCM_DEFINE (scm_sigaction_for_thread, "sigaction", 1, 3, 0,
156 (SCM signum, SCM handler, SCM flags, SCM thread),
0d172d3f 157 "Install or report the signal handler for a specified signal.\n\n"
b380b885
MD
158 "@var{signum} is the signal number, which can be specified using the value\n"
159 "of variables such as @code{SIGINT}.\n\n"
2fbc8609 160 "If @var{handler} is omitted, @code{sigaction} returns a pair: the\n"
b380b885
MD
161 "CAR is the current\n"
162 "signal hander, which will be either an integer with the value @code{SIG_DFL}\n"
163 "(default action) or @code{SIG_IGN} (ignore), or the Scheme procedure which\n"
164 "handles the signal, or @code{#f} if a non-Scheme procedure handles the\n"
165 "signal. The CDR contains the current @code{sigaction} flags for the handler.\n\n"
2fbc8609
MV
166 "If @var{handler} is provided, it is installed as the new handler for\n"
167 "@var{signum}. @var{handler} can be a Scheme procedure taking one\n"
b380b885
MD
168 "argument, or the value of @code{SIG_DFL} (default action) or\n"
169 "@code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler\n"
2fbc8609
MV
170 "was installed before @code{sigaction} was first used. When\n"
171 "a scheme procedure has been specified, that procedure will run\n"
172 "in the given @var{thread}. When no thread has been given, the\n"
173 "thread that made this call to @code{sigaction} is used.\n"
174 "Flags can "
b380b885 175 "optionally be specified for the new handler (@code{SA_RESTART} will\n"
0d172d3f 176 "always be added if it's available and the system is using restartable\n"
b380b885
MD
177 "system calls.) The return value is a pair with information about the\n"
178 "old handler as described above.\n\n"
179 "This interface does not provide access to the \"signal blocking\"\n"
180 "facility. Maybe this is not needed, since the thread support may\n"
181 "provide solutions to the problem of consistent access to data\n"
182 "structures.")
2fbc8609 183#define FUNC_NAME s_scm_sigaction_for_thread
e1a191a8
GH
184{
185 int csig;
186#ifdef HAVE_SIGACTION
187 struct sigaction action;
188 struct sigaction old_action;
189#else
af68e5e5 190 SIGRETTYPE (* chandler) (int) = SIG_DFL;
e1a191a8
GH
191 SIGRETTYPE (* old_chandler) (int);
192#endif
193 int query_only = 0;
194 int save_handler = 0;
34d19ef6 195
e1a191a8
GH
196 SCM old_handler;
197
34d19ef6 198 SCM_VALIDATE_INUM_COPY (1, signum, csig);
2fbc8609
MV
199 if (csig < 0 || csig > NSIG)
200 SCM_OUT_OF_RANGE (1, signum);
7ee92fce
GH
201#if defined(HAVE_SIGACTION)
202#if defined(SA_RESTART) && defined(HAVE_RESTARTABLE_SYSCALLS)
203 /* don't allow SA_RESTART to be omitted if HAVE_RESTARTABLE_SYSCALLS
204 is defined, since libguile would be likely to produce spurious
205 EINTR errors. */
e1a191a8
GH
206 action.sa_flags = SA_RESTART;
207#else
208 action.sa_flags = 0;
209#endif
210 if (!SCM_UNBNDP (flags))
211 {
34d19ef6 212 SCM_VALIDATE_INUM (3, flags);
e1a191a8
GH
213 action.sa_flags |= SCM_INUM (flags);
214 }
215 sigemptyset (&action.sa_mask);
2fbc8609
MV
216 if (SCM_UNBNDP (thread))
217 thread = scm_current_thread ();
218 else
219 SCM_VALIDATE_THREAD (4, thread);
e1a191a8
GH
220#endif
221 SCM_DEFER_INTS;
2fbc8609 222 old_handler = SCM_VECTOR_REF(*signal_handlers, csig);
e1a191a8
GH
223 if (SCM_UNBNDP (handler))
224 query_only = 1;
9a09deb1 225 else if (SCM_EQ_P (scm_integer_p (handler), SCM_BOOL_T))
e1a191a8 226 {
e4b265d8
DH
227 if (SCM_NUM2LONG (2, handler) == (long) SIG_DFL
228 || SCM_NUM2LONG (2, handler) == (long) SIG_IGN)
e1a191a8
GH
229 {
230#ifdef HAVE_SIGACTION
231 action.sa_handler = (SIGRETTYPE (*) (int)) SCM_INUM (handler);
232#else
233 chandler = (SIGRETTYPE (*) (int)) SCM_INUM (handler);
234#endif
34d19ef6 235 SCM_VECTOR_SET (*signal_handlers, csig, SCM_BOOL_F);
e1a191a8
GH
236 }
237 else
1bbd0b84 238 SCM_OUT_OF_RANGE (2, handler);
e1a191a8
GH
239 }
240 else if (SCM_FALSEP (handler))
241 {
242 /* restore the default handler. */
243#ifdef HAVE_SIGACTION
244 if (orig_handlers[csig].sa_handler == SIG_ERR)
245 query_only = 1;
246 else
247 {
248 action = orig_handlers[csig];
249 orig_handlers[csig].sa_handler = SIG_ERR;
34d19ef6
HWN
250 SCM_VECTOR_SET (*signal_handlers, csig, SCM_BOOL_F);
251
e1a191a8
GH
252 }
253#else
254 if (orig_handlers[csig] == SIG_ERR)
255 query_only = 1;
256 else
257 {
258 chandler = orig_handlers[csig];
259 orig_handlers[csig] = SIG_ERR;
34d19ef6 260 SCM_VECTOR_SET (*signal_handlers, csig, SCM_BOOL_F);
e1a191a8
GH
261 }
262#endif
adb2c53b 263 }
e1a191a8
GH
264 else
265 {
34d19ef6 266 SCM_VALIDATE_NIM (2, handler);
e1a191a8
GH
267#ifdef HAVE_SIGACTION
268 action.sa_handler = take_signal;
269 if (orig_handlers[csig].sa_handler == SIG_ERR)
270 save_handler = 1;
271#else
272 chandler = take_signal;
273 if (orig_handlers[csig] == SIG_ERR)
274 save_handler = 1;
275#endif
34d19ef6 276 SCM_VECTOR_SET (*signal_handlers, csig, handler);
2fbc8609 277 SCM_VECTOR_SET (signal_handler_cells, csig,
e581432e 278 scm_cons (close_1 (handler, signum), SCM_BOOL_F));
2fbc8609 279 SCM_VECTOR_SET (signal_handler_threads, csig, thread);
e1a191a8 280 }
adb2c53b 281
0d172d3f
MV
282 /* XXX - Silently ignore setting handlers for `program error signals'
283 because they can't currently be handled by Scheme code.
284 */
285
286 switch (csig)
287 {
288 /* This list of program error signals is from the GNU Libc
289 Reference Manual */
290 case SIGFPE:
291 case SIGILL:
292 case SIGSEGV:
82893676 293#ifdef SIGBUS
0d172d3f 294 case SIGBUS:
82893676 295#endif
0d172d3f 296 case SIGABRT:
6732de1b 297#if defined(SIGIOT) && (SIGIOT != SIGABRT)
0d172d3f
MV
298 case SIGIOT:
299#endif
82893676 300#ifdef SIGTRAP
0d172d3f 301 case SIGTRAP:
82893676 302#endif
0d172d3f
MV
303#ifdef SIGEMT
304 case SIGEMT:
305#endif
adb2c53b 306#ifdef SIGSYS
0d172d3f 307 case SIGSYS:
adb2c53b 308#endif
0d172d3f
MV
309 query_only = 1;
310 }
311
e1a191a8
GH
312#ifdef HAVE_SIGACTION
313 if (query_only)
314 {
315 if (sigaction (csig, 0, &old_action) == -1)
1bbd0b84 316 SCM_SYSERROR;
e1a191a8
GH
317 }
318 else
319 {
320 if (sigaction (csig, &action , &old_action) == -1)
1bbd0b84 321 SCM_SYSERROR;
e1a191a8
GH
322 if (save_handler)
323 orig_handlers[csig] = old_action;
324 }
325 if (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN)
7ee92fce 326 old_handler = scm_long2num ((long) old_action.sa_handler);
e1a191a8
GH
327 SCM_ALLOW_INTS;
328 return scm_cons (old_handler, SCM_MAKINUM (old_action.sa_flags));
329#else
330 if (query_only)
331 {
332 if ((old_chandler = signal (csig, SIG_IGN)) == SIG_ERR)
1bbd0b84 333 SCM_SYSERROR;
e1a191a8 334 if (signal (csig, old_chandler) == SIG_ERR)
1bbd0b84 335 SCM_SYSERROR;
e1a191a8
GH
336 }
337 else
338 {
339 if ((old_chandler = signal (csig, chandler)) == SIG_ERR)
1bbd0b84 340 SCM_SYSERROR;
e1a191a8
GH
341 if (save_handler)
342 orig_handlers[csig] = old_chandler;
343 }
344 if (old_chandler == SIG_DFL || old_chandler == SIG_IGN)
da6e81b6 345 old_handler = scm_long2num ((long) old_chandler);
e1a191a8
GH
346 SCM_ALLOW_INTS;
347 return scm_cons (old_handler, SCM_MAKINUM (0));
0f2d19dd 348#endif
e1a191a8 349}
1bbd0b84 350#undef FUNC_NAME
e1a191a8 351
adb2c53b 352SCM_DEFINE (scm_restore_signals, "restore-signals", 0, 0, 0,
1bbd0b84 353 (void),
b380b885
MD
354 "Return all signal handlers to the values they had before any call to\n"
355 "@code{sigaction} was made. The return value is unspecified.")
1bbd0b84 356#define FUNC_NAME s_scm_restore_signals
e1a191a8
GH
357{
358 int i;
e1a191a8
GH
359 for (i = 0; i < NSIG; i++)
360 {
361#ifdef HAVE_SIGACTION
362 if (orig_handlers[i].sa_handler != SIG_ERR)
363 {
364 if (sigaction (i, &orig_handlers[i], NULL) == -1)
1bbd0b84 365 SCM_SYSERROR;
e1a191a8 366 orig_handlers[i].sa_handler = SIG_ERR;
34d19ef6 367 SCM_VECTOR_SET (*signal_handlers, i, SCM_BOOL_F);
e1a191a8
GH
368 }
369#else
370 if (orig_handlers[i] != SIG_ERR)
371 {
372 if (signal (i, orig_handlers[i]) == SIG_ERR)
1bbd0b84 373 SCM_SYSERROR;
e1a191a8 374 orig_handlers[i] = SIG_ERR;
34d19ef6 375 SCM_VECTOR_SET (*signal_handlers, i, SCM_BOOL_F);
e1a191a8
GH
376 }
377#endif
378 }
379 return SCM_UNSPECIFIED;
380}
1bbd0b84 381#undef FUNC_NAME
0f2d19dd 382
adb2c53b 383SCM_DEFINE (scm_alarm, "alarm", 1, 0, 0,
1bbd0b84 384 (SCM i),
b380b885
MD
385 "Set a timer to raise a @code{SIGALRM} signal after the specified\n"
386 "number of seconds (an integer). It's advisable to install a signal\n"
387 "handler for\n"
388 "@code{SIGALRM} beforehand, since the default action is to terminate\n"
389 "the process.\n\n"
390 "The return value indicates the time remaining for the previous alarm,\n"
391 "if any. The new value replaces the previous alarm. If there was\n"
392 "no previous alarm, the return value is zero.")
1bbd0b84 393#define FUNC_NAME s_scm_alarm
0f2d19dd
JB
394{
395 unsigned int j;
34d19ef6 396 SCM_VALIDATE_INUM (1, i);
e1a191a8 397 j = alarm (SCM_INUM (i));
0f2d19dd
JB
398 return SCM_MAKINUM (j);
399}
1bbd0b84 400#undef FUNC_NAME
0f2d19dd 401
53f8a0d2
RB
402#ifdef HAVE_SETITIMER
403SCM_DEFINE (scm_setitimer, "setitimer", 5, 0, 0,
404 (SCM which_timer,
405 SCM interval_seconds, SCM interval_microseconds,
406 SCM value_seconds, SCM value_microseconds),
407 "Set the timer specified by @var{which_timer} according to the given\n"
408 "@var{interval_seconds}, @var{interval_microseconds},\n"
409 "@var{value_seconds}, and @var{value_microseconds} values.\n"
410 "\n"
411 "Return information about the timer's previous setting."
412 "\n"
413 "Errors are handled as described in the guile info pages under ``POSIX\n"
414 "Interface Conventions''.\n"
415 "\n"
9401323e 416 "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
53f8a0d2
RB
417 "and @code{ITIMER_PROF}.\n"
418 "\n"
419 "The return value will be a list of two cons pairs representing the\n"
420 "current state of the given timer. The first pair is the seconds and\n"
421 "microseconds of the timer @code{it_interval}, and the second pair is\n"
9401323e 422 "the seconds and microseconds of the timer @code{it_value}.")
53f8a0d2
RB
423#define FUNC_NAME s_scm_setitimer
424{
425 int rv;
426 int c_which_timer;
427 struct itimerval new_timer;
428 struct itimerval old_timer;
429
430 c_which_timer = SCM_NUM2INT(1, which_timer);
431 new_timer.it_interval.tv_sec = SCM_NUM2LONG(2, interval_seconds);
432 new_timer.it_interval.tv_usec = SCM_NUM2LONG(3, interval_microseconds);
433 new_timer.it_value.tv_sec = SCM_NUM2LONG(4, value_seconds);
434 new_timer.it_value.tv_usec = SCM_NUM2LONG(5, value_microseconds);
435
436 SCM_SYSCALL(rv = setitimer(c_which_timer, &new_timer, &old_timer));
437
438 if(rv != 0)
439 SCM_SYSERROR;
440
441 return scm_list_2(scm_cons(scm_long2num(old_timer.it_interval.tv_sec),
442 scm_long2num(old_timer.it_interval.tv_usec)),
443 scm_cons(scm_long2num(old_timer.it_value.tv_sec),
444 scm_long2num(old_timer.it_value.tv_usec)));
445}
446#undef FUNC_NAME
447#endif /* HAVE_SETITIMER */
448
449#ifdef HAVE_GETITIMER
450SCM_DEFINE (scm_getitimer, "getitimer", 1, 0, 0,
451 (SCM which_timer),
452 "Return information about the timer specified by @var{which_timer}"
453 "\n"
454 "Errors are handled as described in the guile info pages under ``POSIX\n"
455 "Interface Conventions''.\n"
456 "\n"
9401323e 457 "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
53f8a0d2
RB
458 "and @code{ITIMER_PROF}.\n"
459 "\n"
460 "The return value will be a list of two cons pairs representing the\n"
461 "current state of the given timer. The first pair is the seconds and\n"
462 "microseconds of the timer @code{it_interval}, and the second pair is\n"
9401323e 463 "the seconds and microseconds of the timer @code{it_value}.")
53f8a0d2
RB
464#define FUNC_NAME s_scm_getitimer
465{
466 int rv;
467 int c_which_timer;
468 struct itimerval old_timer;
469
470 c_which_timer = SCM_NUM2INT(1, which_timer);
471
472 SCM_SYSCALL(rv = getitimer(c_which_timer, &old_timer));
473
474 if(rv != 0)
475 SCM_SYSERROR;
476
477 return scm_list_2(scm_cons(scm_long2num(old_timer.it_interval.tv_sec),
478 scm_long2num(old_timer.it_interval.tv_usec)),
479 scm_cons(scm_long2num(old_timer.it_value.tv_sec),
480 scm_long2num(old_timer.it_value.tv_usec)));
481}
482#undef FUNC_NAME
483#endif /* HAVE_GETITIMER */
484
0e958795 485#ifdef HAVE_PAUSE
adb2c53b 486SCM_DEFINE (scm_pause, "pause", 0, 0, 0,
1bbd0b84 487 (),
b380b885
MD
488 "Pause the current process (thread?) until a signal arrives whose\n"
489 "action is to either terminate the current process or invoke a\n"
490 "handler procedure. The return value is unspecified.")
1bbd0b84 491#define FUNC_NAME s_scm_pause
0f2d19dd
JB
492{
493 pause ();
494 return SCM_UNSPECIFIED;
495}
1bbd0b84 496#undef FUNC_NAME
0e958795 497#endif
0f2d19dd 498
adb2c53b 499SCM_DEFINE (scm_sleep, "sleep", 1, 0, 0,
1bbd0b84 500 (SCM i),
b380b885
MD
501 "Wait for the given number of seconds (an integer) or until a signal\n"
502 "arrives. The return value is zero if the time elapses or the number\n"
503 "of seconds remaining otherwise.")
1bbd0b84 504#define FUNC_NAME s_scm_sleep
0f2d19dd 505{
b74f4728 506 unsigned long j;
34d19ef6 507 SCM_VALIDATE_INUM_MIN (1, i,0);
b74f4728
JB
508#ifdef USE_THREADS
509 j = scm_thread_sleep (SCM_INUM(i));
510#else
e1a191a8 511 j = sleep (SCM_INUM(i));
b74f4728
JB
512#endif
513 return scm_ulong2num (j);
0f2d19dd 514}
1bbd0b84 515#undef FUNC_NAME
0f2d19dd 516
ed618cc9 517#if defined(USE_THREADS) || defined(HAVE_USLEEP) || defined(__MINGW32__)
adb2c53b 518SCM_DEFINE (scm_usleep, "usleep", 1, 0, 0,
1bbd0b84 519 (SCM i),
5352393c
MG
520 "Sleep for I microseconds. @code{usleep} is not available on\n"
521 "all platforms.")
1bbd0b84 522#define FUNC_NAME s_scm_usleep
ce874f2d 523{
34d19ef6 524 SCM_VALIDATE_INUM_MIN (1, i,0);
b74f4728
JB
525
526#ifdef USE_THREADS
527 /* If we have threads, we use the thread system's sleep function. */
528 {
529 unsigned long j = scm_thread_usleep (SCM_INUM (i));
530 return scm_ulong2num (j);
531 }
532#else
0935d604
MD
533#ifdef USLEEP_RETURNS_VOID
534 usleep (SCM_INUM (i));
535 return SCM_INUM0;
536#else
b74f4728
JB
537 {
538 int j = usleep (SCM_INUM (i));
539 return SCM_MAKINUM (j);
540 }
0935d604 541#endif
ce874f2d 542#endif
b74f4728 543}
1bbd0b84 544#undef FUNC_NAME
ed618cc9 545#endif /* USE_THREADS || HAVE_USLEEP || __MINGW32__ */
ce874f2d 546
adb2c53b 547SCM_DEFINE (scm_raise, "raise", 1, 0, 0,
1bbd0b84 548 (SCM sig),
b380b885
MD
549 "Sends a specified signal @var{sig} to the current process, where\n"
550 "@var{sig} is as described for the kill procedure.")
1bbd0b84 551#define FUNC_NAME s_scm_raise
0f2d19dd 552{
34d19ef6 553 SCM_VALIDATE_INUM (1, sig);
e1a191a8
GH
554 SCM_DEFER_INTS;
555 if (kill (getpid (), (int) SCM_INUM (sig)) != 0)
1bbd0b84 556 SCM_SYSERROR;
e1a191a8
GH
557 SCM_ALLOW_INTS;
558 return SCM_UNSPECIFIED;
0f2d19dd 559}
1bbd0b84 560#undef FUNC_NAME
0f2d19dd
JB
561
562\f
0f2d19dd 563
e1a191a8
GH
564void
565scm_init_scmsigs ()
0f2d19dd 566{
e1a191a8
GH
567 int i;
568
569 signal_handlers =
86d31dfe
MV
570 SCM_VARIABLE_LOC (scm_c_define ("signal-handlers",
571 scm_c_make_vector (NSIG, SCM_BOOL_F)));
2fbc8609
MV
572 signal_handler_cells =
573 scm_permanent_object (scm_c_make_vector (NSIG, SCM_BOOL_F));
574 signal_handler_threads =
575 scm_permanent_object (scm_c_make_vector (NSIG, SCM_BOOL_F));
e1a191a8
GH
576
577 for (i = 0; i < NSIG; i++)
578 {
e1a191a8
GH
579#ifdef HAVE_SIGACTION
580 orig_handlers[i].sa_handler = SIG_ERR;
840ae05d 581
e1a191a8
GH
582#else
583 orig_handlers[i] = SIG_ERR;
0f2d19dd 584#endif
840ae05d 585
08b8c694 586#ifdef HAVE_RESTARTABLE_SYSCALLS
7ee92fce 587 /* If HAVE_RESTARTABLE_SYSCALLS is defined, it's important that
adb2c53b 588 signals really are restartable. don't rely on the same
7ee92fce
GH
589 run-time that configure got: reset the default for every signal.
590 */
591#ifdef HAVE_SIGINTERRUPT
592 siginterrupt (i, 0);
de881428 593#elif defined(SA_RESTART)
840ae05d
JB
594 {
595 struct sigaction action;
596
597 sigaction (i, NULL, &action);
598 if (!(action.sa_flags & SA_RESTART))
599 {
3efb80f2 600 action.sa_flags |= SA_RESTART;
840ae05d
JB
601 sigaction (i, &action, NULL);
602 }
603 }
7ee92fce
GH
604#endif
605 /* if neither siginterrupt nor SA_RESTART are available we may
606 as well assume that signals are always restartable. */
840ae05d 607#endif
e1a191a8 608 }
1cc91f1b 609
86d31dfe
MV
610 scm_c_define ("NSIG", scm_long2num (NSIG));
611 scm_c_define ("SIG_IGN", scm_long2num ((long) SIG_IGN));
612 scm_c_define ("SIG_DFL", scm_long2num ((long) SIG_DFL));
e1a191a8 613#ifdef SA_NOCLDSTOP
86d31dfe 614 scm_c_define ("SA_NOCLDSTOP", scm_long2num (SA_NOCLDSTOP));
0f2d19dd 615#endif
e1a191a8 616#ifdef SA_RESTART
86d31dfe 617 scm_c_define ("SA_RESTART", scm_long2num (SA_RESTART));
0f2d19dd 618#endif
1cc91f1b 619
53f8a0d2
RB
620#if defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER)
621 /* Stuff needed by setitimer and getitimer. */
622 scm_c_define ("ITIMER_REAL", SCM_MAKINUM (ITIMER_REAL));
623 scm_c_define ("ITIMER_VIRTUAL", SCM_MAKINUM (ITIMER_VIRTUAL));
624 scm_c_define ("ITIMER_PROF", SCM_MAKINUM (ITIMER_PROF));
625#endif /* defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER) */
626
a0599745 627#include "libguile/scmsigs.x"
0f2d19dd
JB
628}
629
89e00824
ML
630
631/*
632 Local Variables:
633 c-file-style: "gnu"
634 End:
635*/