Skip to content

Commit 58ef44d

Browse files
ppennaCopilot
andcommitted
nvx: name the stat fields that the Linux process start-time reader takes
parse_stat read the starttime of a /proc/<pid>/stat line with a bare fields.get(19), whose value, field 22 less field 3 of proc_pid_stat(5), only a comment gave, and the state with an unchecked fields[0], which held only because the get(19) above it succeeded. Name the state and starttime field numbers in proc_pid_stat(5), index the fields with their difference, and read the state with fields.first(), which returns the same malformed error. The index stays 19 and the state the first field, so every line yields the same values or the same error as before. The host::process Linux tests, which cover start_time_from_stat, pass unchanged at b269fea and with this change, in WSL 2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b269fea commit 58ef44d

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎nvx/src/host/platform/linux/process.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ pub(crate) fn start_time_from_stat(
5858
}
5959
}
6060

61+
/// The number of the `state` field of a `/proc/<pid>/stat` line, as proc_pid_stat(5) numbers the
62+
/// fields from 1: the first after the PID and the command name.
63+
const STAT_STATE_FIELD: usize = 3;
64+
65+
/// The number of the `starttime` field of a `/proc/<pid>/stat` line in proc_pid_stat(5).
66+
const STAT_START_TIME_FIELD: usize = 22;
67+
6168
/// Returns the state and the start time in the `/proc/<pid>/stat` line of `pid`.
6269
fn parse_stat(pid: u32, stat: &[u8]) -> io::Result<(&str, u64)> {
6370
let malformed = || io::Error::other(format!("/proc/{pid}/stat has an unexpected format"));
@@ -71,12 +78,13 @@ fn parse_stat(pid: u32, stat: &[u8]) -> io::Result<(&str, u64)> {
7178
.map_err(|_| malformed())?
7279
.split_whitespace()
7380
.collect();
74-
// Field 22 of the stat line; the fields after the command name start at field 3.
81+
// `fields` starts at the state field.
7582
let start_time = fields
76-
.get(19)
83+
.get(STAT_START_TIME_FIELD - STAT_STATE_FIELD)
7784
.and_then(|value| value.parse().ok())
7885
.ok_or_else(malformed)?;
79-
Ok((fields[0], start_time))
86+
let state = fields.first().copied().ok_or_else(malformed)?;
87+
Ok((state, start_time))
8088
}
8189

8290
/// Returns whether a process has threads besides its main thread.

0 commit comments

Comments
 (0)