From 32bae2bcf4307ab607ef6a0e5736c23b78e8712d Mon Sep 17 00:00:00 2001 From: Etienne Cordonnier Date: Tue, 9 Dec 2025 13:37:22 +0100 Subject: [PATCH] nice: use Command::exec() instead of libc::execvp() No need to use the unsafe `libc::execvp()`, the standard rust library provides the functionality via the safe function `Command::exec()`. Signed-off-by: Etienne Cordonnier --- src/uu/nice/src/nice.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/uu/nice/src/nice.rs b/src/uu/nice/src/nice.rs index 8e47e9d07..e68931287 100644 --- a/src/uu/nice/src/nice.rs +++ b/src/uu/nice/src/nice.rs @@ -3,13 +3,14 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) getpriority execvp setpriority nstr PRIO cstrs ENOENT +// spell-checker:ignore (ToDO) getpriority setpriority nstr PRIO use clap::{Arg, ArgAction, Command}; -use libc::{PRIO_PROCESS, c_char, c_int, execvp}; -use std::ffi::{CString, OsString}; -use std::io::{Error, Write}; -use std::ptr; +use libc::PRIO_PROCESS; +use std::ffi::OsString; +use std::io::{Error, ErrorKind, Write}; +use std::os::unix::process::CommandExt; +use std::process; use uucore::translate; use uucore::{ @@ -156,21 +157,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } } - let cstrs: Vec = matches - .get_many::(options::COMMAND) - .unwrap() - .map(|x| CString::new(x.as_bytes()).unwrap()) - .collect(); + let mut cmd_iter = matches.get_many::(options::COMMAND).unwrap(); + let cmd = cmd_iter.next().unwrap(); + let args: Vec<&String> = cmd_iter.collect(); - let mut args: Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect(); - args.push(ptr::null::()); - unsafe { - execvp(args[0], args.as_mut_ptr()); - } + let err = process::Command::new(cmd).args(args).exec(); - show_error!("execvp: {}", Error::last_os_error()); + show_error!("{}: {}", cmd, err); - let exit_code = if Error::last_os_error().raw_os_error().unwrap() as c_int == libc::ENOENT { + let exit_code = if err.kind() == ErrorKind::NotFound { 127 } else { 126