]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/daemon.3
zfs: merge openzfs/zfs@797f55ef1
[FreeBSD/FreeBSD.git] / lib / libc / gen / daemon.3
1 .\" Copyright (c) 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)daemon.3    8.1 (Berkeley) 6/9/93
29 .\"
30 .Dd February 27, 2023
31 .Dt DAEMON 3
32 .Os
33 .Sh NAME
34 .Nm daemon
35 .Nd run in the background
36 .Sh LIBRARY
37 .Lb libc
38 .Sh SYNOPSIS
39 .In stdlib.h
40 .Ft int
41 .Fn daemon "int nochdir" "int noclose"
42 .Ft int
43 .Fn daemonfd "int chdirfd" "int nullfd"
44 .Sh DESCRIPTION
45 The
46 .Fn daemon
47 function is for programs wishing to detach themselves from the
48 controlling terminal and run in the background as system daemons.
49 .Pp
50 If the argument
51 .Fa nochdir
52 is zero,
53 .Fn daemon
54 changes the current working directory to the root
55 .Pq Pa / .
56 .Pp
57 If the argument
58 .Fa noclose
59 is zero,
60 .Fn daemon
61 redirects standard input, standard output, and standard error to
62 .Pa /dev/null .
63 .Pp
64 The
65 .Fn daemonfd
66 function is equivalent to the
67 .Fn daemon
68 function except that arguments are the descriptors for the current working
69 directory and to the descriptor to
70 .Pa /dev/null .
71 .Pp
72 If
73 .Fa chdirfd
74 is equal to
75 .Pq -1
76 the current working directory is not changed.
77 .Pp
78 If
79 .Fa nullfd
80 is equals to
81 .Pq -1
82 the redirection of standard input, standard output, and standard error is not
83 closed.
84 .Sh RETURN VALUES
85 .Rv -std daemon daemonfd
86 .Sh ERRORS
87 The
88 .Fn daemon
89 and
90 .Fn daemonfd
91 function may fail and set
92 .Va errno
93 for any of the errors specified for the library functions
94 .Xr fork 2
95 .Xr open 2 ,
96 and
97 .Xr setsid 2 .
98 .Sh SEE ALSO
99 .Xr fork 2 ,
100 .Xr setsid 2 ,
101 .Xr sigaction 2
102 .Sh HISTORY
103 The
104 .Fn daemon
105 function first appeared in
106 .Bx 4.4 .
107 The
108 .Fn daemonfd
109 function first appeared in
110 .Fx 12.0 .
111 .Sh CAVEATS
112 Unless the
113 .Fa noclose
114 argument is non-zero,
115 .Fn daemon
116 will close the first three file descriptors and redirect them to
117 .Pa /dev/null .
118 Normally, these correspond to standard input, standard output, and
119 standard error.
120 However, if any of those file descriptors refer to something else, they
121 will still be closed, resulting in incorrect behavior of the calling program.
122 This can happen if any of standard input, standard output, or standard
123 error have been closed before the program was run.
124 Programs using
125 .Fn daemon
126 should therefore either call
127 .Fn daemon
128 before opening any files or sockets, or verify that any file
129 descriptors obtained have values greater than 2.
130 .Pp
131 The
132 .Fn daemon
133 function temporarily ignores
134 .Dv SIGHUP
135 while calling
136 .Xr setsid 2
137 to prevent a parent session group leader's calls to
138 .Xr fork 2
139 and then
140 .Xr _exit 2
141 from prematurely terminating the child process.