-
Notifications
You must be signed in to change notification settings - Fork 0
/
udevcheck.go
150 lines (126 loc) · 2.93 KB
/
udevcheck.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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"time"
)
const baseDevPath = "/sys/devices"
//Device type for devices
// contains a, "obj" which is the path to the kernel file and "env" which is a
// mapping that contains information about the device
type Device struct {
obj string
env map[string]string
}
//RunWaitUdev wait on changes to devices (every 5 seconds)
func RunWaitUdev(changed chan<- bool) {
devices, err := ExistingDevices("usb")
if err != nil {
fmt.Println(err)
}
for {
time.Sleep(time.Second * 5)
ndevices, err := ExistingDevices("usb")
if err != nil {
fmt.Println(err)
}
if !reflect.DeepEqual(devices, ndevices) {
devchanged := len(devices) - len(ndevices)
if devchanged > 0 {
warnLog.Printf("%d devices removed\n", devchanged)
} else {
warnLog.Printf("%d devices added\n", devchanged*-1)
}
select {
case changed <- true:
default:
}
devices = ndevices
}
}
}
//ExistingDevices return all plugged devices matched by the matcher
// All uevent files inside /sys/devices is crawled to match right env values
func ExistingDevices(subsystem string) ([]Device, error) {
devices := make([]Device, 0)
err := filepath.Walk(baseDevPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || info.Name() != "uevent" {
return nil
}
env, err := getEventFromUEventFile(path)
if err != nil {
return err
}
kernelObject := filepath.Dir(path)
// Append to env subsystem if existing
subsys := filepath.Join(kernelObject, "subsystem")
if link, err := os.Readlink(subsys); err == nil {
if ss := filepath.Base(link); strings.Contains(ss, subsystem) {
env["SUBSYSTEM"] = ss
} else {
return nil
}
}
// get human readable product name.
product := filepath.Join(kernelObject, "product")
if _, err := os.Stat(product); err == nil {
b, err := ioutil.ReadFile(product)
if err != nil {
return err
}
env["PRODUCTNAME"] = string(b)
}
if len(env) < 1 {
return nil
}
if env["DRIVER"] != "usb" {
return nil
}
devices = append(devices,
Device{
obj: kernelObject,
env: env,
})
return nil
})
sort.Slice(devices, func(i, j int) bool {
return devices[i].obj > devices[j].obj
})
return devices, err
}
// getEventFromUEventFile return all env var define in file
// syntax: name=value for each line
// Function use for /sys/.../uevent files
func getEventFromUEventFile(path string) (rv map[string]string, err error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
rv = make(map[string]string)
buf := bufio.NewScanner(bytes.NewBuffer(data))
var line string
for buf.Scan() {
line = buf.Text()
field := strings.SplitN(line, "=", 2)
if len(field) != 2 {
return
}
rv[field[0]] = field[1]
}
return
}