66cb7fa6a5d73fdabe8c3713dd5ea7769b34e2fe
1 /* Copyright (C) 1995,1996,1997 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. */
55 /* The thread system has its own sleep and usleep functions. */
58 #if defined(MISSING_SLEEP_DECL)
62 #if defined(HAVE_USLEEP) && defined(MISSING_USLEEP_DECL)
70 #ifdef USE_MIT_PTHREADS
72 #define signal pthread_signal
77 /* SIGRETTYPE is the type that signal handlers return. See <signal.h> */
80 # define SIGRETTYPE RETSIGTYPE
83 # define SIGRETTYPE void
85 # define SIGRETTYPE int
91 /* take_signal is installed as the C signal handler whenever a Scheme
92 handler is set. when a signal arrives, take_signal marks the corresponding
93 element of got_signal and marks signal_async. the thunk in signal_async
94 (sys_deliver_signals) will be run at the next opportunity, outside a
95 critical section. sys_deliver_signals runs each Scheme handler for
96 which got_signal is set. */
98 static SCM signal_async
;
100 static char got_signal
[NSIG
];
102 /* a Scheme vector of handler procedures. */
103 static SCM
*signal_handlers
;
105 /* saves the original C handlers, when a new handler is installed.
106 set to SIG_ERR if the original handler is installed. */
107 #ifdef HAVE_SIGACTION
108 static struct sigaction orig_handlers
[NSIG
];
110 static SIGRETTYPE (*orig_handlers
)(int)[NSIG
];
114 take_signal (int signum
)
117 if (!scm_ints_disabled
)
119 /* For reasons of speed, the SCM_NEWCELL macro doesn't defer
120 interrupts. Instead, it first sets its argument to point to
121 the first cell in the list, and then advances the freelist
122 pointer to the next cell. Now, if this procedure is
123 interrupted, the only anomalous state possible is to have
124 both SCM_NEWCELL's argument and scm_freelist pointing to the
125 same cell. To deal with this case, we always throw away the
126 first cell in scm_freelist here.
128 At least, that's the theory. I'm not convinced that that's
129 the only anomalous path we need to worry about. */
130 SCM_NEWCELL (ignored
);
132 got_signal
[signum
] = 1;
134 /* unblock the signal before the scheme handler gets to run, since
135 it may use longjmp to escape (i.e., throw an exception). */
139 sigaddset (&set
, signum
);
140 sigprocmask (SIG_UNBLOCK
, &set
, NULL
);
143 scm_system_async_mark (signal_async
);
147 sys_deliver_signals (void)
151 for (i
= 0; i
< NSIG
; i
++)
155 /* The flag is reset before calling the handler in case the
156 handler doesn't return. If the handler doesn't return
157 but leaves other signals flagged, they their handlers
158 will be applied some time later when the async is checked
159 again. It would probably be better to reset the flags
160 after doing a longjmp. */
162 #ifndef HAVE_SIGACTION
163 signal (i
, take_signal
);
165 scm_apply (SCM_VELTS (*signal_handlers
)[i
],
166 scm_listify (SCM_MAKINUM (i
), SCM_UNDEFINED
),
170 return SCM_UNSPECIFIED
;
173 /* user interface for installation of signal handlers. */
174 SCM_PROC(s_sigaction
, "sigaction", 1, 2, 0, scm_sigaction
);
176 scm_sigaction (SCM signum
, SCM handler
, SCM flags
)
179 #ifdef HAVE_SIGACTION
180 struct sigaction action
;
181 struct sigaction old_action
;
183 SIGRETTYPE (* chandler
) (int);
184 SIGRETTYPE (* old_chandler
) (int);
187 int save_handler
= 0;
188 SCM
*scheme_handlers
= SCM_VELTS (*signal_handlers
);
191 SCM_ASSERT (SCM_INUMP (signum
), signum
, SCM_ARG1
, s_sigaction
);
192 csig
= SCM_INUM (signum
);
193 #ifdef HAVE_SIGACTION
194 /* always use restartable system calls if available. */
196 action
.sa_flags
= SA_RESTART
;
200 if (!SCM_UNBNDP (flags
))
202 SCM_ASSERT (SCM_INUMP (flags
), flags
, SCM_ARG3
, s_sigaction
);
203 action
.sa_flags
|= SCM_INUM (flags
);
205 sigemptyset (&action
.sa_mask
);
208 old_handler
= scheme_handlers
[csig
];
209 if (SCM_UNBNDP (handler
))
211 else if (SCM_INUMP (handler
))
213 if (SCM_INUM (handler
) == (int) SIG_DFL
214 || SCM_INUM (handler
) == (int) SIG_IGN
)
216 #ifdef HAVE_SIGACTION
217 action
.sa_handler
= (SIGRETTYPE (*) (int)) SCM_INUM (handler
);
219 chandler
= (SIGRETTYPE (*) (int)) SCM_INUM (handler
);
221 scheme_handlers
[csig
] = SCM_BOOL_F
;
224 scm_out_of_range (s_sigaction
, handler
);
226 else if (SCM_FALSEP (handler
))
228 /* restore the default handler. */
229 #ifdef HAVE_SIGACTION
230 if (orig_handlers
[csig
].sa_handler
== SIG_ERR
)
234 action
= orig_handlers
[csig
];
235 orig_handlers
[csig
].sa_handler
= SIG_ERR
;
236 scheme_handlers
[csig
] = SCM_BOOL_F
;
239 if (orig_handlers
[csig
] == SIG_ERR
)
243 chandler
= orig_handlers
[csig
];
244 orig_handlers
[csig
] = SIG_ERR
;
245 scheme_handlers
[csig
] = SCM_BOOL_F
;
251 SCM_ASSERT (SCM_NIMP (handler
), handler
, SCM_ARG2
, s_sigaction
);
252 #ifdef HAVE_SIGACTION
253 action
.sa_handler
= take_signal
;
254 if (orig_handlers
[csig
].sa_handler
== SIG_ERR
)
257 chandler
= take_signal
;
258 if (orig_handlers
[csig
] == SIG_ERR
)
261 scheme_handlers
[csig
] = handler
;
263 #ifdef HAVE_SIGACTION
266 if (sigaction (csig
, 0, &old_action
) == -1)
267 scm_syserror (s_sigaction
);
271 if (sigaction (csig
, &action
, &old_action
) == -1)
272 scm_syserror (s_sigaction
);
274 orig_handlers
[csig
] = old_action
;
276 if (old_action
.sa_handler
== SIG_DFL
|| old_action
.sa_handler
== SIG_IGN
)
277 old_handler
= SCM_MAKINUM ((int) old_action
.sa_handler
);
279 return scm_cons (old_handler
, SCM_MAKINUM (old_action
.sa_flags
));
283 if ((old_chandler
= signal (csig
, SIG_IGN
)) == SIG_ERR
)
284 scm_syserror (s_sigaction
);
285 if (signal (csig
, old_chandler
) == SIG_ERR
)
286 scm_syserror (s_sigaction
);
290 if ((old_chandler
= signal (csig
, chandler
)) == SIG_ERR
)
291 scm_syserror (s_sigaction
);
293 orig_handlers
[csig
] = old_chandler
;
295 if (old_chandler
== SIG_DFL
|| old_chandler
== SIG_IGN
)
296 old_handler
= SCM_MAKINUM ((int) old_chandler
);
298 return scm_cons (old_handler
, SCM_MAKINUM (0));
302 SCM_PROC (s_restore_signals
, "restore-signals", 0, 0, 0, scm_restore_signals
);
304 scm_restore_signals (void)
307 SCM
*scheme_handlers
= SCM_VELTS (*signal_handlers
);
309 for (i
= 0; i
< NSIG
; i
++)
311 #ifdef HAVE_SIGACTION
312 if (orig_handlers
[i
].sa_handler
!= SIG_ERR
)
314 if (sigaction (i
, &orig_handlers
[i
], NULL
) == -1)
315 scm_syserror (s_restore_signals
);
316 orig_handlers
[i
].sa_handler
= SIG_ERR
;
317 scheme_handlers
[i
] = SCM_BOOL_F
;
320 if (orig_handlers
[i
] != SIG_ERR
)
322 if (signal (i
, orig_handlers
[i
]) == SIG_ERR
)
323 scm_syserror (s_restore_signals
);
324 orig_handlers
[i
] = SIG_ERR
;
325 scheme_handlers
[i
] = SCM_BOOL_F
;
329 return SCM_UNSPECIFIED
;
332 SCM_PROC(s_alarm
, "alarm", 1, 0, 0, scm_alarm
);
339 SCM_ASSERT (SCM_INUMP (i
) && (SCM_INUM (i
) >= 0), i
, SCM_ARG1
, s_alarm
);
340 j
= alarm (SCM_INUM (i
));
341 return SCM_MAKINUM (j
);
345 SCM_PROC(s_pause
, "pause", 0, 0, 0, scm_pause
);
351 return SCM_UNSPECIFIED
;
355 SCM_PROC(s_sleep
, "sleep", 1, 0, 0, scm_sleep
);
362 SCM_ASSERT (SCM_INUMP (i
) && (SCM_INUM (i
) >= 0), i
, SCM_ARG1
, s_sleep
);
364 j
= scm_thread_sleep (SCM_INUM(i
));
366 j
= sleep (SCM_INUM(i
));
368 return scm_ulong2num (j
);
371 #if defined(USE_THREADS) || defined(HAVE_USLEEP)
372 SCM_PROC(s_usleep
, "usleep", 1, 0, 0, scm_usleep
);
378 SCM_ASSERT (SCM_INUMP (i
) && (SCM_INUM (i
) >= 0), i
, SCM_ARG1
, s_usleep
);
381 /* If we have threads, we use the thread system's sleep function. */
383 unsigned long j
= scm_thread_usleep (SCM_INUM (i
));
384 return scm_ulong2num (j
);
387 #ifdef USLEEP_RETURNS_VOID
388 usleep (SCM_INUM (i
));
392 int j
= usleep (SCM_INUM (i
));
393 return SCM_MAKINUM (j
);
398 #endif /* GUILE_ISELECT || HAVE_USLEEP */
400 SCM_PROC(s_raise
, "raise", 1, 0, 0, scm_raise
);
406 SCM_ASSERT(SCM_INUMP(sig
), sig
, SCM_ARG1
, s_raise
);
408 if (kill (getpid (), (int) SCM_INUM (sig
)) != 0)
409 scm_syserror (s_raise
);
411 return SCM_UNSPECIFIED
;
423 SCM_CDRLOC (scm_sysintern ("signal-handlers",
424 scm_make_vector (SCM_MAKINUM (NSIG
),
426 thunk
= scm_make_gsubr ("%deliver-signals", 0, 0, 0,
427 sys_deliver_signals
);
428 signal_async
= scm_system_async (thunk
);
430 for (i
= 0; i
< NSIG
; i
++)
433 #ifdef HAVE_SIGACTION
434 orig_handlers
[i
].sa_handler
= SIG_ERR
;
436 orig_handlers
[i
] = SIG_ERR
;
440 scm_sysintern ("NSIG", scm_long2num (NSIG
));
441 scm_sysintern ("SIG_IGN", scm_long2num ((long) SIG_IGN
));
442 scm_sysintern ("SIG_DFL", scm_long2num ((long) SIG_DFL
));
444 scm_sysintern ("SA_NOCLDSTOP", scm_long2num (SA_NOCLDSTOP
));
447 scm_sysintern ("SA_RESTART", scm_long2num (SA_RESTART
));