-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdevice.go
105 lines (89 loc) · 2.17 KB
/
device.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
package crawler
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pilebones/go-udev/netlink"
)
const (
BASE_DEVPATH = "/sys/devices"
)
type Device struct {
KObj string
Env map[string]string
}
var ErrAbortReceived = errors.New("abort signal received")
// ExistingDevices return all plugged devices matched by the matcher
// All uevent files inside /sys/devices is crawled to match right env values
func ExistingDevices(queue chan Device, errs chan error, matcher netlink.Matcher) chan struct{} {
quit := make(chan struct{}, 1)
if matcher != nil {
if err := matcher.Compile(); err != nil {
errs <- fmt.Errorf("wrong matcher, err: %w", err)
quit <- struct{}{}
close(queue)
return quit
}
}
go func() {
err := filepath.Walk(BASE_DEVPATH, func(path string, info os.FileInfo, err error) error {
select {
case <-quit:
return ErrAbortReceived
default:
if err != nil {
return err
}
if info.IsDir() || info.Name() != "uevent" {
return nil
}
env, err := getEventFromUEventFile(path)
if err != nil {
return err
}
kObj := filepath.Dir(path)
// Append to env subsystem if existing
if link, err := os.Readlink(kObj + "/subsystem"); err == nil {
env["SUBSYSTEM"] = filepath.Base(link)
}
if matcher == nil || matcher.EvaluateEnv(env) {
queue <- Device{
KObj: kObj,
Env: env,
}
}
return nil
}
})
if err != nil {
errs <- err
}
close(queue)
}()
return quit
}
// getEventFromUEventFile return all variables defined in /sys/**/uevent files
func getEventFromUEventFile(path string) (map[string]string, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return getEventFromUEventData(data)
}
// syntax: name=value for each line
func getEventFromUEventData(data []byte) (map[string]string, error) {
rv := make(map[string]string)
buf := bufio.NewScanner(bytes.NewBuffer(data))
for buf.Scan() {
field := strings.SplitN(buf.Text(), "=", 2)
if len(field) != 2 {
return rv, fmt.Errorf("cannot parse uevent data: did not find '=' in '%s'", buf.Text())
}
rv[field[0]] = field[1]
}
return rv, nil
}