| 1 | //! Linux process code for getting process data via `/proc/`. |
| 2 | //! Based on the [procfs](https://github.com/eminence/procfs) crate. |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 5 | fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, |
| 6 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#}, |
| 7 | pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, |
| 8 | syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::OnceLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/OnceLock#, |
| 9 | }; |
| 10 | |
| 11 | use anyhowrust-analyzer cargo anyhow 1.0.104 crate/::anyhowrust-analyzer cargo anyhow 1.0.104 macros/anyhow!; |
| 12 | use libcrust-analyzer cargo libc 0.2.189 crate/::uid_trust-analyzer cargo libc 0.2.189 unix/uid_t#; |
| 13 | use rustixrust-analyzer cargo rustix 1.1.4 crate/::{ |
| 14 | fdrust-analyzer cargo rustix 1.1.4 fd/::OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#, |
| 15 | fsrust-analyzer cargo rustix 1.1.4 fs/::{Moderust-analyzer cargo rustix 1.1.4 backend/fs/types/Mode#, OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#}, |
| 16 | pathrust-analyzer cargo rustix 1.1.4 path/::Argrust-analyzer cargo rustix 1.1.4 path/arg/Arg#, |
| 17 | }; |
| 18 | |
| 19 | use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::processesrust-analyzer cargo bottom 0.14.9 collection/processes/::{Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#, linuxrust-analyzer cargo bottom 0.14.9 collection/processes/linux/::is_str_numericrust-analyzer cargo bottom 0.14.9 collection/processes/linux/is_str_numeric().}; |
| 20 | |
| 21 | static PAGESIZErust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/PAGESIZE.: OnceLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/OnceLock#<u64> = OnceLockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/OnceLock#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/impl#[`OnceLock<T>`]new().(); |
| 22 | |
| 23 | #[inline] |
| 24 | fn next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().<'a>(iter: &mut impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'a str>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'a str, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 25 | iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() |
| 26 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][`From<ErrorKind>`]from().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#)) |
| 27 | } |
| 28 | |
| 29 | /// A wrapper around the data in `/proc/<PID>/stat`. For documentation, see: |
| 30 | /// - <https://manpages.ubuntu.com/manpages/noble/man5/proc_pid_stat.5.html> |
| 31 | /// - <https://man7.org/linux/man-pages/man5/proc_pid_status.5.html> |
| 32 | /// |
| 33 | /// Note this does not necessarily get all fields, only the ones we use in |
| 34 | /// bottom. |
| 35 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat# { |
| 36 | /// The filename of the executable without parentheses. |
| 37 | pub commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 38 | |
| 39 | /// The current process state, represented by a char. |
| 40 | pub staterust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#state.: char, |
| 41 | |
| 42 | /// The parent process PID. |
| 43 | pub ppidrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#ppid.: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#, |
| 44 | |
| 45 | /// The amount of time this process has been scheduled in user mode in clock |
| 46 | /// ticks. |
| 47 | pub utimerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#utime.: u64, |
| 48 | |
| 49 | /// The amount of time this process has been scheduled in kernel mode in |
| 50 | /// clock ticks. |
| 51 | pub stimerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#stime.: u64, |
| 52 | |
| 53 | /// The resident set size, or the number of pages the process has in real |
| 54 | /// memory. |
| 55 | rssrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#rss.: u64, |
| 56 | |
| 57 | /// The virtual memory size in bytes. |
| 58 | pub vsizerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#vsize.: u64, |
| 59 | |
| 60 | /// The start time of the process, represented in clock ticks. |
| 61 | pub start_timerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#start_time.: u64, |
| 62 | |
| 63 | /// Kernel thread |
| 64 | pub is_kernel_threadrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#is_kernel_thread.: bool, |
| 65 | |
| 66 | /// The kernel scheduling priority. |
| 67 | pub priorityrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#priority.: i32, |
| 68 | |
| 69 | /// The nice value (user-settable scheduling hint). |
| 70 | #[cfg(unix)] |
| 71 | pub nicerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#nice.: i32, |
| 72 | } |
| 73 | |
| 74 | impl Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat# { |
| 75 | /// Get process stats from a file; this assumes the file is located at |
| 76 | /// `/proc/<PID>/stat`. For documentation, see |
| 77 | /// [here](https://manpages.ubuntu.com/manpages/noble/man5/proc_pid_stat.5.html) as a reference. |
| 78 | fn from_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Stat]from_file().(mut f: Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#> { |
| 79 | // Since this is just one line, we can read it all at once. However, since it |
| 80 | // (technically) might have non-utf8 characters, we can't just use |
| 81 | // read_to_string. |
| 82 | f.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_end().(unsafe { buffer.as_mut_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_mut_vec().() })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | |
| 84 | // TODO: Is this needed? |
| 85 | let line = buffer.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 86 | |
| 87 | // Comm is represented by a string in parentheses (e.g. `(foo)`, `((bar))`). |
| 88 | // To handle that second case, we need to find the "last" closing parentheses. |
| 89 | let (comm, rest) = { |
| 90 | let start_paren = line |
| 91 | .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('(') |
| 92 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| anyhowrust-analyzer cargo anyhow 1.0.104 macros/anyhow!!("start paren missing"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 93 | |
| 94 | // So, we _could_ try and be smart and only parse a limited slice of the string - however, |
| 95 | // there appears to be no ABI guarantees of comm length anymore, so we just take the hit and do an rsplit |
| 96 | // over the full string. |
| 97 | // |
| 98 | // Sources/discussion: |
| 99 | // - https://man.archlinux.org/man/proc_pid_stat.5.en |
| 100 | // - https://stackoverflow.com/questions/23534263/what-is-the-maximum-allowed-limit-on-the-length-of-a-process-name#comment138697304_23534499 |
| 101 | // - https://elixir.bootlin.com/linux/v7.1.3/source/fs/proc/array.c#L100 |
| 102 | // - https://github.com/ClementTsang/bottom/pull/2163#issuecomment-5017857303 |
| 103 | let (comm, rest) = line[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start_paren rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 104 | .rsplit_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]rsplit_once().(") ") |
| 105 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| anyhowrust-analyzer cargo anyhow 1.0.104 macros/anyhow!!("stat string is malformed"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 106 | |
| 107 | (comm.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), rest) |
| 108 | }; |
| 109 | |
| 110 | let mut rest = rest.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(' '); |
| 111 | let state = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 112 | .charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() |
| 113 | .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() |
| 114 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| anyhowrust-analyzer cargo anyhow 1.0.104 macros/anyhow!!("missing state"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 115 | let ppid: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid# = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 116 | |
| 117 | // Skip 4 fields (pgrp, session, tty_nr, tpgid) |
| 118 | let mut rest = rest.skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(4); |
| 119 | |
| 120 | // read flags for kernel thread (PF_KTHREAD from include/linux/sched.h) |
| 121 | let flags: u32 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 122 | let is_kernel_thread: bool = flags rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x00200000 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0; |
| 123 | |
| 124 | // Skip 4 fields (minflt, cminflt, majflt, cmajflt) |
| 125 | let mut rest = rest.skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(4); |
| 126 | let utime: u64 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 127 | let stime: u64 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 128 | |
| 129 | // cutime |
| 130 | let _ = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 131 | // cstime |
| 132 | let _ = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 133 | // priority |
| 134 | let priority: i32 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 135 | // nice |
| 136 | let nice: i32 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 137 | // num_threads |
| 138 | let _ = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 139 | // itrealvalue |
| 140 | let _ = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 141 | |
| 142 | let start_time: u64 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 143 | let vsize: u64 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 144 | let rss: u64 = next_partrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/next_part().(&mut rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 145 | |
| 146 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat# { |
| 147 | commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm., |
| 148 | staterust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#state., |
| 149 | ppidrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#ppid., |
| 150 | utimerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#utime., |
| 151 | stimerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#stime., |
| 152 | rssrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#rss., |
| 153 | vsizerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#vsize., |
| 154 | start_timerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#start_time., |
| 155 | is_kernel_threadrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#is_kernel_thread., |
| 156 | priorityrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#priority., |
| 157 | nicerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#nice., |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | /// Returns the Resident Set Size in bytes. |
| 162 | #[inline] |
| 163 | pub fn rss_bytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Stat]rss_bytes().(&self) -> u64 { |
| 164 | self.rssrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#rss. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<&u64>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<&u64>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<&u64>`]mul().PAGESIZErust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/PAGESIZE..get_or_initrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/once_lock/impl#[`OnceLock<T>`]get_or_init().(|| rustixrust-analyzer cargo rustix 1.1.4 crate/::paramrust-analyzer cargo rustix 1.1.4 param/::page_sizerust-analyzer cargo rustix 1.1.4 param/auxv/page_size().() as u64) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// A wrapper around the data in `/proc/<PID>/io`. |
| 169 | /// |
| 170 | /// Note this does not necessarily get all fields, only the ones we use in |
| 171 | /// bottom. |
| 172 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct Iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io# { |
| 173 | pub read_bytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#read_bytes.: u64, |
| 174 | pub write_bytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#write_bytes.: u64, |
| 175 | } |
| 176 | |
| 177 | impl Iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io# { |
| 178 | #[inline] |
| 179 | fn from_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Io]from_file().(f: Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#> { |
| 180 | const NUM_FIELDSrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/NUM_FIELDS.: u16 = 0; // Make sure to update this if you want more fields! |
| 181 | enum Fieldsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields# { |
| 182 | ReadBytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#ReadBytes#, |
| 183 | WriteBytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#WriteBytes#, |
| 184 | } |
| 185 | |
| 186 | let mut read_fields = 0; |
| 187 | let mut reader = BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(f); |
| 188 | |
| 189 | let mut read_bytes = 0; |
| 190 | let mut write_bytes = 0; |
| 191 | |
| 192 | // This saves us from doing a string allocation on each iteration compared to |
| 193 | // `lines()`. |
| 194 | while let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes) = reader.read_linerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#read_line().(buffer) { |
| 195 | if bytes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 196 | if buffer.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 197 | // Empty, no need to clear. |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | let mut parts = buffer.split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().(); |
| 202 | |
| 203 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(field) = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitWhitespace<'a>`][Iterator]next().() { |
| 204 | let curr_field = match field { |
| 205 | "read_bytes:" => Fieldsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#::ReadBytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#ReadBytes#, |
| 206 | "write_bytes:" => Fieldsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#::WriteBytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#WriteBytes#, |
| 207 | _ => { |
| 208 | buffer.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 209 | continue; |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitWhitespace<'a>`][Iterator]next().() { |
| 214 | let value = value.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 215 | match curr_field { |
| 216 | Fieldsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#::ReadBytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#ReadBytes# => { |
| 217 | read_bytes = value; |
| 218 | read_fields rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign().1; |
| 219 | } |
| 220 | Fieldsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#::WriteBytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Fields#WriteBytes# => { |
| 221 | write_bytes = value; |
| 222 | read_fields rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`AddAssign<Self>`]add_assign().1; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Quick short circuit if we have already read all the required fields. |
| 229 | if read_fields rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialEq<Self>`]eq().NUM_FIELDSrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/NUM_FIELDS. { |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | buffer.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 234 | } else { |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io# { |
| 240 | read_bytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#read_bytes., |
| 241 | write_bytesrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#write_bytes., |
| 242 | }) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// A wrapper around a Linux process operations in `/proc/<PID>`. |
| 247 | /// |
| 248 | /// Core documentation based on [proc's manpages](https://man7.org/linux/man-pages/man5/proc.5.html). |
| 249 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct Processrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process# { |
| 250 | pub pidrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#pid.: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#, |
| 251 | pub uidrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#uid.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<uid_trust-analyzer cargo libc 0.2.189 unix/uid_t#>, |
| 252 | pub statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#stat.: Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#, |
| 253 | pub iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#io.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#>, |
| 254 | pub cmdlinerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#cmdline.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 255 | } |
| 256 | |
| 257 | #[inline] |
| 258 | fn resetrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/reset().(root: &mut PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) { |
| 259 | root.poprust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]pop().(); |
| 260 | buffer.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 261 | } |
| 262 | |
| 263 | impl Processrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process# { |
| 264 | /// Creates a new [`Process`] given a `/proc/<PID>` path. This may fail if |
| 265 | /// the process no longer exists or there are permissions issues. |
| 266 | /// |
| 267 | /// Note that this pre-allocates fields on **creation**! As such, some data |
| 268 | /// might end up "outdated" depending on when you call some of the |
| 269 | /// methods. Therefore, this struct is only useful for either fields |
| 270 | /// that are unlikely to change, or are short-lived and |
| 271 | /// will be discarded quickly. |
| 272 | /// |
| 273 | /// This takes in a buffer to avoid allocs; this function will clear the |
| 274 | /// buffer. |
| 275 | #[inline] |
| 276 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn from_pathrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Process]from_path().( |
| 277 | pid_path: PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, get_threads: bool, |
| 278 | ) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<(Processrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>)> { |
| 279 | buffer.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 280 | |
| 281 | let pid_dir = rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::openatrust-analyzer cargo rustix 1.1.4 fs/at/openat().( |
| 282 | rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::CWDrust-analyzer cargo rustix 1.1.4 fs/special/CWD., |
| 283 | pid_path.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().(), |
| 284 | OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::PATHrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]PATH. rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::DIRECTORYrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]DIRECTORY. rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::CLOEXECrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]CLOEXEC., |
| 285 | Moderust-analyzer cargo rustix 1.1.4 backend/fs/types/Mode#::emptyrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[Mode]empty().(), |
| 286 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 287 | |
| 288 | let pid = pid_path |
| 289 | .as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().() |
| 290 | .componentsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]components().() |
| 291 | .next_backrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[`Components<'a>`][DoubleEndedIterator]next_back().() |
| 292 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| s.to_string_lossyrust-analyzer cargo rustix 1.1.4 path/arg/impl#[`Component<'a>`][Arg]to_string_lossy().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#>().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 293 | .or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| { |
| 294 | rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::readlinkatrust-analyzer cargo rustix 1.1.4 fs/at/readlinkat().(rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::CWDrust-analyzer cargo rustix 1.1.4 fs/special/CWD., pid_path.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().(), vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]) |
| 295 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 296 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| s.to_string_lossyrust-analyzer cargo rustix 1.1.4 path/arg/impl#[CString][Arg]to_string_lossy().().parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#>().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 297 | }) |
| 298 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| anyhowrust-analyzer cargo anyhow 1.0.104 macros/anyhow!!("PID for {pid_path:?} was not found"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 299 | |
| 300 | let uid = { |
| 301 | let metadata = rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::fstatrust-analyzer cargo rustix 1.1.4 fs/fd/fstat().(&pid_dir); |
| 302 | match metadata { |
| 303 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(md) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(md.st_uidrust-analyzer cargo rustix 1.1.4 backend/fs/types/Stat#st_uid.), |
| 304 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | let mut root = pid_path; |
| 309 | |
| 310 | // NB: Whenever you add a new stat, make sure to pop the root and clear the |
| 311 | // buffer! |
| 312 | |
| 313 | // Stat is pretty long, do this first to pre-allocate up-front. |
| 314 | let stat = |
| 315 | open_atrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/open_at().(&mut root, "stat", &pid_dir).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|file| Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#::from_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Stat]from_file().(file, buffer))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 316 | resetrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/reset().(&mut root, buffer); |
| 317 | |
| 318 | let cmdline = if cmdlinerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/cmdline().(&mut root, &pid_dir, buffer).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().() { |
| 319 | // The clone will give a string with the capacity of the length of buffer, don't |
| 320 | // worry. |
| 321 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(buffer.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().()) |
| 322 | } else { |
| 323 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 324 | }; |
| 325 | resetrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/reset().(&mut root, buffer); |
| 326 | |
| 327 | let io = open_atrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/open_at().(&mut root, "io", &pid_dir) |
| 328 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|file| Iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Io#::from_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Io]from_file().(file, buffer)) |
| 329 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 330 | |
| 331 | resetrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/reset().(&mut root, buffer); |
| 332 | |
| 333 | let threads = threadsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/threads().(&mut root, pid, get_threads); |
| 334 | |
| 335 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(( |
| 336 | Processrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process# { |
| 337 | pidrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#pid., |
| 338 | uidrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#uid., |
| 339 | statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#stat., |
| 340 | iorust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#io., |
| 341 | cmdlinerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Process#cmdline., |
| 342 | }, |
| 343 | threads, |
| 344 | )) |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | #[inline] |
| 349 | fn cmdlinerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/cmdline().(root: &mut PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, fd: &OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#, buffer: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 350 | let _ = open_atrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/open_at().(root, "cmdline", fd).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|mut file| file.read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(buffer))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 351 | |
| 352 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 353 | } |
| 354 | |
| 355 | /// Opens a path. Note that this function takes in a mutable root - this will |
| 356 | /// mutate it to avoid allocations. You probably will want to pop the most |
| 357 | /// recent child after if you need to use the buffer again. |
| 358 | #[inline] |
| 359 | fn open_atrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/open_at().(root: &mut PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, child: &str, fd: &OwnedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/OwnedFd#) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#> { |
| 360 | root.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(child); |
| 361 | let new_fd = rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::openatrust-analyzer cargo rustix 1.1.4 fs/at/openat().(fd, &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().root, OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::RDONLYrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]RDONLY. rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::CLOEXECrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]CLOEXEC., Moderust-analyzer cargo rustix 1.1.4 backend/fs/types/Mode#::emptyrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[Mode]empty().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 362 | |
| 363 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`fs::File`][`From<OwnedFd>`]from().(new_fd)) |
| 364 | } |
| 365 | |
| 366 | #[inline] |
| 367 | fn threadsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/threads().(root: &mut PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, pid: Pidrust-analyzer cargo bottom 0.14.9 collection/processes/Pid#, get_threads: bool) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 368 | if get_threads { |
| 369 | root.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().("task"); |
| 370 | |
| 371 | let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(task_dir) = rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::openatrust-analyzer cargo rustix 1.1.4 fs/at/openat().( |
| 372 | rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::CWDrust-analyzer cargo rustix 1.1.4 fs/special/CWD., |
| 373 | root.as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().(), |
| 374 | OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::RDONLYrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]RDONLY. rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::DIRECTORYrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]DIRECTORY. rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags][`BitOr<Self>`]bitor().OFlagsrust-analyzer cargo rustix 1.1.4 backend/fs/types/OFlags#::CLOEXECrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[OFlags]CLOEXEC., |
| 375 | Moderust-analyzer cargo rustix 1.1.4 backend/fs/types/Mode#::emptyrust-analyzer cargo rustix 1.1.4 backend/fs/types/impl#[Mode]empty().(), |
| 376 | ) else { |
| 377 | return Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 378 | }; |
| 379 | |
| 380 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(task) = rustixrust-analyzer cargo rustix 1.1.4 crate/::fsrust-analyzer cargo rustix 1.1.4 fs/::Dirrust-analyzer cargo rustix 1.1.4 backend/fs/dir/Dir#::read_fromrust-analyzer cargo rustix 1.1.4 backend/fs/dir/impl#[Dir]read_from().(task_dir) { |
| 381 | let pid_str = pid.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 382 | |
| 383 | return task |
| 384 | .flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().() |
| 385 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|thread_dir| { |
| 386 | let file_name = thread_dir.file_namerust-analyzer cargo rustix 1.1.4 backend/fs/dir/impl#[DirEntry]file_name().(); |
| 387 | let file_name = file_name.to_string_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CStr]to_string_lossy().(); |
| 388 | let file_name = file_name.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 389 | |
| 390 | if is_str_numericrust-analyzer cargo bottom 0.14.9 collection/processes/linux/is_str_numeric().(file_name) && file_name rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]ne().!=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]ne(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]ne().pid_str { |
| 391 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(root.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(file_name).to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().()) |
| 392 | } else { |
| 393 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 394 | } |
| 395 | }) |
| 396 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() |
| 401 | } |
| 402 | |
| 403 | #[cfg(test)] |
| 404 | mod testsrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/ { |
| 405 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 406 | |
| 407 | use superrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/::*; |
| 408 | |
| 409 | fn stat_from_namerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_from_name().(name: &str) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#> { |
| 410 | // Kinda garbage data but it should be fine. |
| 411 | let stat = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 412 | "0 ({name}) R 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" |
| 413 | ); |
| 414 | |
| 415 | stat_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_file().(&stat) |
| 416 | } |
| 417 | |
| 418 | fn stat_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_file().(stat: &str) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#> { |
| 419 | let mut file = tempfilerust-analyzer cargo tempfile 3.27.0 crate/::tempfilerust-analyzer cargo tempfile 3.27.0 file/tempfile().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 420 | file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(stat.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 421 | file.rewindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#rewind().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 422 | |
| 423 | Statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#::from_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/impl#[Stat]from_file().(file, &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().()) |
| 424 | } |
| 425 | |
| 426 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 427 | fn parse_short_commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/parse_short_comm().() { |
| 428 | let stat = stat_from_namerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_from_name().("kworker/u16:2").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 429 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stat.commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm., "kworker/u16:2"); |
| 430 | } |
| 431 | |
| 432 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 433 | fn parse_long_commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/parse_long_comm().() { |
| 434 | let stat = stat_from_namerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_from_name().("kworker/u16:2-events_unbound").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 435 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stat.commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm., "kworker/u16:2-events_unbound"); |
| 436 | } |
| 437 | |
| 438 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 439 | fn parse_64_char_commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/parse_64_char_comm().() { |
| 440 | let comm = "a".repeatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]repeat().(64); |
| 441 | let stat = stat_from_namerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_from_name().(comm.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 442 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stat.commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm., comm); |
| 443 | } |
| 444 | |
| 445 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 446 | fn parse_double_paren_commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/parse_double_paren_comm().() { |
| 447 | let stat = stat_from_namerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_from_name().("(sd-pam)").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 448 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stat.commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm., "(sd-pam)"); |
| 449 | } |
| 450 | |
| 451 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 452 | fn parse_comm_with_spacerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/parse_comm_with_space().() { |
| 453 | let stat = stat_from_namerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_from_name().("a test").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 454 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stat.commrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/Stat#comm., "a test"); |
| 455 | } |
| 456 | |
| 457 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 458 | fn parse_invalid_statrust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/parse_invalid_stat().() { |
| 459 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stat_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_file().("1 (blah").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().(), "missing end paren"); |
| 460 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stat_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_file().("1 (blah)").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().(), "too short"); |
| 461 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stat_filerust-analyzer cargo bottom 0.14.9 collection/processes/linux/process/tests/stat_file().("1 )(").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().(), "wrong order"); |
| 462 | } |
| 463 | } |