mermaid_cli/utils/
proc.rs1use std::process::Stdio;
21use std::time::Duration;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum Grace {
26 Immediate,
28 Graceful,
30}
31
32const GRACE_PERIOD: Duration = Duration::from_millis(400);
34
35pub async fn terminate_tree(pid: u32, grace: Grace) {
38 #[cfg(not(target_os = "windows"))]
39 {
40 if grace == Grace::Graceful {
41 unix_kill("-TERM", pid).await;
42 tokio::time::sleep(GRACE_PERIOD).await;
43 }
44 unix_kill("-KILL", pid).await;
45 }
46 #[cfg(target_os = "windows")]
47 {
48 let _ = grace; taskkill_tree(pid).await;
50 }
51}
52
53pub fn terminate_tree_blocking(pid: u32, grace: Grace) {
56 #[cfg(not(target_os = "windows"))]
57 {
58 if grace == Grace::Graceful {
59 unix_kill_blocking("-TERM", pid);
60 std::thread::sleep(GRACE_PERIOD);
61 }
62 unix_kill_blocking("-KILL", pid);
63 }
64 #[cfg(target_os = "windows")]
65 {
66 let _ = grace;
67 taskkill_tree_blocking(pid);
68 }
69}
70
71#[cfg(not(target_os = "windows"))]
72async fn unix_kill(signal: &str, pid: u32) {
73 let _ = tokio::process::Command::new("kill")
74 .args([signal, "--", &format!("-{pid}"), &pid.to_string()])
75 .stdin(Stdio::null())
76 .stdout(Stdio::null())
77 .stderr(Stdio::null())
78 .status()
79 .await;
80}
81
82#[cfg(not(target_os = "windows"))]
83fn unix_kill_blocking(signal: &str, pid: u32) {
84 let _ = std::process::Command::new("kill")
85 .args([signal, "--", &format!("-{pid}"), &pid.to_string()])
86 .stdin(Stdio::null())
87 .stdout(Stdio::null())
88 .stderr(Stdio::null())
89 .status();
90}
91
92#[cfg(target_os = "windows")]
93async fn taskkill_tree(pid: u32) {
94 let _ = tokio::process::Command::new("taskkill")
95 .args(["/PID", &pid.to_string(), "/T", "/F"])
96 .stdin(Stdio::null())
97 .stdout(Stdio::null())
98 .stderr(Stdio::null())
99 .status()
100 .await;
101}
102
103#[cfg(target_os = "windows")]
104fn taskkill_tree_blocking(pid: u32) {
105 let _ = std::process::Command::new("taskkill")
106 .args(["/PID", &pid.to_string(), "/T", "/F"])
107 .stdin(Stdio::null())
108 .stdout(Stdio::null())
109 .stderr(Stdio::null())
110 .status();
111}