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
|
package xenstoreclient
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
"time"
)
type Permission int
const (
PERM_NONE Permission = iota
PERM_READ
PERM_WRITE
PERM_READWRITE
)
type Operation uint32
const (
XS_READ Operation = 2
XS_GET_PERMS Operation = 3
XS_WATCH Operation = 4
XS_UNWATCH Operation = 5
XS_TRANSACTION_START Operation = 6
XS_TRANSACTION_END Operation = 7
XS_WRITE Operation = 11
XS_MKDIR Operation = 12
XS_RM Operation = 13
XS_SET_PERMS Operation = 14
XS_WATCH_EVENT Operation = 15
XS_ERROR Operation = 16
)
type Packet struct {
OpCode Operation
Req uint32
TxID uint32
Length uint32
Value []byte
}
type Event struct {
Token string
Data []byte
}
type XenStoreClient interface {
Close() error
DO(packet *Packet) (*Packet, error)
Read(path string) (string, error)
Mkdir(path string) error
Rm(path string) error
Write(path string, value string) error
GetPermission(path string) (map[int]Permission, error)
Watch(path string) (<-chan Event, error)
StopWatch() error
}
func ReadPacket(r io.Reader) (packet *Packet, err error) {
packet = &Packet{}
err = binary.Read(r, binary.LittleEndian, &packet.OpCode)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &packet.Req)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &packet.TxID)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.LittleEndian, &packet.Length)
if err != nil {
return nil, err
}
if packet.Length > 0 {
packet.Value = make([]byte, packet.Length)
_, err = io.ReadFull(r, packet.Value)
if err != nil {
return nil, err
}
if packet.OpCode == XS_ERROR {
return nil, errors.New(strings.Split(string(packet.Value), "\x00")[0])
}
}
return packet, nil
}
func (p *Packet) Write(w io.Writer) (err error) {
var bw *bufio.Writer
if w1, ok := w.(*bufio.Writer); ok {
bw = w1
} else {
bw = bufio.NewWriter(w)
}
defer bw.Flush()
err = binary.Write(bw, binary.LittleEndian, p.OpCode)
if err != nil {
return err
}
err = binary.Write(bw, binary.LittleEndian, p.Req)
if err != nil {
return err
}
err = binary.Write(bw, binary.LittleEndian, p.TxID)
if err != nil {
return err
}
err = binary.Write(bw, binary.LittleEndian, p.Length)
if err != nil {
return err
}
if p.Length > 0 {
_, err = bw.Write(p.Value)
if err != nil {
return err
}
}
return nil
}
type XenStore struct {
tx uint32
xbFile io.ReadWriteCloser
xbFileReader *bufio.Reader
muWatch *sync.Mutex
onceWatch *sync.Once
watchQueues map[string]chan Event
watchStopChan chan struct{}
watchStoppedChan chan struct{}
nonWatchQueue chan []byte
}
func NewXenstore(tx uint32) (XenStoreClient, error) {
devPath, err := getDevPath()
if err != nil {
return nil, err
}
xbFile, err := os.OpenFile(devPath, os.O_RDWR, 0666)
if err != nil {
return nil, err
}
return newXenstore(tx, xbFile)
}
func newXenstore(tx uint32, rwc io.ReadWriteCloser) (XenStoreClient, error) {
return &XenStore{
tx: tx,
xbFile: rwc,
xbFileReader: bufio.NewReader(rwc),
watchQueues: make(map[string]chan Event, 0),
nonWatchQueue: nil,
watchStopChan: make(chan struct{}, 1),
watchStoppedChan: make(chan struct{}, 1),
onceWatch: &sync.Once{},
muWatch: &sync.Mutex{},
}, nil
}
func (xs *XenStore) Close() error {
return xs.xbFile.Close()
}
func (xs *XenStore) DO(req *Packet) (resp *Packet, err error) {
err = req.Write(xs.xbFile)
if err != nil {
return nil, err
}
var r io.Reader
if xs.nonWatchQueue != nil {
data := <-xs.nonWatchQueue
r = bytes.NewReader(data)
} else {
r = xs.xbFileReader
}
resp, err = ReadPacket(r)
return resp, err
}
func (xs *XenStore) Read(path string) (string, error) {
v := []byte(path + "\x00")
req := &Packet{
OpCode: XS_READ,
Req: 0,
TxID: xs.tx,
Length: uint32(len(v)),
Value: v,
}
resp, err := xs.DO(req)
if err != nil {
return "", err
}
return string(resp.Value), nil
}
func (xs *XenStore) Mkdir(path string) error {
v := []byte(path + "\x00")
req := &Packet{
OpCode: XS_WRITE,
Req: 0,
TxID: xs.tx,
Length: uint32(len(v)),
Value: v,
}
_, err := xs.DO(req)
return err
}
func (xs *XenStore) Rm(path string) error {
v := []byte(path + "\x00")
req := &Packet{
OpCode: XS_RM,
Req: 0,
TxID: xs.tx,
Length: uint32(len(v)),
Value: v,
}
_, err := xs.DO(req)
return err
}
func (xs *XenStore) Write(path string, value string) error {
v := []byte(path + "\x00" + value)
req := &Packet{
OpCode: XS_WRITE,
Req: 0,
TxID: xs.tx,
Length: uint32(len(v)),
Value: v,
}
_, err := xs.DO(req)
return err
}
func (xs *XenStore) GetPermission(path string) (map[int]Permission, error) {
perm := make(map[int]Permission, 0)
v := []byte(path + "\x00")
req := &Packet{
OpCode: XS_GET_PERMS,
Req: 0,
TxID: xs.tx,
Length: uint32(len(v)),
Value: v,
}
resp, err := xs.DO(req)
if err != nil {
return nil, err
}
for _, e := range strings.Split(string(resp.Value[:len(resp.Value)-1]), "\x00") {
k, err := strconv.Atoi(e[1:])
if err != nil {
return nil, err
}
var p Permission
switch e[0] {
case 'n':
p = PERM_NONE
case 'r':
p = PERM_READ
case 'w':
p = PERM_WRITE
case 'b':
p = PERM_READWRITE
}
perm[k] = p
}
return perm, nil
}
func (xs *XenStore) Watch(path string) (<-chan Event, error) {
watcher := func() {
type XSData struct {
*Packet
Error error
}
xsDataChan := make(chan XSData, 100)
go func(r io.Reader, out chan<- XSData) {
for {
p, err := ReadPacket(r)
out <- XSData{Packet: p, Error: err}
}
}(xs.xbFileReader, xsDataChan)
xs.nonWatchQueue = make(chan []byte, 100)
for {
select {
case <-xs.watchStopChan:
fmt.Printf("watch receive stop signal, quit.")
xs.watchStopChan <- struct{}{}
return
case xsdata := <-xsDataChan:
if xsdata.Error != nil {
fmt.Printf("watch receive error: %#v", xsdata.Error)
return
}
switch xsdata.Packet.OpCode {
case XS_WATCH_EVENT:
parts := strings.SplitN(string(xsdata.Value), "\x00", 2)
path := parts[0]
token := parts[1]
data := []byte(parts[2])
if c, ok := xs.watchQueues[path]; ok {
c <- Event{token, data}
}
default:
var b bytes.Buffer
xsdata.Packet.Write(&b)
xs.nonWatchQueue <- b.Bytes()
}
}
}
}
xs.onceWatch.Do(watcher)
xs.muWatch.Lock()
defer xs.muWatch.Unlock()
if _, ok := xs.watchQueues[path]; !ok {
xs.watchQueues[path] = make(chan Event, 100)
}
return xs.watchQueues[path], nil
}
func (xs *XenStore) StopWatch() error {
xs.watchStopChan <- struct{}{}
<-xs.watchStoppedChan
xs.nonWatchQueue = nil
return nil
}
type CachedXenStore struct {
xs XenStoreClient
writeCache map[string]string
lastCommit map[string]time.Time
}
func NewCachedXenstore(tx uint32) (XenStoreClient, error) {
xs, err := NewXenstore(tx)
if err != nil {
return nil, err
}
return &CachedXenStore{
xs: xs,
writeCache: make(map[string]string, 0),
lastCommit: make(map[string]time.Time, 0),
}, nil
}
func (xs *CachedXenStore) Write(path string, value string) error {
if v, ok := xs.writeCache[path]; ok && v == value {
if t, ok := xs.lastCommit[path]; ok && t.After(time.Now().Add(-2*time.Minute)) {
return nil
}
}
err := xs.xs.Write(path, value)
if err != nil {
xs.writeCache[path] = value
xs.lastCommit[path] = time.Now()
}
return err
}
func (xs *CachedXenStore) Close() error {
return xs.xs.Close()
}
func (xs *CachedXenStore) DO(req *Packet) (resp *Packet, err error) {
return xs.xs.DO(req)
}
func (xs *CachedXenStore) Read(path string) (string, error) {
return xs.xs.Read(path)
}
func (xs *CachedXenStore) Mkdir(path string) error {
return xs.xs.Mkdir(path)
}
func (xs *CachedXenStore) Rm(path string) error {
return xs.xs.Rm(path)
}
func (xs *CachedXenStore) GetPermission(path string) (map[int]Permission, error) {
return xs.xs.GetPermission(path)
}
func (xs *CachedXenStore) Watch(path string) (<-chan Event, error) {
return xs.xs.Watch(path)
}
func (xs *CachedXenStore) StopWatch() error {
return xs.xs.StopWatch()
}
func (xs *CachedXenStore) Clear() {
xs.writeCache = make(map[string]string, 0)
xs.lastCommit = make(map[string]time.Time, 0)
}
func getDevPath() (devPath string, err error) {
devPaths := []string{
"/proc/xen/xenbus",
"/dev/xen/xenbus",
"/kern/xen/xenbus",
}
for _, devPath = range devPaths {
if _, err = os.Stat(devPath); err == nil {
return devPath, err
}
}
return "", fmt.Errorf("Cannot locate xenbus dev path in %v", devPaths)
}
|