/
main_daemon.go
95 lines (77 loc) · 2.11 KB
/
main_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
95
package main
import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"github.com/lxc/incus/internal/server/sys"
"github.com/lxc/incus/shared/logger"
)
type cmdDaemon struct {
global *cmdGlobal
// Common options
flagGroup string
}
func (c *cmdDaemon) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "incusd"
cmd.Short = "The Incus daemon"
cmd.Long = `Description:
The Incus daemon
This is the incus daemon command line. It's typically started directly by your
init system and interacted with through a tool like ` + "`incus`" + `.
`
cmd.RunE = c.Run
cmd.Flags().StringVar(&c.flagGroup, "group", "", "The group of users that will be allowed to talk to Incus"+"``")
return cmd
}
func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
if len(args) > 1 || (len(args) == 1 && args[0] != "daemon" && args[0] != "") {
return fmt.Errorf("unknown command \"%s\" for \"%s\"", args[0], cmd.CommandPath())
}
// Only root should run this
if os.Geteuid() != 0 {
return fmt.Errorf("This must be run as root")
}
neededPrograms := []string{"ip", "rsync", "setfattr", "tar", "unsquashfs", "xz"}
for _, p := range neededPrograms {
_, err := exec.LookPath(p)
if err != nil {
return err
}
}
defer logger.Info("Daemon stopped")
conf := defaultDaemonConfig()
conf.Group = c.flagGroup
conf.Trace = c.global.flagLogTrace
d := newDaemon(conf, sys.DefaultOS())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, unix.SIGPWR)
signal.Notify(sigCh, unix.SIGINT)
signal.Notify(sigCh, unix.SIGQUIT)
signal.Notify(sigCh, unix.SIGTERM)
chIgnore := make(chan os.Signal, 1)
signal.Notify(chIgnore, unix.SIGHUP)
err := d.Init()
if err != nil {
return err
}
for {
select {
case sig := <-sigCh:
logger.Info("Received signal", logger.Ctx{"signal": sig})
if d.shutdownCtx.Err() != nil {
logger.Warn("Ignoring signal, shutdown already in progress", logger.Ctx{"signal": sig})
} else {
go func() {
d.shutdownDoneCh <- d.Stop(context.Background(), sig)
}()
}
case err = <-d.shutdownDoneCh:
return err
}
}
}