1- //! Standard symbolic constants and types
2- //!
1+ //! Safe wrappers around functions found in libc "unistd.h" header
2+
33use { Errno , Error , Result , NixPath } ;
44use fcntl:: { fcntl, OFlag , O_CLOEXEC , FD_CLOEXEC } ;
55use fcntl:: FcntlArg :: F_SETFD ;
@@ -344,6 +344,12 @@ fn to_exec_array(args: &[CString]) -> Vec<*const c_char> {
344344 args_p
345345}
346346
347+ /// Replace the current process image with a new one (see
348+ /// [exec(3)](http://man7.org/linux/man-pages/man3/exec.3.html)).
349+ ///
350+ /// See the `::nix::unistd::execve` system call for additional details. `execv`
351+ /// performs the same action but does not allow for customization of the
352+ /// environment for the new process.
347353#[ inline]
348354pub fn execv ( path : & CString , argv : & [ CString ] ) -> Result < Void > {
349355 let args_p = to_exec_array ( argv) ;
@@ -355,6 +361,24 @@ pub fn execv(path: &CString, argv: &[CString]) -> Result<Void> {
355361 Err ( Error :: Sys ( Errno :: last ( ) ) )
356362}
357363
364+
365+ /// Replace the current process image with a new one (see
366+ /// [execve(2)](http://man7.org/linux/man-pages/man2/execve.2.html)).
367+ ///
368+ /// The execve system call allows for another process to be "called" which will
369+ /// replace the current process image. That is, this process becomes the new
370+ /// command that is run. On success, this function will not return. Instead,
371+ /// the new program will run until it exits.
372+ ///
373+ /// If an error occurs, this function will return with an indication of the
374+ /// cause of failure. See
375+ /// [execve(2)#errors](http://man7.org/linux/man-pages/man2/execve.2.html#ERRORS)
376+ /// for a list of potential problems that maight cause execv to fail.
377+ ///
378+ /// `::nix::unistd::execv` and `::nix::unistd::execve` take as arguments a slice
379+ /// of `::std::ffi::CString`s for `args` and `env` (for `execve`). Each element
380+ /// in the `args` list is an argument to the new process. Each element in the
381+ /// `env` list should be a string in the form "key=value".
358382#[ inline]
359383pub fn execve ( path : & CString , args : & [ CString ] , env : & [ CString ] ) -> Result < Void > {
360384 let args_p = to_exec_array ( args) ;
@@ -367,6 +391,15 @@ pub fn execve(path: &CString, args: &[CString], env: &[CString]) -> Result<Void>
367391 Err ( Error :: Sys ( Errno :: last ( ) ) )
368392}
369393
394+ /// Replace the current process image with a new one and replicate shell `PATH`
395+ /// searching behavior (see
396+ /// [exec(3)](http://man7.org/linux/man-pages/man3/exec.3.html)).
397+ ///
398+ /// See `::nix::unistd::execve` for additoinal details. `execvp` behaves the
399+ /// same as execv except that it will examine the `PATH` environment variables
400+ /// for file names not specified with a leading slash. For example, `execv`
401+ /// would not work if "bash" was specified for the path argument, but `execvp`
402+ /// would assuming that a bash executable was on the system `PATH`.
370403#[ inline]
371404pub fn execvp ( filename : & CString , args : & [ CString ] ) -> Result < Void > {
372405 let args_p = to_exec_array ( args) ;
@@ -378,6 +411,37 @@ pub fn execvp(filename: &CString, args: &[CString]) -> Result<Void> {
378411 Err ( Error :: Sys ( Errno :: last ( ) ) )
379412}
380413
414+ /// Daemonize this process by detaching from the controlling terminal (see
415+ /// [daemon(3)](http://man7.org/linux/man-pages/man3/daemon.3.html)).
416+ ///
417+ /// When a process is launched it is typically associated with a parent and it,
418+ /// in turn, by its controlling terminal/process. In order for a process to run
419+ /// in the "background" it must daemonize itself by detaching itself. Under
420+ /// posix, this is done by doing the following:
421+ ///
422+ /// 1. Parent process (this one) forks
423+ /// 2. Parent process exits
424+ /// 3. Child process continues to run.
425+ ///
426+ /// `nochdir`:
427+ ///
428+ /// * `nochdir = true`: The current working directory after daemonizing will
429+ /// be the current working directory.
430+ /// * `nochdir = false`: The current working directory after daemonizing will
431+ /// be the root direcory, `/`.
432+ ///
433+ /// `noclose`:
434+ ///
435+ /// * `noclose = true`: The process' current stdin, stdout, and stderr file
436+ /// descriptors will remain identical after daemonizing.
437+ /// * `noclose = false`: The process' stdin, stdout, and stderr will point to
438+ /// `/dev/null` after daemonizing.
439+ ///
440+ /// The underlying implementation (in libc) calls both
441+ /// [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html) and
442+ /// [setsid(2)](http://man7.org/linux/man-pages/man2/setsid.2.html) and, as
443+ /// such, error that could be returned by either of those functions could also
444+ /// show up as errors here.
381445pub fn daemon ( nochdir : bool , noclose : bool ) -> Result < ( ) > {
382446 let res = unsafe { libc:: daemon ( nochdir as c_int , noclose as c_int ) } ;
383447 Errno :: result ( res) . map ( drop)
0 commit comments