try to fix daemon bug

This commit is contained in:
AlteredCoder 2020-06-18 12:32:34 +02:00
parent fc67ba91d4
commit 6799f7cb66
2 changed files with 36 additions and 46 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"syscall"
_ "net/http/pprof" _ "net/http/pprof"
"time" "time"
@ -13,6 +14,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/outputs" "github.com/crowdsecurity/crowdsec/pkg/outputs"
"github.com/crowdsecurity/crowdsec/pkg/parser" "github.com/crowdsecurity/crowdsec/pkg/parser"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/sevlyar/go-daemon"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -240,6 +242,29 @@ func main() {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
if cConfig.Daemonize {
daemon.SetSigHandler(termHandler, syscall.SIGTERM)
daemon.SetSigHandler(reloadHandler, syscall.SIGHUP)
daemon.SetSigHandler(debugHandler, syscall.SIGUSR1)
daemonCTX := &daemon.Context{
PidFileName: cConfig.PIDFolder + "/crowdsec.pid",
PidFilePerm: 0644,
WorkDir: "./",
Umask: 027,
}
d, err := daemonCTX.Reborn()
if err != nil {
log.Fatalf("unable to run daemon: %s ", err.Error())
}
if d != nil {
return
}
defer daemonCTX.Release() //nolint:errcheck // won't bother checking this error in defer statement
}
log.Infof("Crowdsec %s", cwversion.VersionStr()) log.Infof("Crowdsec %s", cwversion.VersionStr())
// Enable profiling early // Enable profiling early
@ -302,7 +327,16 @@ func main() {
acquisition.AcquisStartReading(acquisitionCTX, inputLineChan, &acquisTomb) acquisition.AcquisStartReading(acquisitionCTX, inputLineChan, &acquisTomb)
if err = serve(*OutputRunner); err != nil { if !cConfig.Daemonize {
log.Fatalf(err.Error()) if err = serveOneTimeRun(*OutputRunner); err != nil {
log.Errorf(err.Error())
} else {
return
}
} else {
err = daemon.ServeSignals()
if err != nil {
log.Fatalf("serveDaemon error : %s", err.Error())
}
} }
} }

View file

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"syscall"
"time" "time"
"github.com/crowdsecurity/crowdsec/pkg/acquisition" "github.com/crowdsecurity/crowdsec/pkg/acquisition"
@ -136,35 +135,6 @@ func termHandler(sig os.Signal) error {
return daemon.ErrStop return daemon.ErrStop
} }
func serveDaemon() error {
var daemonCTX *daemon.Context
daemon.SetSigHandler(termHandler, syscall.SIGTERM)
daemon.SetSigHandler(reloadHandler, syscall.SIGHUP)
daemon.SetSigHandler(debugHandler, syscall.SIGUSR1)
daemonCTX = &daemon.Context{
PidFileName: cConfig.PIDFolder + "/crowdsec.pid",
PidFilePerm: 0644,
WorkDir: "./",
Umask: 027,
}
d, err := daemonCTX.Reborn()
if err != nil {
return fmt.Errorf("unable to run daemon: %s ", err.Error())
}
if d != nil {
return nil
}
defer daemonCTX.Release() //nolint:errcheck // won't bother checking this error in defer statement
err = daemon.ServeSignals()
if err != nil {
return fmt.Errorf("serveDaemon error : %s", err.Error())
}
return nil
}
func serveOneTimeRun(outputRunner outputs.Output) error { func serveOneTimeRun(outputRunner outputs.Output) error {
log.Infof("waiting for acquisition to finish") log.Infof("waiting for acquisition to finish")
@ -202,17 +172,3 @@ func serveOneTimeRun(outputRunner outputs.Output) error {
log.Warningf("all routines are done, bye.") log.Warningf("all routines are done, bye.")
return nil return nil
} }
func serve(outputRunner outputs.Output) error {
var err error
if cConfig.Daemonize {
if err = serveDaemon(); err != nil {
return fmt.Errorf(err.Error())
}
} else {
if err = serveOneTimeRun(outputRunner); err != nil {
return fmt.Errorf(err.Error())
}
}
return nil
}