w: don't fail when tty device is missing - #719
Franklin-Qi wants to merge 1 commit into
Conversation
|
can we also get a test in https://github.com/uutils/procps/blob/main/tests/by-util/test_w.rs ? thanks |
Codecov Report❌ Patch coverage is
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
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:
|
@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
27f0328 to
be0fca8
Compare
| #[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); | ||
| } | ||
|
|
There was a problem hiding this comment.
Could you tell me why and how you build these test? It seems related to glibc's defination?
Krysztal112233
left a comment
There was a problem hiding this comment.
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 :)
/dev/<tty>as zero idle time instead of exiting with an errorCloses: #718