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