man-pages/man3/daemon.3.html
2021-03-31 01:06:50 +01:00

208 lines
4.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of DAEMON</TITLE>
</HEAD><BODY>
<H1>DAEMON</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-11-26<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
daemon - run in the background
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int daemon(int </B><I>nochdir</I><B>, int </B><I>noclose</I><B>);</B>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>daemon</B>():
<PRE>
Since glibc 2.21:
_DEFAULT_SOURCE
In glibc 2.19 and 2.20:
_DEFAULT_SOURCE || (_XOPEN_SOURCE &amp;&amp; _XOPEN_SOURCE&nbsp;&lt;&nbsp;500)
Up to and including glibc 2.19:
_BSD_SOURCE || (_XOPEN_SOURCE &amp;&amp; _XOPEN_SOURCE&nbsp;&lt;&nbsp;500)
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>daemon</B>()
function is for programs wishing to detach themselves from the
controlling terminal and run in the background as system daemons.
<P>
If
<I>nochdir</I>
is zero,
<B>daemon</B>()
changes the process's current working directory
to the root directory (&quot;/&quot;);
otherwise, the current working directory is left unchanged.
<P>
If
<I>noclose</I>
is zero,
<B>daemon</B>()
redirects standard input, standard output and standard error
to
<I>/dev/null</I>;
otherwise, no changes are made to these file descriptors.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
(This function forks, and if the
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
succeeds, the parent calls
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
so that further errors are seen by the child only.)
On success
<B>daemon</B>()
returns zero.
If an error occurs,
<B>daemon</B>()
returns -1 and sets
<I>errno</I>
to any of the errors specified for the
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?2+setsid">setsid</A></B>(2).
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>daemon</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
Not in POSIX.1.
A similar function appears on the BSDs.
The
<B>daemon</B>()
function first appeared in 4.4BSD.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The glibc implementation can also return -1 when
<I>/dev/null</I>
exists but is not a character device with the expected
major and minor numbers.
In this case,
<I>errno</I>
need not be set.
<A NAME="lbAI">&nbsp;</A>
<H2>BUGS</H2>
The GNU C library implementation of this function was taken from BSD,
and does not employ the double-fork technique (i.e.,
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setsid">setsid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2))
that is necessary to ensure that the resulting daemon process is
not a session leader.
Instead, the resulting daemon
<I>is</I>
a session leader.
On systems that follow System V semantics (e.g., Linux),
this means that if the daemon opens a terminal that is not
already a controlling terminal for another session,
then that terminal will inadvertently become
the controlling terminal for the daemon.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setsid">setsid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+daemon">daemon</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?8+logrotate">logrotate</A></B>(8)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="6"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="7"><A HREF="#lbAH">NOTES</A><DD>
<DT id="8"><A HREF="#lbAI">BUGS</A><DD>
<DT id="9"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="10"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:38 GMT, March 31, 2021
</BODY>
</HTML>