MCPcopy Create free account
hub / github.com/dim-an/cod / daemonMain

Function daemonMain

daemon.go:35–73  ·  view source on GitHub ↗

https://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux/17955149#17955149

(foreground bool)

Source from the content-addressed store, hash-verified

33
34// https://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux/17955149#17955149
35func daemonMain(foreground bool) {
36 configuration, err := server.DefaultConfiguration()
37 verifyFatal(err)
38 if foreground {
39 daemonProc(configuration, 0)
40 }
41
42 logDir := configuration.GetLogDir()
43 err = util.CreateDirIfNotExists(logDir)
44 verifyFatal(err)
45
46 runDir := configuration.GetRunDir()
47 err = util.CreateDirIfNotExists(runDir)
48 verifyFatal(err)
49
50 executable, err := os.Executable()
51 verifyFatal(err)
52
53 sigqueue := make(chan os.Signal, 1)
54 signal.Notify(sigqueue, unix.SIGUSR1)
55 go func() {
56 time.Sleep(time.Second * 2)
57 sigqueue <- unix.SIGUSR2
58 }()
59
60 pid := os.Getpid()
61 cmdArgs := []string{executable, "api", "forked-daemon", strconv.Itoa(pid)}
62 cmd := exec.Command(executable, cmdArgs[1:]...)
63 err = cmd.Start()
64 if err != nil {
65 err = fmt.Errorf("command failed %q: %w", shells.Quote(cmdArgs), err)
66 fatal(err)
67 }
68
69 s := <-sigqueue
70 if s != unix.SIGUSR1 {
71 panic(fmt.Errorf("could not wait for daemon"))
72 }
73}
74
75func cleanFile(file string) (err error) {
76 err = os.Remove(file)

Callers 1

mainFunction · 0.85

Calls 8

DefaultConfigurationFunction · 0.92
CreateDirIfNotExistsFunction · 0.92
QuoteFunction · 0.92
verifyFatalFunction · 0.85
daemonProcFunction · 0.85
GetLogDirMethod · 0.80
GetRunDirMethod · 0.80
fatalFunction · 0.70

Tested by

no test coverage detected