xref: /freebsd/lib/libc/gen/daemon.3 (revision 2008043f386721d58158e37e0d7e50df8095942d)
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
45The
46.Fn daemon
47function is for programs wishing to detach themselves from the
48controlling terminal and run in the background as system daemons.
49.Pp
50If the argument
51.Fa nochdir
52is zero,
53.Fn daemon
54changes the current working directory to the root
55.Pq Pa / .
56.Pp
57If the argument
58.Fa noclose
59is zero,
60.Fn daemon
61redirects standard input, standard output, and standard error to
62.Pa /dev/null .
63.Pp
64The
65.Fn daemonfd
66function is equivalent to the
67.Fn daemon
68function except that arguments are the descriptors for the current working
69directory and to the descriptor to
70.Pa /dev/null .
71.Pp
72If
73.Fa chdirfd
74is equal to
75.Pq -1
76the current working directory is not changed.
77.Pp
78If
79.Fa nullfd
80is equals to
81.Pq -1
82the redirection of standard input, standard output, and standard error is not
83closed.
84.Sh RETURN VALUES
85.Rv -std daemon daemonfd
86.Sh ERRORS
87The
88.Fn daemon
89and
90.Fn daemonfd
91function may fail and set
92.Va errno
93for any of the errors specified for the library functions
94.Xr fork 2
95.Xr open 2 ,
96and
97.Xr setsid 2 .
98.Sh SEE ALSO
99.Xr fork 2 ,
100.Xr setsid 2 ,
101.Xr sigaction 2
102.Sh HISTORY
103The
104.Fn daemon
105function first appeared in
106.Bx 4.4 .
107The
108.Fn daemonfd
109function first appeared in
110.Fx 12.0 .
111.Sh CAVEATS
112Unless the
113.Fa noclose
114argument is non-zero,
115.Fn daemon
116will close the first three file descriptors and redirect them to
117.Pa /dev/null .
118Normally, these correspond to standard input, standard output, and
119standard error.
120However, if any of those file descriptors refer to something else, they
121will still be closed, resulting in incorrect behavior of the calling program.
122This can happen if any of standard input, standard output, or standard
123error have been closed before the program was run.
124Programs using
125.Fn daemon
126should therefore either call
127.Fn daemon
128before opening any files or sockets, or verify that any file
129descriptors obtained have values greater than 2.
130.Pp
131The
132.Fn daemon
133function temporarily ignores
134.Dv SIGHUP
135while calling
136.Xr setsid 2
137to prevent a parent session group leader's calls to
138.Xr fork 2
139and then
140.Xr _exit 2
141from prematurely terminating the child process.
142