/
mod.rs
234 lines (188 loc) 路 6.58 KB
/
mod.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::cmp;
use std::hash;
use std::io;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use heim_common::prelude::*;
use heim_common::units::Time;
use heim_runtime as rt;
use super::{pid_exists, pids};
use crate::os::unix::Signal;
use crate::sys::common::UniqueId;
use crate::sys::linux::process::procfs::process_file_path;
use crate::sys::unix::{pid_kill, pid_priority, pid_setpriority, pid_wait};
use crate::{Pid, ProcessError, ProcessResult, Status};
mod procfs;
pub use self::procfs::{Command, CommandIter, CpuTime, Environment, IoCounters, Memory};
#[derive(Debug)]
pub struct Process {
pid: Pid,
unique_id: UniqueId,
}
impl Process {
pub fn pid(&self) -> Pid {
self.pid
}
pub async fn parent_pid(&self) -> ProcessResult<Pid> {
let procfs::Stat { ppid, .. } = procfs::stat(self.pid).await?;
Ok(ppid)
}
pub async fn name(&self) -> ProcessResult<String> {
let procfs::Stat { name, .. } = procfs::stat(self.pid).await?;
// TODO: Move `15` to the const
if name.len() >= 15 {
let command = match procfs::command(self.pid).await {
Ok(command) => command,
// Reading process command might fail, so we should better fall back to what we got
Err(..) => return Ok(name),
};
// There might be an absolute path to executable
let path = command
.into_iter()
.next()
.map(Path::new)
.and_then(Path::file_name);
match path {
// We can assume that on Linux paths and filenames are UTF-8,
// and since OsStr does not has the `starts_with` method,
// we could compare raw bytes
Some(exe) if exe.as_bytes().starts_with(name.as_bytes()) => {
Ok(exe.to_string_lossy().into_owned())
}
_ => Ok(name),
}
} else {
Ok(name)
}
}
pub async fn exe(&self) -> ProcessResult<PathBuf> {
match rt::fs::read_link(process_file_path(self.pid, "exe")).await {
Ok(path) => Ok(path),
Err(..) => {
// log::trace!() ?
if pid_exists(self.pid).await? {
// Not enough permissions to read the symlink
Ok(PathBuf::new())
} else {
Err(ProcessError::ZombieProcess(self.pid))
}
}
}
}
pub async fn command(&self) -> ProcessResult<Command> {
procfs::command(self.pid).await
}
pub async fn cwd(&self) -> ProcessResult<PathBuf> {
match rt::fs::read_link(process_file_path(self.pid, "cwd")).await {
Ok(path) => Ok(path),
Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {
Err(ProcessError::AccessDenied(self.pid))
}
Err(..) => {
if pid_exists(self.pid).await? {
Err(ProcessError::ZombieProcess(self.pid))
} else {
Err(ProcessError::AccessDenied(self.pid))
}
}
}
}
pub async fn status(&self) -> ProcessResult<Status> {
let procfs::Stat { state, .. } = procfs::stat(self.pid).await?;
Ok(state)
}
pub async fn environment(&self) -> ProcessResult<Environment> {
procfs::environment(self.pid).await
}
pub async fn create_time(&self) -> ProcessResult<Time> {
Ok(self.unique_id.create_time())
}
pub async fn cpu_time(&self) -> ProcessResult<CpuTime> {
procfs::stat(self.pid).await.map(Into::into)
}
pub async fn memory(&self) -> ProcessResult<Memory> {
procfs::stat_memory(self.pid).await
}
pub async fn niceness(&self) -> ProcessResult<i32> {
pid_priority(self.pid)
}
pub async fn set_niceness(&self, value: libc::c_int) -> ProcessResult<()> {
pid_setpriority(self.pid, value)
}
pub async fn is_running(&self) -> ProcessResult<bool> {
let other = get(self.pid).await?;
Ok(other == *self)
}
pub async fn _signal(&self, signal: Signal) -> ProcessResult<()> {
if self.is_running().await? {
pid_kill(self.pid, signal)
} else {
Err(ProcessError::NoSuchProcess(self.pid))
}
}
pub fn signal(&self, signal: Signal) -> BoxFuture<ProcessResult<()>> {
self._signal(signal).boxed()
}
pub async fn suspend(&self) -> ProcessResult<()> {
self._signal(Signal::Stop).await
}
pub async fn resume(&self) -> ProcessResult<()> {
self._signal(Signal::Cont).await
}
pub async fn terminate(&self) -> ProcessResult<()> {
self._signal(Signal::Term).await
}
pub async fn kill(&self) -> ProcessResult<()> {
self.signal(Signal::Kill).await
}
pub async fn wait(&self) -> ProcessResult<()> {
pid_wait(self.pid).await
}
// Linux-specific methods
pub async fn io_counters(&self) -> ProcessResult<IoCounters> {
procfs::io(self.pid).await
}
pub async fn net_io_counters(
&self,
) -> ProcessResult<BoxStream<'_, ProcessResult<heim_net::IoCounters>>> {
// TODO: Convert specific errors into ProcessResult error variants
let stream = match heim_net::os::linux::io_counters_for_pid(self.pid()).await {
Ok(stream) => stream,
Err(e) if e.kind() == io::ErrorKind::NotFound => {
return match self.status().await {
Ok(Status::Zombie) => Err(ProcessError::ZombieProcess(self.pid)),
_ => Err(e.into()),
}
}
Err(e) => return Err(e.into()),
};
Ok(stream.map_err(Into::into).boxed())
}
}
impl hash::Hash for Process {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.unique_id.hash(state);
}
}
impl cmp::PartialEq for Process {
fn eq(&self, other: &Self) -> bool {
self.unique_id == other.unique_id
}
}
impl cmp::Eq for Process {}
pub async fn processes() -> Result<impl Stream<Item = ProcessResult<Process>>> {
let pids = pids().await?;
let stream = pids.map_err(Into::into).and_then(get);
Ok(stream)
}
pub async fn get(pid: Pid) -> ProcessResult<Process> {
let procfs::Stat { create_time, .. } = procfs::stat(pid).await?;
Ok(Process {
pid,
unique_id: UniqueId::new(pid, create_time),
})
}
pub async fn current() -> ProcessResult<Process> {
let pid = unsafe { libc::getpid() };
get(pid).await
}