-
Notifications
You must be signed in to change notification settings - Fork 1
/
windows.go
155 lines (131 loc) · 3.92 KB
/
windows.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// +build windows
package initme
import (
"errors"
"fmt"
"time"
"golang.org/x/sys/windows/svc"
)
func init() {
serviceType = WindowsService{}
}
type WindowsService struct {
Conf Config
}
func (self WindowsService) New(c Config) Service {
self.Conf = c
return self
}
func (self WindowsService) Register() (output string, err error, code int) {
args, err := self.buildScArgs("create")
if err != nil {
return
}
return execute(self.Conf.Log, "sc.exe", args...)
}
func (self WindowsService) Start() (output string, err error, code int) {
return execute(self.Conf.Log, "sc.exe", "start", self.Conf.Name)
}
func (self WindowsService) Stop() (output string, err error, code int) {
return execute(self.Conf.Log, "sc.exe", "stop", self.Conf.Name)
}
func (self WindowsService) Status() (output string, err error, code int) {
return execute(self.Conf.Log, "sc.exe", "query", self.Conf.Name)
}
func (self WindowsService) Disable() (output string, err error, code int) {
return execute(self.Conf.Log, "sc.exe", "config", self.Conf.Name, "start=", "disabled")
}
func (self WindowsService) Delete() (output string, err error, code int) {
return execute(self.Conf.Log, "sc.exe", "delete", self.Conf.Name)
}
// https://support.microsoft.com/en-us/kb/251192
func (self WindowsService) buildScArgs(init ...string) (args []string, err error) {
args = make([]string, 0)
args = append(args, init...)
if self.Conf.Name != "" {
args = append(args, self.Conf.Name)
} else {
return nil, errors.New("Name is mandatory")
}
if self.Conf.Type != "" {
args = append(args, "type=", self.Conf.Type)
}
if self.Conf.StartType != "" {
args = append(args, "start=", self.Conf.StartType)
}
if self.Conf.Error != "" {
args = append(args, "error=", self.Conf.Error)
}
if self.Conf.BinPath != "" {
args = append(args, "binpath=", self.Conf.BinPath)
} else {
return nil, errors.New("BinPath is mandatory")
}
if self.Conf.Group != "" {
args = append(args, "group=", self.Conf.Group)
}
if self.Conf.Tag != "" {
args = append(args, "tag=", self.Conf.Tag)
}
if self.Conf.Depend != "" {
args = append(args, "depend=", self.Conf.Depend)
}
if self.Conf.Obj != "" {
if self.Conf.Password != "" {
return nil, errors.New("Password is mandatory if Obj is set")
}
args = append(args, "obj=", self.Conf.Obj)
}
if self.Conf.DisplayName != "" {
args = append(args, "DisplayName=", self.Conf.DisplayName)
}
if self.Conf.Password != "" {
if self.Conf.Obj != "" {
return nil, errors.New("Password is meanful only if Obj is set")
}
args = append(args, "password=", self.Conf.Password)
}
return
}
func (self WindowsService) IsAnInteractiveSession() (bool, error) {
return svc.IsAnInteractiveSession()
}
func (self WindowsService) Run() {
svc.Run(self.Conf.Name, self)
}
func (self WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
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}
go self.Conf.Job()
loop:
for {
select {
case <-tick:
//self.eventLog.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:
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:
self.Conf.Log.Println(fmt.Sprintf("unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}