-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_windows.go
More file actions
205 lines (178 loc) · 4.69 KB
/
service_windows.go
File metadata and controls
205 lines (178 loc) · 4.69 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//go:build windows
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
const serviceName = "RetroSync"
const serviceDisplayName = "RetroSync Sync Service"
const serviceDesc = "Retro gaming save-file synchronisation daemon"
// windowsService implements svc.Handler.
type windowsService struct {
run func() error
stop func()
}
// Execute is called by the SCM in a dedicated goroutine and blocks until stop.
func (ws *windowsService) Execute(args []string, req <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
status <- svc.Status{State: svc.StartPending}
if err := ws.run(); err != nil {
log.Printf("service start error: %v", err)
return false, 1
}
status <- svc.Status{
State: svc.Running,
Accepts: svc.AcceptStop | svc.AcceptShutdown,
}
for c := range req {
switch c.Cmd {
case svc.Stop, svc.Shutdown:
status <- svc.Status{State: svc.StopPending}
ws.stop()
return false, 0
}
}
return false, 0
}
// isWindowsService reports whether the process was launched by the SCM.
func isWindowsService() bool {
ok, err := svc.IsWindowsService()
return err == nil && ok
}
// runAsService hands control to the SCM and blocks until the service stops.
func runAsService(run func() error, stop func()) error {
return svc.Run(serviceName, &windowsService{run: run, stop: stop})
}
// handleServiceCommand processes -service install|uninstall|start|stop.
func handleServiceCommand(cmd, exePath string, extraArgs []string) error {
switch cmd {
case "install":
return installService(exePath, extraArgs)
case "uninstall":
return uninstallService()
case "start":
return startService()
case "stop":
return stopService()
default:
return fmt.Errorf("unknown service command %q; valid: install, uninstall, start, stop", cmd)
}
}
func installService(exePath string, extraArgs []string) error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("connect to SCM: %w", err)
}
defer m.Disconnect()
s, err := m.OpenService(serviceName)
if err == nil {
s.Close()
return fmt.Errorf("service %q already exists", serviceName)
}
s, err = m.CreateService(
serviceName,
exePath,
mgr.Config{
DisplayName: serviceDisplayName,
Description: serviceDesc,
StartType: mgr.StartAutomatic,
},
extraArgs...,
)
if err != nil {
return fmt.Errorf("create service: %w", err)
}
s.Close()
log.Printf("service %q installed (binary: %s)", serviceName, exePath)
return nil
}
func uninstallService() error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("connect to SCM: %w", err)
}
defer m.Disconnect()
s, err := m.OpenService(serviceName)
if err != nil {
return fmt.Errorf("service %q not found: %w", serviceName, err)
}
defer s.Close()
if err := s.Delete(); err != nil {
return fmt.Errorf("delete service: %w", err)
}
log.Printf("service %q uninstalled", serviceName)
return nil
}
func startService() error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("connect to SCM: %w", err)
}
defer m.Disconnect()
s, err := m.OpenService(serviceName)
if err != nil {
return fmt.Errorf("service %q not found: %w", serviceName, err)
}
defer s.Close()
if err := s.Start(); err != nil {
return fmt.Errorf("start service: %w", err)
}
log.Printf("service %q start requested", serviceName)
return nil
}
func stopService() error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("connect to SCM: %w", err)
}
defer m.Disconnect()
s, err := m.OpenService(serviceName)
if err != nil {
return fmt.Errorf("service %q not found: %w", serviceName, err)
}
defer s.Close()
status, err := s.Control(svc.Stop)
if err != nil {
return fmt.Errorf("stop service: %w", err)
}
timeout := time.Now().Add(10 * time.Second)
for status.State != svc.Stopped {
if time.Now().After(timeout) {
return fmt.Errorf("timed out waiting for service to stop")
}
time.Sleep(300 * time.Millisecond)
status, err = s.Query()
if err != nil {
return fmt.Errorf("query service status: %w", err)
}
}
log.Printf("service %q stopped", serviceName)
return nil
}
// setupLogFile redirects log output to a file.
// If path is empty it defaults to <binary-dir>/retrosync.log.
// When interactive is true it also tees to stderr.
func setupLogFile(path string, interactive bool) (*os.File, error) {
if path == "" {
exe, err := os.Executable()
if err != nil {
return nil, err
}
path = filepath.Join(filepath.Dir(exe), "retrosync.log")
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
if interactive {
log.SetOutput(io.MultiWriter(f, os.Stderr))
} else {
log.SetOutput(f)
}
return f, nil
}