| 1 |
'\" t |
| 2 |
.\" Copyright 1993, The Regents of the University of California. |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: BSD-4-Clause-UC |
| 6 |
.\" |
| 7 |
.TH daemon 3 2025-05-17 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
daemon \- run in the background |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <unistd.h> |
| 16 |
.P |
| 17 |
.BI "int daemon(int " nochdir ", int " noclose ); |
| 18 |
.fi |
| 19 |
.P |
| 20 |
.RS -4 |
| 21 |
Feature Test Macro Requirements for glibc (see |
| 22 |
.BR feature_test_macros (7)): |
| 23 |
.RE |
| 24 |
.P |
| 25 |
.BR daemon (): |
| 26 |
.nf |
| 27 |
Since glibc 2.21: |
| 28 |
.\" commit 266865c0e7b79d4196e2cc393693463f03c90bd8 |
| 29 |
_DEFAULT_SOURCE |
| 30 |
In glibc 2.19 and 2.20: |
| 31 |
_DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) |
| 32 |
Up to and including glibc 2.19: |
| 33 |
_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) |
| 34 |
.fi |
| 35 |
.SH DESCRIPTION |
| 36 |
The |
| 37 |
.BR daemon () |
| 38 |
function is for programs wishing to detach themselves from the |
| 39 |
controlling terminal and run in the background as system daemons. |
| 40 |
.P |
| 41 |
If |
| 42 |
.I nochdir |
| 43 |
is zero, |
| 44 |
.BR daemon () |
| 45 |
changes the process's current working directory |
| 46 |
to the root directory ("/"); |
| 47 |
otherwise, the current working directory is left unchanged. |
| 48 |
.P |
| 49 |
If |
| 50 |
.I noclose |
| 51 |
is zero, |
| 52 |
.BR daemon () |
| 53 |
redirects standard input, standard output, and standard error |
| 54 |
to |
| 55 |
.IR /dev/null ; |
| 56 |
otherwise, no changes are made to these file descriptors. |
| 57 |
.SH RETURN VALUE |
| 58 |
(This function forks, and if the |
| 59 |
.BR fork (2) |
| 60 |
succeeds, the parent calls |
| 61 |
.\" not .IR in order not to underline _ |
| 62 |
.BR _exit (2), |
| 63 |
so that further errors are seen by the child only.) |
| 64 |
On success |
| 65 |
.BR daemon () |
| 66 |
returns zero. |
| 67 |
If an error occurs, |
| 68 |
.BR daemon () |
| 69 |
returns \-1 and sets |
| 70 |
.I errno |
| 71 |
to any of the errors specified for the |
| 72 |
.BR fork (2) |
| 73 |
and |
| 74 |
.BR setsid (2). |
| 75 |
.SH ATTRIBUTES |
| 76 |
For an explanation of the terms used in this section, see |
| 77 |
.BR attributes (7). |
| 78 |
.TS |
| 79 |
allbox; |
| 80 |
lbx lb lb |
| 81 |
l l l. |
| 82 |
Interface Attribute Value |
| 83 |
T{ |
| 84 |
.na |
| 85 |
.nh |
| 86 |
.BR daemon () |
| 87 |
T} Thread safety MT-Safe |
| 88 |
.TE |
| 89 |
.SH VERSIONS |
| 90 |
A similar function appears on the BSDs. |
| 91 |
.P |
| 92 |
The glibc implementation can also return \-1 when |
| 93 |
.I /dev/null |
| 94 |
exists but is not a character device with the expected |
| 95 |
major and minor numbers. |
| 96 |
In this case, |
| 97 |
.I errno |
| 98 |
need not be set. |
| 99 |
.SH STANDARDS |
| 100 |
None. |
| 101 |
.SH HISTORY |
| 102 |
4.4BSD. |
| 103 |
.SH BUGS |
| 104 |
The GNU C library implementation of this function was taken from BSD, |
| 105 |
and does not employ the double-fork technique (i.e., |
| 106 |
.BR fork (2), |
| 107 |
.BR setsid (2), |
| 108 |
.BR fork (2)) |
| 109 |
that is necessary to ensure that the resulting daemon process is |
| 110 |
not a session leader. |
| 111 |
Instead, the resulting daemon |
| 112 |
.I is |
| 113 |
a session leader. |
| 114 |
.\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=19144 |
| 115 |
.\" Tested using a program that uses daemon() and then opens an |
| 116 |
.\" otherwise unused console device (/dev/ttyN) that does not |
| 117 |
.\" have an associated getty process. |
| 118 |
On systems that follow System V semantics (e.g., Linux), |
| 119 |
this means that if the daemon opens a terminal that is not |
| 120 |
already a controlling terminal for another session, |
| 121 |
then that terminal will inadvertently become |
| 122 |
the controlling terminal for the daemon. |
| 123 |
.SH SEE ALSO |
| 124 |
.BR fork (2), |
| 125 |
.BR setsid (2), |
| 126 |
.BR daemon (7), |
| 127 |
.BR logrotate (8) |