/
machine.rs
152 lines (131 loc) · 3.42 KB
/
machine.rs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
use std::{
ffi::OsString,
path::{Path, PathBuf},
time::Duration,
};
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
pub fn process_at_path_exists(pid: u32, name: &Path) -> bool {
let mut sys = System::new();
let pid = Pid::from_u32(pid);
if !sys.refresh_process(pid) {
return false;
}
let name_str = format!("{}", name.display());
if let Some(process) = sys.process(pid) {
for cmd in process.cmd() {
if cmd.contains(&name_str) {
return true;
}
}
}
false
}
pub fn process_exists(pid: u32) -> bool {
let mut sys = System::new();
sys.refresh_process(Pid::from_u32(pid))
}
pub fn kill_pid(pid: u32) -> bool {
let mut sys = System::new();
let pid = Pid::from_u32(pid);
sys.refresh_process(pid);
if let Some(p) = sys.process(pid) {
p.kill()
} else {
false
}
}
pub async fn wait_until_process_exits(pid: Pid, poll_ms: u64) {
let mut s = System::new();
let duration = Duration::from_millis(poll_ms);
while s.refresh_process(pid) {
tokio::time::sleep(duration).await;
}
}
pub fn find_running_process(name: &Path) -> Option<u32> {
let mut sys = System::new();
sys.refresh_processes();
let name_str = format!("{}", name.display());
for (pid, process) in sys.processes() {
for cmd in process.cmd() {
if cmd.contains(&name_str) {
return Some(pid.as_u32());
}
}
}
None
}
pub async fn wait_until_exe_deleted(current_exe: &Path, poll_ms: u64) {
let duration = Duration::from_millis(poll_ms);
while current_exe.exists() {
tokio::time::sleep(duration).await;
}
}
/// Gets the canonical current exe location, referring to the "current" symlink
/// if running inside snap.
pub fn canonical_exe() -> std::io::Result<PathBuf> {
canonical_exe_inner(
std::env::current_exe(),
std::env::var_os("SNAP"),
std::env::var_os("SNAP_REVISION"),
)
}
#[inline(always)]
#[allow(unused_variables)]
fn canonical_exe_inner(
exe: std::io::Result<PathBuf>,
snap: Option<OsString>,
rev: Option<OsString>,
) -> std::io::Result<PathBuf> {
let exe = exe?;
#[cfg(target_os = "linux")]
if let (Some(snap), Some(rev)) = (snap, rev) {
if !exe.starts_with(snap) {
return Ok(exe);
}
let mut out = PathBuf::new();
for part in exe.iter() {
if part == rev {
out.push("current")
} else {
out.push(part)
}
}
return Ok(out);
}
Ok(exe)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
#[cfg(target_os = "linux")]
fn test_canonical_exe_in_snap() {
let exe = canonical_exe_inner(
Ok(PathBuf::from("/snap/my-snap/1234/some/exe")),
Some("/snap/my-snap/1234".into()),
Some("1234".into()),
)
.unwrap();
assert_eq!(exe, PathBuf::from("/snap/my-snap/current/some/exe"));
}
#[test]
fn test_canonical_exe_not_in_snap() {
let exe = canonical_exe_inner(
Ok(PathBuf::from("/not-in-snap")),
Some("/snap/my-snap/1234".into()),
Some("1234".into()),
)
.unwrap();
assert_eq!(exe, PathBuf::from("/not-in-snap"));
}
#[test]
fn test_canonical_exe_not_in_snap2() {
let exe = canonical_exe_inner(Ok(PathBuf::from("/not-in-snap")), None, None).unwrap();
assert_eq!(exe, PathBuf::from("/not-in-snap"));
}
}