signal_handling
import std::process::signal_handling; · source
Receiving the interrupt and terminate signals, so a program can shut down cleanly instead of being ended. Signals::listen() starts receiving them. While any Signals is alive they no longer end the process: wait, wait_timeout, and try_wait hand each one over instead. Once the last Signals is dropped they end the process again, as they did before.
import std::process::signal::{ Signal };
import std::process::signal_handling::{ Signals };
const signals: Signals = Signals::listen()?;
const received: Signal = signals.wait(); // Ctrl-C, or a SIGTERM
What arrives:
| Platform | Interrupt (Signal::interrupt()) | Terminate (Signal::terminate()) |
|---|---|---|
| POSIX | SIGINT | SIGTERM |
| Windows | Ctrl-C and Ctrl-Break | closing the console, logging off, shutting down |
A terminate on Windows cannot be refused: the process ends once the program exits or Windows' time limit runs out, whichever comes first. The limit is a system setting, by default 5 seconds (20 for a service at shutdown), so cleanup after it must be quick. Until then the program runs as normal and wait hands the terminate over. Logoff and shutdown reach only services, and only a program that has not loaded the Windows user interface libraries (user32 or gdi32); Windows ends any other program at logoff and shutdown without a signal.
Every Signals in the process shares one record of what arrived, so each signal is handed to one waiter, whichever asks first. Signals that arrive before anyone asks are each kept; when both kinds are waiting, an interrupt is handed over before a terminate.
Types
Signals
type struct Signals {}
listen() fails only when the operating system refuses to set up the handler or the channel it wakes waiters through. wait() blocks until a signal is handed over, wait_timeout(duration) gives up with None when the time is up, and try_wait() answers None at once when nothing is waiting.
Constructors
static listen() -> Result<Signals, IoError>;
static listen() -> Result<Signals, IoError>;Start receiving the interrupt and terminate signals. From now until the last Signals is dropped they no longer end the process. An error is the operating system's refusal to set up the handler or the channel it wakes waiters through.
Methods
try_wait(&this) -> Option<Signal>;
try_wait(&this) -> Option<Signal>;The next signal not yet handed over, or None at once when there is none.
wait(&this) -> Signal;
wait(&this) -> Signal;Block until a signal arrives, and hand it over. One that arrived before the call is handed over at once.
wait_timeout(&this, duration: Duration) -> Option<Signal>;
wait_timeout(&this, duration: Duration) -> Option<Signal>;As wait, giving up with None once duration has passed.
Trait implementations
implement trait Drop for struct Signals
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct Signal with [Eq] {}A signal, sent with Child.send_signal and received with signal_handling::Signals. Build one with a named constructor (Signal::kill()), from a SignalNumber with new, or from a raw number with from_i32.
namespace std::process::signal_handling;std::process::signal_handling: receiving the interrupt and terminate signals, so a program can shut down cleanly instead of being ended.
module pagetype struct Signals {}Receives the interrupt and terminate signals for as long as it lives; see the module documentation.
std::process::signal_handlingconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const signals: Signalsstatic Signals::listen() -> Result<Signals, IoError>Start receiving the interrupt and terminate signals. From now until the last Signals is dropped they no longer end the process. An error is the operating system's refusal to set up the handler or the channel it wakes waiters through.
value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. It works on any type implementing Try, in a function whose return type implements FromResidual of it; an error of another type converts through From.
const received: Signalconst signals: SignalsSignals::wait(&this) -> SignalBlock until a signal arrives, and hand it over. One that arrived before the call is handed over at once.
std::process::signal_handlingtype struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Signals {}Receives the interrupt and terminate signals for as long as it lives; see the module documentation.
Signals::wait_timeout(&this, duration: Duration) -> Option<Signal>As wait, giving up with None once duration has passed.
Signals::try_wait(&this) -> Option<Signal>The next signal not yet handed over, or None at once when there is none.
static Signal::interrupt() -> SignalSIGINT (2): an interrupt from the keyboard (Ctrl-C); by default it ends the process.
std::process::signalstatic Signal::terminate() -> SignalSIGTERM (15): a request to end, which the process may catch to shut down cleanly; by default it ends the process. Windows: refused by send_signal (Unsupported) - use kill; Signals hands over the console closing, logoff and shutdown as this signal.
Option::Nonestatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}The error of a failed I/O operation: its kind, the operating system's own code, and, when the operation named one, what it was doing and the path. It prints as could not open config.toml: not found.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
(parameter) duration: Durationtype struct Duration with [Eq, Ord, Debug] {
const NANOSECONDS_PER_SECOND: u64 = 1000000000;
const NANOSECONDS_PER_MILLISECOND: u64 = 1000000;
const NANOSECONDS_PER_MICROSECOND: u64 = 1000;
const MILLISECONDS_PER_SECOND: u64 = 1000;
const MICROSECONDS_PER_SECOND: u64 = 1000000;
const SECONDS_PER_MINUTE: u64 = 60;
const SECONDS_PER_HOUR: u64 = 3600;
const SECONDS_PER_DAY: u64 = 86400;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop. Ordered by seconds, then nanoseconds.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Drop {
drop(mut &this) -> void;
}A type that releases something when a value of it ends: memory, a file handle, a lock. The compiler calls drop once when an owned value goes out of scope; release a value early with mem::drop(value).
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference