#signals #linux #api-bindings #sigset-t

pakr-signals

A set of functions to wrap Linux' sigset_t functionality and to do a rudimentary signals managemet

1 stable release

1.0.0 Sep 26, 2020

#905 in Unix APIs


Used in pakr-mio-signalfd

BSD-2-Clause

15KB
295 lines

A set of tools wrapping Linux' libc::sigset_t functionality and supporting Rust-firendly signals and pids definition.

Examples

use pakr_signals::*;

// Create empty SigSet
let mut sigset = SigSet::new();

// Add SIGUSR1 to SigSet
sigset.add(Sig::USR1);

// Tests of presence

// SIGUSR1 is present
assert!(sigset.has(Sig::USR1));

// SIGUSR2 is not present
assert!(!sigset.has(Sig::USR2));

// Has at least one of SIGUSR1/SIGUSR2
assert!(sigset.has_any(&[Sig::USR1,Sig::USR2]));

// Doesn't have every of SIGUSR1/SIGUSR2
assert!(!sigset.has_all(&[Sig::USR1,Sig::USR2]));

// Hide from runtime. SIGINT (aka ^C) won't be handled by runtime anymore (^C won't break the
// program). It may be then handled by user-defined handler down the code.

let sigint = SigSet::from(&[Sig::INT]);
sigint.disable_default_handler().expect("Can't disable default handler for SIGINT");

let my_pid =  Pid::own().expect("Cant't get own PID");

// Send SIGINT to self (should be ignored)
Sig::INT.send_to(my_pid).expect("Can't send SIGINT");

// other syntax:
my_pid.send(Sig::INT).expect("Can't send SIGINT");

// Mark as processed by runtime. SIGQUIT (aka ^\) will be handled by runtime and would break the
// program with a core dump (if enabled in system).
//
// This is the default state - initally all signals are delivered to runtime, regardless of
// whether runtime ignores them (like SIGUSR1) or not (like SIGQUIT)
let sigquit = SigSet::from(&[Sig::QUIT]);
sigquit.enable_default_handler().expect("Can't enable default handler for SIGQUIT");

Dependencies

~44KB