@@ -411,6 +411,40 @@ pub fn execvp(filename: &CString, args: &[CString]) -> Result<Void> {
411411 Err ( Error :: Sys ( Errno :: last ( ) ) )
412412}
413413
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+ /// There are a couple options here whose names and meaning can be a bit
427+ /// confusing, so we'll describe the behavior for each state.
428+ ///
429+ /// For `nochdir`:
430+ ///
431+ /// * `nochdir = true`: The current working directory after daemonizing will
432+ /// be the current working directory.
433+ /// * `nochdir = false`: The current working directory after daemonizing will
434+ /// be the root direcory, `/`.
435+ ///
436+ /// For `noclose`:
437+ ///
438+ /// * `noclose = true`: The process' current stdin, stdout, and stderr file
439+ /// descriptors will remain identical after daemonizing.
440+ /// * `noclose = false`: The process' stdin, stdout, and stderr will point to
441+ /// `/dev/null` after daemonizing.
442+ ///
443+ /// The underlying implementation (in libc) calls both
444+ /// [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html) and
445+ /// [setsid(2)](http://man7.org/linux/man-pages/man2/setsid.2.html) and, as
446+ /// such, error that could be returned by either of those functions could also
447+ /// show up as errors here.
414448pub fn daemon ( nochdir : bool , noclose : bool ) -> Result < ( ) > {
415449 let res = unsafe { libc:: daemon ( nochdir as c_int , noclose as c_int ) } ;
416450 Errno :: result ( res) . map ( drop)
0 commit comments