summaryrefslogtreecommitdiffstats
path: root/src/data_collection/processes/unix/user_table.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/data_collection/processes/unix/user_table.rs')
-rw-r--r--src/data_collection/processes/unix/user_table.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/data_collection/processes/unix/user_table.rs b/src/data_collection/processes/unix/user_table.rs
new file mode 100644
index 00000000..6245a858
--- /dev/null
+++ b/src/data_collection/processes/unix/user_table.rs
@@ -0,0 +1,31 @@
+use hashbrown::HashMap;
+
+use crate::utils::error;
+
+#[derive(Debug, Default)]
+pub struct UserTable {
+ pub uid_user_mapping: HashMap<libc::uid_t, String>,
+}
+
+impl UserTable {
+ pub fn get_uid_to_username_mapping(&mut self, uid: libc::uid_t) -> error::Result<String> {
+ if let Some(user) = self.uid_user_mapping.get(&uid) {
+ Ok(user.clone())
+ } else {
+ // SAFETY: getpwuid returns a null pointer if no passwd entry is found for the uid
+ let passwd = unsafe { libc::getpwuid(uid) };
+
+ if passwd.is_null() {
+ Err(error::BottomError::QueryError("Missing passwd".into()))
+ } else {
+ // SAFETY: We return early if passwd is null.
+ let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) }
+ .to_str()?
+ .to_string();
+ self.uid_user_mapping.insert(uid, username.clone());
+
+ Ok(username)
+ }
+ }
+ }
+}