Module: Signal

Defined in:
signal.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listHash

Returns a list of signal names mapped to the corresponding underlying signal numbers.

Signal.list #=> "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

Returns:



# File 'signal.c'

/*
 * 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}
 */
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;
}

.trap(signal, proc) ⇒ Object .trap(signal) {|| ... } ⇒ Object

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

Overloads:



# File 'signal.c'

/*
 * 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
 */
static VALUE
sig_trap(argc, argv)
    int argc;
    VALUE *argv;
{
    struct trap_arg arg;

    rb_secure(2);
    if (argc == 0 || argc > 2) {
    rb_raise(rb_eArgError, "wrong number of arguments -- trap(sig, cmd)/trap(sig){...}");
    }

    arg.sig = argv[0];
    if (argc == 1) {
    arg.cmd = rb_block_proc();
    }
    else if (argc == 2) {
    arg.cmd = argv[1];
    }

    if (OBJ_TAINTED(arg.cmd)) {
    rb_raise(rb_eSecurityError, "Insecure: tainted signal trap");
    }
#if USE_TRAP_MASK
    /* disable interrupt */
# ifdef HAVE_SIGPROCMASK
    sigfillset(&arg.mask);
    sigprocmask(SIG_BLOCK, &arg.mask, &arg.mask);
# else
    arg.mask = sigblock(~0);
# endif

    return rb_ensure(trap, (VALUE)&arg, trap_ensure, (VALUE)&arg);
#else
    return trap(&arg);
#endif
}

Instance Method Details

#new(sig) ⇒ Object

Construct a new SignalException object. sig should be a known

signal name, or a signal number.


# File 'signal.c'

/*
 * call-seq:
 *    SignalException.new(sig)   =>  signal_exception
 *
 *  Construct a new SignalException object.  +sig+ should be a known
 *  signal name, or a signal number.
 */

static VALUE
esignal_init(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    int argnum = 1;
    VALUE sig = Qnil;
    int signo;
    const char *signm;
    char tmpnm[(sizeof(int)*CHAR_BIT)/3+4];

    if (argc > 0) {
    sig = argv[0];
    if (FIXNUM_P(sig)) argnum = 2;
    }
    if (argc < 1 || argnum < argc) {
    rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)",
         argc, argnum);
    }
    if (argnum == 2) {
    signo = FIX2INT(sig);
    if (signo < 0 || signo > NSIG) {
        rb_raise(rb_eArgError, "invalid signal number (%d)", signo);
    }
    if (argc > 1) {
        sig = argv[1];
    }
    else {
        signm = signo2signm(signo);
        if (signm) {
        snprintf(tmpnm, sizeof(tmpnm), "SIG%s", signm);
        }
        else {
        snprintf(tmpnm, sizeof(tmpnm), "SIG%u", signo);
        }
        sig = rb_str_new2(signm = tmpnm);
    }
    }
    else {
    signm = SYMBOL_P(sig) ? rb_id2name(SYM2ID(sig)) : StringValuePtr(sig);
    if (strncmp(signm, "SIG", 3) == 0) signm += 3;
    signo = signm2signo(signm);
    if (!signo) {
        rb_raise(rb_eArgError, "unsupported name `SIG%s'", signm);
    }
    if (SYMBOL_P(sig)) {
        sig = rb_str_new2(signm);
    }
    }
    rb_call_super(1, &sig);
    rb_iv_set(self, "signo", INT2NUM(signo));

    return self;
}