-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_windows.go
114 lines (103 loc) · 3.19 KB
/
main_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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
// +build windows
package main
import (
"fmt"
"github.com/google/deck/backends/eventlog"
"github.com/google/deck"
"github.com/google/aukera/auklib"
"github.com/google/aukera/server"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc"
)
// Type winSvc implements svc.Handler.
type winSvc struct{}
func setup() error {
evt, err := eventlog.InitWithDefaultInstall("aukera")
if err != nil {
return err
}
deck.Add(evt)
return nil
}
func startService(isDebug bool) error {
deck.Infof("Starting %s service.", auklib.ServiceName)
run := svc.Run
if isDebug {
run = debug.Run
}
if err := run(auklib.ServiceName, winSvc{}); err != nil {
return fmt.Errorf("%s service failed: %v", auklib.ServiceName, err)
}
deck.Infof("%s service stopped.", auklib.ServiceName)
return nil
}
// Execute starts the internal goroutine and waits for service
// signals from Windows. Execute is called by svc.Run which runs
// in a loop itself and interprets data in the changes channel
// for windows. When we receive a command to Stop or Shutdown,
// we break out of the loop and send a StopPending status to
// Windows, which will stop the service process and all child processes.
func (m winSvc) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
var (
ssec bool
errno uint32
)
errch := make(chan error)
changes <- svc.Status{State: svc.StartPending}
go func() {
errch <- server.Run(*port)
}()
deck.Infof("Service started.")
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
// Watch for the aukera goroutine to fail for some reason.
case err := <-errch:
deck.Errorf("%s goroutine has failed: %v", auklib.ServiceName, err)
break loop
// Watch for service signals.
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
break loop
case svc.Pause:
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
case svc.Continue:
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
default:
deck.Errorf("unexpected control request #%d", c)
}
}
}
changes <- svc.Status{State: svc.StopPending}
return ssec, errno
}
func run() error {
isIntSess, err := svc.IsAnInteractiveSession()
if err != nil {
return fmt.Errorf("Failed to determine if running in an interactive session: %v", err)
}
// Running as Service
if !isIntSess {
return startService(*runInDebug)
}
return fmt.Errorf("interactive sessions are unsupported")
}