sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use super::*;

use super::parse::{parse_ascii_checked_pid_t, parse_ascii_checked_uid_t};

pub(super) fn refresh_user_group_ids(
    p: &mut ProcessInner,
    ids: Option<Ids>,
    refresh_kind: ProcessRefreshKind,
) {
    if !refresh_kind.user().needs_update(|| p.user_id.is_none()) {
        return;
    }

    if let Some(Ids {
        user_id,
        effective_user_id,
        group_id,
        effective_group_id,
    }) = ids
    {
        p.user_id = Some(Uid(user_id));
        p.effective_user_id = Some(Uid(effective_user_id));
        p.group_id = Some(Gid(group_id));
        p.effective_group_id = Some(Gid(effective_group_id));
    }
}

pub(super) struct Ids {
    pub(super) user_id: uid_t,
    pub(super) effective_user_id: uid_t,
    pub(super) group_id: gid_t,
    pub(super) effective_group_id: gid_t,
}

// Fetches the real and effective UID and GID.
pub(super) fn get_uid_and_gid_and_tgid(
    proc_path: &Path,
    buffers: &mut ProcessBuffers,
) -> Option<(Ids, Pid)> {
    /// Writes `{base}/{file_name}` into `buf` and returns a reference to it.
    fn build_path<'a>(buf: &'a mut PathBuf, base: &Path, file_name: &str) -> &'a Path {
        buf.as_mut_os_string().clear();
        buf.push(base);
        buf.push(file_name);
        buf
    }

    let status_path = build_path(&mut buffers.path, proc_path, "status");
    read_path_into(status_path, &mut buffers.buf).ok()?;
    let status_data = &buffers.buf;

    // Only lines starting with Uid: and Gid: are relevant.

    let f = |h: &[u8], n: &[u8]| -> (Option<uid_t>, Option<uid_t>) {
        if let Some(h) = h.strip_prefix(n) {
            let mut ids = h
                .split(|c| *c == b'\t' || *c == b' ')
                .filter(|s| !s.is_empty());
            let real = ids.next().and_then(parse_ascii_checked_uid_t);
            let effective = ids.next().and_then(parse_ascii_checked_uid_t);

            (real, effective)
        } else {
            (None, None)
        }
    };
    let mut uid = None;
    let mut effective_uid = None;
    let mut gid = None;
    let mut effective_gid = None;
    let mut tgid = None;
    for line in status_data.split(|c| *c == b'\n') {
        if let (Some(real), Some(effective)) = f(line, b"Uid:") {
            debug_assert!(uid.is_none() && effective_uid.is_none());
            uid = Some(real);
            effective_uid = Some(effective);
        } else if let (Some(real), Some(effective)) = f(line, b"Gid:") {
            debug_assert!(gid.is_none() && effective_gid.is_none());
            gid = Some(real);
            effective_gid = Some(effective);
        } else if let Some(line) = line.strip_prefix(b"Tgid:")
            && let Some(real) = parse_ascii_checked_pid_t(line.trim_ascii())
        {
            debug_assert!(tgid.is_none());
            tgid = Some(real);
        } else {
            continue;
        }
        if uid.is_some() && gid.is_some() && tgid.is_some() {
            break;
        }
    }
    match (uid, effective_uid, gid, effective_gid, tgid) {
        (
            Some(user_id),
            Some(effective_user_id),
            Some(group_id),
            Some(effective_group_id),
            Some(thread_group_id),
        ) => Some((
            Ids {
                user_id,
                effective_user_id,
                group_id,
                effective_group_id,
            },
            Pid(thread_group_id),
        )),
        _ => None,
    }
}