Properly handle service shutdown on windows (#1662)

This commit is contained in:
blotus 2022-07-13 11:54:12 +02:00 committed by GitHub
parent 5c8e2a8510
commit 8decbe7670
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 42 deletions

View file

@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"runtime"
"sort" "sort"
"strings" "strings"
@ -265,6 +266,11 @@ func LoadConfig(cConfig *csconfig.Config) error {
log.Warn("Deprecation warning: the pid_dir config can be safely removed and is not required") log.Warn("Deprecation warning: the pid_dir config can be safely removed and is not required")
} }
if cConfig.Common.Daemonize && runtime.GOOS == "windows" {
log.Debug("Daemonization is not supported on Windows, disabling")
cConfig.Common.Daemonize = false
}
return nil return nil
} }

View file

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwversion" "github.com/crowdsecurity/crowdsec/pkg/cwversion"
@ -10,7 +9,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc"
) )
@ -60,14 +58,6 @@ func WindowsRun() error {
err error err error
) )
log.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr
Writer: os.Stderr,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
},
})
cConfig, err = csconfig.NewConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI) cConfig, err = csconfig.NewConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI)
if err != nil { if err != nil {
return err return err

View file

@ -304,10 +304,10 @@ func Serve(cConfig *csconfig.Config, apiReady chan bool, agentReady chan bool) e
select { select {
case <-apiTomb.Dead(): case <-apiTomb.Dead():
log.Infof("api shutdown") log.Infof("api shutdown")
os.Exit(0) return nil
case <-crowdsecTomb.Dead(): case <-crowdsecTomb.Dead():
log.Infof("crowdsec shutdown") log.Infof("crowdsec shutdown")
os.Exit(0) return nil
} }
} }
} }

View file

@ -22,43 +22,41 @@ type crowdsec_winservice struct {
} }
func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending} changes <- svc.Status{State: svc.StartPending}
fasttick := time.Tick(500 * time.Millisecond) tick := time.Tick(500 * time.Millisecond)
slowtick := time.Tick(2 * time.Second)
tick := fasttick
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
go WindowsRun()
loop: go func() {
for { loop:
select { for {
case <-tick: select {
case <-tick:
case c := <-r: case c := <-r:
switch c.Cmd { switch c.Cmd {
case svc.Interrogate: case svc.Interrogate:
changes <- c.CurrentStatus changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown: case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending} changes <- svc.Status{State: svc.StopPending}
err := shutdown(nil, m.config) err := shutdown(nil, m.config)
if err != nil { if err != nil {
log.Errorf("Error while shutting down: %s", err) log.Errorf("Error while shutting down: %s", err)
//don't return, we still want to notify windows that we are stopped ? //don't return, we still want to notify windows that we are stopped ?
}
break loop
default:
log.Errorf("unexpected control request #%d", c)
} }
break loop
case svc.Pause:
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
tick = slowtick
case svc.Continue:
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
tick = fasttick
default:
log.Errorf("unexpected control request #%d", c)
} }
} }
} }()
err := WindowsRun()
changes <- svc.Status{State: svc.Stopped} changes <- svc.Status{State: svc.Stopped}
if err != nil {
log.Fatalf(err.Error())
}
return return
} }

View file

@ -1,5 +1,5 @@
common: common:
daemonize: true daemonize: false
log_media: file log_media: file
log_level: info log_level: info
log_dir: C:\ProgramData\CrowdSec\log\ log_dir: C:\ProgramData\CrowdSec\log\