]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/daemon.3
Many pages: Use correct letter case in page titles (TH)
[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.\"
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
10daemon \- run in the background
b813014f
AC
11.SH LIBRARY
12Standard 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
22Feature 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
37The
38.BR daemon ()
39function is for programs wishing to detach themselves from the
40controlling terminal and run in the background as system daemons.
41.PP
5a76509a 42If
fea681da 43.I nochdir
5a76509a 44is zero,
fea681da 45.BR daemon ()
614e5d13 46changes the process's current working directory
5a76509a 47to the root directory ("/");
51a492bd 48otherwise, the current working directory is left unchanged.
fea681da 49.PP
5a76509a 50If
fea681da 51.I noclose
5a76509a 52is zero,
fea681da 53.BR daemon ()
3d81c05a 54redirects standard input, standard output, and standard error
c9942389
MK
55to
56.IR /dev/null ;
5a76509a 57otherwise, 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 61succeeds, the parent calls
fea681da 62.\" not .IR in order not to underline _
d4fdff35 63.BR _exit (2),
fea681da 64so that further errors are seen by the child only.)
7e05b05e
MK
65On success
66.BR daemon ()
67returns zero.
fea681da
MK
68If an error occurs,
69.BR daemon ()
28d03ce9 70returns \-1 and sets
fea681da 71.I errno
db4bf819 72to any of the errors specified for the
fea681da 73.BR fork (2)
c13182ef 74and
fea681da 75.BR setsid (2).
be63524e 76.SH ATTRIBUTES
dce3395b
MK
77For 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
82allbox;
c466875e 83lbx lb lb
dce3395b
MK
84l l l.
85Interface Attribute Value
86T{
be63524e 87.BR daemon ()
dce3395b
MK
88T} Thread safety MT-Safe
89.TE
c466875e
MK
90.hy
91.ad
92.sp 1
3113c7f3 93.SH STANDARDS
cb85526a 94Not in POSIX.1.
2b2581ee
MK
95A similar function appears on the BSDs.
96The
97.BR daemon ()
98function first appeared in 4.4BSD.
fea681da
MK
99.SH NOTES
100The glibc implementation can also return \-1 when
101.I /dev/null
102exists but is not a character device with the expected
c13182ef 103major and minor numbers.
e787ab6d 104In this case,
fea681da
MK
105.I errno
106need not be set.
630551b9
MK
107.SH BUGS
108The GNU C library implementation of this function was taken from BSD,
109and does not employ the double-fork technique (i.e.,
110.BR fork (2),
111.BR setsid (2),
112.BR fork (2))
113that is necessary to ensure that the resulting daemon process is
114not a session leader.
115Instead, the resulting daemon
116.I is
117a 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.
122On systems that follow System V semantics (e.g., Linux),
123this means that if the daemon opens a terminal that is not
124already a controlling terminal for another session,
125then that terminal will inadvertently become
126the 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)