fallow_cli/signal/
registry.rs1use std::sync::atomic::{AtomicU64, Ordering};
15use std::sync::{Mutex, OnceLock};
16use std::time::{Duration, Instant};
17
18use rustc_hash::FxHashMap;
19
20static NEXT_ID: AtomicU64 = AtomicU64::new(1);
21static REGISTRY: OnceLock<Mutex<FxHashMap<u64, u32>>> = OnceLock::new();
22
23static DRAINING: AtomicU64 = AtomicU64::new(0);
26
27fn registry() -> &'static Mutex<FxHashMap<u64, u32>> {
28 REGISTRY.get_or_init(|| Mutex::new(FxHashMap::default()))
29}
30
31pub(super) fn register(pid: u32) -> u64 {
34 let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
35 registry()
36 .lock()
37 .unwrap_or_else(|e| e.into_inner())
38 .insert(id, pid);
39 id
40}
41
42pub(super) fn deregister(id: u64) {
44 registry()
45 .lock()
46 .unwrap_or_else(|e| e.into_inner())
47 .remove(&id);
48}
49
50pub(super) fn drain_and_kill() {
57 if DRAINING
58 .compare_exchange(0, 1, Ordering::SeqCst, Ordering::SeqCst)
59 .is_err()
60 {
61 return;
62 }
63
64 let pids: Vec<u32> = {
65 registry()
66 .lock()
67 .unwrap_or_else(|e| e.into_inner())
68 .drain()
69 .map(|(_id, pid)| pid)
70 .collect()
71 };
72
73 for pid in &pids {
74 kill_pid(*pid);
75 }
76
77 let deadline = Instant::now() + drain_budget();
78 while Instant::now() < deadline {
79 if !pids.iter().copied().any(pid_is_alive) {
80 return;
81 }
82 std::thread::sleep(Duration::from_millis(50));
83 }
84}
85
86#[cfg(unix)]
87fn kill_pid(pid: u32) {
88 let _ = std::process::Command::new("kill")
95 .args(["-9", &pid.to_string()])
96 .stdout(std::process::Stdio::null())
97 .stderr(std::process::Stdio::null())
98 .status();
99}
100
101#[cfg(windows)]
102fn kill_pid(pid: u32) {
103 use windows_sys::Win32::Foundation::{CloseHandle, FALSE, HANDLE};
104 use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_TERMINATE, TerminateProcess};
105 unsafe {
109 let handle: HANDLE = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
110 if handle.is_null() {
111 return;
112 }
113 let _ = TerminateProcess(handle, 1);
114 let _ = CloseHandle(handle);
115 }
116}
117
118#[cfg(not(any(unix, windows)))]
119fn kill_pid(_pid: u32) {
120 }
122
123#[cfg(unix)]
124fn pid_is_alive(pid: u32) -> bool {
125 std::process::Command::new("kill")
126 .args(["-0", &pid.to_string()])
127 .stdout(std::process::Stdio::null())
128 .stderr(std::process::Stdio::null())
129 .status()
130 .is_ok_and(|s| s.success())
131}
132
133#[cfg(windows)]
134fn pid_is_alive(pid: u32) -> bool {
135 use windows_sys::Win32::Foundation::{CloseHandle, FALSE, HANDLE, WAIT_OBJECT_0};
136 use windows_sys::Win32::System::Threading::{
137 OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, WaitForSingleObject,
138 };
139 unsafe {
141 let handle: HANDLE = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
142 if handle.is_null() {
143 return false;
144 }
145 let result = WaitForSingleObject(handle, 0);
146 let _ = CloseHandle(handle);
147 result != WAIT_OBJECT_0
148 }
149}
150
151#[cfg(not(any(unix, windows)))]
152fn pid_is_alive(_pid: u32) -> bool {
153 false
154}
155
156#[cfg(unix)]
157const fn drain_budget() -> Duration {
158 Duration::from_millis(500)
159}
160
161#[cfg(windows)]
162const fn drain_budget() -> Duration {
163 Duration::from_millis(1500)
164}
165
166#[cfg(not(any(unix, windows)))]
167const fn drain_budget() -> Duration {
168 Duration::from_millis(500)
169}
170
171#[cfg(test)]
172mod tests {
173 use super::*;
174
175 #[test]
176 fn register_deregister_roundtrip() {
177 let id = register(42);
178 assert!(id > 0);
179 deregister(id);
180 deregister(id);
182 }
183
184 #[test]
185 fn ids_are_monotonic() {
186 let a = register(100);
187 let b = register(200);
188 assert!(b > a);
189 deregister(a);
190 deregister(b);
191 }
192}