Skip to content

Commit a28367c

Browse files
authored
Merge pull request #474 from Bluemangoo/feature/snice-no-action
snice: implement `--no-action`
2 parents 1be5929 + fd9344f commit a28367c

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-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: 5 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"));
@@ -184,6 +185,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
184185
if settings.verbose {
185186
let output = construct_verbose_result(&pids, &results).trim().to_owned();
186187
println!("{output}");
188+
} else if !take_action {
189+
pids.iter().for_each(|pid| println!("{pid}"));
187190
}
188191
}
189192

@@ -255,7 +258,7 @@ pub fn uu_app() -> Command {
255258
// arg!(-i --interactive "interactive"),
256259
arg!(-l --list "list all signal names"),
257260
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"),
261+
arg!(-n --"no-action" "do not actually kill processes; just print what would happen"),
259262
arg!(-v --verbose "explain what is being done"),
260263
// arg!(-w --warnings "enable warnings (not implemented)"),
261264
// Expressions

0 commit comments

Comments
 (0)