Blame man3/daemon.3

Packit 7cfc04
.\" Copyright (c) 1993
Packit 7cfc04
.\"	The Regents of the University of California.  All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"	This product includes software developed by the University of
Packit 7cfc04
.\"	California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"	@(#)daemon.3	8.1 (Berkeley) 6/9/93
Packit 7cfc04
.\" Added mentioning of glibc weirdness wrt unistd.h. 5/11/98, Al Viro
Packit 7cfc04
.TH DAEMON 3 2017-11-26 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
daemon \- run in the background
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int daemon(int " nochdir ", int " noclose );
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR daemon ():
Packit 7cfc04
.nf
Packit 7cfc04
    Since glibc 2.21:
Packit 7cfc04
.\"             commit 266865c0e7b79d4196e2cc393693463f03c90bd8
Packit 7cfc04
        _DEFAULT_SOURCE
Packit 7cfc04
    In glibc 2.19 and 2.20:
Packit 7cfc04
        _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
Packit 7cfc04
    Up to and including glibc 2.19:
Packit 7cfc04
        _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
function is for programs wishing to detach themselves from the
Packit 7cfc04
controlling terminal and run in the background as system daemons.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I nochdir
Packit 7cfc04
is zero,
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
changes the process's current working directory
Packit 7cfc04
to the root directory ("/");
Packit 7cfc04
otherwise, the current working directory is left unchanged.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I noclose
Packit 7cfc04
is zero,
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
redirects standard input, standard output and standard error
Packit 7cfc04
to
Packit 7cfc04
.IR /dev/null ;
Packit 7cfc04
otherwise, no changes are made to these file descriptors.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
(This function forks, and if the
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
succeeds, the parent calls
Packit 7cfc04
.\" not .IR in order not to underline _
Packit 7cfc04
.BR _exit (2),
Packit 7cfc04
so that further errors are seen by the child only.)
Packit 7cfc04
On success
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
returns zero.
Packit 7cfc04
If an error occurs,
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
returns \-1 and sets
Packit 7cfc04
.I errno
Packit 7cfc04
to any of the errors specified for the
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
and
Packit 7cfc04
.BR setsid (2).
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
Not in POSIX.1.
Packit 7cfc04
A similar function appears on the BSDs.
Packit 7cfc04
The
Packit 7cfc04
.BR daemon ()
Packit 7cfc04
function first appeared in 4.4BSD.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
The glibc implementation can also return \-1 when
Packit 7cfc04
.I /dev/null
Packit 7cfc04
exists but is not a character device with the expected
Packit 7cfc04
major and minor numbers.
Packit 7cfc04
In this case,
Packit 7cfc04
.I errno
Packit 7cfc04
need not be set.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The GNU C library implementation of this function was taken from BSD,
Packit 7cfc04
and does not employ the double-fork technique (i.e.,
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
.BR setsid (2),
Packit 7cfc04
.BR fork (2))
Packit 7cfc04
that is necessary to ensure that the resulting daemon process is
Packit 7cfc04
not a session leader.
Packit 7cfc04
Instead, the resulting daemon
Packit 7cfc04
.I is
Packit 7cfc04
a session leader.
Packit 7cfc04
.\" FIXME . https://sourceware.org/bugzilla/show_bug.cgi?id=19144
Packit 7cfc04
.\" Tested using a program that uses daemon() and then opens an
Packit 7cfc04
.\" otherwise unused console device (/dev/ttyN) that does not
Packit 7cfc04
.\" have an associated getty process.
Packit 7cfc04
On systems that follow System V semantics (e.g., Linux),
Packit 7cfc04
this means that if the daemon opens a terminal that is not
Packit 7cfc04
already a controlling terminal for another session,
Packit 7cfc04
then that terminal will inadvertently become
Packit 7cfc04
the controlling terminal for the daemon.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
.BR setsid (2),
Packit 7cfc04
.BR daemon (7),
Packit 7cfc04
.BR logrotate (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.