-
Notifications
You must be signed in to change notification settings - Fork 3
/
daemon.go
94 lines (86 loc) · 2.49 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package daemon
import (
"errors"
"os"
"github.com/adrg/xdg"
godaemon "github.com/sevlyar/go-daemon"
"github.com/peterldowns/localias/pkg/config"
"github.com/peterldowns/localias/pkg/server"
)
// Start will fork a daemon process that loops until it receives
// sigint/sigterm/sigabrt.
// Start will fork and start the daemon process, which will start the caddy
// server and the mdns server (if needed) to proxy routes based on the current
// configuration. It also starts an RPC server that will handle requests to
// reload the server with a new configuration or stop the servers and exit the
// daemon.
func Start(cfg *config.Config) error {
if err := Kill(); err != nil {
return err
}
instance := &server.Server{Config: cfg}
if err := instance.Start(); err != nil {
return err
}
cntxt := daemonContext()
proc, err := cntxt.Reborn()
if err != nil {
return err
}
// parent process, after fork succeeds just exit
if proc != nil {
return nil
}
// child (daemon) process, after fork succeeds. the caddy/mdns servers are
// started and already running; just need to run until
// sigint/sigterm/sigabrt. waiting on the channel is cheaper than
// spinlocking and more correct, too.
server.WaitForExitSignal()
return cntxt.Release()
}
// Status will determine whether or not the daemon process is running. If it is,
// it returns the non-nil os.Process of that daemon.
func Status() (*os.Process, error) {
cntxt := daemonContext()
proc, err := cntxt.Search()
if err != nil && !isIgnorableError(err) {
return nil, err
}
return proc, nil
}
// Kill force-kills the daemon if it's running, otherwise does nothing.
func Kill() error {
proc, err := daemonContext().Search()
if err != nil && !isIgnorableError(err) {
return err
}
if proc != nil {
return proc.Kill()
}
return nil
}
// If the pidfile for the daemon doesn't exist, cntxt.Search() throws a
// PathError. In that case, we assume the daemon is not running, and return nil.
func isIgnorableError(err error) bool {
var pathError *os.PathError
if errors.As(err, &pathError) {
return true
}
if err.Error() == "EOF" {
return true
}
return false
}
// daemonContext returns a consistent go-daemon context that is used to control
// the daemon process.
func daemonContext() *godaemon.Context {
logFile, _ := xdg.StateFile("localias/daemon.log")
pidFile, _ := xdg.StateFile("localias/daemon.pid")
return &godaemon.Context{
LogFileName: logFile,
LogFilePerm: 0o644,
PidFileName: pidFile,
PidFilePerm: 0o644,
Umask: 0o27,
}
}