]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/daemon.3
daemon.3: Note that damon is buggy with respect to controlling tty acquisition
[thirdparty/man-pages.git] / man3 / daemon.3
CommitLineData
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
fe0fefbf 36.TH DAEMON 3 2015-03-02 "GNU" "Linux Programmer's Manual"
fea681da
MK
37.SH NAME
38daemon \- run in the background
39.SH SYNOPSIS
40.B #include <unistd.h>
41.sp
42.BI "int daemon(int " nochdir ", int " noclose );
cc4615cc
MK
43.sp
44.in -4n
45Feature Test Macro Requirements for glibc (see
46.BR feature_test_macros (7)):
47.in
48.sp
49.BR daemon ():
50_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
fea681da
MK
51.SH DESCRIPTION
52The
53.BR daemon ()
54function is for programs wishing to detach themselves from the
55controlling terminal and run in the background as system daemons.
56.PP
5a76509a 57If
fea681da 58.I nochdir
5a76509a 59is zero,
fea681da 60.BR daemon ()
51a492bd 61changes the calling process's current working directory
5a76509a 62to the root directory ("/");
51a492bd 63otherwise, the current working directory is left unchanged.
fea681da 64.PP
5a76509a 65If
fea681da 66.I noclose
5a76509a 67is zero,
fea681da 68.BR daemon ()
5a76509a 69redirects standard input, standard output and standard error
c9942389
MK
70to
71.IR /dev/null ;
5a76509a 72otherwise, no changes are made to these file descriptors.
47297adb 73.SH RETURN VALUE
fea681da 74(This function forks, and if the
fb186734 75.BR fork (2)
9fe10273 76succeeds, the parent calls
fea681da 77.\" not .IR in order not to underline _
d4fdff35 78.BR _exit (2),
fea681da 79so that further errors are seen by the child only.)
7e05b05e
MK
80On success
81.BR daemon ()
82returns zero.
fea681da
MK
83If an error occurs,
84.BR daemon ()
28d03ce9 85returns \-1 and sets
fea681da 86.I errno
db4bf819 87to any of the errors specified for the
fea681da 88.BR fork (2)
c13182ef 89and
fea681da 90.BR setsid (2).
be63524e 91.SH ATTRIBUTES
dce3395b
MK
92For an explanation of the terms used in this section, see
93.BR attributes (7).
94.TS
95allbox;
96lb lb lb
97l l l.
98Interface Attribute Value
99T{
be63524e 100.BR daemon ()
dce3395b
MK
101T} Thread safety MT-Safe
102.TE
2b2581ee 103.SH CONFORMING TO
cb85526a 104Not in POSIX.1.
2b2581ee
MK
105A similar function appears on the BSDs.
106The
107.BR daemon ()
108function first appeared in 4.4BSD.
fea681da
MK
109.SH NOTES
110The glibc implementation can also return \-1 when
111.I /dev/null
112exists but is not a character device with the expected
c13182ef 113major and minor numbers.
e787ab6d 114In this case,
fea681da
MK
115.I errno
116need not be set.
630551b9
MK
117.SH BUGS
118The GNU C library implementation of this function was taken from BSD,
119and does not employ the double-fork technique (i.e.,
120.BR fork (2),
121.BR setsid (2),
122.BR fork (2))
123that is necessary to ensure that the resulting daemon process is
124not a session leader.
125Instead, the resulting daemon
126.I is
127a session leader.
128.\" FIXME https://sourceware.org/bugzilla/show_bug.cgi?id=19144
129.\" Tested using a program that uses daemon() and then opens an
130.\" otherwise unused console device (/dev/ttyN) that does not
131.\" have an associated getty process.
132On systems that follow System V semantics (e.g., Linux),
133this means that if the daemon opens a terminal that is not
134already a controlling terminal for another session,
135then that terminal will inadvertently become
136the controlling terminal for the daemon.
47297adb 137.SH SEE ALSO
e37e3282
MK
138.BR fork (2),
139.BR setsid (2)