This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows_service.go
102 lines (86 loc) · 2.5 KB
/
windows_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
// +build windows
package systemservice
import (
"fmt"
"strings"
"time"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/mgr"
// "golang.org/x/sys/windows/svc/eventlog"
)
/*
connectService connects to a Window service by name and
returns the service or an error
*/
func connectService(name string) (s *mgr.Service, err error) {
m, err := mgr.Connect()
if err != nil {
logger.Log("open manager error: ", err)
return nil, err
}
s, err = m.OpenService(name)
if err != nil {
e := err.Error()
logger.Log("open manager error: ", e)
if strings.Contains(e, "specified service does not exist") {
return nil, &ServiceDoesNotExistError{serviceName: name}
}
return nil, err
}
// defer s.Close()
return s, nil
}
/*
runScCommand makes calls to the sc.exe binary.
See this page for reference:
https://www.computerhope.com/sc-command.htm
*/
func runScCommand(args ...string) (out string, err error) {
logger.Log("running command: sc ", strings.Join(args, " "))
return runCommand("sc", args...)
}
var elog debug.Log
type windowsService struct{}
func (m *windowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
logger.Log("execute called")
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
changes <- svc.Status{State: svc.StartPending}
// fasttick := time.Tick(500 * time.Millisecond)
// slowtick := time.Tick(2 * time.Second)
// tick := fasttick
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
// logger.Log("Loop!")
select {
// case <-tick:
// beep()
// elog.Info(1, "beep")
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, svc.Shutdown:
// golang.org/x/sys/windows/svc.TestExample is verifying this output.
testOutput := strings.Join(args, "-")
testOutput += fmt.Sprintf("-%d", c.Context)
elog.Info(1, testOutput)
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:
elog.Error(1, fmt.Sprintf("unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}