Debian Codegraph @

daemon/daemon.go

raw symbols SCIP
1 // +build !windows
2
3 // Package daemon provides functions to assist with the writing of UNIX-style
4 // daemons in go.
5 package daemon
6
7 import (
8 "gopkg.in/hlandau/svcutils.v1/dupfd"
9 "gopkg.in/hlandau/svcutils.v1/exepath"
10 "os"
11 "syscall"
12 )
13
14 // Initialises a daemon with recommended values. Called by Daemonize.
15 //
16 // Currently, this only calls umask(0) and chdir("/").
17 func Init() error {
18 syscall.Umask(0)
19
20 err := syscall.Chdir("/")
21 if err != nil {
22 return err
23 }
24
25 // setrlimit RLIMIT_CORE
26 return nil
27 }
28
29 const forkedArg = "$*_FORKED_*$"
30
31 // Psuedo-forks by re-executing the current binary with a special command line
32 // argument telling it not to re-execute itself again. Returns true in the
33 // parent process and false in the child.
34 func Fork() (isParent bool, err error) {
35 if os.Args[len(os.Args)-1] == forkedArg {
36 os.Args = os.Args[0 : len(os.Args)-1]
37 return false, nil
38 }
39
40 newArgs := make([]string, 0, len(os.Args))
41 newArgs = append(newArgs, exepath.Abs)
42 newArgs = append(newArgs, os.Args[1:]...)
43 newArgs = append(newArgs, forkedArg)
44
45 // Start the child process.
46 //
47 // Pass along the standard FD for now - we'll remap them to /dev/null
48 // in due time. This ensures anything expecting these to exist isn't confused,
49 // and allows pre-daemonization failures to at least get output to somewhere.
50 proc, err := os.StartProcess(exepath.Abs, newArgs, &os.ProcAttr{
51 Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
52 })
53 if err != nil {
54 return true, err
55 }
56
57 proc.Release()
58 return true, nil
59 }
60
61 var haveStderr = true
62
63 // Returns true unless stderr has been closed (remapped to /dev/null) as part
64 // of daemonization. Can be used to determine whether logging to stderr is
65 // useful.
66 func HaveStderr() bool {
67 return haveStderr
68 }
69
70 // Daemonizes but doesn't fork.
71 //
72 // The stdin, stdout and, unless keepStderr is specified, stderr fds are
73 // remapped to /dev/null. setsid is called.
74 //
75 // The process changes its current directory to /.
76 //
77 // If you intend to call DropPrivileges, call it after calling this function,
78 // as /dev/null will no longer be available after privileges are dropped.
79 func Daemonize(keepStderr bool) error {
80 null_f, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
81 if err != nil {
82 return err
83 }
84 defer null_f.Close()
85
86 stdin_fd := int(os.Stdin.Fd())
87 stdout_fd := int(os.Stdout.Fd())
88 stderr_fd := int(os.Stderr.Fd())
89
90 // ... reopen fds 0, 1, 2 as /dev/null ...
91 // Since dup2 closes fds which are already open we needn't close the above fds.
92 // This lets us avoid race conditions.
93 null_fd := int(null_f.Fd())
94 err = dupfd.Dup2(null_fd, stdin_fd)
95 if err != nil {
96 return err
97 }
98
99 err = dupfd.Dup2(null_fd, stdout_fd)
100 if err != nil {
101 return err
102 }
103
104 if !keepStderr {
105 err = dupfd.Dup2(null_fd, stderr_fd)
106 if err != nil {
107 return err
108 }
109
110 haveStderr = false
111 }
112
113 // This may fail if we're not root
114 syscall.Setsid()
115
116 // Daemonize implies Init.
117 return Init()
118 }