scsh-0.5/scsh/sighandlers1.c

198 lines
5.1 KiB
C
Raw Normal View History

1996-08-22 16:13:14 -04:00
/* Need to define sig2interrupt vector.
** Interrupt-system mutators should probably hold interrupts while they
** operate.
*/
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
1996-09-12 00:34:28 -04:00
#include <stdio.h>
1996-08-22 16:13:14 -04:00
#include "cstuff.h"
/* Make sure our exports match up w/the implementation: */
#include "sighandlers1.h"
/* Import the OS-dependent set of signals and their translations
** to S48 vm interrupts.
*/
1996-09-12 02:31:17 -04:00
#include "signals1.h"
1996-09-12 00:34:28 -04:00
1996-08-22 16:13:14 -04:00
extern int errno;
extern scheme_value Spending_interruptsS, Sinterrupt_handlersS;
1996-09-12 00:34:28 -04:00
/* Translate Unix signal numbers to S48 interrupt numbers. */
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
int sig2interrupt(int signal)
1996-08-22 16:13:14 -04:00
{
1996-09-12 00:34:28 -04:00
return ( signal < 0 || signal > max_sig ) ? -1 : sig2int[signal];
1996-08-22 16:13:14 -04:00
}
1996-09-12 00:34:28 -04:00
1996-08-22 16:13:14 -04:00
/* Hack the blocked-signal mask.
*******************************************************************************
*/
#include "machine/sigset.h"
int set_procmask(int hi, int lo, int *old_lo_p)
{
sigset_t mask, old_mask;
int old_hi;
make_sigset(&mask, hi, lo);
sigprocmask(SIG_SETMASK, &mask, &old_mask);
split_sigset(old_mask, &old_hi, old_lo_p);
return old_hi;
}
int get_procmask(int *old_lo_p)
{
sigset_t old_mask;
int old_hi;
sigprocmask(SIG_SETMASK, NULL, &old_mask);
split_sigset(old_mask, &old_hi, old_lo_p);
return old_hi;
}
/* Set/Get signal handlers
*******************************************************************************
*/
static void scm_handle_sig(int sig)
{
/*fprintf(stderr, "scm_handle_sig(%d) = int %d\n", sig, sig2int[sig]);*/
1996-09-12 00:34:28 -04:00
Spending_interruptsS |= (1<<sig2int[sig]);
1996-08-22 16:13:14 -04:00
}
1996-09-12 00:34:28 -04:00
/* handler_code: 0 => ignore, 1 => default, 2 => S48 VM */
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
/* Common code for two functions above. */
static scheme_value scsh_ret_sig(int retval, struct sigaction *oldsa,
int *old_hc, int *oflags)
{
if( retval ) {
*old_hc = -1;
*oflags = -1;
1996-08-22 16:13:14 -04:00
return ENTER_FIXNUM(errno);
}
1996-09-12 00:34:28 -04:00
if( oldsa->sa_handler == SIG_IGN ) *old_hc = 0;
else if( oldsa->sa_handler == SIG_DFL ) *old_hc = 1;
else if( oldsa->sa_handler == scm_handle_sig ) *old_hc = 2;
else *old_hc = ENTER_FIXNUM(3); /* Unknown signal handler. */
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
*oflags = oldsa->sa_flags;
1996-08-22 16:13:14 -04:00
return SCHFALSE;
}
1996-09-12 00:34:28 -04:00
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
scheme_value scsh_set_sig(int sig, int handler_code, int flags,
int *old_hc, int *oflags)
1996-08-22 16:13:14 -04:00
{
1996-09-12 00:34:28 -04:00
struct sigaction new, old;
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
sigemptyset(&new.sa_mask); /* WTF */
new.sa_flags = flags;
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
switch( handler_code ) {
case 0: new.sa_handler = SIG_IGN; break;
case 1: new.sa_handler = SIG_DFL; break;
case 2: new.sa_handler = scm_handle_sig; break;
default:
fprintf(stderr, "Impossible handler_code in set_sig_handler: %d\n",
handler_code);
exit(-1);
1996-08-22 16:13:14 -04:00
}
1996-09-12 00:34:28 -04:00
return scsh_ret_sig(sigaction(sig, &new, &old),
&old, old_hc, oflags);
}
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
scheme_value scsh_get_sig(int signal, int *old_hc, int *oflags)
1996-08-22 16:13:14 -04:00
{
1996-09-12 00:34:28 -04:00
struct sigaction old;
return scsh_ret_sig(sigaction(signal, NULL, &old),
&old, old_hc, oflags);
}
1996-08-22 16:13:14 -04:00
/* This guy is responsible for making the default action for a
** Unix signal happen. Because S48's signal handler system is
** interposed between delivery-to-the-process and
** delivery-to-the-scheme-handler, when the user sets a signal
** handler to default, we install a Scheme proc that calls this
** guy, instead of just slapping a SIGDFL in as the Unix handler.
** We only have to do this for signals whose default isn't "ignore," i.e.:
** Posix: SIGALRM SIGHUP SIGINT SIGQUIT SIGTERM SIGUSR1 SIGUSR2
** Non-Posix: SIGINFO SIGPOLL SIGPROF SIGVTALRM SIGXCPU SIGXFSZ SIGIO
** This way, the S48 signal-blocking mechanism can work.
**
** Weird, I know.
*/
void do_default_sigaction(int signal)
{
sigset_t ss, old_ss;
struct sigaction default_action, old_action;
1996-09-12 02:31:17 -04:00
/* fprintf(stderr, "Doing default for signal %d\n", signal); */
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
sigfillset(&ss); /* Block everyone. */
sigprocmask(SIG_SETMASK, &ss, &old_ss);
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
default_action.sa_handler = SIG_DFL; /* Set for default. */
sigemptyset(&default_action.sa_mask);
default_action.sa_flags = 0;
sigaction(signal, &default_action, &old_action);
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
raise(signal); /* Raise the signal. */
sigdelset(&ss, signal);
sigprocmask(SIG_SETMASK, &ss, 0); /* Handle it. */
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
/* Most likely, we'll never get to here, as the default for
** the signals we're handling is "terminate," but we'll play it safe.
*/
sigaction(signal, &old_action, 0); /* Restore old handler, */
sigprocmask(SIG_SETMASK, &old_ss, 0); /* and mask. */
1996-08-22 16:13:14 -04:00
}
1996-09-12 00:34:28 -04:00
/* Set up the Unix signal system the way we want it for scsh. */
void install_scsh_handlers(void)
{
struct sigaction new;
int i;
sigemptyset(&new.sa_mask); /* WTF */
new.sa_handler = scm_handle_sig;
new.sa_flags = 0;
1996-09-12 00:34:28 -04:00
for(i=max_sig; i>=0; i--)
if( sig2int[i] ) {
/* This is a signal we want the S48 interrupt system to handle. */
sigaction(i, &new, 0);
}
/* Turn off SIGPIPE and SIGSYS -- they are handled by synchronous exceptions
** triggered by errno returns.
*/
new.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &new, 0);
#ifdef SIGSYS
sigaction(SIGSYS, &new, 0);
#endif
}
1996-08-22 16:13:14 -04:00
1996-09-12 00:34:28 -04:00
/* Sneak me the S48 interrupt handlers vector. */
scheme_value get_int_handlers(void)
1996-08-22 16:13:14 -04:00
{
1996-09-12 00:34:28 -04:00
return Sinterrupt_handlersS;
1996-08-22 16:13:14 -04:00
}