158f0484fSRodney W. Grimes.\" Copyright (c) 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 658f0484fSRodney W. Grimes.\" are met: 758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 858f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1058f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1158f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors 1358f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1458f0484fSRodney W. Grimes.\" without specific prior written permission. 1558f0484fSRodney W. Grimes.\" 1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2658f0484fSRodney W. Grimes.\" SUCH DAMAGE. 2758f0484fSRodney W. Grimes.\" 28*6e9b4e3eSIhor Antonov.Dd February 27, 2023 2958f0484fSRodney W. Grimes.Dt DAEMON 3 3058f0484fSRodney W. Grimes.Os 3158f0484fSRodney W. Grimes.Sh NAME 3258f0484fSRodney W. Grimes.Nm daemon 3358f0484fSRodney W. Grimes.Nd run in the background 3425bb73e0SAlexey Zelkin.Sh LIBRARY 3525bb73e0SAlexey Zelkin.Lb libc 3658f0484fSRodney W. Grimes.Sh SYNOPSIS 3732eef9aeSRuslan Ermilov.In stdlib.h 387dd9508fSGary Palmer.Ft int 3958f0484fSRodney W. Grimes.Fn daemon "int nochdir" "int noclose" 4016545cf5SMariusz Zaborski.Ft int 4116545cf5SMariusz Zaborski.Fn daemonfd "int chdirfd" "int nullfd" 4258f0484fSRodney W. Grimes.Sh DESCRIPTION 4358f0484fSRodney W. GrimesThe 4458f0484fSRodney W. Grimes.Fn daemon 4558f0484fSRodney W. Grimesfunction is for programs wishing to detach themselves from the 4658f0484fSRodney W. Grimescontrolling terminal and run in the background as system daemons. 4758f0484fSRodney W. Grimes.Pp 48*6e9b4e3eSIhor AntonovIf the argument 4958f0484fSRodney W. Grimes.Fa nochdir 50*6e9b4e3eSIhor Antonovis zero, 5158f0484fSRodney W. Grimes.Fn daemon 52971730fcSCrist J. Clarkchanges the current working directory to the root 53971730fcSCrist J. Clark.Pq Pa / . 5458f0484fSRodney W. Grimes.Pp 55*6e9b4e3eSIhor AntonovIf the argument 5658f0484fSRodney W. Grimes.Fa noclose 57*6e9b4e3eSIhor Antonovis zero, 5858f0484fSRodney W. Grimes.Fn daemon 59*6e9b4e3eSIhor Antonovredirects standard input, standard output, and standard error to 60971730fcSCrist J. Clark.Pa /dev/null . 6116545cf5SMariusz Zaborski.Pp 6216545cf5SMariusz ZaborskiThe 6316545cf5SMariusz Zaborski.Fn daemonfd 6416545cf5SMariusz Zaborskifunction is equivalent to the 6516545cf5SMariusz Zaborski.Fn daemon 6616545cf5SMariusz Zaborskifunction except that arguments are the descriptors for the current working 6716545cf5SMariusz Zaborskidirectory and to the descriptor to 6816545cf5SMariusz Zaborski.Pa /dev/null . 6916545cf5SMariusz Zaborski.Pp 7016545cf5SMariusz ZaborskiIf 7116545cf5SMariusz Zaborski.Fa chdirfd 7216545cf5SMariusz Zaborskiis equal to 7316545cf5SMariusz Zaborski.Pq -1 7416545cf5SMariusz Zaborskithe current working directory is not changed. 7516545cf5SMariusz Zaborski.Pp 7616545cf5SMariusz ZaborskiIf 7716545cf5SMariusz Zaborski.Fa nullfd 7816545cf5SMariusz Zaborskiis equals to 7916545cf5SMariusz Zaborski.Pq -1 8016545cf5SMariusz Zaborskithe redirection of standard input, standard output, and standard error is not 8116545cf5SMariusz Zaborskiclosed. 82971730fcSCrist J. Clark.Sh RETURN VALUES 8316545cf5SMariusz Zaborski.Rv -std daemon daemonfd 84971730fcSCrist J. Clark.Sh ERRORS 8589ac4eccSRuslan ErmilovThe 86971730fcSCrist J. Clark.Fn daemon 8716545cf5SMariusz Zaborskiand 8816545cf5SMariusz Zaborski.Fn daemonfd 8989ac4eccSRuslan Ermilovfunction may fail and set 9058f0484fSRodney W. Grimes.Va errno 91971730fcSCrist J. Clarkfor any of the errors specified for the library functions 92ffcdc347SGuy Helmer.Xr fork 2 9316545cf5SMariusz Zaborski.Xr open 2 , 9458f0484fSRodney W. Grimesand 9558f0484fSRodney W. Grimes.Xr setsid 2 . 9658f0484fSRodney W. Grimes.Sh SEE ALSO 977dd9508fSGary Palmer.Xr fork 2 , 988b2b22e8SGuy Helmer.Xr setsid 2 , 998b2b22e8SGuy Helmer.Xr sigaction 2 10058f0484fSRodney W. Grimes.Sh HISTORY 10158f0484fSRodney W. GrimesThe 10258f0484fSRodney W. Grimes.Fn daemon 1037bdf80e5SMike Pritchardfunction first appeared in 1047bdf80e5SMike Pritchard.Bx 4.4 . 10516545cf5SMariusz ZaborskiThe 10616545cf5SMariusz Zaborski.Fn daemonfd 10716545cf5SMariusz Zaborskifunction first appeared in 10816545cf5SMariusz Zaborski.Fx 12.0 . 109971730fcSCrist J. Clark.Sh CAVEATS 110971730fcSCrist J. ClarkUnless the 11189ac4eccSRuslan Ermilov.Fa noclose 112971730fcSCrist J. Clarkargument is non-zero, 113971730fcSCrist J. Clark.Fn daemon 114971730fcSCrist J. Clarkwill close the first three file descriptors and redirect them to 115971730fcSCrist J. Clark.Pa /dev/null . 116971730fcSCrist J. ClarkNormally, these correspond to standard input, standard output, and 117971730fcSCrist J. Clarkstandard error. 11889ac4eccSRuslan ErmilovHowever, if any of those file descriptors refer to something else, they 119971730fcSCrist J. Clarkwill still be closed, resulting in incorrect behavior of the calling program. 120971730fcSCrist J. ClarkThis can happen if any of standard input, standard output, or standard 121971730fcSCrist J. Clarkerror have been closed before the program was run. 122971730fcSCrist J. ClarkPrograms using 123971730fcSCrist J. Clark.Fn daemon 124971730fcSCrist J. Clarkshould therefore either call 125971730fcSCrist J. Clark.Fn daemon 126971730fcSCrist J. Clarkbefore opening any files or sockets, or verify that any file 127971730fcSCrist J. Clarkdescriptors obtained have values greater than 2. 1288b2b22e8SGuy Helmer.Pp 1298b2b22e8SGuy HelmerThe 1308b2b22e8SGuy Helmer.Fn daemon 1318b2b22e8SGuy Helmerfunction temporarily ignores 1328b2b22e8SGuy Helmer.Dv SIGHUP 1338b2b22e8SGuy Helmerwhile calling 1348b2b22e8SGuy Helmer.Xr setsid 2 1358b2b22e8SGuy Helmerto prevent a parent session group leader's calls to 1368b2b22e8SGuy Helmer.Xr fork 2 1378b2b22e8SGuy Helmerand then 1388b2b22e8SGuy Helmer.Xr _exit 2 1398b2b22e8SGuy Helmerfrom prematurely terminating the child process. 140