pub struct Signals(/* private fields */);Expand description
A handle for subscribing to OS signals delivered through a shared channel.
Signals is cheaply cloneable (backed by an Arc). Only one Signals
instance may be active at a time per process; calling Signals::new while
another instance exists returns SignalError::AlreadyInitialized.
OS-level signal handlers are installed automatically by Signals::new,
so signals are delivered from the moment the value is returned. Call
subscribe to obtain a Receiver.
Dropping the last clone releases all resources including the background thread.
§Example
use signal_msg::Signals;
let signals = Signals::new().expect("failed to create signal handler");
for sig in signals.subscribe() {
println!("received: {}", sig);
if sig.is_terminating() { break; }
}Implementations§
Source§impl Signals
impl Signals
Sourcepub fn new() -> Result<Self, SignalError>
pub fn new() -> Result<Self, SignalError>
Creates a new signal channel and installs OS-level signal handlers.
Allocates a self-pipe, spawns a background dispatch thread named
signal-msg, and registers handlers for all supported signals. Call
subscribe to obtain a Receiver.
§Examples
let signals = signal_msg::Signals::new().expect("signal setup failed");
let receiver = signals.subscribe();§Errors
Returns SignalError::AlreadyInitialized if another Signals instance
is already active. Returns SignalError::OsError if an OS-level
operation fails (pipe creation, fcntl, or thread spawn).
Examples found in repository?
3fn main() {
4 let signals = Signals::new().expect("failed to create signal handler");
5 println!("Waiting for signals...");
6 println!("(try SIGUSR1, SIGWINCH, SIGCONT; send SIGINT or SIGTERM to exit)\n");
7
8 for sig in signals.subscribe() {
9 println!("Got signal: {}", sig);
10 if sig.is_terminating() {
11 println!("\nTerminating on {}.", sig);
12 break;
13 }
14 }
15}Sourcepub fn subscribe(&self) -> Receiver ⓘ
pub fn subscribe(&self) -> Receiver ⓘ
Returns a new Receiver that will receive all subsequent signals.
Multiple independent receivers can be created from the same Signals
handle; each receives its own copy of every delivered signal.
§Examples
use signal_msg::Signals;
let signals = Signals::new().expect("signal setup failed");
let r1 = signals.subscribe();
let r2 = signals.subscribe();
// r1 and r2 each receive independent copies of every signal.Examples found in repository?
3fn main() {
4 let signals = Signals::new().expect("failed to create signal handler");
5 println!("Waiting for signals...");
6 println!("(try SIGUSR1, SIGWINCH, SIGCONT; send SIGINT or SIGTERM to exit)\n");
7
8 for sig in signals.subscribe() {
9 println!("Got signal: {}", sig);
10 if sig.is_terminating() {
11 println!("\nTerminating on {}.", sig);
12 break;
13 }
14 }
15}