Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process::Process:name is truncated for Linux #154

Closed
svartalf opened this issue Sep 4, 2019 · 8 comments
Closed

process::Process:name is truncated for Linux #154

svartalf opened this issue Sep 4, 2019 · 8 comments
Labels
A-process Area: heim-process crate C-enhancement Category: new feature or request O-linux Operating system: Linux
Projects
Milestone

Comments

@svartalf
Copy link
Member

svartalf commented Sep 4, 2019

/proc/{pid}/stat returns truncated (capped to 15 chars) process name, Process::cmdline could be used to fetch full process name if necessary.

Blocked by #97

@svartalf svartalf added A-process Area: heim-process crate C-enhancement Category: new feature or request O-linux Operating system: Linux labels Sep 4, 2019
@svartalf svartalf added this to the Release 0.0.8 milestone Sep 4, 2019
@ChristianWitts
Copy link

/proc/{pid}/cmdline packs the entire command line executed into a single non-delimited string.
What could be done is following the symlink from /proc/{pid}/exe to the binary being executed ala this hackyness readlink -f /proc/{pid}/exe | rev | cut -d/ -f1 | rev but using nicer Rust stdlib (although having a check in the sys::unix::fs it looks to just use libcs stat for the so I'm not sure it is a solution out of the box).

@svartalf
Copy link
Member Author

svartalf commented Sep 4, 2019

@ChristianWitts thank you for a note, it is a good option.

cmdline one has an important advantage, though, unprivileged processes can read it:

$ readlink exe
$ echo $?
1
$ cat cmdline
/usr/lib/polkit-kde-authentication-agent-1\0

Process::name could possibly try the readlink -f exe method and fall back to cmdline if the former method fails with the EACCES

@svartalf svartalf added this to To Do in Linux Sep 4, 2019
@Lomanic
Copy link

Lomanic commented Sep 4, 2019

gopsutil reads /proc/$PID/status to get numerous information for a process, including its name https://github.com/shirou/gopsutil/blob/e4ec7b275ada47ca32799106c2dba142d96aaf93/process/process_linux.go#L1006

This file is readable by everybody even for processes owned by root.

@svartalf
Copy link
Member Author

svartalf commented Sep 4, 2019

@Lomanic brilliant idea! Reading only the first line from one file will be much cheaper comparing to "/stat" -> "readlink" -> "/cmdline" chain

@Lomanic
Copy link

Lomanic commented Sep 4, 2019

In fact we fallback to reading /proc/$PID/cmdline if process name is longer or equals 15 chars in /proc/$PID/status (it's also capped in this file).

@svartalf
Copy link
Member Author

svartalf commented Sep 4, 2019

@Lomanic, ah, okay, /proc/$PID/status is truncating the process name too, I misunderstood that part.
Well then, back to original idea :)

@ChristianWitts
Copy link

So I am actually an idiot, and did not notice the \0 null separator (because my cat did not reflect the \0 unlike less), so something like this would yield a Vector containing the executable, and all the arguments passed to it.

use std::{fs, path::Path};

fn cmd_args(filename: impl AsRef<Path>) -> String {
    let cmdline: String = fs::read_to_string(filename).expect("no such file")
        .parse().expect("Unable to parse file");
    cmdline
}

fn main() {
    let cmdline = cmd_args("/proc/$PID/cmdline");
    // Trim trailing NULL char before splitting into components
    let d: Vec<&str> = cmdline.trim_matches('\0').split('\0').collect();
    println!("{:?}", d);
}

@svartalf
Copy link
Member Author

Yeah, cmdline parsing is implemented already, see it here and here, I just need to find time to address this issue.

@svartalf svartalf moved this from To Do to Done in Linux Sep 26, 2019
ClementTsang added a commit to ClementTsang/bottom that referenced this issue Oct 1, 2020
Change inspired by heim-rs/heim#154.  This was
the cause of some process names getting cut off and looking weird for
Linux (and Linux only, I'm not directly responsible for the other OSes).

This also adds spaces in between command line flags.  Before, they were
usually separated by either spaces (good) or null terminators (bad).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-process Area: heim-process crate C-enhancement Category: new feature or request O-linux Operating system: Linux
Projects
Linux
  
Done
Development

No branches or pull requests

3 participants