Skip to content

pidof: match processes by executable and by rewritten titles - #818

Open
jayhemnani9910 wants to merge 3 commits into
uutils:mainfrom
jayhemnani9910:pidof-rewritten-titles
Open

jayhemnani9910 wants to merge 3 commits into
uutils:mainfrom
jayhemnani9910:pidof-rewritten-titles

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

pidof takes argv[0] as the first space-separated word of the joined command line, and only compares that. Two common cases miss:

  • Servers that rewrite their title. sshd's listener shows up as sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups, which becomes sshd:. So pidof sshd finds nothing and pidof sshd: matches. Any program with a title like name: worker process has the same problem.
  • A process started with another argv[0] (exec -a, or a program that sets its own name) isn't found by its program name.
$ ./target/debug/procps pidof sshd; echo $?
1
$ pidof sshd    # procps-ng 4.0.4
2096

Now it matches the way procps-ng's pidof does:

  • the base name of the whole argv[0], read from /proc/PID/cmdline without joining the arguments
  • the base name of /proc/PID/exe, also when cut to the 15 bytes the kernel keeps for the process name
  • the process name, when argv[0] has a space in it (a rewritten title)

The -x script check reads its argument the same way. Nothing else changes.

Testing:

  • New test_process_with_another_argv0 copies /bin/sh under its own name and runs it once with the title pidoftitletest: worker process and once with argv[0] another-argv0. pidof pidoftitletest has to find it and pidof pidoftitletest: must not. It fails on main. The spawn retries on ETXTBSY, since other tests fork while the copy can still be open for writing.
  • cargo test: 225 passed (224 on main), clippy and rustfmt clean.
  • Compared with pidof from procps-ng 4.0.4 (built from the release tarball, since Ubuntu's pidof is sysvinit's) on the same running system: 36 names over test processes (titles with spaces, exec -a, a 19-byte program name, a path with a space, a renamed perl $0, shell scripts with and without -x) and system daemons (sshd, systemd, init, cron, dbus-daemon, polkitd, ...). All match, as do -s, -o, -q and several names at once. The rest were a short-lived subshell in pidof bash and the -d alias, which this pidof adds on purpose.

pidof took argv[0] as the first space-separated word of the joined
command line. Servers that rewrite their title, like
"sshd: /usr/sbin/sshd -D [listener]", came out as "sshd:", so
"pidof sshd" found nothing, while "pidof sshd:" matched. A process
started with another argv[0] (exec -a) was not found by its program
name either.

Match like procps-ng: the base name of the whole argv[0], the base name
of /proc/PID/exe (also cut to the 15 bytes the kernel keeps), or, when
argv[0] has a space in it, the process name.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 05:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.71429% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 20.48%. Comparing base (67832aa) to head (cceb09b).
⚠️ Report is 59 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/pidof/src/pidof.rs 0.00% 15 Missing ⚠️
src/uu/pgrep/src/process.rs 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #818      +/-   ##
==========================================
+ Coverage   19.20%   20.48%   +1.28%     
==========================================
  Files          68       68              
  Lines       10156    10163       +7     
  Branches      546      553       +7     
==========================================
+ Hits         1950     2082     +132     
+ Misses       8206     8081     -125     
Flag Coverage Δ
macos_latest 7.09% <0.00%> (+0.34%) ⬆️
ubuntu_latest 20.87% <60.71%> (+1.32%) ⬆️
windows_latest 0.02% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/uu/pidof/src/pidof.rs Outdated
let path = binding.first().unwrap().to_string();
// `cmdline` has the arguments joined with spaces, so read them again to
// get argv[0] as it is.
let Ok(raw) = fs::read(format!("/proc/{}/cmdline", process.pid)) else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProcessInformation already reads cmdline, could it keep the raw args instead of reading /proc a second time here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0308dbc: ProcessInformation now keeps the raw arguments in cmdline_args (split on NUL when cmdline is read), and pidof uses those, so the second read is gone.

Comment thread src/uu/pidof/src/pidof.rs
// the kernel cuts it to.
if let Ok(exe) = fs::read_link(format!("/proc/{}/exe", process.pid)) {
if let Some(exe) = exe.file_name().and_then(|f| f.to_str()) {
if exe == name_to_match || exe.as_bytes().get(..15) == Some(name_to_match.as_bytes()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test for the 15 bytes case
pidoftitletest is 14 bytes so this branch isn't covered

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0308dbc: the test binary is now pidoftitletest-long, and the test checks both the full name and the 15-byte pidoftitletest- the kernel cuts it to.

Comment thread src/uu/pidof/src/pidof.rs Outdated

// A process that rewrites its title, like "sshd: /usr/sbin/sshd -D", is
// matched by its process name too.
if path.contains(' ') && process.name().unwrap() == name_to_match {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid the new unwrap, process.name().is_ok_and(|n| n == name_to_match) works

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0308dbc, using process.name().is_ok_and(|n| n == name_to_match).


for argv0 in ["pidoftitletest: worker process", "another-argv0"] {
// "; :" keeps the shell from replacing itself with sleep. Other tests
// forking while the copy was open for writing make exec fail with

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sh -c forks sleep here, and killing the shell leaves it orphaned for 5s
could you please use exec sleep or kill the group?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0308dbc: the shell now starts in its own process group and the test kills the whole group, so sleep goes with it. I kept the fork because exec sleep would change the executable the test matches on.

Keep the raw cmdline arguments in ProcessInformation so pidof does not
read /proc/<pid>/cmdline a second time, drop a new unwrap, test the
15-byte truncated name and kill the test's process group so the forked
sleep does not linger.
Copilot AI balanced review requested due to automatic review settings November 24, 2026 05:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/pidof/src/pidof.rs Outdated
) -> bool {
let binding = process.cmdline.split(' ').collect::<Vec<_>>();
let path = binding.first().unwrap().to_string();
let binding = process.cmdline_args.clone();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why clone? &process.cmdline_args should be enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A plain borrow doesn't compile here, because process.name() takes &mut self later in the function. In cceb09b it no longer clones the whole Vec: it copies only cmdline_args[0] and cmdline_args[1], the two values used below.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 05:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants