diff --git a/src/uu/snice/src/action.rs b/src/uu/snice/src/action.rs index 3fac9ada..0a25d169 100644 --- a/src/uu/snice/src/action.rs +++ b/src/uu/snice/src/action.rs @@ -111,7 +111,7 @@ impl Display for ActionResult { /// /// But we don't know if the process of pid are exist, if [None], the process doesn't exist #[cfg(target_os = "linux")] -fn set_priority(pid: u32, prio: &Priority) -> Option { +fn set_priority(pid: u32, prio: &Priority, take_action: bool) -> Option { use libc::{getpriority, setpriority, PRIO_PROCESS}; use nix::errno::Errno; @@ -136,6 +136,10 @@ fn set_priority(pid: u32, prio: &Priority) -> Option { prio }; + if !take_action { + return Some(ActionResult::Success); + } + let prio = match prio { Priority::Increase(prio) => current_priority + *prio as i32, Priority::Decrease(prio) => current_priority - *prio as i32, @@ -159,11 +163,15 @@ fn set_priority(pid: u32, prio: &Priority) -> Option { // TODO: Implemented this on other platform #[cfg(not(target_os = "linux"))] -fn set_priority(_pid: u32, _prio: &Priority) -> Option { +fn set_priority(_pid: u32, _prio: &Priority, _take_action: bool) -> Option { None } -pub(crate) fn perform_action(pids: &[u32], prio: &Priority) -> Vec> { - let f = |pid: &u32| set_priority(*pid, prio); +pub(crate) fn perform_action( + pids: &[u32], + prio: &Priority, + take_action: bool, +) -> Vec> { + let f = |pid: &u32| set_priority(*pid, prio, take_action); pids.iter().map(f).collect() } diff --git a/src/uu/snice/src/snice.rs b/src/uu/snice/src/snice.rs index cebf5df6..caa7c9ee 100644 --- a/src/uu/snice/src/snice.rs +++ b/src/uu/snice/src/snice.rs @@ -173,9 +173,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } // Case1: Perform priority + let take_action = !matches.get_flag("no-action"); if let Some(targets) = settings.expressions { let pids = collect_pids(&targets); - let results = perform_action(&pids, &settings.priority); + let results = perform_action(&pids, &settings.priority, take_action); if results.iter().all(|it| it.is_none()) || results.is_empty() { return Err(USimpleError::new(1, "no process selection criteria")); @@ -184,6 +185,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { if settings.verbose { let output = construct_verbose_result(&pids, &results).trim().to_owned(); println!("{output}"); + } else if !take_action { + pids.iter().for_each(|pid| println!("{pid}")); } } @@ -255,7 +258,7 @@ pub fn uu_app() -> Command { // arg!(-i --interactive "interactive"), arg!(-l --list "list all signal names"), arg!(-L --table "list all signal names in a nice table"), - // arg!(-n --"no-action" "do not actually kill processes; just print what would happen"), + arg!(-n --"no-action" "do not actually kill processes; just print what would happen"), arg!(-v --verbose "explain what is being done"), // arg!(-w --warnings "enable warnings (not implemented)"), // Expressions