/
service.go
138 lines (124 loc) · 3.42 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//go:build windows
// +build windows
// This file is part of thor (https://github.com/notapipeline/thor).
//
// Copyright (c) 2024 Martin Proffitt <mproffitt@choclab.net>.
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
package windows
import (
"fmt"
"os"
"time"
"github.com/notapipeline/thor/pkg/agent/app"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
)
const (
FAST = 500
SLOW = 2000
NOTIFY_AT = 30000
)
var (
elog debug.Log
)
func (service *Service) 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(FAST * time.Millisecond)
slowtick := time.Tick(SLOW * time.Millisecond)
tick := fasttick
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
stop := make(chan bool)
go func() {
for {
select {
case <-stop:
elog.Info(1, "Shutting down error log channel")
return
case err := <-service.errors:
switch err.Level {
case app.INFO:
elog.Info(1, err.Message)
case app.WARN:
elog.Warning(1, err.Message)
case app.ERROR:
elog.Error(1, err.Message)
}
default:
time.Sleep(app.Duration)
}
}
}()
var err error
if service.app, err = app.NewApp(&service.errors, os.Args[0]); err != nil {
service.errors <- app.NewLogItem(app.ERROR, fmt.Sprintf("%s service failed setting up main app: %v", "thor", err))
return
}
loop:
for {
select {
case <-tick:
if tick == fasttick {
service.app.Notify()
}
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
time.Sleep(100 * time.Millisecond)
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
elog.Info(1, "Shutting down service")
service.app.Stop <- true
stop <- true
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
}
func (service *Service) runService(name string, isDebug bool) {
var err error
if isDebug {
elog = debug.New(name)
} else {
elog, err = eventlog.Open(name)
if err != nil {
return
}
}
defer elog.Close()
elog.Info(1, fmt.Sprintf("%s: Setting up run", name))
run := svc.Run
if isDebug {
run = debug.Run
}
elog.Info(1, "App configured, moving into run...")
err = run(name, service)
if err != nil {
elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err))
return
}
elog.Info(1, fmt.Sprintf("%s: stopped", name))
}