1DAEMON(3) Linux Programmer's Manual DAEMON(3)
2
3
4
6 daemon - run in the background
7
9 #include <unistd.h>
10
11 int daemon(int nochdir, int noclose);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 daemon():
16 Since glibc 2.21:
17 _DEFAULT_SOURCE
18 In glibc 2.19 and 2.20:
19 _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
20 Up to and including glibc 2.19:
21 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
22
24 The daemon() function is for programs wishing to detach themselves from
25 the controlling terminal and run in the background as system daemons.
26
27 If nochdir is zero, daemon() changes the process's current working
28 directory to the root directory ("/"); otherwise, the current working
29 directory is left unchanged.
30
31 If noclose is zero, daemon() redirects standard input, standard output
32 and standard error to /dev/null; otherwise, no changes are made to
33 these file descriptors.
34
36 (This function forks, and if the fork(2) succeeds, the parent calls
37 _exit(2), so that further errors are seen by the child only.) On suc‐
38 cess daemon() returns zero. If an error occurs, daemon() returns -1
39 and sets errno to any of the errors specified for the fork(2) and set‐
40 sid(2).
41
43 For an explanation of the terms used in this section, see
44 attributes(7).
45
46 ┌──────────┬───────────────┬─────────┐
47 │Interface │ Attribute │ Value │
48 ├──────────┼───────────────┼─────────┤
49 │daemon() │ Thread safety │ MT-Safe │
50 └──────────┴───────────────┴─────────┘
52 Not in POSIX.1. A similar function appears on the BSDs. The daemon()
53 function first appeared in 4.4BSD.
54
56 The glibc implementation can also return -1 when /dev/null exists but
57 is not a character device with the expected major and minor numbers.
58 In this case, errno need not be set.
59
61 The GNU C library implementation of this function was taken from BSD,
62 and does not employ the double-fork technique (i.e., fork(2), set‐
63 sid(2), fork(2)) that is necessary to ensure that the resulting daemon
64 process is not a session leader. Instead, the resulting daemon is a
65 session leader. On systems that follow System V semantics (e.g.,
66 Linux), this means that if the daemon opens a terminal that is not
67 already a controlling terminal for another session, then that terminal
68 will inadvertently become the controlling terminal for the daemon.
69
71 fork(2), setsid(2), daemon(7), logrotate(8)
72
74 This page is part of release 5.02 of the Linux man-pages project. A
75 description of the project, information about reporting bugs, and the
76 latest version of this page, can be found at
77 https://www.kernel.org/doc/man-pages/.
78
79
80
81GNU 2017-11-26 DAEMON(3)