Skip to content

pgrep: accept terminal names without /dev/ prefix - #866

Open
og-orca wants to merge 2 commits into
uutils:mainfrom
og-orca:fix/179-tty-detection
Open

og-orca wants to merge 2 commits into
uutils:mainfrom
og-orca:fix/179-tty-detection

Conversation

@og-orca

@og-orca og-orca commented Nov 30, 2026

Copy link
Copy Markdown

Fixes #179

Since #578, pgrep reads the tty from /proc/<pid>/stat, so a normal user can see the tty of other users' processes. But the tty is stored as a full path (/dev/pts/0), while the -t value is kept as typed (pts/0). They never match, so pgrep -t pts/0 or pgrep -t tty1 prints nothing for anyone, root included.

Now I add /dev/ to the -t value when it is missing. -t /dev/pts/0 still works.

Before (user bob, sleep running as alice on pts/0):

$ pgrep -t pts/0          # procps-ng
11448
11449
$ procps pgrep -t pts/0   # uutils, main
$ echo $?
1

After:

$ procps pgrep -t pts/0
12534
12535

Testing:

  • New unit test test_teletype_from_name_without_dev_prefix. It fails on main (Known("pts/0") vs Known("/dev/pts/0")) and passes now.
  • Manual check in a Debian container: alice runs script -qfc 'sleep 300', bob runs pgrep -t pts/0.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean.

Limits: snice uses the same parsing, so its -t gets the same change; I only checked that its tests still pass. In my container test_cgroup fails, but it fails on main the same way.

The tty of a process is now read from /proc/<pid>/stat and stored as a
full device path like /dev/pts/0. The --terminal value was kept as typed,
so "pgrep -t pts/0" or "pgrep -t tty1" never matched anything.

Prepend /dev/ to the given name when it is missing.

Fixes uutils#179
@og-orca
og-orca marked this pull request as ready for review November 30, 2026 17:27
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 20.32%. Comparing base (67832aa) to head (ef3e374).
⚠️ Report is 62 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/pgrep/src/process.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #866      +/-   ##
==========================================
+ Coverage   19.20%   20.32%   +1.12%     
==========================================
  Files          68       68              
  Lines       10156    10127      -29     
  Branches      546      551       +5     
==========================================
+ Hits         1950     2058     +108     
+ Misses       8206     8069     -137     
Flag Coverage Δ
macos_latest 7.10% <0.00%> (+0.35%) ⬆️
ubuntu_latest 20.71% <0.00%> (+1.15%) ⬆️
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/pgrep/src/process.rs Outdated
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::try_from(PathBuf::from(value))
let name = value.strip_prefix("/dev/").unwrap_or(value);
Self::try_from(PathBuf::from(format!("/dev/{name}")))

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.

could be Ok(Self::Known(format!("/dev/{name}"))) directly, no?
no need to go through PathBuf and back to a String

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point. The PathBuf only got turned back into a String, so I build the Known value directly now.

@og-orca
og-orca requested a review from sylvestre November 30, 2026 22:17
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.

Terminal(teletype?) detection implementation is wrong

2 participants