Skip to content

Commit 91ce3c1

Browse files
committed
nice: Remove unsafe and reduce bin size
1 parent ac72aa8 commit 91ce3c1

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

.vscode/cspell.dictionaries/workspace.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ advapi32-sys
88
aho-corasick
99
backtrace
1010
blake2b_simd
11+
rustix
1112

1213
# * uutils project
1314
uutils

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ rstest = "0.26.0"
435435
rstest_reuse = "0.7.0"
436436
rustc-hash = "2.1.1"
437437
rust-ini = "0.21.0"
438+
rustix = "1.1.4"
438439
same-file = "1.0.6"
439440
self_cell = "1.0.4"
440441
selinux = "=0.6.0"

src/uu/nice/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ path = "src/nice.rs"
2020

2121
[dependencies]
2222
clap = { workspace = true }
23-
libc = { workspace = true }
2423
uucore = { workspace = true }
2524
fluent = { workspace = true }
2625

2726
[target.'cfg(unix)'.dependencies]
28-
nix = { workspace = true }
27+
rustix = { workspace = true, features = ["process"] }
2928

3029
[[bin]]
3130
name = "nice"

src/uu/nice/src/nice.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
// spell-checker:ignore (ToDO) getpriority setpriority nstr PRIO
77

88
use clap::{Arg, ArgAction, Command};
9-
use libc::PRIO_PROCESS;
109
use std::ffi::OsString;
11-
use std::io::{Error, ErrorKind, Write, stdout};
10+
use std::io::{ErrorKind, Write, stdout};
1211
use std::num::IntErrorKind;
1312
use std::os::unix::process::CommandExt;
1413
use std::process;
@@ -110,14 +109,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
110109
let matches =
111110
uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 125)?;
112111

113-
nix::errno::Errno::clear();
114-
let mut niceness = unsafe { libc::getpriority(PRIO_PROCESS, 0) };
115-
if Error::last_os_error().raw_os_error().unwrap() != 0 {
116-
return Err(USimpleError::new(
117-
125,
118-
format!("getpriority: {}", Error::last_os_error()),
119-
));
120-
}
112+
let mut niceness = match rustix::process::getpriority_process(None) {
113+
Ok(p) => p,
114+
Err(e) => {
115+
return Err(USimpleError::new(125, format!("getpriority: {e}")));
116+
}
117+
};
121118

122119
let adjustment = if let Some(nstr) = matches.get_one::<String>(options::ADJUSTMENT) {
123120
if !matches.contains_id(options::COMMAND) {
@@ -152,8 +149,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
152149
// isn't writable. The GNU test suite checks specifically that the
153150
// exit code when failing to write the advisory is 125, but Rust
154151
// will produce an exit code of 101 when it panics.
155-
if unsafe { libc::setpriority(PRIO_PROCESS, 0, niceness) } == -1 {
156-
let warning_msg = translate!("nice-warning-setpriority", "util_name" => uucore::util_name(), "error" => Error::last_os_error());
152+
if let Err(e) = rustix::process::setpriority_process(None, niceness) {
153+
let warning_msg = translate!("nice-warning-setpriority", "util_name" => "nice", "error" => e.to_string() );
157154

158155
if write!(std::io::stderr(), "{warning_msg}").is_err() {
159156
set_exit_code(125);
@@ -179,13 +176,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
179176
}
180177

181178
pub fn uu_app() -> Command {
182-
Command::new(uucore::util_name())
179+
Command::new("nice")
183180
.about(translate!("nice-about"))
184181
.override_usage(format_usage(&translate!("nice-usage")))
185182
.trailing_var_arg(true)
186183
.infer_long_args(true)
187184
.version(uucore::crate_version!())
188-
.help_template(uucore::localized_help_template(uucore::util_name()))
185+
.help_template(uucore::localized_help_template("nice"))
189186
.arg(
190187
Arg::new(options::ADJUSTMENT)
191188
.short('n')

0 commit comments

Comments
 (0)