Properly handle service shutdown on windows (#1662)
This commit is contained in:
parent
5c8e2a8510
commit
8decbe7670
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,15 +22,13 @@ 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() {
|
||||||
|
loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tick:
|
case <-tick:
|
||||||
|
@ -47,18 +45,18 @@ loop:
|
||||||
//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
|
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:
|
default:
|
||||||
log.Errorf("unexpected control request #%d", c)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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\
|
||||||
|
|
Loading…
Reference in a new issue