Skip to content

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

Open
jayhemnani9910 wants to merge 1 commit into
uutils:mainfrom
jayhemnani9910:pidof-rewritten-titles
Open

jayhemnani9910 wants to merge 1 commit into
uutils:mainfrom
jayhemnani9910:pidof-rewritten-titles

Conversation

@jayhemnani9910

Copy link
Copy Markdown

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 51.11111% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 19.53%. Comparing base (67832aa) to head (9537be9).
⚠️ Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/pidof/src/pidof.rs 0.00% 19 Missing ⚠️
tests/by-util/test_pidof.rs 88.46% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #818      +/-   ##
==========================================
+ Coverage   19.20%   19.53%   +0.33%     
==========================================
  Files          68       68              
  Lines       10156    10268     +112     
  Branches      546      556      +10     
==========================================
+ Hits         1950     2006      +56     
- Misses       8206     8262      +56     
Flag Coverage Δ
macos_latest 6.73% <0.00%> (-0.02%) ⬇️
ubuntu_latest 19.89% <51.11%> (+0.33%) ⬆️
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.

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.

2 participants