Debian Codegraph @

upstream/debian-trixie/man3/daemon.3

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