sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::process::ExitStatus;

use crate::Pid;

use super::ProcessInner;

impl ProcessInner {
    pub(crate) fn wait(&self) -> Option<ExitStatus> {
        crate::platform::unix::utils::wait_process(self.pid)
    }

    pub(crate) fn session_id(&self) -> Option<Pid> {
        unsafe {
            let session_id = libc::getsid(self.pid.0);
            if session_id < 0 {
                None
            } else {
                Some(Pid(session_id))
            }
        }
    }

    pub(crate) fn switch_updated(&mut self) -> bool {
        std::mem::replace(&mut self.updated, false)
    }

    pub(crate) fn set_nonexistent(&mut self) {
        self.exists = false;
    }

    pub(crate) fn exists(&self) -> bool {
        self.exists
    }

    pub(crate) fn open_files(&self) -> Option<usize> {
        let buffer_size_bytes = unsafe {
            libc::proc_pidinfo(
                self.pid.0,
                libc::PROC_PIDLISTFDS,
                0,
                std::ptr::null_mut(),
                0,
            )
        };

        if buffer_size_bytes < 0 {
            sinf_debug!("proc_pidinfo failed");
            return None;
        }

        let mut buffer = vec![0u8; buffer_size_bytes as usize];

        let buffer_filled_bytes = unsafe {
            libc::proc_pidinfo(
                self.pid.0,
                libc::PROC_PIDLISTFDS,
                0,
                buffer.as_mut_ptr() as *mut std::ffi::c_void,
                buffer_size_bytes,
            )
        };

        if buffer_filled_bytes < 0 {
            sinf_debug!("proc_pidinfo failed");
            return None;
        }

        Some(buffer_filled_bytes as usize / std::mem::size_of::<libc::proc_fdinfo>())
    }
}