std.signal
Handling of Unix signals.
This module allows processes to wait for Unix signals to be received,
overriding their default behaviour in the process. To do so, create an
instance of the Signal
enum and call Signal.wait
on it:
import std.signal (Signal)
Signal.User1.wait
Signals are handled asynchronously, meaning there may be a small delay between
receiving the signal and Signal.wait
returning. The order in which signals
are received is unspecified. The underlying platform may also coalesce
multiple instances of the same signal being received into a single receive,
such that sending e.g. SIGHUP
3 times in a short period results in it only
being received once.
Default signal handlers
If no process is waiting for a specific signal and the signal is received, it's default behaviour according to the underlying platform is invoked. For example, the default signal handler for SIGUSR1 terminates the current process.
Supported signals
To increase portability and to prevent users from handling problematic signals (SIGSEGV or SIGFPE for example), this module only supports a limited number of signals.
Handling of SIGPIPE isn't supported as this is almost always a terrible idea, and the runtime disables/masks this signal as to not interfere with (amongst other things) non-blocking sockets.
Waiting for multiple signals
If you need to wait for multiple signals at once, you can do so using the
Signals
type:
import std.signal (Signal, Signals)
let signals = Signals.new
signals.add(Signal.Quit)
signals.add(Signal.Terminate)
loop {
match signals.wait {
case Quit -> {
# handle SIGQUIT
}
case Terminate -> {
# handle SIGTERM
}
case _ -> {
# Other signals are never delivered here, so these can be ignored.
}
}
}
Types
Value | Signal | A Unix signal. |
Signals | A collection of signals to respond to. |