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

unistd: add execveat() on Linux and Android #800

Merged
merged 1 commit into from
Nov 21, 2017
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added `nix::unistd::{getgroups, setgroups, getgrouplist, initgroups}`. ([#733](https://github.com/nix-rust/nix/pull/733))
- Added `nix::sys::socket::UnixAddr::as_abstract` on Linux and Android.
([#785](https://github.com/nix-rust/nix/pull/785))
- Added `nix::unistd::execveat` on Linux and Android.
([#800](https://github.com/nix-rust/nix/pull/800))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
25 changes: 25 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,31 @@ pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result<Void> {
Err(Error::Sys(Errno::last()))
}

/// Execute program relative to a directory file descriptor (see
/// [execveat(2)](http://man7.org/linux/man-pages/man2/execveat.2.html)).
///
/// The `execveat` function allows for another process to be "called" which will
/// replace the current process image. That is, this process becomes the new
/// command that is run. On success, this function will not return. Instead,
/// the new program will run until it exits.
///
/// This function is similar to `execve`, except that the program to be executed
/// 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> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);

unsafe {
libc::syscall(libc::SYS_execveat, dirfd, pathname.as_ptr(),
args_p.as_ptr(), env_p.as_ptr(), flags);
};

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

/// Daemonize this process by detaching from the controlling terminal (see
/// [daemon(3)](http://man7.org/linux/man-pages/man3/daemon.3.html)).
///
Expand Down
25 changes: 23 additions & 2 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate tempdir;

use nix::fcntl;
use nix::unistd::*;
use nix::unistd::ForkResult::*;
use nix::sys::wait::*;
Expand Down Expand Up @@ -190,7 +191,7 @@ fn test_initgroups() {
}

macro_rules! execve_test_factory(
($test_name:ident, $syscall:ident, $exe: expr) => (
($test_name:ident, $syscall:ident, $exe: expr $(, $pathname:expr, $flags:expr)*) => (
#[test]
fn $test_name() {
#[allow(unused_variables)]
Expand All @@ -211,12 +212,14 @@ 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(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()]).unwrap();
CString::new(b"baz=quux".as_ref()).unwrap()]
$(, $flags)*).unwrap();
},
Parent { child } => {
// Wait for the child to exit.
Expand Down Expand Up @@ -252,6 +255,24 @@ cfg_if!{
}
}

cfg_if!{
if #[cfg(target_os = "android")] {
execve_test_factory!(test_execveat_empty, execveat, File::open("/system/bin/sh").unwrap().into_raw_fd(),
"", fcntl::AT_EMPTY_PATH);
execve_test_factory!(test_execveat_relative, execveat, File::open("/system/bin/").unwrap().into_raw_fd(),
"./sh", fcntl::AtFlags::empty());
execve_test_factory!(test_execveat_absolute, execveat, File::open("/").unwrap().into_raw_fd(),
"/system/bin/sh", fcntl::AtFlags::empty());
} else if #[cfg(all(target_os = "linux"), any(target_arch ="x86_64", target_arch ="x86"))] {
execve_test_factory!(test_execveat_empty, execveat, File::open("/bin/sh").unwrap().into_raw_fd(),
"", fcntl::AT_EMPTY_PATH);
execve_test_factory!(test_execveat_relative, execveat, File::open("/bin/").unwrap().into_raw_fd(),
"./sh", fcntl::AtFlags::empty());
execve_test_factory!(test_execveat_absolute, execveat, File::open("/").unwrap().into_raw_fd(),
"/bin/sh", fcntl::AtFlags::empty());
}
}

#[test]
fn test_fchdir() {
// fchdir changes the process's cwd
Expand Down