Add renice

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Daniel Hofstetter
2025-03-12 10:05:26 +01:00
parent 13eb760be5
commit e9d1600bc4
8 changed files with 118 additions and 0 deletions

17
src/uu/renice/Cargo.toml Normal file
View File

@@ -0,0 +1,17 @@
[package]
name = "uu_renice"
version = "0.0.1"
edition = "2021"
description = "renice ~ (uutils) Alter priority of running processes"
[lib]
path = "src/renice.rs"
[[bin]]
name = "renice"
path = "src/main.rs"
[dependencies]
clap = { workspace = true }
libc = { workspace = true }
uucore = { workspace = true }

7
src/uu/renice/renice.md Normal file
View File

@@ -0,0 +1,7 @@
# renice
```
renice [--priority|--relative] priority [-g|-p|-u] identifier...
```
Alter priority of running processes

View File

@@ -0,0 +1 @@
uucore::bin!(uu_renice);

View File

@@ -0,0 +1,65 @@
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::{crate_version, Arg, Command};
#[cfg(not(windows))]
use libc::PRIO_PROCESS;
use std::env;
#[cfg(not(windows))]
use std::io::Error;
use std::process;
use uucore::{error::UResult, format_usage, help_about, help_usage};
const ABOUT: &str = help_about!("renice.md");
const USAGE: &str = help_usage!("renice.md");
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let nice_value_str = matches.get_one::<String>("nice_value").unwrap(); // Retrieve as String
let nice_value = nice_value_str.parse::<i32>().unwrap_or_else(|_| {
eprintln!("Invalid nice value");
process::exit(1);
});
let pid_str = matches.get_one::<String>("pid").unwrap(); // Retrieve as String
let pid = pid_str.parse::<i32>().unwrap_or_else(|_| {
eprintln!("Invalid PID");
process::exit(1);
});
// TODO: implement functionality on windows
#[cfg(not(windows))]
if unsafe { libc::setpriority(PRIO_PROCESS, pid.try_into().unwrap(), nice_value) } == -1 {
eprintln!("Failed to set nice value: {}", Error::last_os_error());
process::exit(1);
}
println!("Nice value of process {} set to {}", pid, nice_value);
Ok(())
}
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.arg(
Arg::new("nice_value")
.value_name("NICE_VALUE")
.help("The new nice value for the process")
.required(true)
.index(1),
)
.arg(
Arg::new("pid")
.value_name("PID")
.help("The PID of the process")
.required(true)
.index(2),
)
}