Skip to content

Commit 96bbf99

Browse files
committed
snice: implement --no-action
1 parent 1c8283b commit 96bbf99

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/uu/snice/src/action.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Display for ActionResult {
111111
///
112112
/// But we don't know if the process of pid are exist, if [None], the process doesn't exist
113113
#[cfg(target_os = "linux")]
114-
fn set_priority(pid: u32, prio: &Priority) -> Option<ActionResult> {
114+
fn set_priority(pid: u32, prio: &Priority, take_action: bool) -> Option<ActionResult> {
115115
use libc::{getpriority, setpriority, PRIO_PROCESS};
116116
use nix::errno::Errno;
117117

@@ -136,6 +136,10 @@ fn set_priority(pid: u32, prio: &Priority) -> Option<ActionResult> {
136136
prio
137137
};
138138

139+
if !take_action {
140+
return Some(ActionResult::Success);
141+
}
142+
139143
let prio = match prio {
140144
Priority::Increase(prio) => current_priority + *prio as i32,
141145
Priority::Decrease(prio) => current_priority - *prio as i32,
@@ -159,11 +163,15 @@ fn set_priority(pid: u32, prio: &Priority) -> Option<ActionResult> {
159163

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

166-
pub(crate) fn perform_action(pids: &[u32], prio: &Priority) -> Vec<Option<ActionResult>> {
167-
let f = |pid: &u32| set_priority(*pid, prio);
170+
pub(crate) fn perform_action(
171+
pids: &[u32],
172+
prio: &Priority,
173+
take_action: bool,
174+
) -> Vec<Option<ActionResult>> {
175+
let f = |pid: &u32| set_priority(*pid, prio, take_action);
168176
pids.iter().map(f).collect()
169177
}

src/uu/snice/src/snice.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
173173
}
174174

175175
// Case1: Perform priority
176+
let take_action = !matches.get_flag("no-action");
176177
if let Some(targets) = settings.expressions {
177178
let pids = collect_pids(&targets);
178-
let results = perform_action(&pids, &settings.priority);
179+
let results = perform_action(&pids, &settings.priority, take_action);
179180

180181
if results.iter().all(|it| it.is_none()) || results.is_empty() {
181182
return Err(USimpleError::new(1, "no process selection criteria"));
@@ -255,7 +256,7 @@ pub fn uu_app() -> Command {
255256
// arg!(-i --interactive "interactive"),
256257
arg!(-l --list "list all signal names"),
257258
arg!(-L --table "list all signal names in a nice table"),
258-
// arg!(-n --"no-action" "do not actually kill processes; just print what would happen"),
259+
arg!(-n --"no-action" "do not actually kill processes; just print what would happen"),
259260
arg!(-v --verbose "explain what is being done"),
260261
// arg!(-w --warnings "enable warnings (not implemented)"),
261262
// Expressions

0 commit comments

Comments
 (0)