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