ioctl_tty.2: Update DTR example
[man-pages.git] / man3 / daemon.3
blobabe9a43c234d6f34f27ddf44d08d2ac75c56ecf9
1 .\" Copyright (c) 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
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.
32 .\" %%%LICENSE_END
33 .\"
34 .\"     @(#)daemon.3    8.1 (Berkeley) 6/9/93
35 .\" Added mentioning of glibc weirdness wrt unistd.h. 5/11/98, Al Viro
36 .TH DAEMON 3 2021-03-22 "GNU" "Linux Programmer's Manual"
37 .SH NAME
38 daemon \- run in the background
39 .SH SYNOPSIS
40 .nf
41 .B #include <unistd.h>
42 .PP
43 .BI "int daemon(int " nochdir ", int " noclose );
44 .fi
45 .PP
46 .RS -4
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .RE
50 .PP
51 .BR daemon ():
52 .nf
53     Since glibc 2.21:
54 .\"             commit 266865c0e7b79d4196e2cc393693463f03c90bd8
55         _DEFAULT_SOURCE
56     In glibc 2.19 and 2.20:
57         _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
58     Up to and including glibc 2.19:
59         _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
60 .fi
61 .SH DESCRIPTION
62 The
63 .BR daemon ()
64 function is for programs wishing to detach themselves from the
65 controlling terminal and run in the background as system daemons.
66 .PP
68 .I nochdir
69 is zero,
70 .BR daemon ()
71 changes the process's current working directory
72 to the root directory ("/");
73 otherwise, the current working directory is left unchanged.
74 .PP
76 .I noclose
77 is zero,
78 .BR daemon ()
79 redirects standard input, standard output, and standard error
81 .IR /dev/null ;
82 otherwise, no changes are made to these file descriptors.
83 .SH RETURN VALUE
84 (This function forks, and if the
85 .BR fork (2)
86 succeeds, the parent calls
87 .\" not .IR in order not to underline _
88 .BR _exit (2),
89 so that further errors are seen by the child only.)
90 On success
91 .BR daemon ()
92 returns zero.
93 If an error occurs,
94 .BR daemon ()
95 returns \-1 and sets
96 .I errno
97 to any of the errors specified for the
98 .BR fork (2)
99 and
100 .BR setsid (2).
101 .SH ATTRIBUTES
102 For an explanation of the terms used in this section, see
103 .BR attributes (7).
104 .ad l
107 allbox;
108 lbx lb lb
109 l l l.
110 Interface       Attribute       Value
112 .BR daemon ()
113 T}      Thread safety   MT-Safe
117 .sp 1
118 .SH CONFORMING TO
119 Not in POSIX.1.
120 A similar function appears on the BSDs.
122 .BR daemon ()
123 function first appeared in 4.4BSD.
124 .SH NOTES
125 The glibc implementation can also return \-1 when
126 .I /dev/null
127 exists but is not a character device with the expected
128 major and minor numbers.
129 In this case,
130 .I errno
131 need not be set.
132 .SH BUGS
133 The GNU C library implementation of this function was taken from BSD,
134 and does not employ the double-fork technique (i.e.,
135 .BR fork (2),
136 .BR setsid (2),
137 .BR fork (2))
138 that is necessary to ensure that the resulting daemon process is
139 not a session leader.
140 Instead, the resulting daemon
141 .I is
142 a session leader.
143 .\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=19144
144 .\" Tested using a program that uses daemon() and then opens an
145 .\" otherwise unused console device (/dev/ttyN) that does not
146 .\" have an associated getty process.
147 On systems that follow System V semantics (e.g., Linux),
148 this means that if the daemon opens a terminal that is not
149 already a controlling terminal for another session,
150 then that terminal will inadvertently become
151 the controlling terminal for the daemon.
152 .SH SEE ALSO
153 .BR fork (2),
154 .BR setsid (2),
155 .BR daemon (7),
156 .BR logrotate (8)