1daemon(3C)               Standard C Library Functions               daemon(3C)
2
3
4

NAME

6       daemon - basic daemonization function
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int daemon(int nochdir, int noclose);
12
13

DESCRIPTION

15       The  daemon()  function provides a means for applications to run in the
16       background.
17
18
19       This function ensures that the process calling this function:
20
21           o      runs in the background
22
23           o      detaches from the controlling terminal
24
25           o      forms a new process group
26
27           o      is not a session group leader.
28
29
30       The arguments to this function are treated as boolean variables and are
31       evaluated using negative logic.
32
33
34       If  the  nochdir argument is zero the working directory will be changed
35       to the root directory (/); otherwise it will not be.
36
37
38       If the noclose argument is zero the descriptors 0, 1, and  2  (normally
39       corresponding  to standard input, output and error output, depending on
40       the application) will be redirected to /dev/null; otherwise  they  will
41       not be.
42

RETURN VALUES

44       Upon successful completion, daemon() returns 0. Otherwise it returns -1
45       and sets errno to the values specified for fork(2), setsid(2), open(2),
46       and dup(2).
47
48
49       If  daemon()  is  called  with  noclose  set to 0 and fails to redirect
50       descriptors 0, 1, and 2 to /dev/null, those descriptors are not guaran‐
51       teed to be the same as before the call.
52

EXAMPLES

54       Example 1 Using daemon to run a process in the background.
55
56
57       The main() function of a network server could look like this:
58
59
60         int background;     /* background flag */
61
62         /* Load and verify the configuration. */
63
64         /* Go into background. */
65         if (background && daemon(0, 0) < 0)
66                 err(1, "daemon");
67
68         /* Process requests here. */
69
70

ATTRIBUTES

72       See attributes(5) for descriptions of the following attributes:
73
74
75
76
77       ┌─────────────────────────────┬─────────────────────────────┐
78       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
79       ├─────────────────────────────┼─────────────────────────────┤
80       │Interface Stability          │Committed                    │
81       ├─────────────────────────────┼─────────────────────────────┤
82       │MT-Level                     │Async-Signal-Safe            │
83       └─────────────────────────────┴─────────────────────────────┘
84

SEE ALSO

86       Intro(2), dup(2), fork(2), open(2), setsid(2), attributes(5)
87
88
89
90SunOS 5.11                        15 Sep 2009                       daemon(3C)
Impressum