From 34e10f9aa83ebf6b93b11f40095dd70eb7b12156 Mon Sep 17 00:00:00 2001 From: Ed Smith Date: Sun, 15 Jan 2023 12:26:14 +0000 Subject: [PATCH] nice: Remove use of show_warning This is required to pass the GNU nice test suite. Failure to produce the advisory message when unable to change the process priority must be fatal, and without this commit our version of nice will exit, but before the commit the exit code will be 101 (due to eprintln! panicking), and it must be 125 to pass the test suite. Signed-off-by: Ed Smith --- src/uu/nice/src/nice.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/uu/nice/src/nice.rs b/src/uu/nice/src/nice.rs index dd507e33f..e6efb5140 100644 --- a/src/uu/nice/src/nice.rs +++ b/src/uu/nice/src/nice.rs @@ -9,13 +9,13 @@ use libc::{c_char, c_int, execvp, PRIO_PROCESS}; use std::ffi::{CString, OsString}; -use std::io::Error; +use std::io::{Error, Write}; use std::ptr; use clap::{crate_version, Arg, ArgAction, Command}; use uucore::{ error::{set_exit_code, UClapError, UResult, USimpleError, UUsageError}, - format_usage, show_error, show_warning, + format_usage, show_error, }; pub mod options { @@ -152,8 +152,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; niceness += adjustment; - if unsafe { libc::setpriority(PRIO_PROCESS, 0, niceness) } == -1 { - show_warning!("setpriority: {}", Error::last_os_error()); + // We can't use `show_warning` because that will panic if stderr + // isn't writable. The GNU test suite checks specifically that the + // exit code when failing to write the advisory is 125, but Rust + // will produce an exit code of 101 when it panics. + if unsafe { libc::setpriority(PRIO_PROCESS, 0, niceness) } == -1 + && write!( + std::io::stderr(), + "{}: warning: setpriority: {}", + uucore::util_name(), + Error::last_os_error() + ) + .is_err() + { + set_exit_code(125); + return Ok(()); } let cstrs: Vec = matches