Skip to content

Commit 4b6b7b4

Browse files
committed
unistd: add documentation for the daemon function
Signed-off-by: Paul Osborne <osbpau@gmail.com>
1 parent c270fd2 commit 4b6b7b4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/unistd.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
414448
pub 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

Comments
 (0)