Skip to content

w: Fix formatting of command line value#666

Open
alxndrv wants to merge 1 commit intouutils:mainfrom
alxndrv:w-fix-cmdline-formatting
Open

w: Fix formatting of command line value#666
alxndrv wants to merge 1 commit intouutils:mainfrom
alxndrv:w-fix-cmdline-formatting

Conversation

@alxndrv
Copy link
Copy Markdown

@alxndrv alxndrv commented Apr 8, 2026

The command line value exposed by the kernel in /proc/<pid>/cmdline represents whitespace as NULL bytes.
Using read_to_string() on this directly will cause the output to join all parts of the original command into one long string.
This PR adds a very simple fix to avoid this.

Before:

$ cargo run -q w
 22:16:22 up  2:00,  1 user,  load average: 0.32, 0.37, 0.38
USER     TTY       LOGIN@   IDLE   JCPU   PCPU  WHAT
alx      tty1      20:16    1:59m  0.00   0     /bin/sh/usr/bin/niri-session-l

After:

$ cargo run -q w
 22:17:02 up  2:00,  1 user,  load average: 0.67, 0.46, 0.41
USER     TTY       LOGIN@   IDLE   JCPU   PCPU  WHAT
alx      tty1      20:16    2:00m  0.00   0     /bin/sh /usr/bin/niri-session -l 

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 19.19%. Comparing base (ee98c0a) to head (45fbba4).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/w/src/w.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #666   +/-   ##
=======================================
  Coverage   19.19%   19.19%           
=======================================
  Files          68       68           
  Lines       10135    10135           
  Branches      545      545           
=======================================
  Hits         1945     1945           
  Misses       8190     8190           
Flag Coverage Δ
macos_latest 6.74% <ø> (ø)
ubuntu_latest 19.54% <0.00%> (ø)
windows_latest 0.02% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 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.

@Bluemangoo
Copy link
Copy Markdown
Collaborator

Can you fix the tests?

@alxndrv alxndrv force-pushed the w-fix-cmdline-formatting branch from e28cc66 to 5aaa4cd Compare April 9, 2026 14:12
@alxndrv
Copy link
Copy Markdown
Author

alxndrv commented Apr 9, 2026

Tests are fixed now, but I'm a bit confused by the coverage check. Is there anything I can do to make it come up as successful?

@Krysztal112233
Copy link
Copy Markdown
Collaborator

I feel the current test coverage is not very important, and it's already sufficient :)

Comment on lines +456 to +460
fn test_fetch_cmdline() {
// uucore's utmpx returns an i32, so we cast to that to mimic it.
let pid = process::id() as i32;
let path = Path::new("/proc").join(pid.to_string()).join("cmdline");
assert_eq!(
fs::read_to_string(path).unwrap(),
fetch_cmdline(pid).unwrap()
);
let cmdline = std::env::args().collect::<Vec<String>>().join(" ");
assert_eq!(cmdline, fetch_cmdline(pid));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you tell me why it needs to be changed to this?

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.

The original implementation was basically a 1:1 copy of the code that's inside the fetch_cmdline function (both just build a Path from a PID and read_to_string it).
So previously, we had a test where both sides of the assert_eq are results of basically identical code. This kind of approach guarantees that things "work", but does not test for correctness. Both sides could be outputting the same incorrect result, but the test would pass. You can probably see how that's potentially problematic 😅

Since we're already using std::process::id() to figure out the PID of the process, I thought it would make more sense to compare the result of our fetch_cmdline with a "known correct" value which in this case would be std::env::args(). Hope my explanation makes sense 😅

@Krysztal112233
Copy link
Copy Markdown
Collaborator

Krysztal112233 commented Apr 12, 2026

Can you add a comment for the fetch_cmdline function? I noticed that the man page mentions this point after checking it, so you could reference the man page there:

https://man7.org/linux/man-pages/man5/proc_pid_cmdline.5.html

@Krysztal112233
Copy link
Copy Markdown
Collaborator

Great work, I think this work can inspire us to consider similar issues elsewhere :)

@alxndrv
Copy link
Copy Markdown
Author

alxndrv commented Apr 12, 2026

@Krysztal112233 Thanks for the thorough review. My Rust is... rusty, so these pointers are very much needed.
@Bluemangoo Feel free to hold off on the review until I address all of these comments 🙂

@alxndrv alxndrv force-pushed the w-fix-cmdline-formatting branch from 6089ea1 to c1f9af2 Compare April 12, 2026 13:20
@alxndrv alxndrv force-pushed the w-fix-cmdline-formatting branch from c1f9af2 to 45fbba4 Compare April 12, 2026 13:20
@alxndrv alxndrv requested a review from Krysztal112233 April 12, 2026 13:28
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