wasi_virt_layer_cli/
ctrlc_handler.rs1use std::sync::{Arc, Mutex, OnceLock};
2
3type CleanupFn = Box<dyn Fn() + Send + Sync>;
4
5static CLEANUPS: OnceLock<Arc<Mutex<Vec<CleanupFn>>>> = OnceLock::new();
6
7pub fn init() {
8 CLEANUPS.get_or_init(|| {
9 let cleanups = Arc::new(Mutex::new(Vec::<CleanupFn>::new()));
10 let cleanups_clone = cleanups.clone();
11
12 let _ = ctrlc::set_handler(move || {
14 if let Ok(guards) = cleanups_clone.lock() {
15 for guard in guards.iter().rev() {
16 guard();
17 }
18 }
19 std::process::exit(130);
20 });
21
22 cleanups
23 });
24}
25
26pub fn register(f: impl Fn() + Send + Sync + 'static) {
27 init(); if let Some(cleanups) = CLEANUPS.get() {
29 cleanups.lock().unwrap().push(Box::new(f));
30 }
31}