unix::signal
The signal module provides support for Unix signal handlers. Typical applications will provide a signal handler to handle to configure it for the desired signal, possibly along with flags and a signal mask. This function returns the previous signal handler, which can be passed to restore to restore the previous behavior.
Signal handling is stupidly complicated and easy to get wrong. The standard library makes little effort to help you deal with this. Consult your local man pages, particularly signal-safety(7) on Linux, and perhaps a local priest as well. We advise you to get out of the signal handler as soon as possible, for example via the "self-pipe trick".
Note that the necessary sa_restorer functionality is implemented (and imposed) by the standard library.
Index
Types
type code = enum {
USER = 0, KERNEL = 128, QUEUE = -1, TIMER = -2, MESQ = -3, ASYNCIO = -4, SIGIO = -5,
TKILL = -6, ASYNCNL = -60,
ILLOPC = 1, ILLOPN = 2, ILLADR = 3, ILLTRP = 4, PRVOPC = 5, PRVREG = 6, COPROC = 7, BADSTK = 8, INTDIV = 1, INTOVF = 2, FLTDIV = 3, FLTOVF = 4, FLTUND = 5, FLTRES = 6, FLTINV = 7, FLTSUB = 8, MAPERR = 1, ACCERR = 2, BNDERR = 3, PKUERR = 4, MTEAERR = 8, MTESERR = 9, ADRALN = 1, ADRERR = 2, OBJERR = 3, MCEERR_AR = 4, MCEERR_AO = 5, BRKPT = 1, TRACE = 2, BRANCH = 3, HWBKPT = 4, UNK = 5, EXITED = 1, KILLED = 2, DUMPED = 3, TRAPPED = 4, STOPPED = 5, CONTINUED = 6, IN = 1, OUT = 2, MSG = 3, ERR = 4, PRI = 5, HUP = 6, };
type flag = enum u64 {
NONE = 0,
NOCLDSTOP = rt::SA_NOCLDSTOP,
NOCLDWAIT = rt::SA_NOCLDWAIT,
ONSTACK = rt::SA_ONSTACK,
RESTART = rt::SA_RESTART,
NODEFER = rt::SA_NODEFER,
RESETHAND = rt::SA_RESETHAND,
};
type handler = fn(sig: sig, info: *siginfo, ucontext: *opaque) void;
type how = enum {
BLOCK = rt::SIG_BLOCK, UNBLOCK = rt::SIG_UNBLOCK, SETMASK = rt::SIG_SETMASK, };
type sig = enum {
HUP = rt::SIGHUP, INT = rt::SIGINT, QUIT = rt::SIGQUIT, ILL = rt::SIGILL, TRAP = rt::SIGTRAP, ABRT = rt::SIGABRT, BUS = rt::SIGBUS, FPE = rt::SIGFPE, KILL = rt::SIGKILL, USR1 = rt::SIGUSR1, SEGV = rt::SIGSEGV, USR2 = rt::SIGUSR2, PIPE = rt::SIGPIPE, ALRM = rt::SIGALRM, TERM = rt::SIGTERM, CHLD = rt::SIGCHLD, CONT = rt::SIGCONT, STOP = rt::SIGSTOP, TSTP = rt::SIGTSTP, TTIN = rt::SIGTTIN, TTOU = rt::SIGTTOU, URG = rt::SIGURG, XCPU = rt::SIGXCPU, XFSZ = rt::SIGXFSZ, VTALRM = rt::SIGVTALRM, PROF = rt::SIGPROF, WINCH = rt::SIGWINCH, IO = rt::SIGIO, POLL = rt::SIGPOLL, PWR = rt::SIGPWR, SYS = rt::SIGSYS, };
type siginfo = union {
struct {
signo: sig,
errno: rt::errno,
code: code,
union {
struct {
pid: unix::pid,
uid: unix::uid,
status: int,
},
struct {
addr: *opaque,
},
},
},
_si_pad: [128 - 3 * size(int)]u8,
};
type sigaction = rt::sigact;
type sigset = rt::sigset;
Functions
fn alarm(sec: uint) uint;
fn block(signals: sig...) sigset;
fn getprocmask() sigset;
fn handle(signum: sig, handler: *handler, flags: flag = flag::NONE, mask: nullable *sigset = null) sigaction;
fn ignore(signum: sig) void;
fn newsigset(items: sig...) sigset;
fn read(fd: io::file) (siginfo | errors::error);
fn reset(signum: sig) void;
fn resetall() void;
fn restore(signum: sig, action: *sigaction) void;
fn setprocmask(how: how, mask: *sigset) sigset;
fn signalfd(signals: sig...) (io::file | errors::error);
fn signame(sig: sig) const str;
fn sigset_add(set: *sigset, items: sig...) void;
fn sigset_del(set: *sigset, items: sig...) void;
fn sigset_empty(set: *sigset) void;
fn sigset_fill(set: *sigset) void;
fn sigset_member(set: *sigset, item: sig) bool;
fn timedwait(set: *sigset, timeout: time::duration) (siginfo | errors::interrupted | errors::again);
fn unblock(signals: sig...) sigset;
fn update(fd: io::file, signals: sig...) (void | errors::error);
fn wait(set: *sigset) (sig | errors::interrupted);
fn waitinfo(set: *sigset) (siginfo | errors::interrupted);
Types
type code
type code = enum {
USER = 0, KERNEL = 128, QUEUE = -1, TIMER = -2, MESQ = -3, ASYNCIO = -4, SIGIO = -5,
TKILL = -6, ASYNCNL = -60,
ILLOPC = 1, ILLOPN = 2, ILLADR = 3, ILLTRP = 4, PRVOPC = 5, PRVREG = 6, COPROC = 7, BADSTK = 8, INTDIV = 1, INTOVF = 2, FLTDIV = 3, FLTOVF = 4, FLTUND = 5, FLTRES = 6, FLTINV = 7, FLTSUB = 8, MAPERR = 1, ACCERR = 2, BNDERR = 3, PKUERR = 4, MTEAERR = 8, MTESERR = 9, ADRALN = 1, ADRERR = 2, OBJERR = 3, MCEERR_AR = 4, MCEERR_AO = 5, BRKPT = 1, TRACE = 2, BRANCH = 3, HWBKPT = 4, UNK = 5, EXITED = 1, KILLED = 2, DUMPED = 3, TRAPPED = 4, STOPPED = 5, CONTINUED = 6, IN = 1, OUT = 2, MSG = 3, ERR = 4, PRI = 5, HUP = 6, };
A code indicating why a signal was sent.
type flag
type flag = enum u64 {
NONE = 0,
NOCLDSTOP = rt::SA_NOCLDSTOP,
NOCLDWAIT = rt::SA_NOCLDWAIT,
ONSTACK = rt::SA_ONSTACK,
RESTART = rt::SA_RESTART,
NODEFER = rt::SA_NODEFER,
RESETHAND = rt::SA_RESETHAND,
};
Flags used to configure the behavior of a signal handler.
type handler
type handler = fn(sig: sig, info: *siginfo, ucontext: *opaque) void;
A function which handles a signal. The first argument is the signal number which was caught, the second provides information about the signal, and the third argument is the ucontext, which is usually ignored by most signal handlers.
type how
type how = enum {
BLOCK = rt::SIG_BLOCK, UNBLOCK = rt::SIG_UNBLOCK, SETMASK = rt::SIG_SETMASK, };
Defines the modes of operation for setprocmask.
type sig
type sig = enum {
HUP = rt::SIGHUP, INT = rt::SIGINT, QUIT = rt::SIGQUIT, ILL = rt::SIGILL, TRAP = rt::SIGTRAP, ABRT = rt::SIGABRT, BUS = rt::SIGBUS, FPE = rt::SIGFPE, KILL = rt::SIGKILL, USR1 = rt::SIGUSR1, SEGV = rt::SIGSEGV, USR2 = rt::SIGUSR2, PIPE = rt::SIGPIPE, ALRM = rt::SIGALRM, TERM = rt::SIGTERM, CHLD = rt::SIGCHLD, CONT = rt::SIGCONT, STOP = rt::SIGSTOP, TSTP = rt::SIGTSTP, TTIN = rt::SIGTTIN, TTOU = rt::SIGTTOU, URG = rt::SIGURG, XCPU = rt::SIGXCPU, XFSZ = rt::SIGXFSZ, VTALRM = rt::SIGVTALRM, PROF = rt::SIGPROF, WINCH = rt::SIGWINCH, IO = rt::SIGIO, POLL = rt::SIGPOLL, PWR = rt::SIGPWR, SYS = rt::SIGSYS, };
All possible signals.
type siginfo
type siginfo = union {
struct {
signo: sig,
errno: rt::errno,
code: code,
union {
struct {
pid: unix::pid,
uid: unix::uid,
status: int,
},
struct {
addr: *opaque,
},
},
},
_si_pad: [128 - 3 * size(int)]u8,
};
Provides additional information about signal deliveries. Only the members defined by POSIX are available here; cast to rt::siginfo to access non-portable members.
type sigaction
Show undocumented member
type sigaction = rt::sigact;
type sigset
Show undocumented member
type sigset = rt::sigset;
Functions
fn alarm
fn alarm(sec: uint) uint;
Requests that sig::ALRM is delivered to the calling process in (about) "sec" seconds. Returns the number of seconds until the previously scheduled alarm, or zero if none was scheduled.
fn block
fn block(signals: sig...) sigset;
Adds the given list of signals to the process's current signal mask, returning the old signal mask. This is a convenience function around setprocmask.
fn getprocmask
fn getprocmask() sigset;
Gets the current process's signal mask.
fn handle
fn handle(signum: sig, handler: *handler, flags: flag = flag::NONE, mask: nullable *sigset = null) sigaction;
Configures a new signal handler, returning the old details (which can be passed to restore to restore its behavior).
fn ignore
fn ignore(signum: sig) void;
Prevents given signal from arriving to the current process. One common use case is to ignore SIGCHLD to avoid zombie child processes.
fn newsigset
fn newsigset(items: sig...) sigset;
Creates a new signal set filled in with the provided signals (or empty if none are provided).
fn read
fn read(fd: io::file) (siginfo | errors::error);
Reads pending signal info from a signalfd.
fn reset
fn reset(signum: sig) void;
Unregisters signal handlers for the specified signal.
fn resetall
fn resetall() void;
Unregisters all signal handlers.
fn restore
fn restore(signum: sig, action: *sigaction) void;
Restores previous signal behavior following handle.
fn setprocmask
fn setprocmask(how: how, mask: *sigset) sigset;
Sets the process's signal mask, returning the previous mask.
fn signalfd
fn signalfd(signals: sig...) (io::file | errors::error);
Creates a signal file that handles the given set of signals.
fn signame
fn signame(sig: sig) const str;
Returns the human friendly name of a given signal.
fn sigset_add
fn sigset_add(set: *sigset, items: sig...) void;
Adds signals to a sigset.
fn sigset_del
fn sigset_del(set: *sigset, items: sig...) void;
Removes signals from a sigset.
fn sigset_empty
fn sigset_empty(set: *sigset) void;
Sets a sigset to empty.
fn sigset_fill
fn sigset_fill(set: *sigset) void;
Adds all platform-defined signals to a sigset.
fn sigset_member
fn sigset_member(set: *sigset, item: sig) bool;
Returns true if the given signal is a member of this sigset.
fn timedwait
fn timedwait(set: *sigset, timeout: time::duration) (siginfo | errors::interrupted | errors::again);
Waits for a signal among the given sigset to be delivered, then returns the corresponding siginfo data.
Returns a siginfo if a signal is successfully processed through this function, or errors::again if the timeout expired. See notes on wait regarding the errors::interrupted case.
This function is designed to provide the portable subset of the semantics of sigtimedwait(3) as defined by POSIX.1-2008. To access the complete siginfo_t structure provided by the underlying platform, use rt::sigtimedwait and rt::siginfo_t directly.
Note that this function is not supported on OpenBSD.
fn unblock
fn unblock(signals: sig...) sigset;
Removes the given list of signals from the process's current signal mask, returning the old signal mask. This is a convenience function around setprocmask.
fn update
fn update(fd: io::file, signals: sig...) (void | errors::error);
Updates a signalfd with a new set of signals. The signal set is overwritten, rather than appended to, with the provided set of signals.
fn wait
fn wait(set: *sigset) (sig | errors::interrupted);
Waits for a signal among the given sigset to be delivered, then returns the signal number.
If a signal is received while waiting, errors::interrupted is returned. Most consumers of this function will likely wish to block all signals and handle them exclusively through wait et al, in which case this error cannot occur.
See also waitinfo and timedwait.
fn waitinfo
fn waitinfo(set: *sigset) (siginfo | errors::interrupted);
Waits for a signal among the given sigset to be delivered, then returns the corresponding siginfo data.
See notes on wait regarding the errors::interrupted case.
This function is designed to provide the portable subset of the semantics of sigwaitinfo(3) as defined by POSIX.1-2008. To access the complete siginfo_t structure provided by the underlying platform, use rt::sigwaitinfo and rt::siginfo_t directly.
Note that this function is not supported on OpenBSD.