File: print.rs

package info (click to toggle)
rust-async-signal 0.2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Inspired by https://github.com/vorner/signal-hook/blob/3473f4520a710f05d352275731100807196de519/examples/print.rs

use async_signal::{Signal, Signals};
use futures_lite::prelude::*;
use signal_hook::low_level;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    async_io::block_on(async {
        // Register the signals we want to receive.
        #[cfg(unix)]
        let signals = Signals::new([
            Signal::Term,
            Signal::Quit,
            Signal::Int,
            Signal::Tstp,
            Signal::Winch,
            Signal::Hup,
            Signal::Child,
            Signal::Cont,
        ])?;

        #[cfg(windows)]
        let signals = Signals::new([Signal::Int])?;

        // Loop over the signals.
        signals
            .for_each(|signal| {
                // Print the signal.
                eprintln!("Received signal {:?}", signal);

                // After printing it, do whatever the signal was supposed to do in the first place.
                low_level::emulate_default_handler(signal.unwrap() as i32).unwrap();
            })
            .await;

        Ok(())
    })
}