1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
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)
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.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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.
40 * If you do not wish that, delete this exception notice. */
48 #include "libguile/_scm.h"
50 #include "libguile/async.h"
51 #include "libguile/eval.h"
52 #include "libguile/root.h"
53 #include "libguile/vectors.h"
55 #include "libguile/validate.h"
56 #include "libguile/scmsigs.h"
62 #ifdef HAVE_SYS_TIME_H
66 /* The thread system has its own sleep and usleep functions. */
69 #if defined(MISSING_SLEEP_DECL)
73 #if defined(HAVE_USLEEP) && defined(MISSING_USLEEP_DECL)
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)
84 #define usleep(usec) (Sleep ((usec) / 1000), 0)
85 #define kill(pid, sig) raise (sig)
90 /* SIGRETTYPE is the type that signal handlers return. See <signal.h> */
93 # define SIGRETTYPE RETSIGTYPE
96 # define SIGRETTYPE void
98 # define SIGRETTYPE int
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. */
111 static SCM signal_async
;
113 static char got_signal
[NSIG
];
115 /* a Scheme vector of handler procedures. */
116 static SCM
*signal_handlers
;
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
121 static struct sigaction orig_handlers
[NSIG
];
123 static SIGRETTYPE (*orig_handlers
[NSIG
])(int);
127 take_signal (int signum
)
129 got_signal
[signum
] = 1;
130 scm_system_async_mark_from_signal_handler (signal_async
);
134 sys_deliver_signals (void)
138 for (i
= 0; i
< NSIG
; i
++)
142 /* The flag is reset before calling the handler in case the
143 handler doesn't return. If the handler doesn't return
144 but leaves other signals flagged, they their handlers
145 will be applied some time later when the async is checked
146 again. It would probably be better to reset the flags
147 after doing a longjmp. */
149 #ifndef HAVE_SIGACTION
150 signal (i
, take_signal
);
152 scm_call_1 (SCM_VELTS (*signal_handlers
)[i
], SCM_MAKINUM (i
));
155 return SCM_UNSPECIFIED
;
158 /* user interface for installation of signal handlers. */
159 SCM_DEFINE (scm_sigaction
, "sigaction", 1, 2, 0,
160 (SCM signum
, SCM handler
, SCM flags
),
161 "Install or report the signal handler for a specified signal.\n\n"
162 "@var{signum} is the signal number, which can be specified using the value\n"
163 "of variables such as @code{SIGINT}.\n\n"
164 "If @var{action} is omitted, @code{sigaction} returns a pair: the\n"
165 "CAR is the current\n"
166 "signal hander, which will be either an integer with the value @code{SIG_DFL}\n"
167 "(default action) or @code{SIG_IGN} (ignore), or the Scheme procedure which\n"
168 "handles the signal, or @code{#f} if a non-Scheme procedure handles the\n"
169 "signal. The CDR contains the current @code{sigaction} flags for the handler.\n\n"
170 "If @var{action} is provided, it is installed as the new handler for\n"
171 "@var{signum}. @var{action} can be a Scheme procedure taking one\n"
172 "argument, or the value of @code{SIG_DFL} (default action) or\n"
173 "@code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler\n"
174 "was installed before @code{sigaction} was first used. Flags can\n"
175 "optionally be specified for the new handler (@code{SA_RESTART} will\n"
176 "always be added if it's available and the system is using restartable\n"
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"
183 #define FUNC_NAME s_scm_sigaction
186 #ifdef HAVE_SIGACTION
187 struct sigaction action
;
188 struct sigaction old_action
;
190 SIGRETTYPE (* chandler
) (int) = SIG_DFL
;
191 SIGRETTYPE (* old_chandler
) (int);
194 int save_handler
= 0;
195 SCM
*scheme_handlers
= SCM_VELTS (*signal_handlers
);
198 SCM_VALIDATE_INUM_COPY (1,signum
,csig
);
199 #if defined(HAVE_SIGACTION)
200 #if defined(SA_RESTART) && defined(HAVE_RESTARTABLE_SYSCALLS)
201 /* don't allow SA_RESTART to be omitted if HAVE_RESTARTABLE_SYSCALLS
202 is defined, since libguile would be likely to produce spurious
204 action
.sa_flags
= SA_RESTART
;
208 if (!SCM_UNBNDP (flags
))
210 SCM_VALIDATE_INUM (3,flags
);
211 action
.sa_flags
|= SCM_INUM (flags
);
213 sigemptyset (&action
.sa_mask
);
216 old_handler
= scheme_handlers
[csig
];
217 if (SCM_UNBNDP (handler
))
219 else if (SCM_EQ_P (scm_integer_p (handler
), SCM_BOOL_T
))
221 if (SCM_NUM2LONG (2, handler
) == (long) SIG_DFL
222 || SCM_NUM2LONG (2, handler
) == (long) SIG_IGN
)
224 #ifdef HAVE_SIGACTION
225 action
.sa_handler
= (SIGRETTYPE (*) (int)) SCM_INUM (handler
);
227 chandler
= (SIGRETTYPE (*) (int)) SCM_INUM (handler
);
229 scheme_handlers
[csig
] = SCM_BOOL_F
;
232 SCM_OUT_OF_RANGE (2, handler
);
234 else if (SCM_FALSEP (handler
))
236 /* restore the default handler. */
237 #ifdef HAVE_SIGACTION
238 if (orig_handlers
[csig
].sa_handler
== SIG_ERR
)
242 action
= orig_handlers
[csig
];
243 orig_handlers
[csig
].sa_handler
= SIG_ERR
;
244 scheme_handlers
[csig
] = SCM_BOOL_F
;
247 if (orig_handlers
[csig
] == SIG_ERR
)
251 chandler
= orig_handlers
[csig
];
252 orig_handlers
[csig
] = SIG_ERR
;
253 scheme_handlers
[csig
] = SCM_BOOL_F
;
259 SCM_VALIDATE_NIM (2,handler
);
260 #ifdef HAVE_SIGACTION
261 action
.sa_handler
= take_signal
;
262 if (orig_handlers
[csig
].sa_handler
== SIG_ERR
)
265 chandler
= take_signal
;
266 if (orig_handlers
[csig
] == SIG_ERR
)
269 scheme_handlers
[csig
] = handler
;
272 /* XXX - Silently ignore setting handlers for `program error signals'
273 because they can't currently be handled by Scheme code.
278 /* This list of program error signals is from the GNU Libc
287 #if defined(SIGIOT) && (SIGIOT != SIGABRT)
302 #ifdef HAVE_SIGACTION
305 if (sigaction (csig
, 0, &old_action
) == -1)
310 if (sigaction (csig
, &action
, &old_action
) == -1)
313 orig_handlers
[csig
] = old_action
;
315 if (old_action
.sa_handler
== SIG_DFL
|| old_action
.sa_handler
== SIG_IGN
)
316 old_handler
= scm_long2num ((long) old_action
.sa_handler
);
318 return scm_cons (old_handler
, SCM_MAKINUM (old_action
.sa_flags
));
322 if ((old_chandler
= signal (csig
, SIG_IGN
)) == SIG_ERR
)
324 if (signal (csig
, old_chandler
) == SIG_ERR
)
329 if ((old_chandler
= signal (csig
, chandler
)) == SIG_ERR
)
332 orig_handlers
[csig
] = old_chandler
;
334 if (old_chandler
== SIG_DFL
|| old_chandler
== SIG_IGN
)
335 old_handler
= scm_long2num ((long) old_chandler
);
337 return scm_cons (old_handler
, SCM_MAKINUM (0));
342 SCM_DEFINE (scm_restore_signals
, "restore-signals", 0, 0, 0,
344 "Return all signal handlers to the values they had before any call to\n"
345 "@code{sigaction} was made. The return value is unspecified.")
346 #define FUNC_NAME s_scm_restore_signals
349 SCM
*scheme_handlers
= SCM_VELTS (*signal_handlers
);
351 for (i
= 0; i
< NSIG
; i
++)
353 #ifdef HAVE_SIGACTION
354 if (orig_handlers
[i
].sa_handler
!= SIG_ERR
)
356 if (sigaction (i
, &orig_handlers
[i
], NULL
) == -1)
358 orig_handlers
[i
].sa_handler
= SIG_ERR
;
359 scheme_handlers
[i
] = SCM_BOOL_F
;
362 if (orig_handlers
[i
] != SIG_ERR
)
364 if (signal (i
, orig_handlers
[i
]) == SIG_ERR
)
366 orig_handlers
[i
] = SIG_ERR
;
367 scheme_handlers
[i
] = SCM_BOOL_F
;
371 return SCM_UNSPECIFIED
;
375 SCM_DEFINE (scm_alarm
, "alarm", 1, 0, 0,
377 "Set a timer to raise a @code{SIGALRM} signal after the specified\n"
378 "number of seconds (an integer). It's advisable to install a signal\n"
380 "@code{SIGALRM} beforehand, since the default action is to terminate\n"
382 "The return value indicates the time remaining for the previous alarm,\n"
383 "if any. The new value replaces the previous alarm. If there was\n"
384 "no previous alarm, the return value is zero.")
385 #define FUNC_NAME s_scm_alarm
388 SCM_VALIDATE_INUM (1,i
);
389 j
= alarm (SCM_INUM (i
));
390 return SCM_MAKINUM (j
);
394 #ifdef HAVE_SETITIMER
395 SCM_DEFINE (scm_setitimer
, "setitimer", 5, 0, 0,
397 SCM interval_seconds
, SCM interval_microseconds
,
398 SCM value_seconds
, SCM value_microseconds
),
399 "Set the timer specified by @var{which_timer} according to the given\n"
400 "@var{interval_seconds}, @var{interval_microseconds},\n"
401 "@var{value_seconds}, and @var{value_microseconds} values.\n"
403 "Return information about the timer's previous setting."
405 "Errors are handled as described in the guile info pages under ``POSIX\n"
406 "Interface Conventions''.\n"
408 "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
409 "and @code{ITIMER_PROF}.\n"
411 "The return value will be a list of two cons pairs representing the\n"
412 "current state of the given timer. The first pair is the seconds and\n"
413 "microseconds of the timer @code{it_interval}, and the second pair is\n"
414 "the seconds and microseconds of the timer @code{it_value}.")
415 #define FUNC_NAME s_scm_setitimer
419 struct itimerval new_timer
;
420 struct itimerval old_timer
;
422 c_which_timer
= SCM_NUM2INT(1, which_timer
);
423 new_timer
.it_interval
.tv_sec
= SCM_NUM2LONG(2, interval_seconds
);
424 new_timer
.it_interval
.tv_usec
= SCM_NUM2LONG(3, interval_microseconds
);
425 new_timer
.it_value
.tv_sec
= SCM_NUM2LONG(4, value_seconds
);
426 new_timer
.it_value
.tv_usec
= SCM_NUM2LONG(5, value_microseconds
);
428 SCM_SYSCALL(rv
= setitimer(c_which_timer
, &new_timer
, &old_timer
));
433 return scm_list_2(scm_cons(scm_long2num(old_timer
.it_interval
.tv_sec
),
434 scm_long2num(old_timer
.it_interval
.tv_usec
)),
435 scm_cons(scm_long2num(old_timer
.it_value
.tv_sec
),
436 scm_long2num(old_timer
.it_value
.tv_usec
)));
439 #endif /* HAVE_SETITIMER */
441 #ifdef HAVE_GETITIMER
442 SCM_DEFINE (scm_getitimer
, "getitimer", 1, 0, 0,
444 "Return information about the timer specified by @var{which_timer}"
446 "Errors are handled as described in the guile info pages under ``POSIX\n"
447 "Interface Conventions''.\n"
449 "The timers available are: @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL},\n"
450 "and @code{ITIMER_PROF}.\n"
452 "The return value will be a list of two cons pairs representing the\n"
453 "current state of the given timer. The first pair is the seconds and\n"
454 "microseconds of the timer @code{it_interval}, and the second pair is\n"
455 "the seconds and microseconds of the timer @code{it_value}.")
456 #define FUNC_NAME s_scm_getitimer
460 struct itimerval old_timer
;
462 c_which_timer
= SCM_NUM2INT(1, which_timer
);
464 SCM_SYSCALL(rv
= getitimer(c_which_timer
, &old_timer
));
469 return scm_list_2(scm_cons(scm_long2num(old_timer
.it_interval
.tv_sec
),
470 scm_long2num(old_timer
.it_interval
.tv_usec
)),
471 scm_cons(scm_long2num(old_timer
.it_value
.tv_sec
),
472 scm_long2num(old_timer
.it_value
.tv_usec
)));
475 #endif /* HAVE_GETITIMER */
478 SCM_DEFINE (scm_pause
, "pause", 0, 0, 0,
480 "Pause the current process (thread?) until a signal arrives whose\n"
481 "action is to either terminate the current process or invoke a\n"
482 "handler procedure. The return value is unspecified.")
483 #define FUNC_NAME s_scm_pause
486 return SCM_UNSPECIFIED
;
491 SCM_DEFINE (scm_sleep
, "sleep", 1, 0, 0,
493 "Wait for the given number of seconds (an integer) or until a signal\n"
494 "arrives. The return value is zero if the time elapses or the number\n"
495 "of seconds remaining otherwise.")
496 #define FUNC_NAME s_scm_sleep
499 SCM_VALIDATE_INUM_MIN (1,i
,0);
501 j
= scm_thread_sleep (SCM_INUM(i
));
503 j
= sleep (SCM_INUM(i
));
505 return scm_ulong2num (j
);
509 #if defined(USE_THREADS) || defined(HAVE_USLEEP) || defined(__MINGW32__)
510 SCM_DEFINE (scm_usleep
, "usleep", 1, 0, 0,
512 "Sleep for I microseconds. @code{usleep} is not available on\n"
514 #define FUNC_NAME s_scm_usleep
516 SCM_VALIDATE_INUM_MIN (1,i
,0);
519 /* If we have threads, we use the thread system's sleep function. */
521 unsigned long j
= scm_thread_usleep (SCM_INUM (i
));
522 return scm_ulong2num (j
);
525 #ifdef USLEEP_RETURNS_VOID
526 usleep (SCM_INUM (i
));
530 int j
= usleep (SCM_INUM (i
));
531 return SCM_MAKINUM (j
);
537 #endif /* USE_THREADS || HAVE_USLEEP || __MINGW32__ */
539 SCM_DEFINE (scm_raise
, "raise", 1, 0, 0,
541 "Sends a specified signal @var{sig} to the current process, where\n"
542 "@var{sig} is as described for the kill procedure.")
543 #define FUNC_NAME s_scm_raise
545 SCM_VALIDATE_INUM (1,sig
);
547 if (kill (getpid (), (int) SCM_INUM (sig
)) != 0)
550 return SCM_UNSPECIFIED
;
563 SCM_VARIABLE_LOC (scm_c_define ("signal-handlers",
564 scm_c_make_vector (NSIG
, SCM_BOOL_F
)));
565 /* XXX - use scm_c_make_gsubr here instead of `define'? */
566 thunk
= scm_c_define_gsubr ("%deliver-signals", 0, 0, 0,
567 sys_deliver_signals
);
568 signal_async
= scm_system_async (thunk
);
570 for (i
= 0; i
< NSIG
; i
++)
573 #ifdef HAVE_SIGACTION
574 orig_handlers
[i
].sa_handler
= SIG_ERR
;
577 orig_handlers
[i
] = SIG_ERR
;
580 #ifdef HAVE_RESTARTABLE_SYSCALLS
581 /* If HAVE_RESTARTABLE_SYSCALLS is defined, it's important that
582 signals really are restartable. don't rely on the same
583 run-time that configure got: reset the default for every signal.
585 #ifdef HAVE_SIGINTERRUPT
587 #elif defined(SA_RESTART)
589 struct sigaction action
;
591 sigaction (i
, NULL
, &action
);
592 if (!(action
.sa_flags
& SA_RESTART
))
594 action
.sa_flags
|= SA_RESTART
;
595 sigaction (i
, &action
, NULL
);
599 /* if neither siginterrupt nor SA_RESTART are available we may
600 as well assume that signals are always restartable. */
604 scm_c_define ("NSIG", scm_long2num (NSIG
));
605 scm_c_define ("SIG_IGN", scm_long2num ((long) SIG_IGN
));
606 scm_c_define ("SIG_DFL", scm_long2num ((long) SIG_DFL
));
608 scm_c_define ("SA_NOCLDSTOP", scm_long2num (SA_NOCLDSTOP
));
611 scm_c_define ("SA_RESTART", scm_long2num (SA_RESTART
));
614 #if defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER)
615 /* Stuff needed by setitimer and getitimer. */
616 scm_c_define ("ITIMER_REAL", SCM_MAKINUM (ITIMER_REAL
));
617 scm_c_define ("ITIMER_VIRTUAL", SCM_MAKINUM (ITIMER_VIRTUAL
));
618 scm_c_define ("ITIMER_PROF", SCM_MAKINUM (ITIMER_PROF
));
619 #endif /* defined(HAVE_SETITIMER) || defined(HAVE_GETITIMER) */
621 #ifndef SCM_MAGIC_SNARFER
622 #include "libguile/scmsigs.x"