diff --git a/src/unistd.rs b/src/unistd.rs index 47057e3477..98a0c7e2c1 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -593,6 +593,33 @@ pub fn execvp(filename: &CString, args: &[CString]) -> Result { Err(Error::Sys(Errno::last())) } +/// Replace the current process image with a new one (see +/// [fexecve(3)](http://man7.org/linux/man-pages/man3/fexecve.3.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. +/// +/// If an error occurs, this function will return with an indication of the +/// cause of failure. See +/// [fexecve(3)#errors](http://man7.org/linux/man-pages/man3/fexecve.3.html#ERRORS) +/// for a list of potential problems that maight cause execv to fail. +/// +/// This function is similar to `execve`, except that the program to be executed +/// is referenced as a file descriptor instead of a path. +#[inline] +pub fn fexecve(fd: RawFd, args: &[CString], env: &[CString]) -> Result { + 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)). ///