-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcabbie.go
692 lines (617 loc) · 23 KB
/
cabbie.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Copyright 2019 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
// The cabbie binary is used to manage and report Windows updates.
package main
import (
"golang.org/x/net/context"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"sync"
"time"
"golang.org/x/sys/windows"
"flag"
"github.com/google/cabbie/metrics"
"github.com/google/cabbie/notification"
"github.com/google/cabbie/cablib"
"github.com/google/cabbie/enforcement"
"github.com/google/cabbie/servicemgr"
"github.com/google/deck/backends/eventlog"
"github.com/google/deck/backends/logger"
"github.com/google/deck"
"github.com/google/aukera/client"
"github.com/scjalliance/comshim"
"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc"
"github.com/google/subcommands"
)
var (
runInDebug = flag.Bool("debug", false, "Run in debug mode")
runNormalPriority = flag.Bool("normalpriority", false, "If specified cabbie runs at normal priority rather than lowering the process priority")
config = new(Settings)
categoryDefaults = []string{"Critical Updates", "Definition Updates", "Security Updates"}
rebootEvent = make(chan bool, 10)
rebootActive = false
excludedDrivers driverExcludes
// Metrics
virusUpdateSuccess = new(metrics.Bool)
listUpdateSuccess = new(metrics.Bool)
driverUpdateSuccess = new(metrics.Bool)
updateInstallSuccess = new(metrics.Bool)
rebootRequired = new(metrics.Bool)
deviceIsPatched = new(metrics.Bool)
requiredUpdateCount = new(metrics.Int)
enforcedUpdateCount = new(metrics.Int)
enforcementWatcherFailures = new(metrics.Int)
installHResult = new(metrics.String)
searchHResult = new(metrics.String)
eventID = eventlog.EventID
)
// Settings contains configurable options.
type Settings struct {
WSUSServers, RequiredCategories []string
UpdateDrivers, UpdateVirusDef, EnableThirdParty, RebootDelay, Deadline, NotifyAvailable uint64
// Aukera Integration
AukeraEnabled uint64
AukeraPort uint64
AukeraName string
// Microsoft Active Hours Integration (Requires Aukera Enabled)
ActiveHoursEnabled uint64
PprofPort uint64
ScriptTimeout time.Duration
}
type tickers struct {
Default, Aukera, List, Virus, Driver, Enforcement *time.Ticker
}
type driverExcludes struct {
mutex sync.Mutex
e []enforcement.DriverExclude
}
func (d *driverExcludes) set(v []enforcement.DriverExclude) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.e = v
}
func (d *driverExcludes) get() []enforcement.DriverExclude {
d.mutex.Lock()
defer d.mutex.Unlock()
return d.e
}
func initTickers() tickers {
return tickers{
Default: time.NewTicker(24 * time.Hour),
Aukera: time.NewTicker(5 * time.Minute),
List: time.NewTicker(2 * time.Hour),
Virus: time.NewTicker(30 * time.Minute),
Driver: time.NewTicker(72 * time.Hour),
Enforcement: time.NewTicker(6 * time.Hour),
}
}
func (t *tickers) stop() {
t.Default.Stop()
t.Aukera.Stop()
t.List.Stop()
t.Virus.Stop()
t.Driver.Stop()
t.Enforcement.Stop()
}
func newSettings() *Settings {
// Set non-Zero defaults.
return &Settings{
AukeraName: cablib.SvcName,
RequiredCategories: categoryDefaults,
UpdateVirusDef: 1,
RebootDelay: 21600,
Deadline: 14,
NotifyAvailable: 1,
AukeraPort: 9119,
ScriptTimeout: 10 * time.Minute,
}
}
func (s *Settings) regLoad(path string) error {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, path, registry.QUERY_VALUE)
if err != nil {
return err
}
defer k.Close()
if m, _, err := k.GetStringsValue("WsusServers"); err == nil {
s.WSUSServers = m
}
if a, _, err := k.GetStringValue("AukeraName"); err == nil {
s.AukeraName = a
} else {
deck.InfofA(
"AukeraName not found in registry, using default Name:\n%v", s.AukeraName).With(eventID(cablib.EvtErrConfig)).Go()
}
if m, _, err := k.GetStringsValue("RequiredCategories"); err == nil {
s.RequiredCategories = m
} else {
deck.InfofA("RequiredCategories not found in registry, using default categories:\n%v", s.RequiredCategories).With(eventID(cablib.EvtErrConfig)).Go()
}
if i, _, err := k.GetIntegerValue("EnableThirdParty"); err == nil {
s.EnableThirdParty = i
}
if i, _, err := k.GetIntegerValue("UpdateDrivers"); err == nil {
s.UpdateDrivers = i
}
if i, _, err := k.GetIntegerValue("UpdateVirusDef"); err == nil {
s.UpdateVirusDef = i
}
if i, _, err := k.GetIntegerValue("RebootDelay"); err == nil {
s.RebootDelay = i
}
if i, _, err := k.GetIntegerValue("Deadline"); err == nil {
s.Deadline = i
}
if i, _, err := k.GetIntegerValue("NotifyAvailable"); err == nil {
s.NotifyAvailable = i
}
if i, _, err := k.GetIntegerValue("AukeraEnabled"); err == nil {
s.AukeraEnabled = i
}
if i, _, err := k.GetIntegerValue("AukeraPort"); err == nil {
s.AukeraPort = i
}
if i, _, err := k.GetIntegerValue("PprofPort"); err == nil {
s.PprofPort = i
}
if i, _, err := k.GetIntegerValue("ActiveHoursEnabled"); err == nil {
s.ActiveHoursEnabled = i
}
if i, _, err := k.GetIntegerValue("ScriptTimeout"); err == nil {
s.ScriptTimeout = time.Duration(i) * time.Minute
}
return nil
}
// Type winSvc implements svc.Handler.
type winSvc struct{}
func startService(isDebug bool) error {
deck.InfofA("Starting %s service.", cablib.SvcName).With(eventID(cablib.EvtServiceStarting)).Go()
run := svc.Run
if isDebug {
run = debug.Run
}
if err := run(cablib.SvcName, winSvc{}); err != nil {
return fmt.Errorf("%s service failed. %v", cablib.SvcName, err)
}
deck.InfofA("%s service stopped.", cablib.SvcName).With(eventID(cablib.EvtServiceStopped)).Go()
return nil
}
func initMetrics() error {
var err error
// bool metrics
virusUpdateSuccess, err = metrics.NewBool(cablib.MetricRoot+"virusUpdateSuccess", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize virusUpdateSuccess metric: %v", err)
}
listUpdateSuccess, err = metrics.NewBool(cablib.MetricRoot+"listUpdateSuccess", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize listUpdateSuccess metric: %v", err)
}
driverUpdateSuccess, err = metrics.NewBool(cablib.MetricRoot+"driverUpdateSuccess", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize driverUpdateSuccess metric: %v", err)
}
updateInstallSuccess, err = metrics.NewBool(cablib.MetricRoot+"updateInstallSuccess", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize updateInstallSuccess metric: %v", err)
}
rebootRequired, err = metrics.NewBool(cablib.MetricRoot+"rebootRequired", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize rebootRequired metric: %v", err)
}
deviceIsPatched, err = metrics.NewBool(cablib.MetricRoot+"deviceIsPatched", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize deviceIsPatched metric: %v", err)
}
// integer metrics
requiredUpdateCount, err = metrics.NewInt(cablib.MetricRoot+"requiredUpdateCount", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize requiredUpdateCount metric: %v", err)
}
enforcedUpdateCount, err = metrics.NewInt(cablib.MetricRoot+"enforcedUpdateCount", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize enforcedUpdateCount metric: %v", err)
}
enforcementWatcherFailures, err = metrics.NewCounter(cablib.MetricRoot+"enforcementWatcherFailures", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to create enforcementWatcherFailures metric: %v", err)
}
// string metrics
installHResult, err = metrics.NewString(cablib.MetricRoot+"installHResult", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize installHResult metric: %v", err)
}
searchHResult, err = metrics.NewString(cablib.MetricRoot+"searchHResult", cablib.MetricSvc)
if err != nil {
return fmt.Errorf("unable to initialize searchHResult metric: %v", err)
}
return nil
}
func setRebootMetric() {
rbr, err := cablib.RebootRequired()
if err != nil {
deck.ErrorA(err).With(eventID(cablib.EvtErrMetricReport)).Go()
return
}
if err := rebootRequired.Set(rbr); err != nil {
deck.ErrorA(err).With(eventID(cablib.EvtErrMetricReport)).Go()
}
if rbr {
rebootEvent <- rbr
}
}
func enforce() error {
updates, err := enforcement.Get()
if err != nil {
return fmt.Errorf("error retrieving required updates: %v", err)
}
if err := enforcedUpdateCount.Set(int64(len(updates.Required))); err != nil {
deck.ErrorfA("Error posting metric:\n%v", err).With(eventID(cablib.EvtErrMetricReport)).Go()
}
var failures error
if len(updates.Required) > 0 {
i := installCmd{kbs: strings.Join(updates.Required, ",")}
if err := i.installUpdates(); err != nil {
failures = fmt.Errorf("error enforcing required updates: %v", err)
deck.ErrorA(failures).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
}
if len(updates.Hidden) > 0 {
if err := hide(NewKBSetFromSlice(updates.Hidden)); err != nil {
failures = fmt.Errorf("error hiding updates: %v", err)
deck.ErrorA(failures).With(eventID(cablib.EvtErrHide)).Go()
}
}
if len(updates.HiddenUpdateID) > 0 {
if err := hideByUpdateID(updates.HiddenUpdateID); err != nil {
failures = fmt.Errorf("error hiding updates by update ID: %v", err)
deck.ErrorA(failures).With(eventID(cablib.EvtErrHide)).Go()
}
}
return failures
}
func initDriverExclusion() error {
updates, err := enforcement.Get()
if err != nil {
return fmt.Errorf("error retrieving required updates: %v", err)
}
excludedDrivers.set(updates.ExcludedDrivers)
return nil
}
func runMainLoop() error {
if err := notification.CleanNotifications(cablib.SvcName); err != nil {
deck.ErrorfA("Error clearing old notifications:\n%v", err).With(eventID(cablib.EvtErrNotifications)).Go()
}
if config.EnableThirdParty == 1 {
if err := enableThirdPartyUpdates(); err != nil {
deck.ErrorfA("Error configuring third party updates:\n%v", err).With(eventID(cablib.EvtErrMisc)).Go()
}
}
setRebootMetric()
// Initialize service tickers.
t := initTickers()
defer t.stop()
// Run filesystem watcher for required updates configuration.
var enforcedFile = make(chan string)
go func() {
for {
if err := enforcement.Watcher(enforcedFile); err == nil {
deck.ErrorfA("failed to initialize enforcement config watcher; relying on default enforcement schedule: %v", err).With(eventID(cablib.EvtErrEnforcement)).Go()
}
if err := enforcementWatcherFailures.Increment(); err != nil {
deck.ErrorfA("unable to increment enforcementWatcherFailures metric: %v", err).With(eventID(cablib.EvtErrMetricReport)).Go()
}
time.Sleep(15 * time.Minute)
}
}()
if config.AukeraEnabled == 1 {
deck.InfoA("Host configured to use Aukera. Ignoring default timer.").With(eventID(cablib.EvtMisc)).Go()
t.Default.Stop()
} else {
deck.InfoA("Using default update interval.").With(eventID(cablib.EvtMisc)).Go()
t.Aukera.Stop()
}
if config.UpdateVirusDef == 0 {
t.Virus.Stop()
}
if config.UpdateDrivers == 0 {
t.Driver.Stop()
}
for {
select {
case <-t.Default.C:
i := installCmd{Interactive: false}
err := i.installUpdates()
if err != nil {
deck.ErrorfA("Error installing system updates:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
if e := updateInstallSuccess.Set(err == nil); e != nil {
deck.ErrorfA("Error posting metric:\n%v", e).With(eventID(cablib.EvtErrMetricReport)).Go()
}
setRebootMetric()
case <-t.Aukera.C:
s, err := client.Label(int(config.AukeraPort), config.AukeraName)
if err != nil {
deck.ErrorfA("Error getting maintenance window %q with error:\n%v", config.AukeraName, err).With(eventID(cablib.EvtErrMaintWindow)).Go()
break
}
if *runInDebug {
fmt.Printf("Cabbie maintenance window schedule:\n%+v", s)
}
if len(s) == 0 {
deck.ErrorfA("Aukera maintenance window label %q not found, skipping update check...", config.AukeraName).With(eventID(cablib.EvtErrMaintWindow)).Go()
break
}
if config.ActiveHoursEnabled == 1 {
deck.InfofA("Active Hours enabled: checking for active_hours schedule.").With(eventID(cablib.EvtMisc)).Go()
ah, err := client.Label(int(config.AukeraPort), `active_hours`)
if err != nil {
deck.ErrorfA("Error getting maintenance window %q with error:\n%v", `active_hours`, err).With(eventID(cablib.EvtErrMaintWindow)).Go()
break
}
if len(ah) == 0 {
deck.ErrorfA("Aukera maintenance window label %q not found, skipping update check...", `active_hours`).With(eventID(cablib.EvtErrMaintWindow)).Go()
break
}
trimmedOpen := ah[0].Opens.Add(time.Hour)
trimmedClose := ah[0].Closes.Add(-time.Hour)
now := time.Now()
today := time.Now().Day()
maintOpenDay := s[0].Opens.Day()
maintCloseDay := s[0].Closes.Day()
deck.InfofA("Active Hours schedule found:\nNow: %v\nTrimmed Active Hours Open Time: %v\nTrimmed Active Hours Close Time: %v\nToday: %v\nMaintenance Open Day: %v\nMaintenance Close Day: %v\n", now, trimmedOpen, trimmedClose, today, maintOpenDay, maintCloseDay).With(eventID(cablib.EvtMisc)).Go()
// We're trimming the leading and trailing hours from the active hours window.
// As long as the current time is within the trimmed window and the current day is
// within the standard `cabbie` maintenance window, we'll install updates.
if trimmedOpen.Before(now) && trimmedClose.After(now) && ((today >= maintOpenDay) && (today <= maintCloseDay)) {
deck.InfofA("Active Hours + Maintenance window open: Starting installation process.").With(eventID(cablib.EvtInstall)).Go()
i := installCmd{Interactive: false}
err := i.installUpdates()
if err != nil {
deck.ErrorfA("Error installing system updates:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
if e := updateInstallSuccess.Set(err == nil); e != nil {
deck.ErrorfA("Error posting updateInstallSuccess metric:\n%v", e).With(eventID(cablib.EvtErrMetricReport)).Go()
}
setRebootMetric()
}
} else {
deck.InfofA("Active Hours disabled: using standard maintenance window schedule.").With(eventID(cablib.EvtMisc)).Go()
// If we're a server, or we don't have an active hours window, we'll install updates
// as long as the standard `cabbie` maintenance window is open.
if s[0].State == "open" {
deck.InfofA("Maintenance window open: Starting installation process.").With(eventID(cablib.EvtInstall)).Go()
i := installCmd{Interactive: false}
err := i.installUpdates()
if err != nil {
deck.ErrorfA("Error installing system updates:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
if e := updateInstallSuccess.Set(err == nil); e != nil {
deck.ErrorfA("Error posting updateInstallSuccess metric:\n%v", e).With(eventID(cablib.EvtErrMetricReport)).Go()
}
setRebootMetric()
}
}
case <-t.List.C:
requiredUpdates, optionalUpdates, err := listUpdates(false, false)
if e := listUpdateSuccess.Set(err == nil); e != nil {
deck.ErrorfA("Error posting listUpdateSuccess metric:\n%v", e).With(eventID(cablib.EvtErrMetricReport)).Go()
}
if err != nil {
deck.ErrorfA("Error getting the list of updates:\n%v", err).With(eventID(cablib.EvtErrQueryFailure)).Go()
break
}
if err := requiredUpdateCount.Set(int64(len(requiredUpdates))); err != nil {
deck.ErrorfA("Error posting requiredUpdateCount metric:\n%v", err).With(eventID(cablib.EvtErrMetricReport)).Go()
}
if len(requiredUpdates) == 0 {
deck.InfoA("No required updates needed to install.").With(eventID(cablib.EvtNoUpdates)).Go()
break
}
deck.InfofA("Found %d required updates.\nRequired updates:\n%s\nOptional updates:\n%s",
len(requiredUpdates),
strings.Join(requiredUpdates, "\n\n"),
strings.Join(optionalUpdates, "\n\n"),
).With(eventID(cablib.EvtUpdatesFound)).Go()
if config.NotifyAvailable == 1 {
if err := notification.NewAvailableUpdateMessage().Push(); err != nil {
deck.ErrorfA("Failed to create notification:\n%v", err).With(eventID(cablib.EvtErrNotifications)).Go()
}
}
if config.Deadline != 0 {
i := installCmd{Interactive: false, deadlineOnly: true}
if err := i.installUpdates(); err != nil {
deck.ErrorfA("Error installing system updates:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
}
case <-t.Virus.C:
i := installCmd{Interactive: false, virusDef: true}
err := i.installUpdates()
if e := virusUpdateSuccess.Set(err == nil); e != nil {
deck.ErrorfA("Error posting virusUpdateSuccess metric:\n%v", err).With(eventID(cablib.EvtErrMetricReport)).Go()
}
if err != nil {
deck.ErrorfA("Error installing virus definitions:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
break
}
case <-t.Driver.C:
i := installCmd{Interactive: false, drivers: true}
err := i.installUpdates()
if e := driverUpdateSuccess.Set(err == nil); e != nil {
deck.ErrorfA("Error posting driverUpdateSuccess metric:\n%v", e).With(eventID(cablib.EvtErrMetricReport)).Go()
}
if err != nil {
deck.ErrorfA("Error installing drivers:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
setRebootMetric()
case file := <-enforcedFile:
deck.InfofA("Enforcement triggered by change in file %q.", file).With(eventID(cablib.EvtEnforcementChange)).Go()
if err := enforce(); err != nil {
deck.ErrorfA("Error enforcing one or more updates:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
case <-t.Enforcement.C:
if err := enforce(); err != nil {
deck.ErrorfA("Error enforcing one or more updates:\n%v", err).With(eventID(cablib.EvtErrInstallFailure)).Go()
}
case <-rebootEvent:
go func() {
if !(rebootActive) {
rebootActive = true
deck.InfoA("Reboot initiated...").With(eventID(cablib.EvtReboot)).Go()
t, err := cablib.RebootTime()
if err != nil {
deck.ErrorfA("Error getting reboot time: %v", err).With(eventID(cablib.EvtErrPowerMgmt)).Go()
return
}
if t.IsZero() {
deck.InfoA("Zero time returned, no reboot defined.").With(eventID(cablib.EvtMisc)).Go()
return
}
deck.InfofA("Reboot time is %s", t.String()).With(eventID(cablib.EvtMisc)).Go()
if err := cablib.SystemReboot(t); err != nil {
deck.ErrorfA("SystemReboot() error:\n%v", err).With(eventID(cablib.EvtErrPowerMgmt)).Go()
}
rebootActive = false
}
}()
}
}
}
// Execute starts the internal goroutine and waits for service signals from Windows.
func (m winSvc) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
errch := make(chan error)
changes <- svc.Status{State: svc.StartPending}
go func() {
errch <- runMainLoop()
}()
deck.InfoA("Service started.").With(eventID(cablib.EvtServiceStarted)).Go()
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
// Watch for the cabbie goroutine to fail for some reason.
case err := <-errch:
deck.ErrorfA("Cabbie goroutine has failed: %v", err).With(eventID(cablib.EvtErrService)).Go()
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
default:
deck.ErrorfA("Unexpected control request #%d", c).With(eventID(cablib.EvtErrService)).Go()
}
}
}
changes <- svc.Status{State: svc.StopPending}
return ssec, errno
}
func enableThirdPartyUpdates() error {
m, err := servicemgr.InitMgrService()
if err != nil {
return fmt.Errorf("failed to initialize Windows update service manager: %v", err)
}
defer m.Close()
r, err := m.QueryServiceRegistration(servicemgr.MicrosoftUpdate)
if err != nil {
return fmt.Errorf("failed to query third party service registration status: %v", err)
}
if r {
return nil
}
return m.AddService(servicemgr.MicrosoftUpdate)
}
// Lower the priority of the Cabbie process to IDLE_PRIORITY_CLASS in the OS scheduler and begin
// background processing to limit user impact see:
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass
func lowerProcessPriority() error {
return windows.SetPriorityClass(windows.CurrentProcess(), windows.IDLE_PRIORITY_CLASS)
}
func main() {
flag.Parse()
var err error
if !*runNormalPriority {
if err := lowerProcessPriority(); err != nil {
deck.ErrorfA("Failed to lower process priority: %v", err).With(eventID(cablib.EvtErrMisc)).Go()
}
}
if *runInDebug {
deck.Add(logger.Init(os.Stdout, 0))
} else {
evt, err := eventlog.Init(cablib.LogSrcName)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
deck.Add(evt)
}
defer deck.Close()
// Load Cabbie config settings.
config = newSettings()
if err = config.regLoad(cablib.RegPath); err != nil {
deck.ErrorfA("Failed to load Cabbie config, using defaults:\n%v\nError:%v", config, err).With(eventID(cablib.EvtErrConfig)).Go()
}
// If a profiling port is specified, start an HTTP server
if config.PprofPort != 0 {
go func() {
http.ListenAndServe(fmt.Sprintf("localhost:%d", config.PprofPort), nil)
}()
}
isSvc, err := svc.IsWindowsService()
if err != nil {
deck.ErrorfA("Failed to determine if we are running in an interactive session: %v", err).With(eventID(cablib.EvtErrMisc)).Go()
os.Exit(2)
}
// Initialize metrics.
if err := initMetrics(); err != nil {
deck.ErrorA(err).With(eventID(cablib.EvtErrMetricReport)).Go()
}
comshim.Add(1)
defer comshim.Done()
// Running as Service.
if isSvc && len(os.Args) == 1 {
if err := startService(*runInDebug); err != nil {
deck.ErrorfA("Failed to run service: %v", err).With(eventID(cablib.EvtErrService)).Go()
os.Exit(2)
}
os.Exit(0)
}
// Running Interactively.
ctx := context.Background()
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&hideCmd{}, "Update management")
subcommands.Register(&historyCmd{}, "Update management")
subcommands.Register(&installCmd{Interactive: true}, "Update management")
subcommands.Register(&listCmd{}, "Update management")
subcommands.Register(&rebootCmd{}, "Reboot management")
subcommands.Register(&serviceCmd{}, "Service registration management")
if *runInDebug {
if err := startService(true); err != nil {
deck.ErrorfA("Failed to run service in debug mode: %v", err).With(eventID(cablib.EvtErrService)).Go()
os.Exit(2)
}
}
os.Exit(int(subcommands.Execute(ctx)))
}