Skip to content

Commit

Permalink
fix #1093
Browse files Browse the repository at this point in the history
  • Loading branch information
tathanhdinh committed Sep 14, 2019
1 parent 1662466 commit d08fe65
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
`self` by value.
([#1107](https://github.com/nix-rust/nix/pull/1107))

- Type `&CString` for parameters of `exec(v|ve|vp|vpe|veat)` are changed to `&CStr`.
([#1121](https://github.com/nix-rust/nix/pull/1121))

### Fixed
- Fix length of abstract socket addresses
([#1120](https://github.com/nix-rust/nix/pull/1120))
Expand Down
23 changes: 11 additions & 12 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fcntl::FcntlArg::F_SETFD;
use libc::{self, c_char, c_void, c_int, c_long, c_uint, size_t, pid_t, off_t,
uid_t, gid_t, mode_t};
use std::{fmt, mem, ptr};
use std::ffi::{CString, CStr, OsString, OsStr};
use std::ffi::{CStr, OsString, OsStr};
use std::os::unix::ffi::{OsStringExt, OsStrExt};
use std::os::unix::io::RawFd;
use std::path::PathBuf;
Expand Down Expand Up @@ -663,10 +663,9 @@ pub fn fchownat<P: ?Sized + NixPath>(
Errno::result(res).map(drop)
}

fn to_exec_array(args: &[CString]) -> Vec<*const c_char> {
let mut args_p: Vec<*const c_char> = args.iter().map(|s| s.as_ptr()).collect();
args_p.push(ptr::null());
args_p
fn to_exec_array(args: &[&CStr]) -> Vec<*const c_char> {
use std::iter::once;
args.iter().map(|s| s.as_ptr()).chain(once(ptr::null())).collect()
}

/// Replace the current process image with a new one (see
Expand All @@ -676,7 +675,7 @@ fn to_exec_array(args: &[CString]) -> Vec<*const c_char> {
/// performs the same action but does not allow for customization of the
/// environment for the new process.
#[inline]
pub fn execv(path: &CString, argv: &[CString]) -> Result<Void> {
pub fn execv(path: &CStr, argv: &[&CStr]) -> Result<Void> {
let args_p = to_exec_array(argv);

unsafe {
Expand All @@ -700,7 +699,7 @@ pub fn execv(path: &CString, argv: &[CString]) -> Result<Void> {
/// in the `args` list is an argument to the new process. Each element in the
/// `env` list should be a string in the form "key=value".
#[inline]
pub fn execve(path: &CString, args: &[CString], env: &[CString]) -> Result<Void> {
pub fn execve(path: &CStr, args: &[&CStr], env: &[&CStr]) -> Result<Void> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);

Expand All @@ -721,7 +720,7 @@ pub fn execve(path: &CString, args: &[CString], env: &[CString]) -> Result<Void>
/// would not work if "bash" was specified for the path argument, but `execvp`
/// would assuming that a bash executable was on the system `PATH`.
#[inline]
pub fn execvp(filename: &CString, args: &[CString]) -> Result<Void> {
pub fn execvp(filename: &CStr, args: &[&CStr]) -> Result<Void> {
let args_p = to_exec_array(args);

unsafe {
Expand All @@ -741,7 +740,7 @@ pub fn execvp(filename: &CString, args: &[CString]) -> Result<Void> {
#[cfg(any(target_os = "haiku",
target_os = "linux",
target_os = "openbsd"))]
pub fn execvpe(filename: &CString, args: &[CString], env: &[CString]) -> Result<Void> {
pub fn execvpe(filename: &CStr, args: &[&CStr], env: &[&CStr]) -> Result<Void> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);

Expand Down Expand Up @@ -769,7 +768,7 @@ pub fn execvpe(filename: &CString, args: &[CString], env: &[CString]) -> Result<
target_os = "linux",
target_os = "freebsd"))]
#[inline]
pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result<Void> {
pub fn fexecve(fd: RawFd, args: &[&CStr], env: &[&CStr]) -> Result<Void> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);

Expand All @@ -792,8 +791,8 @@ pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result<Void> {
/// is referenced as a file descriptor to the base directory plus a path.
#[cfg(any(target_os = "android", target_os = "linux"))]
#[inline]
pub fn execveat(dirfd: RawFd, pathname: &CString, args: &[CString],
env: &[CString], flags: super::fcntl::AtFlags) -> Result<Void> {
pub fn execveat(dirfd: RawFd, pathname: &CStr, args: &[&CStr],
env: &[&CStr], flags: super::fcntl::AtFlags) -> Result<Void> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);

Expand Down
18 changes: 9 additions & 9 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ macro_rules! execve_test_factory(
// exec!
$syscall(
$exe,
$(&CString::new($pathname).unwrap(), )*
&[CString::new(b"".as_ref()).unwrap(),
CString::new(b"-c".as_ref()).unwrap(),
$(CString::new($pathname).unwrap().as_c_str(), )*
&[CString::new(b"".as_ref()).unwrap().as_c_str(),
CString::new(b"-c".as_ref()).unwrap().as_c_str(),
CString::new(b"echo nix!!! && echo foo=$foo && echo baz=$baz"
.as_ref()).unwrap()],
&[CString::new(b"foo=bar".as_ref()).unwrap(),
CString::new(b"baz=quux".as_ref()).unwrap()]
.as_ref()).unwrap().as_c_str()],
&[CString::new(b"foo=bar".as_ref()).unwrap().as_c_str(),
CString::new(b"baz=quux".as_ref()).unwrap().as_c_str()]
$(, $flags)*).unwrap();
},
Parent { child } => {
Expand All @@ -229,18 +229,18 @@ macro_rules! execve_test_factory(

cfg_if!{
if #[cfg(target_os = "android")] {
execve_test_factory!(test_execve, execve, &CString::new("/system/bin/sh").unwrap());
execve_test_factory!(test_execve, execve, CString::new("/system/bin/sh").unwrap().as_c_str());
execve_test_factory!(test_fexecve, fexecve, File::open("/system/bin/sh").unwrap().into_raw_fd());
} else if #[cfg(any(target_os = "freebsd",
target_os = "linux"))] {
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str());
execve_test_factory!(test_fexecve, fexecve, File::open("/bin/sh").unwrap().into_raw_fd());
} else if #[cfg(any(target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))] {
execve_test_factory!(test_execve, execve, &CString::new("/bin/sh").unwrap());
execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str());
// No fexecve() on DragonFly, ios, macos, NetBSD, OpenBSD.
//
// Note for NetBSD and OpenBSD: although rust-lang/libc includes it
Expand Down

0 comments on commit d08fe65

Please sign in to comment.