add renice

This commit is contained in:
Sylvestre Ledru 2024-01-16 19:22:43 +01:00
parent 95b28d96da
commit 3dcdc952d5
2 changed files with 47 additions and 0 deletions
src/uu/renice

13
src/uu/renice/Cargo.toml Normal file

@ -0,0 +1,13 @@
[package]
name = "uu_renice"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = { workspace = true }
[[bin]]
name = "renice"
path = "src/renice.rs"

@ -0,0 +1,34 @@
use std::env;
use std::process;
use std::str::FromStr;
use libc::{c_char, c_int, execvp, PRIO_PROCESS};
use std::io::{Error, Write};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
eprintln!("Usage: renice <nice value> <pid>");
process::exit(1);
}
let nice_value = i32::from_str(&args[1]).unwrap_or_else(|_| {
eprintln!("Invalid nice value");
process::exit(1);
});
let pid = i32::from_str(&args[2]).unwrap_or_else(|_| {
eprintln!("Invalid PID");
process::exit(1);
});
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);
}