2003-09-02 05:51:35 -04:00
|
|
|
/* signal.c
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@zoy.org>, Paris
|
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
#include "unix.h"
|
|
|
|
|
|
|
|
static Object Sym_Exit, Sym_Default, Sym_Ignore;
|
|
|
|
|
|
|
|
static SYMDESCR Signal_Syms[] = {
|
2003-09-19 04:54:08 -04:00
|
|
|
#ifdef SIGALRM
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sigalrm", SIGALRM },
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
2003-08-19 15:19:38 -04:00
|
|
|
#ifdef SIGBUS
|
|
|
|
{ "sigbus", SIGBUS },
|
|
|
|
#endif
|
|
|
|
{ "sigfpe", SIGFPE },
|
2003-09-19 04:54:08 -04:00
|
|
|
#ifdef SIGHUP
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sighup", SIGHUP },
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sigill", SIGILL },
|
|
|
|
{ "sigint", SIGINT },
|
2003-09-19 04:54:08 -04:00
|
|
|
#ifdef SIGKILL
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sigkill", SIGKILL },
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sigpipe", SIGPIPE },
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sigquit", SIGQUIT },
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
2003-08-19 15:19:38 -04:00
|
|
|
{ "sigsegv", SIGSEGV },
|
|
|
|
{ "sigterm", SIGTERM },
|
|
|
|
#ifdef SIGABRT
|
|
|
|
{ "sigabrt", SIGABRT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGAIO
|
|
|
|
{ "sigaio", SIGAIO },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGARRAYSIZE
|
|
|
|
{ "sigarraysize", SIGARRAYSIZE },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
{ "sigchld", SIGCHLD },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCLD
|
|
|
|
{ "sigcld", SIGCLD },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
|
|
|
{ "sigcont", SIGCONT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGDANGER
|
|
|
|
{ "sigdanger", SIGDANGER },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGDIL
|
|
|
|
{ "sigdil", SIGDIL },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGEMT
|
|
|
|
{ "sigemt", SIGEMT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGGRANT
|
|
|
|
{ "siggrant", SIGGRANT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINFO
|
|
|
|
{ "siginfo", SIGINFO },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
|
|
|
{ "sigio", SIGIO },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIOINT
|
|
|
|
{ "sigioint", SIGIOINT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
|
|
|
{ "sigiot", SIGIOT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
|
|
|
{ "siglost", SIGLOST },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGLWP
|
|
|
|
{ "siglwp", SIGLWP },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGMIGRATE
|
|
|
|
{ "sigmigrate", SIGMIGRATE },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGMSG
|
|
|
|
{ "sigmsg", SIGMSG },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
{ "sigpoll", SIGPOLL },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPRE
|
|
|
|
{ "sigpre", SIGPRE },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
|
|
{ "sigprof", SIGPROF },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPTY
|
|
|
|
{ "sigpty", SIGPTY },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
{ "sigpwr", SIGPWR },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGRESERVE
|
|
|
|
{ "sigreserve", SIGRESERVE },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGRETRACT
|
|
|
|
{ "sigretract", SIGRETRACT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSAK
|
|
|
|
{ "sigsak", SIGSAK },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSOUND
|
|
|
|
{ "sigsound", SIGSOUND },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
|
|
|
{ "sigstop", SIGSTOP },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
|
|
|
{ "sigsys", SIGSYS },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
{ "sigtrap", SIGTRAP },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
{ "sigtstp", SIGTSTP },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
|
|
{ "sigttin", SIGTTIN },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
|
|
|
{ "sigttou", SIGTTOU },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
|
|
|
{ "sigurg", SIGURG },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
{ "sigusr1", SIGUSR1 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
{ "sigusr2", SIGUSR2 },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGVIRT
|
|
|
|
{ "sigvirt", SIGVIRT },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
|
|
|
{ "sigvtalrm", SIGVTALRM },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGWAITING
|
|
|
|
{ "sigwaiting", SIGWAITING },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
{ "sigwinch", SIGWINCH },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGWINDOW
|
|
|
|
{ "sigwindow", SIGWINDOW },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
{ "sigxcpu", SIGXCPU },
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
{ "sigxfsz", SIGXFSZ },
|
|
|
|
#endif
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
static Object P_Kill(Object pid, Object sig) {
|
2003-09-19 04:54:08 -04:00
|
|
|
#ifndef WIN32
|
2003-08-19 15:19:38 -04:00
|
|
|
int t, s;
|
|
|
|
|
|
|
|
if ((t = TYPE(sig)) == T_Fixnum || t == T_Bignum)
|
2003-09-02 04:12:11 -04:00
|
|
|
s = Get_Integer(sig);
|
2003-08-19 15:19:38 -04:00
|
|
|
else if (t == T_Symbol)
|
2003-09-02 04:12:11 -04:00
|
|
|
s = Symbols_To_Bits(sig, 0, Signal_Syms);
|
2003-08-19 15:19:38 -04:00
|
|
|
else
|
2003-09-02 04:12:11 -04:00
|
|
|
Wrong_Type_Combination(sig, "symbol or integer");
|
2003-08-19 15:19:38 -04:00
|
|
|
if (kill(Get_Integer(pid), s) == -1)
|
2003-09-02 04:12:11 -04:00
|
|
|
Raise_System_Error("~E");
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
2003-08-19 15:19:38 -04:00
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Object P_List_Signals() {
|
|
|
|
return Syms_To_List(Signal_Syms);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Object P_Pause() {
|
2003-09-19 04:54:08 -04:00
|
|
|
#ifndef WIN32
|
2003-08-19 15:19:38 -04:00
|
|
|
pause();
|
|
|
|
Fatal_Error("pause() returned unexpectedly");
|
2003-09-19 04:54:08 -04:00
|
|
|
#endif
|
|
|
|
return Void;
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
|
2003-09-09 11:18:55 -04:00
|
|
|
#if defined(HAVE_SIGPROCMASK) || defined(HAVE_SIGBLOCK)
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
static Object Handlers;
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
static Object P_Alarm(Object s) {
|
2003-08-19 15:19:38 -04:00
|
|
|
return Make_Unsigned(alarm(Get_Unsigned(s)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
2003-09-04 09:29:16 -04:00
|
|
|
void General_Handler(int sig) {
|
2003-08-19 15:19:38 -04:00
|
|
|
Object fun, args;
|
|
|
|
|
2003-09-09 11:18:55 -04:00
|
|
|
#if defined(HAVE_SIGPROCMASK)
|
2003-08-19 15:19:38 -04:00
|
|
|
(void)signal(sig, General_Handler);
|
|
|
|
#endif
|
|
|
|
Set_Error_Tag("signal-handler");
|
|
|
|
Reset_IO(1);
|
|
|
|
args = Bits_To_Symbols((unsigned long)sig, 0, Signal_Syms);
|
|
|
|
args = Cons(args, Null);
|
|
|
|
fun = VECTOR(Handlers)->data[sig];
|
|
|
|
if (TYPE(fun) != T_Compound)
|
2003-09-02 04:12:11 -04:00
|
|
|
Fatal_Error("no handler for signal %d", sig);
|
2003-08-19 15:19:38 -04:00
|
|
|
(void)Funcall(fun, args, 0);
|
|
|
|
Format(Curr_Output_Port, "~%\7Signal!~%", 15, 0, (Object *)0);
|
|
|
|
(void)P_Reset();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
static Object Action_To_Sym(void (*act)()) {
|
2003-08-19 15:19:38 -04:00
|
|
|
char *sym;
|
|
|
|
|
|
|
|
if (act == Signal_Exit)
|
2003-09-02 04:12:11 -04:00
|
|
|
sym = "exit";
|
2003-08-19 15:19:38 -04:00
|
|
|
else if (act == SIG_IGN)
|
2003-09-02 04:12:11 -04:00
|
|
|
sym = "ignore";
|
2003-08-19 15:19:38 -04:00
|
|
|
else if (act == SIG_DFL || act == (void (*)())-1)
|
2003-09-02 04:12:11 -04:00
|
|
|
sym = "default";
|
2003-08-19 15:19:38 -04:00
|
|
|
else
|
2003-09-02 04:12:11 -04:00
|
|
|
sym = "handler";
|
2003-08-19 15:19:38 -04:00
|
|
|
return Intern(sym);
|
|
|
|
}
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
void Add_To_Mask(int sig) {
|
2003-09-09 11:18:55 -04:00
|
|
|
#ifdef HAVE_SIGPROCMASK
|
2003-08-19 15:19:38 -04:00
|
|
|
sigaddset(&Sigset_Block, sig);
|
|
|
|
#else
|
|
|
|
Sigmask_Block |= sigmask(sig);
|
|
|
|
#endif
|
|
|
|
if (Intr_Level > 0) /* make sure new signal gets blocked */
|
2003-09-02 04:12:11 -04:00
|
|
|
Force_Disable_Interrupts;
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
void Remove_From_Mask(int sig) {
|
2003-09-09 11:18:55 -04:00
|
|
|
#ifdef HAVE_SIGPROCMASK
|
2003-08-19 15:19:38 -04:00
|
|
|
sigdelset(&Sigset_Block, sig);
|
|
|
|
#else
|
|
|
|
Sigmask_Block &= ~sigmask(sig);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
static Object P_Signal(int argc, Object *argv) {
|
2003-08-19 15:19:38 -04:00
|
|
|
int sig;
|
|
|
|
Object handler, old;
|
|
|
|
void (*disp)();
|
|
|
|
|
|
|
|
sig = Symbols_To_Bits(argv[0], 0, Signal_Syms);
|
|
|
|
if (sig >= NSIG)
|
2003-09-02 04:12:11 -04:00
|
|
|
Fatal_Error("signal %d >= NSIG", sig);
|
2003-08-19 15:19:38 -04:00
|
|
|
if (argc == 1) {
|
2003-09-02 04:12:11 -04:00
|
|
|
handler = VECTOR(Handlers)->data[sig];
|
|
|
|
if (Truep(handler))
|
|
|
|
return handler;
|
|
|
|
if ((disp = signal(sig, SIG_DFL)) != SIG_DFL)
|
|
|
|
(void)signal(sig, disp);
|
|
|
|
return Action_To_Sym(disp);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
switch (sig) {
|
|
|
|
#ifdef SIGBUS
|
|
|
|
case SIGBUS:
|
|
|
|
#endif
|
|
|
|
case SIGFPE:
|
|
|
|
case SIGILL:
|
|
|
|
case SIGINT:
|
|
|
|
case SIGKILL:
|
|
|
|
case SIGSEGV:
|
|
|
|
#ifdef SIGABRT
|
|
|
|
case SIGABRT:
|
|
|
|
#endif
|
2003-09-02 04:12:11 -04:00
|
|
|
Primitive_Error("changing signal ~s not permitted", argv[0]);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
handler = argv[1];
|
|
|
|
if (EQ(handler, Sym_Exit)) {
|
2003-09-02 04:12:11 -04:00
|
|
|
disp = Signal_Exit;
|
2003-08-19 15:19:38 -04:00
|
|
|
} else if (EQ(handler, Sym_Default)) {
|
2003-09-02 04:12:11 -04:00
|
|
|
disp = SIG_DFL;
|
2003-08-19 15:19:38 -04:00
|
|
|
} else if (EQ(handler, Sym_Ignore)) {
|
2003-09-02 04:12:11 -04:00
|
|
|
disp = SIG_IGN;
|
2003-08-19 15:19:38 -04:00
|
|
|
} else if (TYPE(handler) == T_Compound) {
|
2003-09-02 04:12:11 -04:00
|
|
|
if (COMPOUND(handler)->min_args > 1 ||
|
|
|
|
COMPOUND(handler)->max_args == 0)
|
|
|
|
Primitive_Error("handler expects wrong number of args");
|
|
|
|
disp = General_Handler;
|
2003-08-19 15:19:38 -04:00
|
|
|
} else
|
2003-09-02 04:12:11 -04:00
|
|
|
Primitive_Error("invalid handler: ~s", handler);
|
2003-08-19 15:19:38 -04:00
|
|
|
old = VECTOR(Handlers)->data[sig];
|
|
|
|
VECTOR(Handlers)->data[sig] = (disp == General_Handler) ? handler : False;
|
|
|
|
if (disp == General_Handler)
|
2003-09-02 04:12:11 -04:00
|
|
|
Add_To_Mask(sig);
|
2003-08-19 15:19:38 -04:00
|
|
|
else
|
2003-09-02 04:12:11 -04:00
|
|
|
Remove_From_Mask(sig);
|
2003-08-19 15:19:38 -04:00
|
|
|
if ((disp = signal(sig, disp)) == (void (*)())-1)
|
2003-09-02 04:12:11 -04:00
|
|
|
Raise_System_Error("~E");
|
2003-08-19 15:19:38 -04:00
|
|
|
return Truep(old) ? old : Action_To_Sym(disp);
|
|
|
|
}
|
2003-09-09 11:18:55 -04:00
|
|
|
#endif /* defined(HAVE_SIGPROCMASK) || defined(HAVE_SIGBLOCK) */
|
2003-08-19 15:19:38 -04:00
|
|
|
|
2003-09-04 09:29:16 -04:00
|
|
|
void elk_init_unix_signal() {
|
2003-08-19 15:19:38 -04:00
|
|
|
Define_Symbol(&Sym_Exit, "exit");
|
|
|
|
Define_Symbol(&Sym_Default, "default");
|
|
|
|
Define_Symbol(&Sym_Ignore, "ignore");
|
|
|
|
Def_Prim(P_Kill, "unix-kill", 2, 2, EVAL);
|
|
|
|
Def_Prim(P_List_Signals, "unix-list-signals", 0, 0, EVAL);
|
|
|
|
Def_Prim(P_Pause, "unix-pause", 0, 0, EVAL);
|
2003-09-09 11:18:55 -04:00
|
|
|
#if defined(HAVE_SIGPROCMASK) || defined(HAVE_SIGBLOCK)
|
2003-08-19 15:19:38 -04:00
|
|
|
Def_Prim(P_Alarm, "unix-alarm", 1, 1, EVAL);
|
|
|
|
Handlers = Make_Vector(NSIG, False);
|
|
|
|
Global_GC_Link(Handlers);
|
|
|
|
Def_Prim(P_Signal, "unix-signal", 1, 2, VARARGS);
|
|
|
|
P_Provide(Intern("unix:reliable-signals"));
|
|
|
|
#endif
|
|
|
|
}
|