Skip to content

Commit a81fd5b

Browse files
committed
unistd: add fexecve()
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
1 parent 087aece commit a81fd5b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1414
([#647](https://github.com/nix-rust/nix/pull/647))
1515
- Added the `pid()` method to `WaitStatus` for extracting the PID.
1616
([#722](https://github.com/nix-rust/nix/pull/722))
17+
- Added `nix::unistd:fexecve`.
18+
([#727](https://github.com/nix-rust/nix/pull/727))
1719

1820
### Changed
1921
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/unistd.rs

+29
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,35 @@ pub fn execvp(filename: &CString, args: &[CString]) -> Result<Void> {
593593
Err(Error::Sys(Errno::last()))
594594
}
595595

596+
/// Replace the current process image with a new one (see
597+
/// [fexecve(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html)).
598+
///
599+
/// The `fexecve` function allows for another process to be "called" which will
600+
/// replace the current process image. That is, this process becomes the new
601+
/// command that is run. On success, this function will not return. Instead,
602+
/// the new program will run until it exits.
603+
///
604+
/// This function is similar to `execve`, except that the program to be executed
605+
/// is referenced as a file descriptor instead of a path.
606+
///
607+
/// # Errors
608+
///
609+
/// If an error occurs, this function will return with an indication of the
610+
/// cause of failure. See
611+
/// [fexecve(2)#errors](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html#tag_16_111_05)
612+
/// for a list of error conditions.
613+
#[inline]
614+
pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result<Void> {
615+
let args_p = to_exec_array(args);
616+
let env_p = to_exec_array(env);
617+
618+
unsafe {
619+
libc::fexecve(fd, args_p.as_ptr(), env_p.as_ptr())
620+
};
621+
622+
Err(Error::Sys(Errno::last()))
623+
}
624+
596625
/// Daemonize this process by detaching from the controlling terminal (see
597626
/// [daemon(3)](http://man7.org/linux/man-pages/man3/daemon.3.html)).
598627
///

0 commit comments

Comments
 (0)