Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make execvpe available for all platforms #349

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ build = "build.rs"

[features]
eventfd = []
execvpe = []
preadv_pwritev = []
signalfd = []

Expand Down
45 changes: 26 additions & 19 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,37 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint {
unsafe { libc::sleep(seconds) }
}

pub fn execvpe(filename: &CString, args: &[CString], env: &[CString]) -> Result<()> {
use std::env;
use std::ffi::OsString;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;

if filename.as_bytes().iter().find(|c| **c == b'/').is_some() {
return execve(filename, args, env);
}

let paths = match env::var_os("PATH") {
Some(val) => val,
None => OsString::from("/usr/local/bin:/bin:/usr/bin"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this default specified somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the same value as musl libc.
If you want to stick to the POSIX standard, it is only "/bin/:usr/bin" according to confstr(3).

};

let name = OsStr::from_bytes(&filename.as_bytes());
let mut res = Err(Error::Sys(Errno::ENOENT));
for path in env::split_paths(&paths) {
let p = path.with_file_name(name);
let p2 = &CString::new(p.as_os_str().as_bytes()).unwrap();
res = execve(p2, args, env);
}

return res;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
use sys::syscall::{syscall, SYSPIVOTROOT};
use {Errno, Result, NixPath};

#[cfg(feature = "execvpe")]
use std::ffi::CString;

pub fn pivot_root<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
new_root: &P1, put_old: &P2) -> Result<()> {
Expand All @@ -394,22 +418,5 @@ mod linux {
Errno::result(res).map(drop)
}

#[inline]
#[cfg(feature = "execvpe")]
pub fn execvpe(filename: &CString, args: &[CString], env: &[CString]) -> Result<()> {
use std::ptr;
use libc::c_char;

let mut args_p: Vec<*const c_char> = args.iter().map(|s| s.as_ptr()).collect();
args_p.push(ptr::null());

let mut env_p: Vec<*const c_char> = env.iter().map(|s| s.as_ptr()).collect();
env_p.push(ptr::null());

unsafe {
super::ffi::execvpe(filename.as_ptr(), args_p.as_ptr(), env_p.as_ptr())
};

Err(Error::Sys(Errno::last()))
}
}