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.Dd June 1, 2026 29.Dt DAEMON 3 30.Os 31.Sh NAME 32.Nm daemon 33.Nd run in the background 34.Sh LIBRARY 35.Lb libc 36.Sh SYNOPSIS 37.In stdlib.h 38.Ft int 39.Fn daemon "int nochdir" "int noclose" 40.Ft int 41.Fn daemonfd "int chdirfd" "int nullfd" 42.Sh DESCRIPTION 43The 44.Fn daemon 45function is for programs wishing to detach themselves from the 46controlling terminal and run in the background as system daemons. 47The 48.Xr fork 2 49system call is used; see 50.Sx CAVEATS 51below about the environment after a 52.Fn fork 53.Pq without a corresponding call to one of the exec routines . 54.Pp 55If the argument 56.Fa nochdir 57is zero, 58.Fn daemon 59changes the current working directory to the root 60.Pq Pa / . 61.Pp 62If the argument 63.Fa noclose 64is zero, 65.Fn daemon 66redirects standard input, standard output, and standard error to 67.Pa /dev/null . 68.Pp 69The 70.Fn daemonfd 71function is equivalent to the 72.Fn daemon 73function except that arguments are the descriptors for the current working 74directory and to the descriptor to 75.Pa /dev/null . 76.Pp 77If 78.Fa chdirfd 79is equal to 80.Pq -1 81the current working directory is not changed. 82.Pp 83If 84.Fa nullfd 85is equals to 86.Pq -1 87the redirection of standard input, standard output, and standard error is not 88closed. 89.Sh RETURN VALUES 90.Rv -std daemon daemonfd 91.Sh ERRORS 92The 93.Fn daemon 94and 95.Fn daemonfd 96function may fail and set 97.Va errno 98for any of the errors specified for the library functions 99.Xr fork 2 , 100.Xr open 2 , 101and 102.Xr setsid 2 . 103.Sh SEE ALSO 104.Xr fork 2 , 105.Xr setsid 2 , 106.Xr sigaction 2 107.Sh HISTORY 108The 109.Fn daemon 110function first appeared in 111.Bx 4.4 . 112The 113.Fn daemonfd 114function first appeared in 115.Fx 12.0 . 116.Sh CAVEATS 117In multithreaded programs, the child process after 118.Fn fork 119inherits copies of all mutexes and other synchronization state, but only the 120calling thread survives. 121Any locks held by other threads at the time of the call will remain permanently 122acquired in the child, causing deadlocks in any code that attempts to 123acquire them. 124Until one of the 125.Xr exec 3 126functions is called, the child should restrict itself to async-signal safe 127operations 128.Po see 129.Xr sigaction 2 130.Pc . 131.Pp 132Unless the 133.Fa noclose 134argument is non-zero, 135.Fn daemon 136will close the first three file descriptors and redirect them to 137.Pa /dev/null . 138Normally, these correspond to standard input, standard output, and 139standard error. 140However, if any of those file descriptors refer to something else, they 141will still be closed, resulting in incorrect behavior of the calling program. 142This can happen if any of standard input, standard output, or standard 143error have been closed before the program was run. 144Programs using 145.Fn daemon 146should therefore either call 147.Fn daemon 148before opening any files or sockets, or verify that any file 149descriptors obtained have values greater than 2. 150.Pp 151The 152.Fn daemon 153function temporarily ignores 154.Dv SIGHUP 155while calling 156.Xr setsid 2 157to prevent a parent session group leader's calls to 158.Xr fork 2 159and then 160.Xr _exit 2 161from prematurely terminating the child process. 162