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