Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/uu/snice/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActionResult> {
fn set_priority(pid: u32, prio: &Priority, take_action: bool) -> Option<ActionResult> {
use libc::{getpriority, setpriority, PRIO_PROCESS};
use nix::errno::Errno;

Expand All @@ -136,6 +136,10 @@ fn set_priority(pid: u32, prio: &Priority) -> Option<ActionResult> {
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,
Expand All @@ -159,11 +163,15 @@ fn set_priority(pid: u32, prio: &Priority) -> Option<ActionResult> {

// TODO: Implemented this on other platform
#[cfg(not(target_os = "linux"))]
fn set_priority(_pid: u32, _prio: &Priority) -> Option<ActionResult> {
fn set_priority(_pid: u32, _prio: &Priority, _take_action: bool) -> Option<ActionResult> {
None
}

pub(crate) fn perform_action(pids: &[u32], prio: &Priority) -> Vec<Option<ActionResult>> {
let f = |pid: &u32| set_priority(*pid, prio);
pub(crate) fn perform_action(
pids: &[u32],
prio: &Priority,
take_action: bool,
) -> Vec<Option<ActionResult>> {
let f = |pid: &u32| set_priority(*pid, prio, take_action);
pids.iter().map(f).collect()
}
7 changes: 5 additions & 2 deletions src/uu/snice/src/snice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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}"));
}
}

Expand Down Expand Up @@ -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
Expand Down
Loading