-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransport_winpv.go
234 lines (186 loc) · 5.26 KB
/
transport_winpv.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
//go:build windows
// +build windows
package xenstore
import (
"fmt"
"strings"
"github.com/go-ole/go-ole"
"github.com/joelnb/wmi"
log "github.com/sirupsen/logrus"
)
const SessionName = "xenstore-go"
type WinPVTransport struct {
base *XenProjectXenStoreBase
pktChannel chan *Packet
Session *XenProjectXenStoreSession
}
func (t *WinPVTransport) Close() error {
wmi.DefaultClient.SWbemServicesClient.Close()
if t.base != nil {
t.base.disp.Release()
}
return nil
}
func (t *WinPVTransport) Send(pkt *Packet) error {
var err error
session := t.Session
if t.Session == nil {
session, err = t.base.AddSession(SessionName)
if err != nil {
return err
}
}
rpkt := &Packet{
Header: &PacketHeader{
Op: pkt.Header.Op,
RqId: pkt.Header.RqId,
TxId: pkt.Header.TxId,
},
}
switch pkt.Header.Op {
case XsDirectory:
val, err := session.GetChildren(string(pkt.Payload))
if err != nil {
return err
}
rpkt.Payload = []byte(strings.Join(val, "\000"))
case XsRead:
val, err := session.GetValue(string(pkt.Payload))
if err != nil {
return err
}
rpkt.Payload = []byte(val)
case XsRm:
if err := session.RemoveValue(string(pkt.Payload)); err != nil {
return err
}
case XsWrite:
args := pkt.Strings()
if err := session.SetValue(args[0], args[1]); err != nil {
return err
}
default:
return fmt.Errorf("WinPVTransport: Unsupported packet: %+v", pkt)
}
rpkt.Header.Length = uint32(len(rpkt.Payload))
t.pktChannel <- rpkt
if t.Session == nil {
if err := session.EndSession(); err != nil {
return err
}
}
return nil
}
func (t *WinPVTransport) Receive() (*Packet, error) {
rsp := <-t.pktChannel
return rsp, nil
}
func (t *WinPVTransport) GetBase() (*XenProjectXenStoreBase, error) {
if t.base != nil {
return t.base, nil
}
log.Debug("GetBase: Requesting base list")
var baseList []XenProjectXenStoreBaseProps
baseQuery := "SELECT Active, InstanceName, XenTime FROM XenProjectXenStoreBase"
baseDispatchList, err := wmi.QueryNamespaceRaw(baseQuery, &baseList, "root\\wmi")
if err != nil {
return nil, err
}
cleanup := func() {
for _, item := range baseDispatchList {
item.Release()
}
}
log.Debugf("GetBase: Got base list %+v", baseList)
if len(baseList) != len(baseDispatchList) {
cleanup()
return nil, fmt.Errorf("WinPVTransport.GetBase: unexpected length mismatch (baseList=%d, baseDispatchList=%d)",
len(baseList),
len(baseDispatchList))
} else if len(baseList) == 0 {
cleanup()
return nil, fmt.Errorf("WinPVTransport.GetBase: Got 0-length list")
} else if len(baseList) > 1 {
cleanup()
return nil, fmt.Errorf("WinPVTransport.GetBase: Unexpected multiple XenProjectXenStoreBase returned")
}
t.base = &XenProjectXenStoreBase{
transport: t,
disp: baseDispatchList[0],
Properties: baseList[0],
}
return t.base, nil
}
func (t *WinPVTransport) GetSession(sessionId int32) (*XenProjectXenStoreSession, error) {
sessionList, sessionDispatchList, err := t.getSessions(sessionId)
if err != nil {
return nil, err
}
cleanup := func() {
for _, item := range sessionDispatchList {
item.Release()
}
}
if len(sessionList) == 0 {
cleanup()
return nil, fmt.Errorf("WinPVTransport.GetSession: Got 0-length list")
} else if len(sessionList) > 1 {
cleanup()
return nil, fmt.Errorf("WinPVTransport.GetSession: Unexpected multiple XenProjectXenStoreBase returned")
}
return &XenProjectXenStoreSession{
transport: t,
disp: sessionDispatchList[0],
Properties: sessionList[0],
}, nil
}
func (t *WinPVTransport) GetSessions() ([]*XenProjectXenStoreSession, error) {
var sessions []*XenProjectXenStoreSession
sessionList, sessionDispatchList, err := t.getSessions()
if err != nil {
return nil, err
}
for i := range sessionList {
sessions = append(sessions, &XenProjectXenStoreSession{
transport: t,
disp: sessionDispatchList[i],
Properties: sessionList[i],
})
}
return sessions, nil
}
func (t *WinPVTransport) getSessions(sessionIds ...int32) ([]XenProjectXenStoreSessionProps, []*ole.IDispatch, error) {
log.Debug("WinPVTransport.getSessions: Requesting session list")
sessionQuery := "SELECT Active, ID, InstanceName, SessionID FROM XenProjectXenStoreSession"
if len(sessionIds) > 0 {
sessionQuery = fmt.Sprintf("%s WHERE SessionId=%d", sessionQuery, sessionIds[0])
}
var sessionList []XenProjectXenStoreSessionProps
sessionDispatchList, err := wmi.QueryNamespaceRaw(sessionQuery, &sessionList, "root\\wmi")
if err != nil {
return []XenProjectXenStoreSessionProps{}, []*ole.IDispatch{}, err
}
log.Debugf("WinPVTransport.getSessions: Got session list: %+v", sessionList)
if len(sessionList) != len(sessionDispatchList) {
for _, item := range sessionDispatchList {
item.Release()
}
return []XenProjectXenStoreSessionProps{}, []*ole.IDispatch{}, fmt.Errorf("WinPVTransport.getSessions: unexpected length mismatch (sessionList=%d, sessionDispatchList=%d)",
len(sessionList),
len(sessionDispatchList))
}
return sessionList, sessionDispatchList, nil
}
func NewWinPVTransport() (*WinPVTransport, error) {
if err := initWmi(); err != nil {
return nil, err
}
transport := &WinPVTransport{}
c := make(chan *Packet)
transport.pktChannel = c
_, err := transport.GetBase()
if err != nil {
return nil, err
}
return transport, nil
}