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