Merge pull request from cakebaker/add_renice

Add `renice`
This commit is contained in:
Sylvestre Ledru 2025-03-13 11:44:36 +01:00 committed by GitHub
commit bf18cb6f62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 118 additions and 0 deletions

10
Cargo.lock generated

@ -978,6 +978,7 @@ dependencies = [
"uu_lsmem",
"uu_mesg",
"uu_mountpoint",
"uu_renice",
"uu_rev",
"uu_setsid",
"uucore",
@ -1085,6 +1086,15 @@ dependencies = [
"uucore",
]
[[package]]
name = "uu_renice"
version = "0.0.1"
dependencies = [
"clap",
"libc",
"uucore",
]
[[package]]
name = "uu_rev"
version = "0.0.1"

@ -36,6 +36,7 @@ feat_common_core = [
"lsmem",
"mesg",
"mountpoint",
"renice",
"rev",
"setsid",
]
@ -82,6 +83,7 @@ lslocks = { optional = true, version = "0.0.1", package = "uu_lslocks", path = "
lsmem = { optional = true, version = "0.0.1", package = "uu_lsmem", path = "src/uu/lsmem" }
mesg = { optional = true, version = "0.0.1", package = "uu_mesg", path = "src/uu/mesg" }
mountpoint = { optional = true, version = "0.0.1", package = "uu_mountpoint", path = "src/uu/mountpoint" }
renice = { optional = true, version = "0.0.1", package = "uu_renice", path = "src/uu/renice" }
rev = { optional = true, version = "0.0.1", package = "uu_rev", path = "src/uu/rev" }
setsid = { optional = true, version = "0.0.1", package = "uu_setsid", path ="src/uu/setsid" }

17
src/uu/renice/Cargo.toml Normal 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

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

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

@ -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),
)
}

@ -0,0 +1,12 @@
// 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.
// spell-checker:ignore (words) symdir somefakedir
use crate::common::util::TestScenario;
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

@ -33,6 +33,10 @@ mod test_blockdev;
#[path = "by-util/test_ctrlaltdel.rs"]
mod test_ctrlaltdel;
#[cfg(feature = "renice")]
#[path = "by-util/test_renice.rs"]
mod test_renice;
#[cfg(feature = "rev")]
#[path = "by-util/test_rev.rs"]
mod test_rev;