Prefer process name over executable path (#13618)

# Description
Prefer process name over executable path. This in practice causes the
`name` column to use just the base executable name.

Also set start_time to nothing on error, because why not.

# User-Facing Changes

Before:

> /opt/google/chrome/chrome

After:

> chrome

Also picks up changes due to `echo test-proc > /proc/$$/comm`.

# Tests + Formatting

No new coverage.
This commit is contained in:
Piotr Kufel 2024-08-14 22:44:01 -07:00 committed by GitHub
parent 04746b8e2d
commit 5473def7ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 14 deletions

View File

@ -115,12 +115,13 @@ fn run_ps(
help: None,
inner: vec![],
})?;
// If we can't get the start time, just use the current time
let proc_start = proc_stat
.starttime()
.get()
.unwrap_or_else(|_| chrono::Local::now());
record.push("start_time", Value::date(proc_start.into(), span));
record.push(
"start_time",
match proc_stat.starttime().get() {
Ok(ts) => Value::date(ts.into(), span),
Err(_) => Value::nothing(span),
},
);
record.push("user_id", Value::int(proc.curr_proc.owner() as i64, span));
// These work and may be helpful, but it just seemed crowded
// record.push("group_id", Value::int(proc_stat.pgrp as i64, span));
@ -180,7 +181,5 @@ fn run_ps(
output.push(Value::record(record, span));
}
Ok(output
.into_iter()
.into_pipeline_data(span, engine_state.signals().clone()))
Ok(output.into_pipeline_data(span, engine_state.signals().clone()))
}

View File

@ -142,6 +142,10 @@ impl ProcessInfo {
/// Name of command
pub fn name(&self) -> String {
if let Some(name) = self.comm() {
return name;
}
// Fall back in case /proc/<pid>/stat source is not available.
if let Ok(mut cmd) = self.curr_proc.cmdline() {
if let Some(name) = cmd.first_mut() {
// Take over the first element and return it without extra allocations
@ -149,7 +153,7 @@ impl ProcessInfo {
return std::mem::take(name);
}
}
self.comm()
String::new()
}
/// Full name of command, with arguments
@ -159,12 +163,12 @@ impl ProcessInfo {
// TODO: Maybe rename this to display_command and add escaping compatible with nushell?
pub fn command(&self) -> String {
if let Ok(cmd) = self.curr_proc.cmdline() {
// TODO: When can it successfully return empty?
// Things like kworker/0:0 still have the cmdline file in proc, even though it's empty.
if !cmd.is_empty() {
return cmd.join(" ").replace(['\n', '\t'], " ");
}
}
self.comm()
self.comm().unwrap_or_default()
}
pub fn cwd(&self) -> String {
@ -227,7 +231,7 @@ impl ProcessInfo {
self.curr_proc.stat().map(|p| p.vsize).unwrap_or_default()
}
fn comm(&self) -> String {
self.curr_proc.stat().map(|st| st.comm).unwrap_or_default()
fn comm(&self) -> Option<String> {
self.curr_proc.stat().map(|st| st.comm).ok()
}
}