Skip to content

Commit 35f3e69

Browse files
committed
utmpx: handle unavailable logind session metadata
1 parent 9d5d41d commit 35f3e69

8 files changed

Lines changed: 383 additions & 79 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/uu/pinky/src/platform/unix.rs‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ fn format_timestamp(ut: &UtmpxRecord) -> String {
192192
};
193193
}
194194

195-
TIME_FORMAT.with(|description| ut.login_time().format(description).unwrap())
195+
ut.try_login_time().map_or_else(
196+
|| "?".into(),
197+
|time| TIME_FORMAT.with(|description| time.format(description).unwrap()),
198+
)
196199
}
197200

198201
/// Pull the real name out of a password entry's comment field: it is the part
@@ -368,6 +371,47 @@ impl Pinky {
368371
mod tests {
369372
use super::{format_quiet_time, real_name};
370373

374+
#[test]
375+
#[cfg(all(target_os = "linux", feature = "systemd_logind"))]
376+
fn missing_session_time_is_displayed_as_unknown() {
377+
use super::*;
378+
use uucore::systemd_logind::{SystemdLoginRecord, SystemdRecordType, SystemdUtmpxCompat};
379+
380+
let record = UtmpxRecord::Systemd(SystemdUtmpxCompat::new(SystemdLoginRecord {
381+
user: "user".into(),
382+
session_id: "legacy".into(),
383+
seat_or_tty: "pts/test".into(),
384+
raw_device: "pts/test".into(),
385+
host: "alias.test".into(),
386+
login_time: std::time::UNIX_EPOCH,
387+
pid: 0,
388+
session_leader_pid: 0,
389+
record_type: SystemdRecordType::UserProcess,
390+
}));
391+
let pinky = Pinky {
392+
resolve_hosts: false,
393+
layout: Layout {
394+
header: false,
395+
real_name: false,
396+
idle: true,
397+
origin: true,
398+
},
399+
details: Details {
400+
home_and_shell: false,
401+
project: false,
402+
plan: false,
403+
},
404+
names: Vec::new(),
405+
};
406+
assert_eq!(
407+
pinky
408+
.session_row(&record)
409+
.split_whitespace()
410+
.collect::<Vec<_>>(),
411+
["user", "?pts/test", "?????", "?", "alias.test"]
412+
);
413+
}
414+
371415
#[test]
372416
fn idle_under_a_minute_is_blank() {
373417
assert_eq!(format_quiet_time(0), " ");

‎src/uu/who/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ readme.workspace = true
1515

1616
[lib]
1717
path = "src/who.rs"
18-
test = false
1918
doctest = false
2019

2120
[dependencies]

‎src/uu/who/src/platform/unix.rs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,36 @@ fn format_timestamp(ut: &UtmpxRecord) -> String {
217217
)
218218
.unwrap()
219219
};
220-
ut.login_time().format(&pattern).unwrap()
220+
ut.try_login_time()
221+
.map_or_else(|| "?".into(), |time| time.format(&pattern).unwrap())
222+
}
223+
224+
fn format_pid(ut: &UtmpxRecord) -> String {
225+
ut.try_pid()
226+
.map_or_else(|| "?".into(), |pid| pid.to_string())
227+
}
228+
229+
#[cfg(all(test, target_os = "linux", feature = "systemd_logind"))]
230+
mod logind_tests {
231+
use super::*;
232+
use uucore::systemd_logind::{SystemdLoginRecord, SystemdRecordType, SystemdUtmpxCompat};
233+
234+
#[test]
235+
fn unavailable_time_and_pid_are_question_marks() {
236+
let record = UtmpxRecord::Systemd(SystemdUtmpxCompat::new(SystemdLoginRecord {
237+
user: "user".into(),
238+
session_id: "legacy".into(),
239+
seat_or_tty: "pts/test".into(),
240+
raw_device: "pts/test".into(),
241+
host: String::new(),
242+
login_time: std::time::UNIX_EPOCH,
243+
pid: 0,
244+
session_leader_pid: 0,
245+
record_type: SystemdRecordType::UserProcess,
246+
}));
247+
assert_eq!(format_timestamp(&record), "?");
248+
assert_eq!(format_pid(&record), "?");
249+
}
221250
}
222251

223252
fn current_tty() -> String {
@@ -301,7 +330,7 @@ impl Who {
301330

302331
fn emit_event(&self, ut: &UtmpxRecord, event: Event) -> UResult<()> {
303332
let time = format_timestamp(ut);
304-
let pid = format!("{}", ut.pid());
333+
let pid = format_pid(ut);
305334
let note = translate!("who-login-id", "id" => ut.terminal_suffix());
306335

307336
// Held outside the match so the borrows below outlive it.
@@ -420,7 +449,7 @@ impl Who {
420449
line: &ut.tty_device(),
421450
time: &format_timestamp(ut),
422451
idle: &idle,
423-
pid: &format!("{}", ut.pid()),
452+
pid: &format_pid(ut),
424453
note: &note,
425454
exit: "",
426455
})?;

‎src/uucore/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ thiserror = { workspace = true }
8989
cfg_aliases = { workspace = true }
9090

9191
[dev-dependencies]
92+
rstest = { workspace = true }
9293
tempfile = { workspace = true }
9394

9495
[target.'cfg(any(target_os = "freebsd", target_os = "hurd", target_os = "linux", target_os = "android", target_os = "macos", target_os = "netbsd"))'.dependencies]

0 commit comments

Comments
 (0)