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 di‐
28 rectory to the root directory ("/"); otherwise, the current working di‐
29 rectory 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 at‐
44 tributes(7).
45
46 ┌────────────────────────────────────────────┬───────────────┬─────────┐
47 │Interface │ Attribute │ Value │
48 ├────────────────────────────────────────────┼───────────────┼─────────┤
49 │daemon() │ Thread safety │ MT-Safe │
50 └────────────────────────────────────────────┴───────────────┴─────────┘
51
53 Not in POSIX.1. A similar function appears on the BSDs. The daemon()
54 function first appeared in 4.4BSD.
55
57 The glibc implementation can also return -1 when /dev/null exists but
58 is not a character device with the expected major and minor numbers.
59 In this case, errno need not be set.
60
62 The GNU C library implementation of this function was taken from BSD,
63 and does not employ the double-fork technique (i.e., fork(2), set‐
64 sid(2), fork(2)) that is necessary to ensure that the resulting daemon
65 process is not a session leader. Instead, the resulting daemon is a
66 session leader. On systems that follow System V semantics (e.g., Lin‐
67 ux), this means that if the daemon opens a terminal that is not already
68 a controlling terminal for another session, then that terminal will in‐
69 advertently become the controlling terminal for the daemon.
70
72 fork(2), setsid(2), daemon(7), logrotate(8)
73
75 This page is part of release 5.13 of the Linux man-pages project. A
76 description of the project, information about reporting bugs, and the
77 latest version of this page, can be found at
78 https://www.kernel.org/doc/man-pages/.
79
80
81
82GNU 2021-03-22 DAEMON(3)