forked from DataDog/datadog-agent
/
service.go
121 lines (108 loc) · 3.31 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//go:build windows
// +build windows
package service
import (
"fmt"
"time"
"github.com/StackVista/stackstate-agent/cmd/agent/common/signals"
"github.com/StackVista/stackstate-agent/cmd/system-probe/app"
"github.com/StackVista/stackstate-agent/cmd/system-probe/config"
"github.com/StackVista/stackstate-agent/pkg/util/log"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
)
const (
// last digit from window_resources/system-probe-msg.mc
serviceStarted = 0x40000003
serviceStopped = 0x40000004
altServiceStarted = 0x40000007
receivedStopCommand = 0x4000000b
receivedShutdownCommand = 0x4000000c
agentShutdownStarting = 0x4000000d
// errors
serviceFailed = 0xc0000008
unexpectedControlMessage = 0xc000000a
agentStartFailure = 0xc000000e
)
var elog debug.Log
type sysprobeWindowService struct{}
func (m *sysprobeWindowService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
defer func() {
changes <- svc.Status{State: svc.Stopped}
}()
if err := app.StartSystemProbe(); err != nil {
if err == app.ErrNotEnabled {
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
return
}
log.Errorf("Failed to start system-probe %v", err)
elog.Error(agentStartFailure, err.Error())
errno = 1 // indicates non-successful return from handler.
return
}
elog.Info(serviceStarted, config.ServiceName)
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
// Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
time.Sleep(100 * time.Millisecond)
changes <- c.CurrentStatus
case svc.Stop:
log.Info("Received stop message from service control manager")
elog.Info(receivedStopCommand, config.ServiceName)
break loop
case svc.PreShutdown:
log.Info("Received pre-shutdown message from service control manager")
elog.Info(0x40000010, config.ServiceName)
break loop
case svc.Shutdown:
log.Info("Received shutdown message from service control manager")
elog.Info(receivedShutdownCommand, config.ServiceName)
break loop
default:
log.Warnf("unexpected control request #%d", c)
elog.Warning(unexpectedControlMessage, fmt.Sprint(c.Cmd))
}
case <-signals.Stopper:
elog.Info(receivedStopCommand, config.ServiceName)
break loop
}
}
elog.Info(agentShutdownStarting, config.ServiceName)
log.Infof("Initiating service shutdown")
changes <- svc.Status{State: svc.StopPending}
app.StopSystemProbe()
return
}
// RunService runs the System Probe as a Windows service
func RunService(isDebug bool) {
var err error
if isDebug {
elog = debug.New(config.ServiceName)
} else {
elog, err = eventlog.Open(config.ServiceName)
if err != nil {
return
}
}
defer elog.Close()
run := svc.Run
if isDebug {
run = debug.Run
}
elog.Info(altServiceStarted, config.ServiceName)
err = run(config.ServiceName, &sysprobeWindowService{})
if err != nil {
elog.Error(serviceFailed, err.Error())
return
}
elog.Info(serviceStopped, config.ServiceName)
}