-
Notifications
You must be signed in to change notification settings - Fork 2
/
kobject.go
92 lines (71 loc) · 1.77 KB
/
kobject.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package kobject implements Linux kernel kobject uvent watcher.
package kobject
import (
"fmt"
"sync"
"github.com/mdlayher/kobject"
"go.uber.org/zap"
)
const readBufferSize = 64 * 1024 * 1024
// Event is exported.
type Event = kobject.Event
// Re-export action constants.
const (
ActionAdd = kobject.Add
ActionRemove = kobject.Remove
ActionChange = kobject.Change
ActionMove = kobject.Move
ActionOnline = kobject.Online
ActionOffline = kobject.Offline
ActionBind = kobject.Bind
ActionUnbind = kobject.Unbind
)
// Watcher is a kobject uvent watcher.
type Watcher struct {
wg sync.WaitGroup
cli *kobject.Client
}
// NewWatcher creates a new kobject watcher.
func NewWatcher() (*Watcher, error) {
cli, err := kobject.New()
if err != nil {
return nil, fmt.Errorf("failed to create kobject client: %w", err)
}
if err = cli.SetReadBuffer(readBufferSize); err != nil {
return nil, err
}
return &Watcher{
cli: cli,
}, nil
}
// Close the watcher.
func (w *Watcher) Close() error {
if err := w.cli.Close(); err != nil {
return err
}
w.wg.Wait()
return nil
}
// Run the watcher, returns the channel of events.
func (w *Watcher) Run(logger *zap.Logger) <-chan *Event {
ch := make(chan *kobject.Event, 128)
w.wg.Add(1)
go func() {
defer w.wg.Done()
defer close(ch)
for {
ev, err := w.cli.Receive()
if err != nil {
if err.Error() != "use of closed file" { // unfortunately not an exported error, just errors.New()
logger.Error("failed to receive kobject event", zap.Error(err))
}
return
}
ch <- ev
}
}()
return ch
}