From 235152a6b7e4e842d121bea3d6fff716255f1f55 Mon Sep 17 00:00:00 2001 From: Thomas Queiroz Date: Tue, 9 Nov 2021 19:57:24 -0300 Subject: [PATCH 1/2] uucore/utmpx: remove unwrap in cannon_host Default to hostname if getaddrinfo fails --- src/uucore/src/lib/features/utmpx.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/uucore/src/lib/features/utmpx.rs b/src/uucore/src/lib/features/utmpx.rs index a96c3f48c..a3078b818 100644 --- a/src/uucore/src/lib/features/utmpx.rs +++ b/src/uucore/src/lib/features/utmpx.rs @@ -236,17 +236,20 @@ impl Utmpx { flags: AI_CANONNAME, ..AddrInfoHints::default() }; - let sockets = getaddrinfo(Some(hostname), None, Some(hints)) - .unwrap() - .collect::>>()?; - for socket in sockets { - if let Some(ai_canonname) = socket.canonname { - return Ok(if display.is_empty() { - ai_canonname - } else { - format!("{}:{}", ai_canonname, display) - }); + if let Ok(sockets) = getaddrinfo(Some(hostname), None, Some(hints)) { + let sockets = sockets.collect::>>()?; + for socket in sockets { + if let Some(ai_canonname) = socket.canonname { + return Ok(if display.is_empty() { + ai_canonname + } else { + format!("{}:{}", ai_canonname, display) + }); + } } + } else { + // GNU coreutils has this behavior + return Ok(hostname.to_string()); } } From cbe6d7d5c1ed5aabe6a6a0a319d1c805756e7fbc Mon Sep 17 00:00:00 2001 From: Thomas Queiroz Date: Tue, 9 Nov 2021 19:58:54 -0300 Subject: [PATCH 2/2] who: use UResult --- src/uu/who/src/who.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/uu/who/src/who.rs b/src/uu/who/src/who.rs index a975c82ba..14f39536d 100644 --- a/src/uu/who/src/who.rs +++ b/src/uu/who/src/who.rs @@ -7,8 +7,8 @@ // spell-checker:ignore (ToDO) ttyname hostnames runlevel mesg wtmp statted boottime deadprocs initspawn clockchange curr runlvline pidstr exitstr hoststr -#[macro_use] -extern crate uucore; +use uucore::display::Quotable; +use uucore::error::{FromIo, UResult}; use uucore::libc::{ttyname, STDIN_FILENO, S_IWGRP}; use uucore::utmpx::{self, time, Utmpx}; @@ -59,7 +59,8 @@ fn get_long_usage() -> String { ) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = args .collect_str(InvalidEncodingHandling::Ignore) .accept_any(); @@ -157,9 +158,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { args: files, }; - who.exec(); - - 0 + who.exec() } pub fn uu_app() -> App<'static, 'static> { @@ -326,7 +325,7 @@ fn current_tty() -> String { } impl Who { - fn exec(&mut self) { + fn exec(&mut self) -> UResult<()> { let run_level_chk = |_record: i16| { #[cfg(not(target_os = "linux"))] return false; @@ -362,7 +361,7 @@ impl Who { for ut in records { if !self.my_line_only || cur_tty == ut.tty_device() { if self.need_users && ut.is_user_process() { - self.print_user(&ut); + self.print_user(&ut)?; } else if self.need_runlevel && run_level_chk(ut.record_type()) { if cfg!(target_os = "linux") { self.print_runlevel(&ut); @@ -383,6 +382,7 @@ impl Who { if ut.record_type() == utmpx::BOOT_TIME {} } } + Ok(()) } #[inline] @@ -464,7 +464,7 @@ impl Who { self.print_line("", ' ', "system boot", &time_string(ut), "", "", "", ""); } - fn print_user(&self, ut: &Utmpx) { + fn print_user(&self, ut: &Utmpx) -> UResult<()> { let mut p = PathBuf::from("/dev"); p.push(ut.tty_device().as_str()); let mesg; @@ -491,7 +491,13 @@ impl Who { }; let s = if self.do_lookup { - crash_if_err!(1, ut.canon_host()) + ut.canon_host().map_err_context(|| { + let host = ut.host(); + format!( + "failed to canonicalize {}", + host.split(':').next().unwrap_or(&host).quote() + ) + })? } else { ut.host() }; @@ -507,6 +513,8 @@ impl Who { hoststr.as_str(), "", ); + + Ok(()) } #[allow(clippy::too_many_arguments)]