Skip to content

Commit 92d7792

Browse files
authored
Merge pull request #9613 from Ecordonnier/eco/nohup-command-exec
nohup: use Command::exec() instead of libc::execvp()
2 parents 2feb6b9 + 10bdc1f commit 92d7792

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/uu/nohup/src/nohup.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (ToDO) execvp SIGHUP cproc vprocmgr cstrs homeout
6+
// spell-checker:ignore (ToDO) SIGHUP cproc vprocmgr homeout
77

88
use clap::{Arg, ArgAction, Command};
9-
use libc::{SIG_IGN, SIGHUP};
10-
use libc::{c_char, dup2, execvp, signal};
9+
use libc::{SIG_IGN, SIGHUP, dup2, signal};
1110
use std::env;
12-
use std::ffi::CString;
1311
use std::fs::{File, OpenOptions};
14-
use std::io::{Error, IsTerminal};
12+
use std::io::{Error, ErrorKind, IsTerminal};
1513
use std::os::unix::prelude::*;
14+
use std::os::unix::process::CommandExt;
1615
use std::path::{Path, PathBuf};
16+
use std::process;
1717
use thiserror::Error;
1818
use uucore::display::Quotable;
1919
use uucore::error::{UError, UResult, set_exit_code};
@@ -68,17 +68,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6868
return Err(NohupError::CannotDetach.into());
6969
}
7070

71-
let cstrs: Vec<CString> = matches
72-
.get_many::<String>(options::CMD)
73-
.unwrap()
74-
.map(|x| CString::new(x.as_bytes()).unwrap())
75-
.collect();
76-
let mut args: Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect();
77-
args.push(std::ptr::null());
78-
79-
let ret = unsafe { execvp(args[0], args.as_mut_ptr()) };
80-
match ret {
81-
libc::ENOENT => set_exit_code(EXIT_ENOENT),
71+
let mut cmd_iter = matches.get_many::<String>(options::CMD).unwrap();
72+
let cmd = cmd_iter.next().unwrap();
73+
let args: Vec<&String> = cmd_iter.collect();
74+
75+
let err = process::Command::new(cmd).args(args).exec();
76+
77+
match err.kind() {
78+
ErrorKind::NotFound => set_exit_code(EXIT_ENOENT),
8279
_ => set_exit_code(EXIT_CANNOT_INVOKE),
8380
}
8481
Ok(())

0 commit comments

Comments
 (0)