Merge pull request #474 from Bluemangoo/feature/snice-no-action

snice: implement `--no-action`
This commit is contained in:
Daniel Hofstetter
2025-08-05 17:03:43 +02:00
committed by GitHub
2 changed files with 17 additions and 6 deletions
+12 -4
View File
@@ -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;
@@ -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,
@@ -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()
}
+5 -2
View File
@@ -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