2000-05-09 08:53:16 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
signal.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Tue Dec 20 10:13:44 JST 1994
|
|
|
|
|
2003-01-16 10:34:03 +03:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
2000-05-01 13:42:38 +04:00
|
|
|
|
2000-05-09 08:53:16 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-20 07:59:39 +03:00
|
|
|
#include "rubysig.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef __BEOS__
|
|
|
|
#undef SIGBUS
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
#ifndef NSIG
|
1999-01-20 07:59:39 +03:00
|
|
|
# ifdef DJGPP
|
|
|
|
# define NSIG SIGMAX
|
|
|
|
# else
|
|
|
|
# define NSIG (_SIGMAX + 1) /* For QNX */
|
|
|
|
# endif
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static struct signals {
|
|
|
|
char *signm;
|
|
|
|
int signo;
|
|
|
|
} siglist [] = {
|
|
|
|
#ifdef SIGHUP
|
2002-04-24 08:54:16 +04:00
|
|
|
{"HUP", SIGHUP},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
2002-04-24 08:54:16 +04:00
|
|
|
{"INT", SIGINT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#ifdef SIGQUIT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"QUIT", SIGQUIT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
2002-04-24 08:54:16 +04:00
|
|
|
{"ILL", SIGILL},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
2002-04-24 08:54:16 +04:00
|
|
|
{"TRAP", SIGTRAP},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"IOT", SIGIOT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"ABRT", SIGABRT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGEMT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"EMT", SIGEMT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
2002-04-24 08:54:16 +04:00
|
|
|
{"FPE", SIGFPE},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGKILL
|
2002-04-24 08:54:16 +04:00
|
|
|
{"KILL", SIGKILL},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
2002-04-24 08:54:16 +04:00
|
|
|
{"BUS", SIGBUS},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
2002-04-24 08:54:16 +04:00
|
|
|
{"SEGV", SIGSEGV},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
2002-04-24 08:54:16 +04:00
|
|
|
{"SYS", SIGSYS},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
2002-04-24 08:54:16 +04:00
|
|
|
{"PIPE", SIGPIPE},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
2002-04-24 08:54:16 +04:00
|
|
|
{"ALRM", SIGALRM},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
2002-04-24 08:54:16 +04:00
|
|
|
{"TERM", SIGTERM},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
2002-04-24 08:54:16 +04:00
|
|
|
{"URG", SIGURG},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
2002-04-24 08:54:16 +04:00
|
|
|
{"STOP", SIGSTOP},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
2002-04-24 08:54:16 +04:00
|
|
|
{"TSTP", SIGTSTP},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"CONT", SIGCONT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
2002-04-24 08:54:16 +04:00
|
|
|
{"CHLD", SIGCHLD},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCLD
|
2002-04-24 08:54:16 +04:00
|
|
|
{"CLD", SIGCLD},
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
# ifdef SIGCHLD
|
2002-04-24 08:54:16 +04:00
|
|
|
{"CLD", SIGCHLD},
|
1998-01-16 15:13:05 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
2002-04-24 08:54:16 +04:00
|
|
|
{"TTIN", SIGTTIN},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
2002-04-24 08:54:16 +04:00
|
|
|
{"TTOU", SIGTTOU},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
2002-04-24 08:54:16 +04:00
|
|
|
{"IO", SIGIO},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
2002-04-24 08:54:16 +04:00
|
|
|
{"XCPU", SIGXCPU},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
2002-04-24 08:54:16 +04:00
|
|
|
{"XFSZ", SIGXFSZ},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
2002-04-24 08:54:16 +04:00
|
|
|
{"VTALRM", SIGVTALRM},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
2002-04-24 08:54:16 +04:00
|
|
|
{"PROF", SIGPROF},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
2002-04-24 08:54:16 +04:00
|
|
|
{"WINCH", SIGWINCH},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
2002-04-24 08:54:16 +04:00
|
|
|
{"USR1", SIGUSR1},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
2002-04-24 08:54:16 +04:00
|
|
|
{"USR2", SIGUSR2},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
2002-04-24 08:54:16 +04:00
|
|
|
{"LOST", SIGLOST},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGMSG
|
2002-04-24 08:54:16 +04:00
|
|
|
{"MSG", SIGMSG},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
2002-04-24 08:54:16 +04:00
|
|
|
{"PWR", SIGPWR},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
2002-04-24 08:54:16 +04:00
|
|
|
{"POLL", SIGPOLL},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGDANGER
|
2002-04-24 08:54:16 +04:00
|
|
|
{"DANGER", SIGDANGER},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGMIGRATE
|
2002-04-24 08:54:16 +04:00
|
|
|
{"MIGRATE", SIGMIGRATE},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPRE
|
2002-04-24 08:54:16 +04:00
|
|
|
{"PRE", SIGPRE},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGGRANT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"GRANT", SIGGRANT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGRETRACT
|
2002-04-24 08:54:16 +04:00
|
|
|
{"RETRACT", SIGRETRACT},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSOUND
|
2002-04-24 08:54:16 +04:00
|
|
|
{"SOUND", SIGSOUND},
|
2001-05-16 13:05:54 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGINFO
|
2002-04-24 08:54:16 +04:00
|
|
|
{"INFO", SIGINFO},
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
2002-04-24 08:54:16 +04:00
|
|
|
{NULL, 0}
|
1998-01-16 15:13:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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 09: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;
|
|
|
|
}
|
|
|
|
|
2003-10-01 19:14:23 +04:00
|
|
|
const char *
|
2003-09-29 06:44:49 +04:00
|
|
|
ruby_signal_name(no)
|
|
|
|
int no;
|
|
|
|
{
|
|
|
|
return signo2signm(no);
|
|
|
|
}
|
|
|
|
|
2004-01-02 09:01:12 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Process.kill(signal, pid, ...) => fixnum
|
|
|
|
*
|
|
|
|
* Sends the given signal to the specified process id(s), or to the
|
|
|
|
* current process if _pid_ is zero. _signal_ may be an
|
|
|
|
* integer signal number or a POSIX signal name (either with or without
|
|
|
|
* a +SIG+ prefix). If _signal_ is negative (or starts
|
|
|
|
* with a minus sign), kills process groups instead of
|
|
|
|
* processes. Not all signals are available on all platforms.
|
|
|
|
*
|
|
|
|
* pid = fork do
|
|
|
|
* Signal.trap("HUP") { puts "Ouch!"; exit }
|
|
|
|
* # ... do some work ...
|
|
|
|
* end
|
|
|
|
* # ...
|
|
|
|
* Process.kill("HUP", pid)
|
|
|
|
* Process.wait
|
|
|
|
*
|
|
|
|
* <em>produces:</em>
|
|
|
|
*
|
|
|
|
* Ouch!
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_kill(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
int negative = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
int sig;
|
|
|
|
int i;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
rb_secure(2);
|
|
|
|
if (argc < 2)
|
2001-11-19 08:03:03 +03:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments -- kill(sig, pid...)");
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (TYPE(argv[0])) {
|
|
|
|
case T_FIXNUM:
|
2000-09-19 11:54:28 +04:00
|
|
|
sig = FIX2INT(argv[0]);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
2000-03-13 10:18:45 +03:00
|
|
|
case T_SYMBOL:
|
|
|
|
s = rb_id2name(SYM2ID(argv[0]));
|
|
|
|
if (!s) rb_raise(rb_eArgError, "bad signal");
|
|
|
|
goto str_signal;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case T_STRING:
|
2002-03-11 11:02:04 +03:00
|
|
|
s = RSTRING(argv[0])->ptr;
|
|
|
|
if (s[0] == '-') {
|
|
|
|
negative++;
|
|
|
|
s++;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-03-11 11:02:04 +03:00
|
|
|
str_signal:
|
|
|
|
if (strncmp("SIG", s, 3) == 0)
|
|
|
|
s += 3;
|
|
|
|
if((sig = signm2signo(s)) == 0)
|
|
|
|
rb_raise(rb_eArgError, "unsupported name `SIG%s'", s);
|
|
|
|
|
|
|
|
if (negative)
|
|
|
|
sig = -sig;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-03-11 11:02:04 +03:00
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
2002-12-19 12:20:20 +03:00
|
|
|
str = rb_check_string_type(argv[0]);
|
2002-03-11 11:02:04 +03:00
|
|
|
if (!NIL_P(str)) {
|
|
|
|
s = RSTRING(str)->ptr;
|
|
|
|
goto str_signal;
|
|
|
|
}
|
|
|
|
rb_raise(rb_eArgError, "bad signal type %s",
|
2003-01-31 07:00:17 +03:00
|
|
|
rb_obj_classname(argv[0]));
|
2002-03-11 11:02:04 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03: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);
|
2001-03-21 06:41:45 +03:00
|
|
|
if (kill(FIX2INT(argv[i]), sig) < 0)
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return INT2FIX(i-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE trap_list[NSIG];
|
2000-11-14 10:10:31 +03:00
|
|
|
static rb_atomic_t trap_pending_list[NSIG];
|
|
|
|
rb_atomic_t rb_trap_pending;
|
|
|
|
rb_atomic_t rb_trap_immediate;
|
2003-05-19 08:52:55 +04:00
|
|
|
int rb_prohibit_interrupt = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_gc_mark_trap_list()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
1998-01-16 15:13:05 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<NSIG; i++) {
|
|
|
|
if (trap_list[i])
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_gc_mark(trap_list[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif /* MACOS_UNUSE_SIGNAL */
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-08-16 11:23:04 +04:00
|
|
|
#ifdef __dietlibc__
|
|
|
|
#define sighandler_t sh_t
|
|
|
|
#endif
|
|
|
|
|
2002-01-21 10:44:06 +03:00
|
|
|
typedef RETSIGTYPE (*sighandler_t)_((int));
|
|
|
|
|
2002-01-21 16:59:41 +03:00
|
|
|
#ifdef POSIX_SIGNAL
|
2002-01-21 10:44:06 +03:00
|
|
|
static sighandler_t
|
|
|
|
ruby_signal(signum, handler)
|
1998-01-16 15:13:05 +03:00
|
|
|
int signum;
|
2002-01-21 10:44:06 +03:00
|
|
|
sighandler_t handler;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-01-21 10:44:06 +03:00
|
|
|
struct sigaction sigact, old;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
sigact.sa_handler = handler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = 0;
|
2001-08-06 07:05:23 +04:00
|
|
|
#if defined(SA_RESTART)
|
2001-11-21 18:42:12 +03:00
|
|
|
/* All other signals but VTALRM shall restart restartable syscall
|
|
|
|
VTALRM will cause EINTR to syscall if interrupted.
|
|
|
|
*/
|
|
|
|
if (signum != SIGVTALRM) {
|
|
|
|
sigact.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
|
|
|
|
}
|
1999-10-12 08:53:36 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SA_NOCLDWAIT
|
1999-10-13 10:44:42 +04:00
|
|
|
if (signum == SIGCHLD && handler == SIG_IGN)
|
1999-10-12 08:53:36 +04:00
|
|
|
sigact.sa_flags |= SA_NOCLDWAIT;
|
|
|
|
#endif
|
2002-01-21 10:44:06 +03:00
|
|
|
sigaction(signum, &sigact, &old);
|
|
|
|
return old.sa_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
posix_signal(signum, handler)
|
|
|
|
int signum;
|
|
|
|
sighandler_t handler;
|
|
|
|
{
|
|
|
|
ruby_signal(signum, handler);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
2002-01-21 10:44:06 +03:00
|
|
|
#define ruby_signal(sig,handler) signal((sig),(handler))
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
2000-11-14 10:10:31 +03:00
|
|
|
static void signal_exec _((int sig));
|
1999-08-13 09: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 20:13:31 +04:00
|
|
|
#ifdef SIGHUP
|
1999-08-13 09: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-20 07:59:39 +03:00
|
|
|
|
2002-01-21 10:44:06 +03:00
|
|
|
static RETSIGTYPE sighandler _((int));
|
1998-01-16 15:13:05 +03:00
|
|
|
static RETSIGTYPE
|
2002-01-21 10:44:06 +03:00
|
|
|
sighandler(sig)
|
1998-01-16 15:13:05 +03:00
|
|
|
int sig;
|
|
|
|
{
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifdef _WIN32
|
2002-05-29 14:22:19 +04:00
|
|
|
#define IN_MAIN_CONTEXT(f, a) (rb_w32_main_context(a, f) ? (void)0 : f(a))
|
2000-09-27 13:11:13 +04:00
|
|
|
#else
|
2000-11-14 10:10:31 +03:00
|
|
|
#define IN_MAIN_CONTEXT(f, a) f(a)
|
2000-09-27 13:11:13 +04:00
|
|
|
#endif
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (sig >= NSIG) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_bug("trap_handler: Bad signal %d", sig);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-11-13 11:19:52 +03:00
|
|
|
#if !defined(BSD_SIGNAL) && !defined(POSIX_SIGNAL)
|
2002-01-21 10:44:06 +03:00
|
|
|
ruby_signal(sig, sighandler);
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
2000-11-14 10:10:31 +03:00
|
|
|
if (ATOMIC_TEST(rb_trap_immediate)) {
|
|
|
|
IN_MAIN_CONTEXT(signal_exec, sig);
|
|
|
|
ATOMIC_SET(rb_trap_immediate, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-11-14 10:10:31 +03:00
|
|
|
ATOMIC_INC(rb_trap_pending);
|
|
|
|
ATOMIC_INC(trap_pending_list[sig]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGBUS
|
2000-08-07 09:05:04 +04:00
|
|
|
static RETSIGTYPE sigbus _((int));
|
1998-01-16 15:13:05 +03:00
|
|
|
static RETSIGTYPE
|
|
|
|
sigbus(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_bug("Bus Error");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGSEGV
|
2000-08-07 09:05:04 +04:00
|
|
|
static RETSIGTYPE sigsegv _((int));
|
1998-01-16 15:13:05 +03:00
|
|
|
static RETSIGTYPE
|
|
|
|
sigsegv(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_bug("Segmentation fault");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-02-19 10:03:06 +03:00
|
|
|
#ifdef SIGPIPE
|
2001-02-20 10:42:03 +03:00
|
|
|
static RETSIGTYPE sigpipe _((int));
|
2001-02-19 10:03:06 +03:00
|
|
|
static RETSIGTYPE
|
|
|
|
sigpipe(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
rb_trap_exit()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
|
|
|
if (trap_list[0]) {
|
2000-11-27 12:23:38 +03:00
|
|
|
VALUE trap_exit = trap_list[0];
|
|
|
|
|
|
|
|
trap_list[0] = 0;
|
2001-11-13 11:19:52 +03:00
|
|
|
rb_eval_cmd(trap_exit, rb_ary_new3(1, INT2FIX(0)), 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_trap_exec()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
1998-01-16 15:13:05 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<NSIG; i++) {
|
|
|
|
if (trap_pending_list[i]) {
|
|
|
|
trap_pending_list[i] = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
signal_exec(i);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif /* MACOS_UNUSE_SIGNAL */
|
|
|
|
rb_trap_pending = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct trap_arg {
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifndef _WIN32
|
1998-01-16 15:13:05 +03:00
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigset_t mask;
|
|
|
|
# else
|
|
|
|
int mask;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
VALUE sig, cmd;
|
|
|
|
};
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
static sigset_t trap_last_mask;
|
|
|
|
# else
|
|
|
|
static int trap_last_mask;
|
|
|
|
# endif
|
|
|
|
|
2000-08-07 09:05:04 +04:00
|
|
|
static RETSIGTYPE sigexit _((int));
|
1998-01-16 15:13:05 +03:00
|
|
|
static RETSIGTYPE
|
2000-08-07 09:05:04 +04:00
|
|
|
sigexit(sig)
|
|
|
|
int sig;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
rb_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
trap(arg)
|
|
|
|
struct trap_arg *arg;
|
|
|
|
{
|
2003-03-25 18:25:18 +03:00
|
|
|
sighandler_t func, oldfunc;
|
|
|
|
VALUE command, oldcmd;
|
2002-04-24 08:54:16 +04:00
|
|
|
int sig = -1;
|
2000-04-10 09:48:43 +04:00
|
|
|
char *s;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-01-21 10:44:06 +03:00
|
|
|
func = sighandler;
|
1998-01-16 15:13:05 +03:00
|
|
|
command = arg->cmd;
|
|
|
|
if (NIL_P(command)) {
|
|
|
|
func = SIG_IGN;
|
|
|
|
}
|
|
|
|
else if (TYPE(command) == T_STRING) {
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(command); /* taint check */
|
1998-01-16 15:13:05 +03:00
|
|
|
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 09:48:43 +04:00
|
|
|
switch (TYPE(arg->sig)) {
|
|
|
|
case T_FIXNUM:
|
2004-03-15 05:27:29 +03:00
|
|
|
sig = FIX2INT(arg->sig);
|
2000-04-10 09:48:43 +04:00
|
|
|
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 15:13:05 +03:00
|
|
|
|
2000-04-10 09:48:43 +04:00
|
|
|
str_signal:
|
1998-01-16 15:13:05 +03:00
|
|
|
if (strncmp("SIG", s, 3) == 0)
|
|
|
|
s += 3;
|
|
|
|
sig = signm2signo(s);
|
|
|
|
if (sig == 0 && strcmp(s, "EXIT") != 0)
|
2002-03-08 10:03:09 +03:00
|
|
|
rb_raise(rb_eArgError, "unsupported signal SIG%s", s);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-04-10 09:48:43 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (sig < 0 || sig > NSIG) {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eArgError, "invalid signal number (%d)", sig);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-12-22 11:23:55 +03:00
|
|
|
#if defined(HAVE_SETITIMER)
|
1998-01-16 15:13:05 +03:00
|
|
|
if (sig == SIGVTALRM) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "SIGVTALRM reserved for Thread; cannot set handler");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (func == SIG_DFL) {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
2000-05-13 20:13:31 +04:00
|
|
|
#ifdef SIGHUP
|
1999-08-13 09: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
|
2002-01-21 10:44:06 +03:00
|
|
|
func = sighandler;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
#ifdef SIGBUS
|
|
|
|
case SIGBUS:
|
|
|
|
func = sigbus;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
|
|
|
case SIGSEGV:
|
|
|
|
func = sigsegv;
|
|
|
|
break;
|
1999-10-12 08:53:36 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
case SIGPIPE:
|
2001-02-19 10:03:06 +03:00
|
|
|
func = sigpipe;
|
1999-10-12 08:53:36 +04:00
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2003-03-25 18:25:18 +03:00
|
|
|
oldfunc = ruby_signal(sig, func);
|
|
|
|
oldcmd = trap_list[sig];
|
|
|
|
if (!oldcmd) {
|
|
|
|
if (oldfunc == SIG_IGN) oldcmd = rb_str_new2("IGNORE");
|
|
|
|
else if (oldfunc == sighandler) oldcmd = rb_str_new2("DEFAULT");
|
|
|
|
else oldcmd = Qnil;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
trap_list[sig] = command;
|
|
|
|
/* enable at least specified signal. */
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifndef _WIN32
|
1998-01-16 15:13:05 +03:00
|
|
|
#ifdef HAVE_SIGPROCMASK
|
|
|
|
sigdelset(&arg->mask, sig);
|
|
|
|
#else
|
|
|
|
arg->mask &= ~sigmask(sig);
|
|
|
|
#endif
|
|
|
|
#endif
|
2003-03-25 18:25:18 +03:00
|
|
|
return oldcmd;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifndef _WIN32
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
1998-01-16 15:13:05 +03: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-20 07:59:39 +03:00
|
|
|
trap_last_mask = arg->mask;
|
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
|
|
|
rb_trap_restore_mask()
|
|
|
|
{
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifndef _WIN32
|
1999-01-20 07:59:39 +03:00
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigprocmask(SIG_SETMASK, &trap_last_mask, NULL);
|
|
|
|
# else
|
|
|
|
sigsetmask(trap_last_mask);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-12-27 19:07:43 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Signal.trap( signal, proc ) => obj
|
|
|
|
* Signal.trap( signal ) {| | block } => obj
|
|
|
|
*
|
|
|
|
* Specifies the handling of signals. The first parameter is a signal
|
|
|
|
* name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a
|
|
|
|
* signal number. The characters ``SIG'' may be omitted from the
|
|
|
|
* signal name. The command or block specifies code to be run when the
|
|
|
|
* signal is raised. If the command is the string ``IGNORE'' or
|
|
|
|
* ``SIG_IGN'', the signal will be ignored. If the command is
|
|
|
|
* ``DEFAULT'' or ``SIG_DFL'', the operating system's default handler
|
|
|
|
* will be invoked. If the command is ``EXIT'', the script will be
|
|
|
|
* terminated by the signal. Otherwise, the given command or block
|
|
|
|
* will be run.
|
|
|
|
* The special signal name ``EXIT'' or signal number zero will be
|
|
|
|
* invoked just prior to program termination.
|
|
|
|
* trap returns the previous handler for the given signal.
|
|
|
|
*
|
|
|
|
* Signal.trap(0, proc { puts "Terminating: #{$$}" })
|
|
|
|
* Signal.trap("CLD") { puts "Child died" }
|
|
|
|
* fork && Process.wait
|
|
|
|
*
|
|
|
|
* produces:
|
|
|
|
* Terminating: 27461
|
|
|
|
* Child died
|
|
|
|
* Terminating: 27460
|
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2001-08-23 10:02:15 +04:00
|
|
|
sig_trap(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
struct trap_arg arg;
|
|
|
|
|
|
|
|
rb_secure(2);
|
|
|
|
if (argc == 0 || argc > 2) {
|
2001-11-19 08:03:03 +03:00
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments -- trap(sig, cmd)/trap(sig){...}");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
arg.sig = argv[0];
|
|
|
|
if (argc == 1) {
|
2003-06-16 11:14:50 +04:00
|
|
|
arg.cmd = rb_block_proc();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
|
|
|
arg.cmd = argv[1];
|
|
|
|
}
|
|
|
|
|
2001-11-13 11:19:52 +03:00
|
|
|
if (OBJ_TAINTED(arg.cmd)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: tainted signal trap");
|
|
|
|
}
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifndef _WIN32
|
1998-01-16 15:13:05 +03:00
|
|
|
/* disable interrupt */
|
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigfillset(&arg.mask);
|
|
|
|
sigprocmask(SIG_BLOCK, &arg.mask, &arg.mask);
|
|
|
|
# else
|
|
|
|
arg.mask = sigblock(~0);
|
|
|
|
# endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ensure(trap, (VALUE)&arg, trap_ensure, (VALUE)&arg);
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
return trap(&arg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-12-27 19:07:43 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Signal.list => a_hash
|
|
|
|
*
|
|
|
|
* Returns a list of signal names mapped to the corresponding
|
|
|
|
* underlying signal numbers.
|
|
|
|
*
|
|
|
|
* Signal.list #=> {"ABRT"=>6, "ALRM"=>14, "BUS"=>7, "CHLD"=>17, "CLD"=>17, "CONT"=>18, "FPE"=>8, "HUP"=>1, "ILL"=>4, "INT"=>2, "IO"=>29, "IOT"=>6, "KILL"=>9, "PIPE"=>13, "POLL"=>29, "PROF"=>27, "PWR"=>30, "QUIT"=>3, "SEGV"=>11, "STOP"=>19, "SYS"=>31, "TERM"=>15, "TRAP"=>5, "TSTP"=>20, "TTIN"=>21, "TTOU"=>22, "URG"=>23, "USR1"=>10, "USR2"=>12, "VTALRM"=>26, "WINCH"=>28, "XCPU"=>24, "XFSZ"=>25}
|
|
|
|
*/
|
2001-08-23 10:02:15 +04:00
|
|
|
static VALUE
|
|
|
|
sig_list()
|
|
|
|
{
|
|
|
|
VALUE h = rb_hash_new();
|
|
|
|
struct signals *sigs;
|
|
|
|
|
|
|
|
for (sigs = siglist; sigs->signm; sigs++) {
|
|
|
|
rb_hash_aset(h, rb_str_new2(sigs->signm), INT2FIX(sigs->signo));
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2002-01-21 10:44:06 +03:00
|
|
|
static void
|
|
|
|
install_sighandler(signum, handler)
|
|
|
|
int signum;
|
|
|
|
sighandler_t handler;
|
|
|
|
{
|
|
|
|
sighandler_t old;
|
|
|
|
|
|
|
|
old = ruby_signal(signum, handler);
|
|
|
|
if (old != SIG_DFL) {
|
|
|
|
ruby_signal(signum, old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-05 17:00:11 +03:00
|
|
|
static void
|
|
|
|
init_sigchld(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
sighandler_t oldfunc;
|
|
|
|
#ifndef _WIN32
|
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigset_t mask;
|
|
|
|
# else
|
|
|
|
int mask;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
/* disable interrupt */
|
|
|
|
# ifdef HAVE_SIGPROCMASK
|
|
|
|
sigfillset(&mask);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &mask);
|
|
|
|
# else
|
|
|
|
mask = sigblock(~0);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
oldfunc = ruby_signal(sig, SIG_DFL);
|
|
|
|
if (oldfunc != SIG_DFL && oldfunc != SIG_IGN) {
|
|
|
|
ruby_signal(sig, oldfunc);
|
|
|
|
} else {
|
|
|
|
trap_list[sig] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#ifdef HAVE_SIGPROCMASK
|
|
|
|
sigdelset(&mask, sig);
|
|
|
|
sigprocmask(SIG_SETMASK, &mask, NULL);
|
|
|
|
#else
|
|
|
|
mask &= ~sigmask(sig);
|
|
|
|
sigsetmask(mask);
|
|
|
|
#endif
|
|
|
|
trap_last_mask = mask;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-12-27 19:07:43 +03:00
|
|
|
/*
|
|
|
|
* Many operating systems allow signals to be sent to running
|
|
|
|
* processes. Some signals have a defined effect on the process, while
|
|
|
|
* others may be trapped at the code level and acted upon. For
|
|
|
|
* example, your process may trap the USR1 signal and use it to toggle
|
|
|
|
* debugging, and may use TERM to initiate a controlled shutdown.
|
|
|
|
*
|
|
|
|
* pid = fork do
|
|
|
|
* Signal.trap("USR1") do
|
|
|
|
* $debug = !$debug
|
|
|
|
* puts "Debug now: #$debug"
|
|
|
|
* end
|
|
|
|
* Signal.trap("TERM") do
|
|
|
|
* puts "Terminating..."
|
|
|
|
* shutdown()
|
|
|
|
* end
|
|
|
|
* # . . . do some work . . .
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* Process.detach(pid)
|
|
|
|
*
|
|
|
|
* # Controlling program:
|
|
|
|
* Process.kill("USR1", pid)
|
|
|
|
* # ...
|
|
|
|
* Process.kill("USR1", pid)
|
|
|
|
* # ...
|
|
|
|
* Process.kill("TERM", pid)
|
|
|
|
*
|
|
|
|
* produces:
|
|
|
|
* Debug now: true
|
|
|
|
* Debug now: false
|
|
|
|
* Terminating...
|
|
|
|
*
|
|
|
|
* The list of available signal names and their interpretation is
|
|
|
|
* system dependent. Signal delivery semantics may also vary between
|
|
|
|
* systems; in particular signal delivery may not always be reliable.
|
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
Init_signal()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifndef MACOS_UNUSE_SIGNAL
|
2001-08-23 10:02:15 +04:00
|
|
|
VALUE mSignal = rb_define_module("Signal");
|
|
|
|
|
|
|
|
rb_define_global_function("trap", sig_trap, -1);
|
|
|
|
rb_define_module_function(mSignal, "trap", sig_trap, -1);
|
|
|
|
rb_define_module_function(mSignal, "list", sig_list, 0);
|
|
|
|
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGINT, sighandler);
|
2000-05-13 20:13:31 +04:00
|
|
|
#ifdef SIGHUP
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGHUP, sighandler);
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGQUIT, sighandler);
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGALRM, sighandler);
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGUSR1, sighandler);
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGUSR2, sighandler);
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
#ifdef SIGBUS
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGBUS, sigbus);
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGSEGV, sigsegv);
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
1999-10-12 08:53:36 +04:00
|
|
|
#ifdef SIGPIPE
|
2002-01-21 10:44:06 +03:00
|
|
|
install_sighandler(SIGPIPE, sigpipe);
|
1999-10-12 08:53:36 +04:00
|
|
|
#endif
|
2003-11-05 17:00:11 +03:00
|
|
|
|
|
|
|
#ifdef SIGCLD
|
|
|
|
init_sigchld(SIGCLD);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
init_sigchld(SIGCHLD);
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif /* MACOS_UNUSE_SIGNAL */
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|