]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
a9cd9cb7 | 4 | .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB) |
fea681da MK |
5 | .\" Redistribution and use in source and binary forms, with or without |
6 | .\" modification, are permitted provided that the following conditions | |
7 | .\" are met: | |
8 | .\" 1. Redistributions of source code must retain the above copyright | |
9 | .\" notice, this list of conditions and the following disclaimer. | |
10 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
11 | .\" notice, this list of conditions and the following disclaimer in the | |
12 | .\" documentation and/or other materials provided with the distribution. | |
13 | .\" 3. All advertising materials mentioning features or use of this software | |
14 | .\" must display the following acknowledgement: | |
15 | .\" This product includes software developed by the University of | |
16 | .\" California, Berkeley and its contributors. | |
17 | .\" 4. Neither the name of the University nor the names of its contributors | |
18 | .\" may be used to endorse or promote products derived from this software | |
19 | .\" without specific prior written permission. | |
20 | .\" | |
21 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | .\" SUCH DAMAGE. | |
8c9302dc | 32 | .\" %%%LICENSE_END |
fea681da MK |
33 | .\" |
34 | .\" @(#)daemon.3 8.1 (Berkeley) 6/9/93 | |
35 | .\" Added mentioning of glibc weirdness wrt unistd.h. 5/11/98, Al Viro | |
1c6f59c2 | 36 | .TH DAEMON 3 2017-11-26 "GNU" "Linux Programmer's Manual" |
fea681da MK |
37 | .SH NAME |
38 | daemon \- run in the background | |
39 | .SH SYNOPSIS | |
40 | .B #include <unistd.h> | |
68e4db0a | 41 | .PP |
fea681da | 42 | .BI "int daemon(int " nochdir ", int " noclose ); |
68e4db0a | 43 | .PP |
cc4615cc MK |
44 | .in -4n |
45 | Feature Test Macro Requirements for glibc (see | |
46 | .BR feature_test_macros (7)): | |
47 | .in | |
68e4db0a | 48 | .PP |
cc4615cc | 49 | .BR daemon (): |
2c767761 | 50 | .nf |
d0544534 MK |
51 | Since glibc 2.21: |
52 | .\" commit 266865c0e7b79d4196e2cc393693463f03c90bd8 | |
53 | _DEFAULT_SOURCE | |
54 | In glibc 2.19 and 2.20: | |
55 | _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500) | |
56 | Up to and including glibc 2.19: | |
57 | _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500) | |
58 | .fi | |
fea681da MK |
59 | .SH DESCRIPTION |
60 | The | |
61 | .BR daemon () | |
62 | function is for programs wishing to detach themselves from the | |
63 | controlling terminal and run in the background as system daemons. | |
64 | .PP | |
5a76509a | 65 | If |
fea681da | 66 | .I nochdir |
5a76509a | 67 | is zero, |
fea681da | 68 | .BR daemon () |
614e5d13 | 69 | changes the process's current working directory |
5a76509a | 70 | to the root directory ("/"); |
51a492bd | 71 | otherwise, the current working directory is left unchanged. |
fea681da | 72 | .PP |
5a76509a | 73 | If |
fea681da | 74 | .I noclose |
5a76509a | 75 | is zero, |
fea681da | 76 | .BR daemon () |
5a76509a | 77 | redirects standard input, standard output and standard error |
c9942389 MK |
78 | to |
79 | .IR /dev/null ; | |
5a76509a | 80 | otherwise, no changes are made to these file descriptors. |
47297adb | 81 | .SH RETURN VALUE |
fea681da | 82 | (This function forks, and if the |
fb186734 | 83 | .BR fork (2) |
9fe10273 | 84 | succeeds, the parent calls |
fea681da | 85 | .\" not .IR in order not to underline _ |
d4fdff35 | 86 | .BR _exit (2), |
fea681da | 87 | so that further errors are seen by the child only.) |
7e05b05e MK |
88 | On success |
89 | .BR daemon () | |
90 | returns zero. | |
fea681da MK |
91 | If an error occurs, |
92 | .BR daemon () | |
28d03ce9 | 93 | returns \-1 and sets |
fea681da | 94 | .I errno |
db4bf819 | 95 | to any of the errors specified for the |
fea681da | 96 | .BR fork (2) |
c13182ef | 97 | and |
fea681da | 98 | .BR setsid (2). |
be63524e | 99 | .SH ATTRIBUTES |
dce3395b MK |
100 | For an explanation of the terms used in this section, see |
101 | .BR attributes (7). | |
102 | .TS | |
103 | allbox; | |
104 | lb lb lb | |
105 | l l l. | |
106 | Interface Attribute Value | |
107 | T{ | |
be63524e | 108 | .BR daemon () |
dce3395b MK |
109 | T} Thread safety MT-Safe |
110 | .TE | |
2b2581ee | 111 | .SH CONFORMING TO |
cb85526a | 112 | Not in POSIX.1. |
2b2581ee MK |
113 | A similar function appears on the BSDs. |
114 | The | |
115 | .BR daemon () | |
116 | function first appeared in 4.4BSD. | |
fea681da MK |
117 | .SH NOTES |
118 | The glibc implementation can also return \-1 when | |
119 | .I /dev/null | |
120 | exists but is not a character device with the expected | |
c13182ef | 121 | major and minor numbers. |
e787ab6d | 122 | In this case, |
fea681da MK |
123 | .I errno |
124 | need not be set. | |
630551b9 MK |
125 | .SH BUGS |
126 | The GNU C library implementation of this function was taken from BSD, | |
127 | and does not employ the double-fork technique (i.e., | |
128 | .BR fork (2), | |
129 | .BR setsid (2), | |
130 | .BR fork (2)) | |
131 | that is necessary to ensure that the resulting daemon process is | |
132 | not a session leader. | |
133 | Instead, the resulting daemon | |
134 | .I is | |
135 | a session leader. | |
dbbdefa5 | 136 | .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=19144 |
630551b9 MK |
137 | .\" Tested using a program that uses daemon() and then opens an |
138 | .\" otherwise unused console device (/dev/ttyN) that does not | |
139 | .\" have an associated getty process. | |
140 | On systems that follow System V semantics (e.g., Linux), | |
141 | this means that if the daemon opens a terminal that is not | |
142 | already a controlling terminal for another session, | |
143 | then that terminal will inadvertently become | |
144 | the controlling terminal for the daemon. | |
47297adb | 145 | .SH SEE ALSO |
e37e3282 | 146 | .BR fork (2), |
8f7a89b0 | 147 | .BR setsid (2), |
940bdd7e | 148 | .BR daemon (7), |
8f7a89b0 | 149 | .BR logrotate (8) |