Repository navigation
pidof: match processes by executable and by rewritten titles - #818
jayhemnani9910 wants to merge 3 commits into
Conversation
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.
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| 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 { |
There was a problem hiding this comment.
ProcessInformation already reads cmdline, could it keep the raw args instead of reading /proc a second time here?
There was a problem hiding this comment.
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.
| // 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()) { |
There was a problem hiding this comment.
please add a test for the 15 bytes case
pidoftitletest is 14 bytes so this branch isn't covered
There was a problem hiding this comment.
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.
|
|
||
| // 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 { |
There was a problem hiding this comment.
please avoid the new unwrap, process.name().is_ok_and(|n| n == name_to_match) works
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
sh -c forks sleep here, and killing the shell leaves it orphaned for 5s
could you please use exec sleep or kill the group?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| ) -> bool { | ||
| let binding = process.cmdline.split(' ').collect::<Vec<_>>(); | ||
| let path = binding.first().unwrap().to_string(); | ||
| let binding = process.cmdline_args.clone(); |
There was a problem hiding this comment.
why clone? &process.cmdline_args should be enough
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pidoftakes argv[0] as the first space-separated word of the joined command line, and only compares that. Two common cases miss:sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups, which becomessshd:. Sopidof sshdfinds nothing andpidof sshd:matches. Any program with a title likename: worker processhas the same problem.exec -a, or a program that sets its own name) isn't found by its program name.Now it matches the way procps-ng's
pidofdoes:/proc/PID/cmdlinewithout joining the arguments/proc/PID/exe, also when cut to the 15 bytes the kernel keeps for the process nameThe
-xscript check reads its argument the same way. Nothing else changes.Testing:
test_process_with_another_argv0copies/bin/shunder its own name and runs it once with the titlepidoftitletest: worker processand once with argv[0]another-argv0.pidof pidoftitletesthas to find it andpidof 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.pidoffrom procps-ng 4.0.4 (built from the release tarball, since Ubuntu'spidofis 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,-qand several names at once. The rest were a short-lived subshell inpidof bashand the-dalias, which this pidof adds on purpose.