镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

w: don't fail when tty device is missing - #719

Open
Franklin-Qi wants to merge 1 commit into
uutils:mainfrom
Franklin-Qi:fix-bug#718-not-fail-when-tty-device-is-missing
Open

Franklin-Qi wants to merge 1 commit into
uutils:mainfrom
Franklin-Qi:fix-bug#718-not-fail-when-tty-device-is-missing

Conversation

@Franklin-Qi

Copy link
Copy Markdown
Contributor
  • Treat missing or unreadable /dev/<tty> as zero idle time instead of exiting with an error
  • Add unit test for missing tty handling

Closes: #718

@sylvestre

Copy link
Copy Markdown
Contributor

can we also get a test in https://github.com/uutils/procps/blob/main/tests/by-util/test_w.rs ? thanks

@codecov

codecov Bot commented Jun 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 40.62500% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 19.28%. Comparing base (67832aa) to head (be0fca8).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/uu/w/src/w.rs 0.00% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #719      +/-   ##
==========================================
+ Coverage   19.20%   19.28%   +0.08%     
==========================================
  Files          68       68              
  Lines       10156    10181      +25     
  Branches      546      549       +3     
==========================================
+ Hits         1950     1963      +13     
- Misses       8206     8218      +12     
Flag Coverage Δ
macos_latest 6.75% <ø> (ø)
ubuntu_latest 19.63% <40.62%> (+0.08%) ⬆️
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 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.

@Franklin-Qi

Copy link
Copy Markdown
Contributor Author

can we also get a test in https://github.com/uutils/procps/blob/main/tests/by-util/test_w.rs ? thanks

@sylvestre Thank you for the review; I will add test cases.

- Treat missing or unreadable `/dev/<tty>` as zero idle time instead of exiting with an error
- Add unit and by-util test for missing tty handling

Closes: uutils#718
@Franklin-Qi
Franklin-Qi force-pushed the fix-bug#718-not-fail-when-tty-device-is-missing branch from 27f0328 to be0fca8 Compare September 30, 2026 10:52
Comment thread src/uu/w/src/w.rs
Comment on lines +443 to +488
#[cfg(all(target_env = "gnu", target_arch = "x86_64"))]
fn fake_user_utmp(user: &str, tty: &str, pid: i32) -> Vec<u8> {
const USER_PROCESS: i16 = 7;
let mut buf = Vec::with_capacity(384);
buf.extend_from_slice(&USER_PROCESS.to_ne_bytes());
buf.extend_from_slice(&[0; 2]); // padding
buf.extend_from_slice(&pid.to_ne_bytes());
let mut line = [0u8; 32];
line[..tty.len()].copy_from_slice(tty.as_bytes());
buf.extend_from_slice(&line);
buf.extend_from_slice(&[0; 4]); // ut_id
let mut name = [0u8; 32];
name[..user.len()].copy_from_slice(user.as_bytes());
buf.extend_from_slice(&name);
buf.extend_from_slice(&[0; 256]); // ut_host
buf.extend_from_slice(&[0; 4]); // ut_exit
buf.extend_from_slice(&0_i32.to_ne_bytes()); // ut_session
buf.extend_from_slice(&1_700_000_000_i32.to_ne_bytes()); // ut_tv.tv_sec
buf.extend_from_slice(&0_i32.to_ne_bytes()); // ut_tv.tv_usec
buf.extend_from_slice(&[0; 16]); // ut_addr_v6
buf.extend_from_slice(&[0; 20]); // reserved
assert_eq!(buf.len(), 384);
buf
}

#[test]
#[cfg(all(target_env = "gnu", target_arch = "x86_64"))]
fn test_fetch_user_info_missing_tty() {
let dir = std::env::temp_dir().join(format!("uu_w_utmp_{}", process::id()));
fs::create_dir_all(&dir).unwrap();
let utmp_path = dir.join("utmp");
fs::write(
&utmp_path,
fake_user_utmp("testuser", "no-such-tty-ci", process::id() as i32),
)
.unwrap();

let users = fetch_user_info_from(Some(&utmp_path)).unwrap();
let _ = fs::remove_dir_all(&dir);

assert_eq!(users.len(), 1);
assert_eq!(users[0].user, "testuser");
assert_eq!(users[0].terminal, "no-such-tty-ci");
assert_eq!(users[0].idle_time, Duration::ZERO);
}

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 and how you build these test? It seems related to glibc's defination?

@Krysztal112233 Krysztal112233 left a comment

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.

Overall, this looks fine. However, the w codebase is relatively old, and in the future it could be migrated to the ProcessInfomation defined by uu_pgrep. That said, this does not fall within the scope of the current pull request :)

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.

w: exits with error when utmp session references a missing tty device

3 participants