forked from GoogleCloudPlatform/osconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_windows.go
148 lines (130 loc) · 3.94 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
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
// Copyright 2019 Google Inc. All Rights Reserved.
//
// 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.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"syscall"
"unsafe"
"github.com/GoogleCloudPlatform/guest-logging-go/logger"
"github.com/GoogleCloudPlatform/osconfig/packages"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc"
)
var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
procLockFileEx = kernel32.NewProc("LockFileEx")
procUnlockFileEx = kernel32.NewProc("UnlockFileEx")
)
const (
serviceName = "google_osconfig_agent"
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex
LOCKFILE_EXCLUSIVE_LOCK = 2
LOCKFILE_FAIL_IMMEDIATELY = 1
)
func lockFileEx(hFile uintptr, dwFlags, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh uint32, lpOverlapped *syscall.Overlapped) (err error) {
ret, _, _ := procLockFileEx.Call(
hFile,
uintptr(dwFlags),
0,
uintptr(nNumberOfBytesToLockLow),
uintptr(nNumberOfBytesToLockHigh),
uintptr(unsafe.Pointer(lpOverlapped)),
)
// If the function succeeds, the return value is nonzero.
if ret == 0 {
return errors.New("LockFileEx unable to obtain lock")
}
return nil
}
func unlockFileEx(hFile uintptr, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh uint32, lpOverlapped *syscall.Overlapped) (err error) {
ret, _, _ := procUnlockFileEx.Call(
hFile,
0,
uintptr(nNumberOfBytesToLockLow),
uintptr(nNumberOfBytesToLockHigh),
uintptr(unsafe.Pointer(lpOverlapped)),
)
// If the function succeeds, the return value is nonzero.
if ret == 0 {
return errors.New("UnlockFileEx unable to unlock")
}
return nil
}
func obtainLock() {
lockFile := `C:\Program Files\Google\OSConfig\lock`
err := os.MkdirAll(filepath.Dir(lockFile), 0755)
if err != nil && !os.IsExist(err) {
logger.Fatalf("Cannot obtain agent lock: %v", err)
}
f, err := os.OpenFile(lockFile, os.O_RDWR|os.O_CREATE, 0600)
if err != nil && !os.IsExist(err) {
logger.Fatalf("Cannot obtain agent lock: %v", err)
}
if err := lockFileEx(f.Fd(), LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 1, 0, &syscall.Overlapped{}); err != nil {
logger.Fatalf("OSConfig agent lock already held, is the agent already running?")
}
deferredFuncs = append(deferredFuncs, func() { unlockFileEx(f.Fd(), 1, 0, &syscall.Overlapped{}); f.Close(); os.Remove(lockFile) })
}
type service struct {
ctx context.Context
run func(context.Context)
}
func (s *service) Execute(_ []string, r <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
status <- svc.Status{State: svc.StartPending}
ctx, cncl := context.WithCancel(s.ctx)
defer cncl()
done := make(chan struct{})
go func() {
s.run(ctx)
close(done)
}()
status <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
for {
select {
case <-done:
status <- svc.Status{State: svc.StopPending}
return false, 0
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
status <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
cncl()
default:
}
}
}
}
func runService(ctx context.Context) {
if err := svc.Run(serviceName, &service{run: run, ctx: ctx}); err != nil {
logger.Fatalf("svc.Run error: %v", err)
}
}
func wuaUpdates(query string) error {
updts, err := packages.WUAUpdates(query)
if err != nil {
return err
}
data, err := json.Marshal(updts)
if err != nil {
return err
}
fmt.Fprint(os.Stdout, string(data))
return nil
}