sysinfo/unix/linux/
process.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::cell::UnsafeCell;
4use std::collections::{HashMap, HashSet};
5use std::ffi::{OsStr, OsString};
6use std::fmt;
7use std::fs::{self, DirEntry, File};
8use std::io::Read;
9use std::os::unix::ffi::OsStrExt;
10use std::path::{Path, PathBuf};
11use std::process::ExitStatus;
12use std::str::{self, FromStr};
13use std::sync::atomic::{AtomicUsize, Ordering};
14
15use libc::{c_ulong, gid_t, uid_t};
16
17use crate::sys::system::SystemInfo;
18use crate::sys::utils::{
19    get_all_data_from_file, get_all_utf8_data, realpath, PathHandler, PathPush,
20};
21use crate::{
22    DiskUsage, Gid, Pid, Process, ProcessRefreshKind, ProcessStatus, ProcessesToUpdate, Signal,
23    ThreadKind, Uid,
24};
25
26use crate::sys::system::remaining_files;
27
28#[doc(hidden)]
29impl From<char> for ProcessStatus {
30    fn from(status: char) -> ProcessStatus {
31        match status {
32            'R' => ProcessStatus::Run,
33            'S' => ProcessStatus::Sleep,
34            'I' => ProcessStatus::Idle,
35            'D' => ProcessStatus::UninterruptibleDiskSleep,
36            'Z' => ProcessStatus::Zombie,
37            'T' => ProcessStatus::Stop,
38            't' => ProcessStatus::Tracing,
39            'X' | 'x' => ProcessStatus::Dead,
40            'K' => ProcessStatus::Wakekill,
41            'W' => ProcessStatus::Waking,
42            'P' => ProcessStatus::Parked,
43            x => ProcessStatus::Unknown(x as u32),
44        }
45    }
46}
47
48impl fmt::Display for ProcessStatus {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        f.write_str(match *self {
51            ProcessStatus::Idle => "Idle",
52            ProcessStatus::Run => "Runnable",
53            ProcessStatus::Sleep => "Sleeping",
54            ProcessStatus::Stop => "Stopped",
55            ProcessStatus::Zombie => "Zombie",
56            ProcessStatus::Tracing => "Tracing",
57            ProcessStatus::Dead => "Dead",
58            ProcessStatus::Wakekill => "Wakekill",
59            ProcessStatus::Waking => "Waking",
60            ProcessStatus::Parked => "Parked",
61            ProcessStatus::UninterruptibleDiskSleep => "UninterruptibleDiskSleep",
62            _ => "Unknown",
63        })
64    }
65}
66
67#[allow(dead_code)]
68#[repr(usize)]
69enum ProcIndex {
70    Pid = 0,
71    State,
72    ParentPid,
73    GroupId,
74    SessionId,
75    Tty,
76    ForegroundProcessGroupId,
77    Flags,
78    MinorFaults,
79    ChildrenMinorFaults,
80    MajorFaults,
81    ChildrenMajorFaults,
82    UserTime,
83    SystemTime,
84    ChildrenUserTime,
85    ChildrenKernelTime,
86    Priority,
87    Nice,
88    NumberOfThreads,
89    IntervalTimerSigalarm,
90    StartTime,
91    VirtualSize,
92    ResidentSetSize,
93    // More exist but we only use the listed ones. For more, take a look at `man proc`.
94}
95
96pub(crate) struct ProcessInner {
97    pub(crate) name: OsString,
98    pub(crate) cmd: Vec<OsString>,
99    pub(crate) exe: Option<PathBuf>,
100    pub(crate) pid: Pid,
101    parent: Option<Pid>,
102    pub(crate) environ: Vec<OsString>,
103    pub(crate) cwd: Option<PathBuf>,
104    pub(crate) root: Option<PathBuf>,
105    pub(crate) memory: u64,
106    pub(crate) virtual_memory: u64,
107    utime: u64,
108    stime: u64,
109    old_utime: u64,
110    old_stime: u64,
111    start_time_without_boot_time: u64,
112    start_time: u64,
113    start_time_raw: u64,
114    run_time: u64,
115    pub(crate) updated: bool,
116    cpu_usage: f32,
117    user_id: Option<Uid>,
118    effective_user_id: Option<Uid>,
119    group_id: Option<Gid>,
120    effective_group_id: Option<Gid>,
121    pub(crate) status: ProcessStatus,
122    pub(crate) tasks: Option<HashSet<Pid>>,
123    stat_file: Option<FileCounter>,
124    old_read_bytes: u64,
125    old_written_bytes: u64,
126    read_bytes: u64,
127    written_bytes: u64,
128    thread_kind: Option<ThreadKind>,
129    proc_path: PathBuf,
130    accumulated_cpu_time: u64,
131    exists: bool,
132}
133
134impl ProcessInner {
135    pub(crate) fn new(pid: Pid, proc_path: PathBuf) -> Self {
136        Self {
137            name: OsString::new(),
138            pid,
139            parent: None,
140            cmd: Vec::new(),
141            environ: Vec::new(),
142            exe: None,
143            cwd: None,
144            root: None,
145            memory: 0,
146            virtual_memory: 0,
147            cpu_usage: 0.,
148            utime: 0,
149            stime: 0,
150            old_utime: 0,
151            old_stime: 0,
152            updated: true,
153            start_time_without_boot_time: 0,
154            start_time: 0,
155            start_time_raw: 0,
156            run_time: 0,
157            user_id: None,
158            effective_user_id: None,
159            group_id: None,
160            effective_group_id: None,
161            status: ProcessStatus::Unknown(0),
162            tasks: None,
163            stat_file: None,
164            old_read_bytes: 0,
165            old_written_bytes: 0,
166            read_bytes: 0,
167            written_bytes: 0,
168            thread_kind: None,
169            proc_path,
170            accumulated_cpu_time: 0,
171            exists: true,
172        }
173    }
174
175    pub(crate) fn kill_with(&self, signal: Signal) -> Option<bool> {
176        let c_signal = crate::sys::system::convert_signal(signal)?;
177        unsafe { Some(libc::kill(self.pid.0, c_signal) == 0) }
178    }
179
180    pub(crate) fn name(&self) -> &OsStr {
181        &self.name
182    }
183
184    pub(crate) fn cmd(&self) -> &[OsString] {
185        &self.cmd
186    }
187
188    pub(crate) fn exe(&self) -> Option<&Path> {
189        self.exe.as_deref()
190    }
191
192    pub(crate) fn pid(&self) -> Pid {
193        self.pid
194    }
195
196    pub(crate) fn environ(&self) -> &[OsString] {
197        &self.environ
198    }
199
200    pub(crate) fn cwd(&self) -> Option<&Path> {
201        self.cwd.as_deref()
202    }
203
204    pub(crate) fn root(&self) -> Option<&Path> {
205        self.root.as_deref()
206    }
207
208    pub(crate) fn memory(&self) -> u64 {
209        self.memory
210    }
211
212    pub(crate) fn virtual_memory(&self) -> u64 {
213        self.virtual_memory
214    }
215
216    pub(crate) fn parent(&self) -> Option<Pid> {
217        self.parent
218    }
219
220    pub(crate) fn status(&self) -> ProcessStatus {
221        self.status
222    }
223
224    pub(crate) fn start_time(&self) -> u64 {
225        self.start_time
226    }
227
228    pub(crate) fn run_time(&self) -> u64 {
229        self.run_time
230    }
231
232    pub(crate) fn cpu_usage(&self) -> f32 {
233        self.cpu_usage
234    }
235
236    pub(crate) fn accumulated_cpu_time(&self) -> u64 {
237        self.accumulated_cpu_time
238    }
239
240    pub(crate) fn disk_usage(&self) -> DiskUsage {
241        DiskUsage {
242            written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
243            total_written_bytes: self.written_bytes,
244            read_bytes: self.read_bytes.saturating_sub(self.old_read_bytes),
245            total_read_bytes: self.read_bytes,
246        }
247    }
248
249    pub(crate) fn user_id(&self) -> Option<&Uid> {
250        self.user_id.as_ref()
251    }
252
253    pub(crate) fn effective_user_id(&self) -> Option<&Uid> {
254        self.effective_user_id.as_ref()
255    }
256
257    pub(crate) fn group_id(&self) -> Option<Gid> {
258        self.group_id
259    }
260
261    pub(crate) fn effective_group_id(&self) -> Option<Gid> {
262        self.effective_group_id
263    }
264
265    pub(crate) fn wait(&self) -> Option<ExitStatus> {
266        // If anything fails when trying to retrieve the start time, better to return `None`.
267        let (data, _) = _get_stat_data_and_file(&self.proc_path).ok()?;
268        let parts = parse_stat_file(&data)?;
269
270        if start_time_raw(&parts) != self.start_time_raw {
271            sysinfo_debug!("Seems to not be the same process anymore");
272            return None;
273        }
274
275        crate::unix::utils::wait_process(self.pid)
276    }
277
278    pub(crate) fn session_id(&self) -> Option<Pid> {
279        unsafe {
280            let session_id = libc::getsid(self.pid.0);
281            if session_id < 0 {
282                None
283            } else {
284                Some(Pid(session_id))
285            }
286        }
287    }
288
289    pub(crate) fn thread_kind(&self) -> Option<ThreadKind> {
290        self.thread_kind
291    }
292
293    pub(crate) fn switch_updated(&mut self) -> bool {
294        std::mem::replace(&mut self.updated, false)
295    }
296
297    pub(crate) fn set_nonexistent(&mut self) {
298        self.exists = false;
299    }
300
301    pub(crate) fn exists(&self) -> bool {
302        self.exists
303    }
304
305    pub(crate) fn open_files(&self) -> Option<usize> {
306        let open_files_dir = self.proc_path.as_path().join("fd");
307        match fs::read_dir(&open_files_dir) {
308            Ok(entries) => Some(entries.count() as _),
309            Err(_error) => {
310                sysinfo_debug!(
311                    "Failed to get open files in `{}`: {_error:?}",
312                    open_files_dir.display(),
313                );
314                None
315            }
316        }
317    }
318
319    pub(crate) fn open_files_limit(&self) -> Option<usize> {
320        let limits_files = self.proc_path.as_path().join("limits");
321        match fs::read_to_string(&limits_files) {
322            Ok(content) => {
323                for line in content.lines() {
324                    if let Some(line) = line.strip_prefix("Max open files ") {
325                        if let Some(nb) = line.split_whitespace().find(|p| !p.is_empty()) {
326                            return usize::from_str(nb).ok();
327                        }
328                    }
329                }
330                None
331            }
332            Err(_error) => {
333                sysinfo_debug!(
334                    "Failed to get limits in `{}`: {_error:?}",
335                    limits_files.display()
336                );
337                None
338            }
339        }
340    }
341}
342
343pub(crate) fn compute_cpu_usage(p: &mut ProcessInner, total_time: f32, max_value: f32) {
344    // First time updating the values without reference, wait for a second cycle to update cpu_usage
345    if p.old_utime == 0 && p.old_stime == 0 {
346        return;
347    }
348
349    // We use `max_value` to ensure that the process CPU usage will never get bigger than:
350    // `"number of CPUs" * 100.`
351    p.cpu_usage = (p
352        .utime
353        .saturating_sub(p.old_utime)
354        .saturating_add(p.stime.saturating_sub(p.old_stime)) as f32
355        / total_time
356        * 100.)
357        .min(max_value);
358}
359
360pub(crate) fn set_time(p: &mut ProcessInner, utime: u64, stime: u64) {
361    p.old_utime = p.utime;
362    p.old_stime = p.stime;
363    p.utime = utime;
364    p.stime = stime;
365}
366
367pub(crate) fn update_process_disk_activity(p: &mut ProcessInner, path: &mut PathHandler) {
368    let data = match get_all_utf8_data(path.join("io"), 16_384) {
369        Ok(d) => d,
370        Err(_) => return,
371    };
372    let mut done = 0;
373    for line in data.split('\n') {
374        let mut parts = line.split(": ");
375        match parts.next() {
376            Some("read_bytes") => {
377                p.old_read_bytes = p.read_bytes;
378                p.read_bytes = parts
379                    .next()
380                    .and_then(|x| x.parse::<u64>().ok())
381                    .unwrap_or(p.old_read_bytes);
382            }
383            Some("write_bytes") => {
384                p.old_written_bytes = p.written_bytes;
385                p.written_bytes = parts
386                    .next()
387                    .and_then(|x| x.parse::<u64>().ok())
388                    .unwrap_or(p.old_written_bytes);
389            }
390            _ => continue,
391        }
392        done += 1;
393        if done > 1 {
394            // No need to continue the reading.
395            break;
396        }
397    }
398}
399
400struct Wrap<'a, T>(UnsafeCell<&'a mut T>);
401
402impl<'a, T> Wrap<'a, T> {
403    fn get(&self) -> &'a mut T {
404        unsafe { *(self.0.get()) }
405    }
406}
407
408#[allow(clippy::non_send_fields_in_send_ty)]
409unsafe impl<T> Send for Wrap<'_, T> {}
410unsafe impl<T> Sync for Wrap<'_, T> {}
411
412#[inline(always)]
413fn start_time_raw(parts: &Parts<'_>) -> u64 {
414    u64::from_str(parts.str_parts[ProcIndex::StartTime as usize]).unwrap_or(0)
415}
416
417#[inline(always)]
418fn compute_start_time_without_boot_time(parts: &Parts<'_>, info: &SystemInfo) -> (u64, u64) {
419    let raw = start_time_raw(parts);
420    // To be noted that the start time is invalid here, it still needs to be converted into
421    // "real" time.
422    (raw, raw / info.clock_cycle)
423}
424
425fn _get_stat_data_and_file(path: &Path) -> Result<(Vec<u8>, File), ()> {
426    let mut file = File::open(path.join("stat")).map_err(|_| ())?;
427    let data = get_all_data_from_file(&mut file, 1024).map_err(|_| ())?;
428    Ok((data, file))
429}
430
431fn _get_stat_data(path: &Path, stat_file: &mut Option<FileCounter>) -> Result<Vec<u8>, ()> {
432    let (data, file) = _get_stat_data_and_file(path)?;
433    *stat_file = FileCounter::new(file);
434    Ok(data)
435}
436
437#[inline(always)]
438fn get_status(p: &mut ProcessInner, part: &str) {
439    p.status = part
440        .chars()
441        .next()
442        .map(ProcessStatus::from)
443        .unwrap_or_else(|| ProcessStatus::Unknown(0));
444}
445
446fn refresh_user_group_ids(
447    p: &mut ProcessInner,
448    path: &mut PathHandler,
449    refresh_kind: ProcessRefreshKind,
450) {
451    if !refresh_kind.user().needs_update(|| p.user_id.is_none()) {
452        return;
453    }
454
455    if let Some(((user_id, effective_user_id), (group_id, effective_group_id))) =
456        get_uid_and_gid(path.join("status"))
457    {
458        p.user_id = Some(Uid(user_id));
459        p.effective_user_id = Some(Uid(effective_user_id));
460        p.group_id = Some(Gid(group_id));
461        p.effective_group_id = Some(Gid(effective_group_id));
462    }
463}
464
465#[allow(clippy::too_many_arguments)]
466fn update_proc_info(
467    p: &mut ProcessInner,
468    parent_pid: Option<Pid>,
469    refresh_kind: ProcessRefreshKind,
470    proc_path: &mut PathHandler,
471    str_parts: &[&str],
472    uptime: u64,
473    info: &SystemInfo,
474) {
475    update_parent_pid(p, parent_pid, str_parts);
476
477    get_status(p, str_parts[ProcIndex::State as usize]);
478    refresh_user_group_ids(p, proc_path, refresh_kind);
479
480    if refresh_kind.exe().needs_update(|| p.exe.is_none()) {
481        // Do not use cmd[0] because it is not the same thing.
482        // See https://github.com/GuillaumeGomez/sysinfo/issues/697.
483        p.exe = realpath(proc_path.join("exe"));
484    }
485
486    if refresh_kind.cmd().needs_update(|| p.cmd.is_empty()) {
487        p.cmd = copy_from_file(proc_path.join("cmdline"));
488    }
489    if refresh_kind.environ().needs_update(|| p.environ.is_empty()) {
490        p.environ = copy_from_file(proc_path.join("environ"));
491    }
492    if refresh_kind.cwd().needs_update(|| p.cwd.is_none()) {
493        p.cwd = realpath(proc_path.join("cwd"));
494    }
495    if refresh_kind.root().needs_update(|| p.root.is_none()) {
496        p.root = realpath(proc_path.join("root"));
497    }
498
499    update_time_and_memory(proc_path, p, str_parts, uptime, info, refresh_kind);
500    if refresh_kind.disk_usage() {
501        update_process_disk_activity(p, proc_path);
502    }
503    // Needs to be after `update_time_and_memory`.
504    if refresh_kind.cpu() {
505        // The external values for CPU times are in "ticks", which are
506        // scaled by "HZ", which is pegged externally at 100 ticks/second.
507        p.accumulated_cpu_time =
508            p.utime.saturating_add(p.stime).saturating_mul(1_000) / info.clock_cycle;
509    }
510    p.updated = true;
511}
512
513fn update_parent_pid(p: &mut ProcessInner, parent_pid: Option<Pid>, str_parts: &[&str]) {
514    p.parent = match parent_pid {
515        Some(parent_pid) if parent_pid.0 != 0 => Some(parent_pid),
516        _ => match Pid::from_str(str_parts[ProcIndex::ParentPid as usize]) {
517            Ok(p) if p.0 != 0 => Some(p),
518            _ => None,
519        },
520    };
521}
522
523fn retrieve_all_new_process_info(
524    pid: Pid,
525    parent_pid: Option<Pid>,
526    parts: &Parts<'_>,
527    path: &Path,
528    info: &SystemInfo,
529    refresh_kind: ProcessRefreshKind,
530    uptime: u64,
531) -> Process {
532    let mut p = ProcessInner::new(pid, path.to_owned());
533    let mut proc_path = PathHandler::new(path);
534    let name = parts.short_exe;
535
536    let (start_time_raw, start_time_without_boot_time) =
537        compute_start_time_without_boot_time(parts, info);
538    p.start_time_raw = start_time_raw;
539    p.start_time_without_boot_time = start_time_without_boot_time;
540    p.start_time = p
541        .start_time_without_boot_time
542        .saturating_add(info.boot_time);
543
544    p.name = OsStr::from_bytes(name).to_os_string();
545    if c_ulong::from_str(parts.str_parts[ProcIndex::Flags as usize])
546        .map(|flags| flags & libc::PF_KTHREAD as c_ulong != 0)
547        .unwrap_or(false)
548    {
549        p.thread_kind = Some(ThreadKind::Kernel);
550    } else if parent_pid.is_some() {
551        p.thread_kind = Some(ThreadKind::Userland);
552    }
553
554    update_proc_info(
555        &mut p,
556        parent_pid,
557        refresh_kind,
558        &mut proc_path,
559        &parts.str_parts,
560        uptime,
561        info,
562    );
563
564    Process { inner: p }
565}
566
567fn update_existing_process(
568    proc: &mut Process,
569    parent_pid: Option<Pid>,
570    uptime: u64,
571    info: &SystemInfo,
572    refresh_kind: ProcessRefreshKind,
573    tasks: Option<HashSet<Pid>>,
574) -> Result<Option<Process>, ()> {
575    let entry = &mut proc.inner;
576    let data = if let Some(mut f) = entry.stat_file.take() {
577        match get_all_data_from_file(&mut f, 1024) {
578            Ok(data) => {
579                // Everything went fine, we put back the file descriptor.
580                entry.stat_file = Some(f);
581                data
582            }
583            Err(_) => {
584                // It's possible that the file descriptor is no longer valid in case the
585                // original process was terminated and another one took its place.
586                _get_stat_data(&entry.proc_path, &mut entry.stat_file)?
587            }
588        }
589    } else {
590        _get_stat_data(&entry.proc_path, &mut entry.stat_file)?
591    };
592    entry.tasks = tasks;
593
594    let parts = parse_stat_file(&data).ok_or(())?;
595    let start_time_raw = start_time_raw(&parts);
596
597    // It's possible that a new process took this same PID when the "original one" terminated.
598    // If the start time differs, then it means it's not the same process anymore and that we
599    // need to get all its information, hence why we check it here.
600    if start_time_raw == entry.start_time_raw {
601        let mut proc_path = PathHandler::new(&entry.proc_path);
602
603        update_proc_info(
604            entry,
605            parent_pid,
606            refresh_kind,
607            &mut proc_path,
608            &parts.str_parts,
609            uptime,
610            info,
611        );
612
613        refresh_user_group_ids(entry, &mut proc_path, refresh_kind);
614        return Ok(None);
615    }
616    // If we're here, it means that the PID still exists but it's a different process.
617    let p = retrieve_all_new_process_info(
618        entry.pid,
619        parent_pid,
620        &parts,
621        &entry.proc_path,
622        info,
623        refresh_kind,
624        uptime,
625    );
626    *proc = p;
627    // Since this PID is already in the HashMap, no need to add it again.
628    Ok(None)
629}
630
631#[allow(clippy::too_many_arguments)]
632pub(crate) fn _get_process_data(
633    path: &Path,
634    proc_list: &mut HashMap<Pid, Process>,
635    pid: Pid,
636    parent_pid: Option<Pid>,
637    uptime: u64,
638    info: &SystemInfo,
639    refresh_kind: ProcessRefreshKind,
640    tasks: Option<HashSet<Pid>>,
641) -> Result<Option<Process>, ()> {
642    if let Some(ref mut entry) = proc_list.get_mut(&pid) {
643        return update_existing_process(entry, parent_pid, uptime, info, refresh_kind, tasks);
644    }
645    let mut stat_file = None;
646    let data = _get_stat_data(path, &mut stat_file)?;
647    let parts = parse_stat_file(&data).ok_or(())?;
648
649    let mut new_process =
650        retrieve_all_new_process_info(pid, parent_pid, &parts, path, info, refresh_kind, uptime);
651    new_process.inner.stat_file = stat_file;
652    new_process.inner.tasks = tasks;
653    Ok(Some(new_process))
654}
655
656fn old_get_memory(entry: &mut ProcessInner, str_parts: &[&str], info: &SystemInfo) {
657    // rss
658    entry.memory = u64::from_str(str_parts[ProcIndex::ResidentSetSize as usize])
659        .unwrap_or(0)
660        .saturating_mul(info.page_size_b);
661    // vsz correspond to the Virtual memory size in bytes.
662    // see: https://man7.org/linux/man-pages/man5/proc.5.html
663    entry.virtual_memory = u64::from_str(str_parts[ProcIndex::VirtualSize as usize]).unwrap_or(0);
664}
665
666fn slice_to_nb(s: &[u8]) -> u64 {
667    let mut nb: u64 = 0;
668
669    for c in s {
670        nb = nb * 10 + (c - b'0') as u64;
671    }
672    nb
673}
674
675fn get_memory(path: &Path, entry: &mut ProcessInner, info: &SystemInfo) -> bool {
676    let mut file = match File::open(path) {
677        Ok(f) => f,
678        Err(_e) => {
679            sysinfo_debug!(
680                "Using old memory information (failed to open {:?}: {_e:?})",
681                path
682            );
683            return false;
684        }
685    };
686    let mut buf = Vec::new();
687    if let Err(_e) = file.read_to_end(&mut buf) {
688        sysinfo_debug!(
689            "Using old memory information (failed to read {:?}: {_e:?})",
690            path
691        );
692        return false;
693    }
694    let mut parts = buf.split(|c| *c == b' ');
695    entry.virtual_memory = parts
696        .next()
697        .map(slice_to_nb)
698        .unwrap_or(0)
699        .saturating_mul(info.page_size_b);
700    entry.memory = parts
701        .next()
702        .map(slice_to_nb)
703        .unwrap_or(0)
704        .saturating_mul(info.page_size_b);
705    true
706}
707
708#[allow(clippy::too_many_arguments)]
709fn update_time_and_memory(
710    path: &mut PathHandler,
711    entry: &mut ProcessInner,
712    str_parts: &[&str],
713    uptime: u64,
714    info: &SystemInfo,
715    refresh_kind: ProcessRefreshKind,
716) {
717    {
718        #[allow(clippy::collapsible_if)]
719        if refresh_kind.memory() {
720            // Keeping this nested level for readability reasons.
721            if !get_memory(path.join("statm"), entry, info) {
722                old_get_memory(entry, str_parts, info);
723            }
724        }
725        set_time(
726            entry,
727            u64::from_str(str_parts[ProcIndex::UserTime as usize]).unwrap_or(0),
728            u64::from_str(str_parts[ProcIndex::SystemTime as usize]).unwrap_or(0),
729        );
730        entry.run_time = uptime.saturating_sub(entry.start_time_without_boot_time);
731    }
732}
733
734struct ProcAndTasks {
735    pid: Pid,
736    parent_pid: Option<Pid>,
737    path: PathBuf,
738    tasks: Option<HashSet<Pid>>,
739}
740
741fn get_all_pid_entries(
742    parent: Option<&OsStr>,
743    parent_pid: Option<Pid>,
744    entry: DirEntry,
745    data: &mut Vec<ProcAndTasks>,
746    enable_task_stats: bool,
747) -> Option<Pid> {
748    let Ok(file_type) = entry.file_type() else {
749        return None;
750    };
751    if !file_type.is_dir() {
752        return None;
753    }
754
755    let entry = entry.path();
756    let name = entry.file_name();
757
758    if name == parent {
759        // Needed because tasks have their own PID listed in the "task" folder.
760        return None;
761    }
762    let name = name?;
763    let pid = Pid::from(usize::from_str(name.to_str()?).ok()?);
764
765    let tasks = if enable_task_stats {
766        let tasks_dir = Path::join(&entry, "task");
767        if let Ok(entries) = fs::read_dir(tasks_dir) {
768            let mut tasks = HashSet::new();
769            for task in entries.into_iter().filter_map(|entry| {
770                get_all_pid_entries(Some(name), Some(pid), entry.ok()?, data, enable_task_stats)
771            }) {
772                tasks.insert(task);
773            }
774            Some(tasks)
775        } else {
776            None
777        }
778    } else {
779        None
780    };
781
782    data.push(ProcAndTasks {
783        pid,
784        parent_pid,
785        path: entry,
786        tasks,
787    });
788    Some(pid)
789}
790
791#[cfg(feature = "multithread")]
792#[inline]
793pub(crate) fn iter<T>(val: T) -> rayon::iter::IterBridge<T>
794where
795    T: rayon::iter::ParallelBridge,
796{
797    val.par_bridge()
798}
799
800#[cfg(not(feature = "multithread"))]
801#[inline]
802pub(crate) fn iter<T>(val: T) -> T
803where
804    T: Iterator,
805{
806    val
807}
808
809/// We're forced to read the whole `/proc` folder because if a process died and another took its
810/// place, we need to get the task parent (if it's a task).
811pub(crate) fn refresh_procs(
812    proc_list: &mut HashMap<Pid, Process>,
813    path: &Path,
814    uptime: u64,
815    info: &SystemInfo,
816    processes_to_update: ProcessesToUpdate<'_>,
817    refresh_kind: ProcessRefreshKind,
818) -> usize {
819    #[cfg(feature = "multithread")]
820    use rayon::iter::ParallelIterator;
821
822    #[inline(always)]
823    fn real_filter(e: &ProcAndTasks, filter: &[Pid]) -> bool {
824        filter.contains(&e.pid)
825    }
826
827    #[inline(always)]
828    fn empty_filter(_e: &ProcAndTasks, _filter: &[Pid]) -> bool {
829        true
830    }
831
832    #[allow(clippy::type_complexity)]
833    let (filter, filter_callback): (
834        &[Pid],
835        &(dyn Fn(&ProcAndTasks, &[Pid]) -> bool + Sync + Send),
836    ) = match processes_to_update {
837        ProcessesToUpdate::All => (&[], &empty_filter),
838        ProcessesToUpdate::Some(pids) => {
839            if pids.is_empty() {
840                return 0;
841            }
842            (pids, &real_filter)
843        }
844    };
845
846    let nb_updated = AtomicUsize::new(0);
847
848    // This code goes through processes (listed in `/proc`) and through tasks (listed in
849    // `/proc/[PID]/task`). However, the stored tasks information is supposed to be already present
850    // in the PIDs listed from `/proc` so there will be no duplicates between PIDs and tasks PID.
851    //
852    // If a task is not listed in `/proc`, then we don't retrieve its information.
853    //
854    // So in short: since we update the `HashMap` itself by adding/removing entries outside of the
855    // parallel iterator, we can safely use it inside the parallel iterator and update its entries
856    // concurrently.
857    let procs = {
858        let d = match fs::read_dir(path) {
859            Ok(d) => d,
860            Err(_err) => {
861                sysinfo_debug!("Failed to read folder {path:?}: {_err:?}");
862                return 0;
863            }
864        };
865        let proc_list = Wrap(UnsafeCell::new(proc_list));
866
867        iter(d)
868            .flat_map(|entry| {
869                let Ok(entry) = entry else { return Vec::new() };
870                let mut entries = Vec::new();
871                get_all_pid_entries(None, None, entry, &mut entries, refresh_kind.tasks());
872                entries
873            })
874            .filter(|e| filter_callback(e, filter))
875            .filter_map(|e| {
876                let proc_list = proc_list.get();
877                let new_process = _get_process_data(
878                    e.path.as_path(),
879                    proc_list,
880                    e.pid,
881                    e.parent_pid,
882                    uptime,
883                    info,
884                    refresh_kind,
885                    e.tasks,
886                )
887                .ok()?;
888                nb_updated.fetch_add(1, Ordering::Relaxed);
889                new_process
890            })
891            .collect::<Vec<_>>()
892    };
893    for proc_ in procs {
894        proc_list.insert(proc_.pid(), proc_);
895    }
896    nb_updated.into_inner()
897}
898
899// FIXME: To be removed once MSRV for this crate is 1.80 and use the `trim_ascii()` method instead.
900fn trim_ascii(mut bytes: &[u8]) -> &[u8] {
901    // Code from Rust code library.
902    while let [rest @ .., last] = bytes {
903        if last.is_ascii_whitespace() {
904            bytes = rest;
905        } else {
906            break;
907        }
908    }
909    while let [first, rest @ ..] = bytes {
910        if first.is_ascii_whitespace() {
911            bytes = rest;
912        } else {
913            break;
914        }
915    }
916    bytes
917}
918
919fn split_content(mut data: &[u8]) -> Vec<OsString> {
920    let mut out = Vec::with_capacity(10);
921    while let Some(pos) = data.iter().position(|c| *c == 0) {
922        let s = trim_ascii(&data[..pos]);
923        if !s.is_empty() {
924            out.push(OsStr::from_bytes(s).to_os_string());
925        }
926        data = &data[pos + 1..];
927    }
928    if !data.is_empty() {
929        let s = trim_ascii(data);
930        if !s.is_empty() {
931            out.push(OsStr::from_bytes(s).to_os_string());
932        }
933    }
934    out
935}
936
937fn copy_from_file(entry: &Path) -> Vec<OsString> {
938    match File::open(entry) {
939        Ok(mut f) => {
940            let mut data = Vec::with_capacity(16_384);
941
942            if let Err(_e) = f.read_to_end(&mut data) {
943                sysinfo_debug!("Failed to read file in `copy_from_file`: {:?}", _e);
944                Vec::new()
945            } else {
946                split_content(&data)
947            }
948        }
949        Err(_e) => {
950            sysinfo_debug!("Failed to open file in `copy_from_file`: {:?}", _e);
951            Vec::new()
952        }
953    }
954}
955
956// Fetch tuples of real and effective UID and GID.
957fn get_uid_and_gid(file_path: &Path) -> Option<((uid_t, uid_t), (gid_t, gid_t))> {
958    let status_data = get_all_utf8_data(file_path, 16_385).ok()?;
959
960    // We're only interested in the lines starting with Uid: and Gid:
961    // here. From these lines, we're looking at the first and second entries to get
962    // the real u/gid.
963
964    let f = |h: &str, n: &str| -> (Option<uid_t>, Option<uid_t>) {
965        if h.starts_with(n) {
966            let mut ids = h.split_whitespace();
967            let real = ids.nth(1).unwrap_or("0").parse().ok();
968            let effective = ids.next().unwrap_or("0").parse().ok();
969
970            (real, effective)
971        } else {
972            (None, None)
973        }
974    };
975    let mut uid = None;
976    let mut effective_uid = None;
977    let mut gid = None;
978    let mut effective_gid = None;
979    for line in status_data.lines() {
980        if let (Some(real), Some(effective)) = f(line, "Uid:") {
981            debug_assert!(uid.is_none() && effective_uid.is_none());
982            uid = Some(real);
983            effective_uid = Some(effective);
984        } else if let (Some(real), Some(effective)) = f(line, "Gid:") {
985            debug_assert!(gid.is_none() && effective_gid.is_none());
986            gid = Some(real);
987            effective_gid = Some(effective);
988        } else {
989            continue;
990        }
991        if uid.is_some() && gid.is_some() {
992            break;
993        }
994    }
995    match (uid, effective_uid, gid, effective_gid) {
996        (Some(uid), Some(effective_uid), Some(gid), Some(effective_gid)) => {
997            Some(((uid, effective_uid), (gid, effective_gid)))
998        }
999        _ => None,
1000    }
1001}
1002
1003struct Parts<'a> {
1004    str_parts: Vec<&'a str>,
1005    short_exe: &'a [u8],
1006}
1007
1008fn parse_stat_file(data: &[u8]) -> Option<Parts<'_>> {
1009    // The stat file is "interesting" to parse, because spaces cannot
1010    // be used as delimiters. The second field stores the command name
1011    // surrounded by parentheses. Unfortunately, whitespace and
1012    // parentheses are legal parts of the command, so parsing has to
1013    // proceed like this: The first field is delimited by the first
1014    // whitespace, the second field is everything until the last ')'
1015    // in the entire string. All other fields are delimited by
1016    // whitespace.
1017
1018    let mut str_parts = Vec::with_capacity(51);
1019    let mut data_it = data.splitn(2, |&b| b == b' ');
1020    str_parts.push(str::from_utf8(data_it.next()?).ok()?);
1021    let mut data_it = data_it.next()?.rsplitn(2, |&b| b == b')');
1022    let data = str::from_utf8(data_it.next()?).ok()?;
1023    let short_exe = data_it.next()?;
1024    str_parts.extend(data.split_whitespace());
1025    Some(Parts {
1026        str_parts,
1027        short_exe: short_exe.strip_prefix(b"(").unwrap_or(short_exe),
1028    })
1029}
1030
1031/// Type used to correctly handle the `REMAINING_FILES` global.
1032struct FileCounter(File);
1033
1034impl FileCounter {
1035    fn new(f: File) -> Option<Self> {
1036        let any_remaining =
1037            remaining_files().fetch_update(Ordering::SeqCst, Ordering::SeqCst, |remaining| {
1038                if remaining > 0 {
1039                    Some(remaining - 1)
1040                } else {
1041                    // All file descriptors we were allowed are being used.
1042                    None
1043                }
1044            });
1045
1046        any_remaining.ok().map(|_| Self(f))
1047    }
1048}
1049
1050impl std::ops::Deref for FileCounter {
1051    type Target = File;
1052
1053    fn deref(&self) -> &Self::Target {
1054        &self.0
1055    }
1056}
1057impl std::ops::DerefMut for FileCounter {
1058    fn deref_mut(&mut self) -> &mut Self::Target {
1059        &mut self.0
1060    }
1061}
1062
1063impl Drop for FileCounter {
1064    fn drop(&mut self) {
1065        remaining_files().fetch_add(1, Ordering::Relaxed);
1066    }
1067}
1068
1069#[cfg(test)]
1070mod tests {
1071    use super::split_content;
1072    use std::ffi::OsString;
1073
1074    // This test ensures that all the parts of the data are split.
1075    #[test]
1076    fn test_copy_file() {
1077        assert_eq!(split_content(b"hello\0"), vec![OsString::from("hello")]);
1078        assert_eq!(split_content(b"hello"), vec![OsString::from("hello")]);
1079        assert_eq!(
1080            split_content(b"hello\0b"),
1081            vec![OsString::from("hello"), "b".into()]
1082        );
1083        assert_eq!(
1084            split_content(b"hello\0\0\0\0b"),
1085            vec![OsString::from("hello"), "b".into()]
1086        );
1087    }
1088}