-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.rs
More file actions
61 lines (56 loc) · 2.23 KB
/
user.rs
File metadata and controls
61 lines (56 loc) · 2.23 KB
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
use serde::Serialize;
use snafu::{OptionExt, ResultExt, Snafu};
use sysinfo::{Gid, Pid, ProcessRefreshKind, Uid, UpdateKind};
use crate::error::SysinfoError;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("failed to get pid of the current process"))]
GetCurrentPid { source: SysinfoError },
#[snafu(display("current pid {pid} could not be resolved to a process"))]
ResolveCurrentProcess { pid: Pid },
}
type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Serialize)]
pub struct User {
pub name: Option<String>, // The name of the current user
pub uid: Option<Uid>, // The user ID (UID)
pub gid: Option<Gid>, // The group ID (GID)
}
impl User {
#[tracing::instrument(name = "User::init", skip(sys))]
pub fn init(sys: &mut sysinfo::System) -> Result<()> {
let pid = sysinfo::get_current_pid()
.map_err(|msg| SysinfoError { msg })
.context(GetCurrentPidSnafu)?;
// The process user is static, and there is a memory leak to updating it for every run, so cache it once and keep that.
sys.refresh_processes_specifics(
sysinfo::ProcessesToUpdate::Some(&[pid]),
false,
ProcessRefreshKind::nothing().with_user(UpdateKind::OnlyIfNotSet),
);
Ok(())
}
#[tracing::instrument(name = "User::collect_current", skip(sys))]
pub fn collect_current(sys: &sysinfo::System) -> Result<Self> {
let pid = sysinfo::get_current_pid()
.map_err(|msg| SysinfoError { msg })
.context(GetCurrentPidSnafu)?;
let current_process = sys
.process(pid)
.context(ResolveCurrentProcessSnafu { pid })?;
let uid = current_process.user_id();
let os_users = sysinfo::Users::new_with_refreshed_list();
let user = Self {
name: uid.and_then(|uid| Some(os_users.get_user_by_id(uid)?.name().to_string())),
uid: uid.cloned(),
gid: current_process.group_id(),
};
tracing::info!(
user.name,
user.uid = user.uid.as_ref().map(|uid| format!("{uid:?}")),
user.gid = user.gid.as_ref().map(|gid| format!("{gid:?}")),
"current user"
);
Ok(user)
}
}