1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999 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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
54 #include "scm_validate.h"
61 /* The thread system has its own sleep and usleep functions. */
64 #if defined(MISSING_SLEEP_DECL)
68 #if defined(HAVE_USLEEP) && defined(MISSING_USLEEP_DECL)
76 #ifdef USE_MIT_PTHREADS
78 #define signal pthread_signal
83 /* SIGRETTYPE is the type that signal handlers return. See <signal.h> */
86 # define SIGRETTYPE RETSIGTYPE
89 # define SIGRETTYPE void
91 # define SIGRETTYPE int
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. */
104 static SCM signal_async
;
106 static char got_signal
[NSIG
];
108 /* a Scheme vector of handler procedures. */
109 static SCM
*signal_handlers
;
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
114 static struct sigaction orig_handlers
[NSIG
];
116 static SIGRETTYPE (*orig_handlers
)(int)[NSIG
];
120 take_signal (int signum
)
122 int saved_errno
= errno
;
124 if (!scm_ints_disabled
)
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.
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
);
139 got_signal
[signum
] = 1;
141 /* unblock the signal before the scheme handler gets to run, since
142 it may use longjmp to escape (i.e., throw an exception). */
146 sigaddset (&set
, signum
);
147 sigprocmask (SIG_UNBLOCK
, &set
, NULL
);
150 scm_system_async_mark (signal_async
);
155 sys_deliver_signals (void)
159 for (i
= 0; i
< NSIG
; i
++)
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. */
170 #ifndef HAVE_SIGACTION
171 signal (i
, take_signal
);
173 scm_apply (SCM_VELTS (*signal_handlers
)[i
],
174 scm_listify (SCM_MAKINUM (i
), SCM_UNDEFINED
),
178 return SCM_UNSPECIFIED
;
181 /* user interface for installation of signal handlers. */
182 GUILE_PROC(scm_sigaction
, "sigaction", 1, 2, 0,
183 (SCM signum
, SCM handler
, SCM flags
),
184 "Install or report the signal hander for a specified signal.
186 @var{signum} is the signal number, which can be specified using the value
187 of variables such as @code{SIGINT}.
189 If @var{action} is omitted, @code{sigaction} returns a pair: the
191 signal hander, which will be either an integer with the value @code{SIG_DFL}
192 (default action) or @code{SIG_IGN} (ignore), or the Scheme procedure which
193 handles the signal, or @code{#f} if a non-Scheme procedure handles the
194 signal. The CDR contains the current @code{sigaction} flags for the handler.
196 If @var{action} is provided, it is installed as the new handler for
197 @var{signum}. @var{action} can be a Scheme procedure taking one
198 argument, or the value of @code{SIG_DFL} (default action) or
199 @code{SIG_IGN} (ignore), or @code{#f} to restore whatever signal handler
200 was installed before @code{sigaction} was first used. Flags can
201 optionally be specified for the new handler (@code{SA_RESTART} will
202 always be added if it's available and the system is using rstartable
203 system calls.) The return value is a pair with information about the
204 old handler as described above.
206 This interface does not provide access to the \"signal blocking\"
207 facility. Maybe this is not needed, since the thread support may
208 provide solutions to the problem of consistent access to data
210 #define FUNC_NAME s_scm_sigaction
213 #ifdef HAVE_SIGACTION
214 struct sigaction action
;
215 struct sigaction old_action
;
217 SIGRETTYPE (* chandler
) (int);
218 SIGRETTYPE (* old_chandler
) (int);
221 int save_handler
= 0;
222 SCM
*scheme_handlers
= SCM_VELTS (*signal_handlers
);
225 SCM_VALIDATE_INT_COPY(1,signum
,csig
);
226 #if defined(HAVE_SIGACTION)
227 #if defined(SA_RESTART) && defined(HAVE_RESTARTABLE_SYSCALLS)
228 /* don't allow SA_RESTART to be omitted if HAVE_RESTARTABLE_SYSCALLS
229 is defined, since libguile would be likely to produce spurious
231 action
.sa_flags
= SA_RESTART
;
235 if (!SCM_UNBNDP (flags
))
237 SCM_VALIDATE_INT(3,flags
);
238 action
.sa_flags
|= SCM_INUM (flags
);
240 sigemptyset (&action
.sa_mask
);
243 old_handler
= scheme_handlers
[csig
];
244 if (SCM_UNBNDP (handler
))
246 else if (scm_integer_p (handler
) == SCM_BOOL_T
)
248 if (SCM_NUM2LONG (2,handler
) == (long) SIG_DFL
249 || SCM_NUM2LONG (2,handler
) == (long) SIG_IGN
)
251 #ifdef HAVE_SIGACTION
252 action
.sa_handler
= (SIGRETTYPE (*) (int)) SCM_INUM (handler
);
254 chandler
= (SIGRETTYPE (*) (int)) SCM_INUM (handler
);
256 scheme_handlers
[csig
] = SCM_BOOL_F
;
259 SCM_OUT_OF_RANGE (2, handler
);
261 else if (SCM_FALSEP (handler
))
263 /* restore the default handler. */
264 #ifdef HAVE_SIGACTION
265 if (orig_handlers
[csig
].sa_handler
== SIG_ERR
)
269 action
= orig_handlers
[csig
];
270 orig_handlers
[csig
].sa_handler
= SIG_ERR
;
271 scheme_handlers
[csig
] = SCM_BOOL_F
;
274 if (orig_handlers
[csig
] == SIG_ERR
)
278 chandler
= orig_handlers
[csig
];
279 orig_handlers
[csig
] = SIG_ERR
;
280 scheme_handlers
[csig
] = SCM_BOOL_F
;
286 SCM_VALIDATE_NIM (2,handler
);
287 #ifdef HAVE_SIGACTION
288 action
.sa_handler
= take_signal
;
289 if (orig_handlers
[csig
].sa_handler
== SIG_ERR
)
292 chandler
= take_signal
;
293 if (orig_handlers
[csig
] == SIG_ERR
)
296 scheme_handlers
[csig
] = handler
;
298 #ifdef HAVE_SIGACTION
301 if (sigaction (csig
, 0, &old_action
) == -1)
306 if (sigaction (csig
, &action
, &old_action
) == -1)
309 orig_handlers
[csig
] = old_action
;
311 if (old_action
.sa_handler
== SIG_DFL
|| old_action
.sa_handler
== SIG_IGN
)
312 old_handler
= scm_long2num ((long) old_action
.sa_handler
);
314 return scm_cons (old_handler
, SCM_MAKINUM (old_action
.sa_flags
));
318 if ((old_chandler
= signal (csig
, SIG_IGN
)) == SIG_ERR
)
320 if (signal (csig
, old_chandler
) == SIG_ERR
)
325 if ((old_chandler
= signal (csig
, chandler
)) == SIG_ERR
)
328 orig_handlers
[csig
] = old_chandler
;
330 if (old_chandler
== SIG_DFL
|| old_chandler
== SIG_IGN
)
331 old_handler
= scm_long2num (old_chandler
);
333 return scm_cons (old_handler
, SCM_MAKINUM (0));
338 GUILE_PROC (scm_restore_signals
, "restore-signals", 0, 0, 0,
340 "Return all signal handlers to the values they had before any call to
341 @code{sigaction} was made. The return value is unspecified.")
342 #define FUNC_NAME s_scm_restore_signals
345 SCM
*scheme_handlers
= SCM_VELTS (*signal_handlers
);
347 for (i
= 0; i
< NSIG
; i
++)
349 #ifdef HAVE_SIGACTION
350 if (orig_handlers
[i
].sa_handler
!= SIG_ERR
)
352 if (sigaction (i
, &orig_handlers
[i
], NULL
) == -1)
354 orig_handlers
[i
].sa_handler
= SIG_ERR
;
355 scheme_handlers
[i
] = SCM_BOOL_F
;
358 if (orig_handlers
[i
] != SIG_ERR
)
360 if (signal (i
, orig_handlers
[i
]) == SIG_ERR
)
362 orig_handlers
[i
] = SIG_ERR
;
363 scheme_handlers
[i
] = SCM_BOOL_F
;
367 return SCM_UNSPECIFIED
;
371 GUILE_PROC(scm_alarm
, "alarm", 1, 0, 0,
373 "Set a timer to raise a @code{SIGALRM} signal after the specified
374 number of seconds (an integer). It's advisable to install a signal
376 @code{SIGALRM} beforehand, since the default action is to terminate
379 The return value indicates the time remaining for the previous alarm,
380 if any. The new value replaces the previous alarm. If there was
381 no previous alarm, the return value is zero.")
382 #define FUNC_NAME s_scm_alarm
385 SCM_VALIDATE_INT(1,i
);
386 j
= alarm (SCM_INUM (i
));
387 return SCM_MAKINUM (j
);
392 GUILE_PROC(scm_pause
, "pause", 0, 0, 0,
394 "Pause the current process (thread?) until a signal arrives whose
395 action is to either terminate the current process or invoke a
396 handler procedure. The return value is unspecified.")
397 #define FUNC_NAME s_scm_pause
400 return SCM_UNSPECIFIED
;
405 GUILE_PROC(scm_sleep
, "sleep", 1, 0, 0,
407 "Wait for the given number of seconds (an integer) or until a signal
408 arrives. The return value is zero if the time elapses or the number
409 of seconds remaining otherwise.")
410 #define FUNC_NAME s_scm_sleep
413 SCM_VALIDATE_INT_MIN(1,i
,0);
415 j
= scm_thread_sleep (SCM_INUM(i
));
417 j
= sleep (SCM_INUM(i
));
419 return scm_ulong2num (j
);
423 #if defined(USE_THREADS) || defined(HAVE_USLEEP)
424 GUILE_PROC(scm_usleep
, "usleep", 1, 0, 0,
427 #define FUNC_NAME s_scm_usleep
429 SCM_VALIDATE_INT_MIN(1,i
,0);
432 /* If we have threads, we use the thread system's sleep function. */
434 unsigned long j
= scm_thread_usleep (SCM_INUM (i
));
435 return scm_ulong2num (j
);
438 #ifdef USLEEP_RETURNS_VOID
439 usleep (SCM_INUM (i
));
443 int j
= usleep (SCM_INUM (i
));
444 return SCM_MAKINUM (j
);
450 #endif /* GUILE_ISELECT || HAVE_USLEEP */
452 GUILE_PROC(scm_raise
, "raise", 1, 0, 0,
455 Sends a specified signal @var{sig} to the current process, where
456 @var{sig} is as described for the kill procedure.")
457 #define FUNC_NAME s_scm_raise
459 SCM_VALIDATE_INT(1,sig
);
461 if (kill (getpid (), (int) SCM_INUM (sig
)) != 0)
464 return SCM_UNSPECIFIED
;
477 SCM_CDRLOC (scm_sysintern ("signal-handlers",
478 scm_make_vector (SCM_MAKINUM (NSIG
),
480 thunk
= scm_make_gsubr ("%deliver-signals", 0, 0, 0,
481 sys_deliver_signals
);
482 signal_async
= scm_system_async (thunk
);
484 for (i
= 0; i
< NSIG
; i
++)
487 #ifdef HAVE_SIGACTION
488 orig_handlers
[i
].sa_handler
= SIG_ERR
;
491 orig_handlers
[i
] = SIG_ERR
;
494 #ifdef HAVE_RESTARTABLE_SYSCALLS
495 /* If HAVE_RESTARTABLE_SYSCALLS is defined, it's important that
496 signals really are restartable. don't rely on the same
497 run-time that configure got: reset the default for every signal.
499 #ifdef HAVE_SIGINTERRUPT
501 #elif defined(SA_RESTART)
503 struct sigaction action
;
505 sigaction (i
, NULL
, &action
);
506 if (!(action
.sa_flags
& SA_RESTART
))
508 action
.sa_flags
&= SA_RESTART
;
509 sigaction (i
, &action
, NULL
);
513 /* if neither siginterrupt nor SA_RESTART are available we may
514 as well assume that signals are always restartable. */
518 scm_sysintern ("NSIG", scm_long2num (NSIG
));
519 scm_sysintern ("SIG_IGN", scm_long2num ((long) SIG_IGN
));
520 scm_sysintern ("SIG_DFL", scm_long2num ((long) SIG_DFL
));
522 scm_sysintern ("SA_NOCLDSTOP", scm_long2num (SA_NOCLDSTOP
));
525 scm_sysintern ("SA_RESTART", scm_long2num (SA_RESTART
));