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