Skip to content

Commit

Permalink
unistd: add fexecve()
Browse files Browse the repository at this point in the history
This adds fexecve() to `nix::unistd`. It is available in libc since 0.2.29.

Ref: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html
  • Loading branch information
lucab committed Aug 11, 2017
1 parent 087aece commit a81fd5b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#647](https://github.com/nix-rust/nix/pull/647))
- Added the `pid()` method to `WaitStatus` for extracting the PID.
([#722](https://github.com/nix-rust/nix/pull/722))
- Added `nix::unistd:fexecve`.
([#727](https://github.com/nix-rust/nix/pull/727))

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

/// Replace the current process image with a new one (see
/// [fexecve(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html)).
///
/// The `fexecve` 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 instead of a path.
///
/// # Errors
///
/// If an error occurs, this function will return with an indication of the
/// cause of failure. See
/// [fexecve(2)#errors](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html#tag_16_111_05)
/// for a list of error conditions.
#[inline]
pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result<Void> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);

unsafe {
libc::fexecve(fd, args_p.as_ptr(), env_p.as_ptr())
};

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

0 comments on commit a81fd5b

Please sign in to comment.