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.\" 2858f0484fSRodney W. Grimes.\" @(#)daemon.3 8.1 (Berkeley) 6/9/93 297f3dea24SPeter Wemm.\" $FreeBSD$ 30fbc400a6SNik Clayton.\" 31*16545cf5SMariusz Zaborski.Dd December 23, 2017 3258f0484fSRodney W. Grimes.Dt DAEMON 3 3358f0484fSRodney W. Grimes.Os 3458f0484fSRodney W. Grimes.Sh NAME 3558f0484fSRodney W. Grimes.Nm daemon 3658f0484fSRodney W. Grimes.Nd run in the background 3725bb73e0SAlexey Zelkin.Sh LIBRARY 3825bb73e0SAlexey Zelkin.Lb libc 3958f0484fSRodney W. Grimes.Sh SYNOPSIS 4032eef9aeSRuslan Ermilov.In stdlib.h 417dd9508fSGary Palmer.Ft int 4258f0484fSRodney W. Grimes.Fn daemon "int nochdir" "int noclose" 43*16545cf5SMariusz Zaborski.Ft int 44*16545cf5SMariusz Zaborski.Fn daemonfd "int chdirfd" "int nullfd" 4558f0484fSRodney W. Grimes.Sh DESCRIPTION 4658f0484fSRodney W. GrimesThe 4758f0484fSRodney W. Grimes.Fn daemon 4858f0484fSRodney W. Grimesfunction is for programs wishing to detach themselves from the 4958f0484fSRodney W. Grimescontrolling terminal and run in the background as system daemons. 5058f0484fSRodney W. Grimes.Pp 5158f0484fSRodney W. GrimesUnless the argument 5258f0484fSRodney W. Grimes.Fa nochdir 5358f0484fSRodney W. Grimesis non-zero, 5458f0484fSRodney W. Grimes.Fn daemon 55971730fcSCrist J. Clarkchanges the current working directory to the root 56971730fcSCrist J. Clark.Pq Pa / . 5758f0484fSRodney W. Grimes.Pp 5858f0484fSRodney W. GrimesUnless the argument 5958f0484fSRodney W. Grimes.Fa noclose 6058f0484fSRodney W. Grimesis non-zero, 6158f0484fSRodney W. Grimes.Fn daemon 62971730fcSCrist J. Clarkwill redirect standard input, standard output, and standard error to 63971730fcSCrist J. Clark.Pa /dev/null . 64*16545cf5SMariusz Zaborski.Pp 65*16545cf5SMariusz ZaborskiThe 66*16545cf5SMariusz Zaborski.Fn daemonfd 67*16545cf5SMariusz Zaborskifunction is equivalent to the 68*16545cf5SMariusz Zaborski.Fn daemon 69*16545cf5SMariusz Zaborskifunction except that arguments are the descriptors for the current working 70*16545cf5SMariusz Zaborskidirectory and to the descriptor to 71*16545cf5SMariusz Zaborski.Pa /dev/null . 72*16545cf5SMariusz Zaborski.Pp 73*16545cf5SMariusz ZaborskiIf 74*16545cf5SMariusz Zaborski.Fa chdirfd 75*16545cf5SMariusz Zaborskiis equal to 76*16545cf5SMariusz Zaborski.Pq -1 77*16545cf5SMariusz Zaborskithe current working directory is not changed. 78*16545cf5SMariusz Zaborski.Pp 79*16545cf5SMariusz ZaborskiIf 80*16545cf5SMariusz Zaborski.Fa nullfd 81*16545cf5SMariusz Zaborskiis equals to 82*16545cf5SMariusz Zaborski.Pq -1 83*16545cf5SMariusz Zaborskithe redirection of standard input, standard output, and standard error is not 84*16545cf5SMariusz Zaborskiclosed. 85971730fcSCrist J. Clark.Sh RETURN VALUES 86*16545cf5SMariusz Zaborski.Rv -std daemon daemonfd 87971730fcSCrist J. Clark.Sh ERRORS 8889ac4eccSRuslan ErmilovThe 89971730fcSCrist J. Clark.Fn daemon 90*16545cf5SMariusz Zaborskiand 91*16545cf5SMariusz Zaborski.Fn daemonfd 9289ac4eccSRuslan Ermilovfunction may fail and set 9358f0484fSRodney W. Grimes.Va errno 94971730fcSCrist J. Clarkfor any of the errors specified for the library functions 95ffcdc347SGuy Helmer.Xr fork 2 96*16545cf5SMariusz Zaborski.Xr open 2, 9758f0484fSRodney W. Grimesand 9858f0484fSRodney W. Grimes.Xr setsid 2 . 9958f0484fSRodney W. Grimes.Sh SEE ALSO 1007dd9508fSGary Palmer.Xr fork 2 , 1018b2b22e8SGuy Helmer.Xr setsid 2 , 1028b2b22e8SGuy Helmer.Xr sigaction 2 10358f0484fSRodney W. Grimes.Sh HISTORY 10458f0484fSRodney W. GrimesThe 10558f0484fSRodney W. Grimes.Fn daemon 1067bdf80e5SMike Pritchardfunction first appeared in 1077bdf80e5SMike Pritchard.Bx 4.4 . 108*16545cf5SMariusz ZaborskiThe 109*16545cf5SMariusz Zaborski.Fn daemonfd 110*16545cf5SMariusz Zaborskifunction first appeared in 111*16545cf5SMariusz Zaborski.Fx 12.0 . 112971730fcSCrist J. Clark.Sh CAVEATS 113971730fcSCrist J. ClarkUnless the 11489ac4eccSRuslan Ermilov.Fa noclose 115971730fcSCrist J. Clarkargument is non-zero, 116971730fcSCrist J. Clark.Fn daemon 117971730fcSCrist J. Clarkwill close the first three file descriptors and redirect them to 118971730fcSCrist J. Clark.Pa /dev/null . 119971730fcSCrist J. ClarkNormally, these correspond to standard input, standard output, and 120971730fcSCrist J. Clarkstandard error. 12189ac4eccSRuslan ErmilovHowever, if any of those file descriptors refer to something else, they 122971730fcSCrist J. Clarkwill still be closed, resulting in incorrect behavior of the calling program. 123971730fcSCrist J. ClarkThis can happen if any of standard input, standard output, or standard 124971730fcSCrist J. Clarkerror have been closed before the program was run. 125971730fcSCrist J. ClarkPrograms using 126971730fcSCrist J. Clark.Fn daemon 127971730fcSCrist J. Clarkshould therefore either call 128971730fcSCrist J. Clark.Fn daemon 129971730fcSCrist J. Clarkbefore opening any files or sockets, or verify that any file 130971730fcSCrist J. Clarkdescriptors obtained have values greater than 2. 1318b2b22e8SGuy Helmer.Pp 1328b2b22e8SGuy HelmerThe 1338b2b22e8SGuy Helmer.Fn daemon 1348b2b22e8SGuy Helmerfunction temporarily ignores 1358b2b22e8SGuy Helmer.Dv SIGHUP 1368b2b22e8SGuy Helmerwhile calling 1378b2b22e8SGuy Helmer.Xr setsid 2 1388b2b22e8SGuy Helmerto prevent a parent session group leader's calls to 1398b2b22e8SGuy Helmer.Xr fork 2 1408b2b22e8SGuy Helmerand then 1418b2b22e8SGuy Helmer.Xr _exit 2 1428b2b22e8SGuy Helmerfrom prematurely terminating the child process. 143