Ver código fonte

try to fix daemon bug

AlteredCoder 5 anos atrás
pai
commit
6799f7cb66
2 arquivos alterados com 36 adições e 46 exclusões
  1. 36 2
      cmd/crowdsec/main.go
  2. 0 44
      cmd/crowdsec/serve.go

+ 36 - 2
cmd/crowdsec/main.go

@@ -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 {
-		log.Fatalf(err.Error())
+	if !cConfig.Daemonize {
+		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())
+		}
 	}
 	}
 }
 }

+ 0 - 44
cmd/crowdsec/serve.go

@@ -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
-}