8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / man / man3c / daemon.3c
blobf0b473e2ecf299ee9f930b9d0fb88c1803e920fd
1 '\" te
2 .\" Copyright (c) 2009, Sun Microsystems Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
4 .\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
5 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH DAEMON 3C "Sep 15, 2009"
7 .SH NAME
8 daemon \- basic daemonization function
9 .SH SYNOPSIS
10 .LP
11 .nf
12 #include <stdlib.h>
14 \fBint\fR \fBdaemon\fR(\fBint\fR \fInochdir\fR, \fBint\fR \fInoclose\fR);
15 .fi
17 .SH DESCRIPTION
18 .sp
19 .LP
20 The \fBdaemon()\fR function provides a means for applications to run in the
21 background.
22 .sp
23 .LP
24 This function ensures that the process calling this function:
25 .RS +4
26 .TP
27 .ie t \(bu
28 .el o
29 runs in the background
30 .RE
31 .RS +4
32 .TP
33 .ie t \(bu
34 .el o
35 detaches from the controlling terminal
36 .RE
37 .RS +4
38 .TP
39 .ie t \(bu
40 .el o
41 forms a new process group
42 .RE
43 .RS +4
44 .TP
45 .ie t \(bu
46 .el o
47 is not a session group leader.
48 .RE
49 .sp
50 .LP
51 The arguments to this function are treated as boolean variables and are
52 evaluated using negative logic.
53 .sp
54 .LP
55 If the \fInochdir\fR argument is zero the working directory will be changed to
56 the root directory (/); otherwise it will not be.
57 .sp
58 .LP
59 If the \fInoclose\fR argument is zero the descriptors 0, 1, and 2 (normally
60 corresponding to standard input, output and error output, depending on the
61 application) will be redirected to \fB/dev/null\fR; otherwise they will not be.
62 .SH RETURN VALUES
63 .sp
64 .LP
65 Upon successful completion, \fBdaemon()\fR returns 0. Otherwise it returns -1
66 and sets \fBerrno\fR to the values specified for \fBfork\fR(2),
67 \fBsetsid\fR(2), \fBopen\fR(2), and \fBdup\fR(2).
68 .sp
69 .LP
70 If \fBdaemon()\fR is called with \fInoclose\fR set to 0 and fails to redirect
71 descriptors 0, 1, and 2 to \fB/dev/null\fR, those descriptors are not
72 guaranteed to be the same as before the call.
73 .SH EXAMPLES
74 .LP
75 \fBExample 1 \fRUsing daemon to run a process in the background.
76 .sp
77 .LP
78 The \fBmain()\fR function of a network server could look like this:
80 .sp
81 .in +2
82 .nf
83 int background; /* background flag */
85 /* Load and verify the configuration. */
87 /* Go into background. */
88 if (background && daemon(0, 0) < 0)
89         err(1, "daemon");
91 /* Process requests here. */
92 .fi
93 .in -2
95 .SH ATTRIBUTES
96 .sp
97 .LP
98 See \fBattributes\fR(5) for descriptions of the following attributes:
99 .sp
103 box;
104 c | c
105 l | l .
106 ATTRIBUTE TYPE  ATTRIBUTE VALUE
108 Interface Stability     Committed
110 MT-Level        Async-Signal-Safe
113 .SH SEE ALSO
116 \fBIntro\fR(2), \fBdup\fR(2), \fBfork\fR(2), \fBopen\fR(2), \fBsetsid\fR(2),
117 \fBattributes\fR(5)