2000-05-09 00:53:16 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
signal.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Tue Dec 20 10:13:44 JST 1994
|
|
|
|
|
2001-02-14 00:52:06 -05:00
|
|
|
Copyright (C) 1993-2001 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
2000-05-01 05:42:38 -04:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-19 23:59:39 -05:00
|
|
|
#include "rubysig.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef __BEOS__
|
|
|
|
#undef SIGBUS
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#ifndef NSIG
|
1999-01-19 23:59:39 -05:00
|
|
|
# ifdef DJGPP
|
|
|
|
# define NSIG SIGMAX
|
|
|
|
# else
|
|
|
|
# define NSIG (_SIGMAX + 1) /* For QNX */
|
|
|
|
# endif
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static struct signals {
|
|
|
|
char *signm;
|
|
|
|
int signo;
|
|
|
|
} siglist [] = {
|
|
|
|
#ifdef SIGHUP
|
|
|
|
"HUP", SIGHUP,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
"INT", SIGINT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
"QUIT", SIGQUIT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
|
|
|
"ILL", SIGILL,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
"TRAP", SIGTRAP,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
|
|
|
"IOT", SIGIOT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
|
|
|
"ABRT", SIGABRT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGEMT
|
|
|
|
"EMT", SIGEMT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
|
|
|
"FPE", SIGFPE,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGKILL
|
|
|
|
"KILL", SIGKILL,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
|
|
|
"BUS", SIGBUS,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
|
|
|
"SEGV", SIGSEGV,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
|
|
|
"SYS", SIGSYS,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
"PIPE", SIGPIPE,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
"ALRM", SIGALRM,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
"TERM", SIGTERM,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
|
|
|
"URG", SIGURG,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
|
|
|
"STOP", SIGSTOP,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
"TSTP", SIGTSTP,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
|
|
|
"CONT", SIGCONT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
"CHLD", SIGCHLD,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCLD
|
|
|
|
"CLD", SIGCLD,
|
|
|
|
#else
|
|
|
|
# ifdef SIGCHLD
|
|
|
|
"CLD", SIGCHLD,
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
|
|
"TTIN", SIGTTIN,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
|
|
|
"TTOU", SIGTTOU,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
|
|
|
"IO", SIGIO,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
"XCPU", SIGXCPU,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
"XFSZ", SIGXFSZ,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
|
|
|
"VTALRM", SIGVTALRM,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
|
|
"PROF", SIGPROF,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
"WINCH", SIGWINCH,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
"USR1", SIGUSR1,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
"USR2", SIGUSR2,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
|
|
|
"LOST", SIGLOST,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGMSG
|
|
|
|
"MSG", SIGMSG,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
"PWR", SIGPWR,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
"POLL", SIGPOLL,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGDANGER
|
|
|
|
"DANGER", SIGDANGER,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGMIGRATE
|
|
|
|
"MIGRATE", SIGMIGRATE,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPRE
|
|
|
|
"PRE", SIGPRE,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGGRANT
|
|
|
|
"GRANT", SIGGRANT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGRETRACT
|
|
|
|
"RETRACT", SIGRETRACT,
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSOUND
|
|
|
|
"SOUND", SIGSOUND,
|
|
|
|
#endif
|
|
|
|
NULL, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
signm2signo(nm)
|
|
|
|
char *nm;
|
|
|
|
{
|
|
|
|
struct signals *sigs;
|
|
|
|
|
|
|
|
for (sigs = siglist; sigs->signm; sigs++)
|
|
|
|
if (strcmp(sigs->signm, nm) == 0)
|
|
|
|
return sigs->signo;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static char*
|
|
|
|
signo2signm(no)
|
|
|
|
int no;
|
|
|
|
{
|
|
|
|
struct signals *sigs;
|
|
|
|
|
|
|
|
for (sigs = siglist; sigs->signm; sigs++)
|
|
|
|
if (sigs->signo == no)
|
|
|
|
return sigs->signm;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_kill(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
int negative = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
int sig;
|
|
|
|
int i;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
rb_secure(2);
|
|
|
|
if (argc < 2)
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong # of arguments -- kill(sig, pid...)");
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (TYPE(argv[0])) {
|
|
|
|
case T_FIXNUM:
|
2000-09-19 03:54:28 -04:00
|
|
|
sig = FIX2INT(argv[0]);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
2000-03-13 02:18:45 -05:00
|
|
|
case T_SYMBOL:
|
|
|
|
s = rb_id2name(SYM2ID(argv[0]));
|
|
|
|
if (!s) rb_raise(rb_eArgError, "bad signal");
|
|
|
|
goto str_signal;
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
case T_STRING:
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
1998-01-16 07:13:05 -05:00
|
|
|
s = RSTRING(argv[0])->ptr;
|
|
|
|
if (s[0] == '-') {
|
|
|
|
negative++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
str_signal:
|
|
|
|
if (strncmp("SIG", s, 3) == 0)
|
|
|
|
s += 3;
|
|
|
|
if((sig = signm2signo(s)) == 0)
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eArgError, "unrecognized signal name `%s'", s);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (negative)
|
|
|
|
sig = -sig;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "bad signal type %s",
|
|
|
|
rb_class2name(CLASS_OF(argv[0])));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig < 0) {
|
|
|
|
sig = -sig;
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
int pid = NUM2INT(argv[i]);
|
|
|
|
#ifdef HAS_KILLPG
|
|
|
|
if (killpg(pid, sig) < 0)
|
|
|
|
#else
|
|
|
|
if (kill(-pid, sig) < 0)
|
|
|
|
#endif
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
Check_Type(argv[i], T_FIXNUM);
|
|
|
|
if (kill(FIX2UINT(argv[i]), sig) < 0)
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INT2FIX(i-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE trap_list[NSIG];
|
2000-11-14 02:10:31 -05:00
|
|
|
static rb_atomic_t trap_pending_list[NSIG];
|
|
|
|
rb_atomic_t rb_trap_pending;
|
|
|
|
rb_atomic_t rb_trap_immediate;
|
1999-01-19 23:59:39 -05:00
|
|
|
int rb_prohibit_interrupt;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark_trap_list()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
1998-01-16 07:13:05 -05:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<NSIG; i++) {
|
|
|
|
if (trap_list[i])
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc_mark(trap_list[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif /* MACOS_UNUSE_SIGNAL */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef POSIX_SIGNAL
|
|
|
|
void
|
|
|
|
posix_signal(signum, handler)
|
|
|
|
int signum;
|
2000-08-07 01:05:04 -04:00
|
|
|
RETSIGTYPE (*handler)_((int));
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
|
|
|
|
sigact.sa_handler = handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = 0;
|
1999-10-12 00:53:36 -04:00
|
|
|
#ifdef SA_RESTART
|
|
|
|
sigact.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
|
|
|
|
#endif
|
|
|
|
#ifdef SA_NOCLDWAIT
|
1999-10-13 02:44:42 -04:00
|
|
|
if (signum == SIGCHLD && handler == SIG_IGN)
|
1999-10-12 00:53:36 -04:00
|
|
|
sigact.sa_flags |= SA_NOCLDWAIT;
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
sigaction(signum, &sigact, 0);
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
#define ruby_signal(sig,handle) posix_signal((sig),(handle))
|
|
|
|
#else
|
|
|
|
#define ruby_signal(sig,handle) signal((sig),(handle))
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
|
2000-11-14 02:10:31 -05:00
|
|
|
static void signal_exec _((int sig));
|
1999-08-13 01:45:20 -04:00
|
|
|
static void
|
|
|
|
signal_exec(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
if (trap_list[sig] == 0) {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
rb_thread_interrupt();
|
|
|
|
break;
|
2000-05-13 12:13:31 -04:00
|
|
|
#ifdef SIGHUP
|
1999-08-13 01:45:20 -04:00
|
|
|
case SIGHUP:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
case SIGQUIT:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
case SIGALRM:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
case SIGUSR1:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
case SIGUSR2:
|
|
|
|
#endif
|
|
|
|
rb_thread_signal_raise(signo2signm(sig));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_thread_trap_eval(trap_list[sig], sig);
|
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2000-08-07 01:05:04 -04:00
|
|
|
static RETSIGTYPE sighandle _((int));
|
1998-01-16 07:13:05 -05:00
|
|
|
static RETSIGTYPE
|
|
|
|
sighandle(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
2000-09-27 05:11:13 -04:00
|
|
|
#ifdef NT
|
2000-11-14 02:10:31 -05:00
|
|
|
#define IN_MAIN_CONTEXT(f, a) (win32_main_context(a, f) ? (void)0 : f(a))
|
2000-09-27 05:11:13 -04:00
|
|
|
#else
|
2000-11-14 02:10:31 -05:00
|
|
|
#define IN_MAIN_CONTEXT(f, a) f(a)
|
2000-09-27 05:11:13 -04:00
|
|
|
#endif
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (sig >= NSIG) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("trap_handler: Bad signal %d", sig);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-10-12 00:53:36 -04:00
|
|
|
#if !defined(BSD_SIGNAL)
|
1999-08-13 01:45:20 -04:00
|
|
|
ruby_signal(sig, sighandle);
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
|
2000-11-14 02:10:31 -05:00
|
|
|
if (ATOMIC_TEST(rb_trap_immediate)) {
|
|
|
|
IN_MAIN_CONTEXT(signal_exec, sig);
|
|
|
|
ATOMIC_SET(rb_trap_immediate, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2000-11-14 02:10:31 -05:00
|
|
|
ATOMIC_INC(rb_trap_pending);
|
|
|
|
ATOMIC_INC(trap_pending_list[sig]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGBUS
|
2000-08-07 01:05:04 -04:00
|
|
|
static RETSIGTYPE sigbus _((int));
|
1998-01-16 07:13:05 -05:00
|
|
|
static RETSIGTYPE
|
|
|
|
sigbus(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("Bus Error");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGSEGV
|
2000-08-07 01:05:04 -04:00
|
|
|
static RETSIGTYPE sigsegv _((int));
|
1998-01-16 07:13:05 -05:00
|
|
|
static RETSIGTYPE
|
|
|
|
sigsegv(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_bug("Segmentation fault");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-02-19 02:03:06 -05:00
|
|
|
#ifdef SIGPIPE
|
2001-02-20 02:42:03 -05:00
|
|
|
static RETSIGTYPE sigpipe _((int));
|
2001-02-19 02:03:06 -05:00
|
|
|
static RETSIGTYPE
|
|
|
|
sigpipe(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
|
|
|
rb_trap_exit()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
|
|
|
if (trap_list[0]) {
|
2000-11-27 04:23:38 -05:00
|
|
|
VALUE trap_exit = trap_list[0];
|
|
|
|
|
|
|
|
trap_list[0] = 0;
|
|
|
|
rb_eval_cmd(trap_exit, rb_ary_new3(1, INT2FIX(0)));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_trap_exec()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
1998-01-16 07:13:05 -05:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<NSIG; i++) {
|
|
|
|
if (trap_pending_list[i]) {
|
|
|
|
trap_pending_list[i] = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
signal_exec(i);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif /* MACOS_UNUSE_SIGNAL */
|
|
|
|
rb_trap_pending = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct trap_arg {
|
2000-03-07 03:37:59 -05:00
|
|
|
#if !defined(NT)
|
1998-01-16 07:13:05 -05:00
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigset_t mask;
|
|
|
|
# else
|
|
|
|
int mask;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
VALUE sig, cmd;
|
|
|
|
};
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
static sigset_t trap_last_mask;
|
|
|
|
# else
|
|
|
|
static int trap_last_mask;
|
|
|
|
# endif
|
|
|
|
|
2000-08-07 01:05:04 -04:00
|
|
|
static RETSIGTYPE sigexit _((int));
|
1998-01-16 07:13:05 -05:00
|
|
|
static RETSIGTYPE
|
2000-08-07 01:05:04 -04:00
|
|
|
sigexit(sig)
|
|
|
|
int sig;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
rb_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
trap(arg)
|
|
|
|
struct trap_arg *arg;
|
|
|
|
{
|
2000-08-07 01:05:04 -04:00
|
|
|
RETSIGTYPE (*func)_((int));
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE command, old;
|
|
|
|
int sig;
|
2000-04-10 01:48:43 -04:00
|
|
|
char *s;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
func = sighandle;
|
|
|
|
command = arg->cmd;
|
|
|
|
if (NIL_P(command)) {
|
|
|
|
func = SIG_IGN;
|
|
|
|
}
|
|
|
|
else if (TYPE(command) == T_STRING) {
|
|
|
|
Check_SafeStr(command); /* taint check */
|
|
|
|
if (RSTRING(command)->len == 0) {
|
|
|
|
func = SIG_IGN;
|
|
|
|
}
|
|
|
|
else if (RSTRING(command)->len == 7) {
|
|
|
|
if (strncmp(RSTRING(command)->ptr, "SIG_IGN", 7) == 0) {
|
|
|
|
func = SIG_IGN;
|
|
|
|
}
|
|
|
|
else if (strncmp(RSTRING(command)->ptr, "SIG_DFL", 7) == 0) {
|
|
|
|
func = SIG_DFL;
|
|
|
|
}
|
|
|
|
else if (strncmp(RSTRING(command)->ptr, "DEFAULT", 7) == 0) {
|
|
|
|
func = SIG_DFL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RSTRING(command)->len == 6) {
|
|
|
|
if (strncmp(RSTRING(command)->ptr, "IGNORE", 6) == 0) {
|
|
|
|
func = SIG_IGN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (RSTRING(command)->len == 4) {
|
|
|
|
if (strncmp(RSTRING(command)->ptr, "EXIT", 4) == 0) {
|
|
|
|
func = sigexit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (func == SIG_IGN || func == SIG_DFL) {
|
|
|
|
command = 0;
|
|
|
|
}
|
|
|
|
|
2000-04-10 01:48:43 -04:00
|
|
|
switch (TYPE(arg->sig)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
sig = NUM2INT(arg->sig);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SYMBOL:
|
|
|
|
s = rb_id2name(SYM2ID(arg->sig));
|
|
|
|
if (!s) rb_raise(rb_eArgError, "bad signal");
|
|
|
|
goto str_signal;
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
s = RSTRING(arg->sig)->ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-04-10 01:48:43 -04:00
|
|
|
str_signal:
|
1998-01-16 07:13:05 -05:00
|
|
|
if (strncmp("SIG", s, 3) == 0)
|
|
|
|
s += 3;
|
|
|
|
sig = signm2signo(s);
|
|
|
|
if (sig == 0 && strcmp(s, "EXIT") != 0)
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eArgError, "invalid signal SIG%s", s);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
if (sig < 0 || sig > NSIG) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eArgError, "invalid signal number (%d)", sig);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
#if defined(HAVE_SETITIMER) && !defined(__BOW__)
|
1998-01-16 07:13:05 -05:00
|
|
|
if (sig == SIGVTALRM) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "SIGVTALRM reserved for Thread; cannot set handler");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (func == SIG_DFL) {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
2000-05-13 12:13:31 -04:00
|
|
|
#ifdef SIGHUP
|
1999-08-13 01:45:20 -04:00
|
|
|
case SIGHUP:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
case SIGQUIT:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
case SIGALRM:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
case SIGUSR1:
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
case SIGUSR2:
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
func = sighandle;
|
|
|
|
break;
|
|
|
|
#ifdef SIGBUS
|
|
|
|
case SIGBUS:
|
|
|
|
func = sigbus;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
|
|
|
case SIGSEGV:
|
|
|
|
func = sigsegv;
|
|
|
|
break;
|
1999-10-12 00:53:36 -04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
case SIGPIPE:
|
2001-02-19 02:03:06 -05:00
|
|
|
func = sigpipe;
|
1999-10-12 00:53:36 -04:00
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
ruby_signal(sig, func);
|
1998-01-16 07:13:05 -05:00
|
|
|
old = trap_list[sig];
|
|
|
|
if (!old) old = Qnil;
|
|
|
|
|
|
|
|
trap_list[sig] = command;
|
|
|
|
/* enable at least specified signal. */
|
2000-03-07 03:37:59 -05:00
|
|
|
#if !defined(NT)
|
1998-01-16 07:13:05 -05:00
|
|
|
#ifdef HAVE_SIGPROCMASK
|
|
|
|
sigdelset(&arg->mask, sig);
|
|
|
|
#else
|
|
|
|
arg->mask &= ~sigmask(sig);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2000-03-07 03:37:59 -05:00
|
|
|
#if !defined(NT)
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
trap_ensure(arg)
|
|
|
|
struct trap_arg *arg;
|
|
|
|
{
|
|
|
|
/* enable interrupt */
|
|
|
|
#ifdef HAVE_SIGPROCMASK
|
|
|
|
sigprocmask(SIG_SETMASK, &arg->mask, NULL);
|
|
|
|
#else
|
|
|
|
sigsetmask(arg->mask);
|
|
|
|
#endif
|
1999-01-19 23:59:39 -05:00
|
|
|
trap_last_mask = arg->mask;
|
|
|
|
return 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
|
|
|
rb_trap_restore_mask()
|
|
|
|
{
|
2000-03-07 03:37:59 -05:00
|
|
|
#if !defined(NT)
|
1999-01-19 23:59:39 -05:00
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigprocmask(SIG_SETMASK, &trap_last_mask, NULL);
|
|
|
|
# else
|
|
|
|
sigsetmask(trap_last_mask);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_trap(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
struct trap_arg arg;
|
|
|
|
|
|
|
|
rb_secure(2);
|
|
|
|
if (argc == 0 || argc > 2) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "wrong # of arguments -- trap(sig, cmd)/trap(sig){...}");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
arg.sig = argv[0];
|
|
|
|
if (argc == 1) {
|
1999-01-19 23:59:39 -05:00
|
|
|
arg.cmd = rb_f_lambda();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
|
|
|
arg.cmd = argv[1];
|
|
|
|
}
|
|
|
|
|
2000-03-07 03:37:59 -05:00
|
|
|
#if !defined(NT)
|
1998-01-16 07:13:05 -05:00
|
|
|
/* disable interrupt */
|
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigfillset(&arg.mask);
|
|
|
|
sigprocmask(SIG_BLOCK, &arg.mask, &arg.mask);
|
|
|
|
# else
|
|
|
|
arg.mask = sigblock(~0);
|
|
|
|
# endif
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_ensure(trap, (VALUE)&arg, trap_ensure, (VALUE)&arg);
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
return trap(&arg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_signal()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
|
|
|
rb_define_global_function("trap", rb_f_trap, -1);
|
1999-08-13 01:45:20 -04:00
|
|
|
ruby_signal(SIGINT, sighandle);
|
2000-05-13 12:13:31 -04:00
|
|
|
#ifdef SIGHUP
|
1999-08-13 01:45:20 -04:00
|
|
|
ruby_signal(SIGHUP, sighandle);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
ruby_signal(SIGQUIT, sighandle);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
ruby_signal(SIGALRM, sighandle);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
ruby_signal(SIGUSR1, sighandle);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
ruby_signal(SIGUSR2, sighandle);
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
1999-08-13 01:45:20 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#ifdef SIGBUS
|
1999-08-13 01:45:20 -04:00
|
|
|
ruby_signal(SIGBUS, sigbus);
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
1999-08-13 01:45:20 -04:00
|
|
|
ruby_signal(SIGSEGV, sigsegv);
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
1999-10-12 00:53:36 -04:00
|
|
|
#ifdef SIGPIPE
|
2001-02-19 02:03:06 -05:00
|
|
|
ruby_signal(SIGPIPE, sigpipe);
|
1999-10-12 00:53:36 -04:00
|
|
|
#endif
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif /* MACOS_UNUSE_SIGNAL */
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|