Function signal_hook::flag::register_conditional_shutdown
source · pub fn register_conditional_shutdown(
signal: c_int,
status: c_int,
condition: Arc<AtomicBool>
) -> Result<SigId, Error>
Expand description
Terminate the application on a signal if the given condition is true.
This can be used for different use cases. One of them (with the condition being always true) is just unconditionally terminate on the given signal.
Another is being able to turn on and off the behaviour by the shared flag.
The last one is handling double CTRL+C ‒ if the user presses CTRL+C, we would like to start a graceful shutdown. But if anything ever gets stuck in the shutdown, second CTRL+C (or other such termination signal) should terminate the application without further delay.
To do that, one can combine this with register
. On the first run, the flag is false
and
this doesn’t terminate. But then the flag is set to true during the first run and „arms“ the
shutdown on the second run. Note that it matters in which order the actions are registered (the
shutdown must go first). And yes, this also allows asking the user „Do you want to terminate“
and disarming the abrupt shutdown if the user answers „No“.
Panics
If the signal is one of the forbidden.