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"
"fmt"
"os"
"runtime"
"sort"
"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")
}
if cConfig.Common.Daemonize && runtime.GOOS == "windows" {
log.Debug("Daemonization is not supported on Windows, disabling")
cConfig.Common.Daemonize = false
}
return nil
}

View file

@ -2,7 +2,6 @@ package main
import (
"fmt"
"os"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
@ -10,7 +9,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"golang.org/x/sys/windows/svc"
)
@ -60,14 +58,6 @@ func WindowsRun() 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)
if err != nil {
return err

View file

@ -304,10 +304,10 @@ func Serve(cConfig *csconfig.Config, apiReady chan bool, agentReady chan bool) e
select {
case <-apiTomb.Dead():
log.Infof("api shutdown")
os.Exit(0)
return nil
case <-crowdsecTomb.Dead():
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) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
fasttick := time.Tick(500 * time.Millisecond)
slowtick := time.Tick(2 * time.Second)
tick := fasttick
tick := time.Tick(500 * time.Millisecond)
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
go WindowsRun()
loop:
for {
select {
case <-tick:
go func() {
loop:
for {
select {
case <-tick:
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending}
err := shutdown(nil, m.config)
if err != nil {
log.Errorf("Error while shutting down: %s", err)
//don't return, we still want to notify windows that we are stopped ?
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending}
err := shutdown(nil, m.config)
if err != nil {
log.Errorf("Error while shutting down: %s", err)
//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}
if err != nil {
log.Fatalf(err.Error())
}
return
}

View file

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