-
Notifications
You must be signed in to change notification settings - Fork 23
/
enumerate.go
296 lines (272 loc) · 8.78 KB
/
enumerate.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
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
// +build linux,cgo
package udev
/*
#cgo LDFLAGS: -ludev
#include <libudev.h>
#include <linux/types.h>
#include <stdlib.h>
#include <linux/kdev_t.h>
*/
import "C"
import (
"errors"
"github.com/jkeiser/iter"
)
// Enumerate is an opaque struct wrapping a udev enumerate object.
type Enumerate struct {
ptr *C.struct_udev_enumerate
u *Udev
}
// Lock the udev context
func (e *Enumerate) lock() {
e.u.m.Lock()
}
// Unlock the udev context
func (e *Enumerate) unlock() {
e.u.m.Unlock()
}
// Unref the Enumerate object
func enumerateUnref(e *Enumerate) {
C.udev_enumerate_unref(e.ptr)
}
// AddMatchSubsystem adds a filter for a subsystem of the device to include in the list.
func (e *Enumerate) AddMatchSubsystem(subsystem string) (err error) {
e.lock()
defer e.unlock()
s := C.CString(subsystem)
defer freeCharPtr(s)
if C.udev_enumerate_add_match_subsystem(e.ptr, s) != 0 {
err = errors.New("udev: udev_enumerate_add_match_subsystem failed")
}
return
}
// AddNomatchSubsystem adds a filter for a subsystem of the device to exclude from the list.
func (e *Enumerate) AddNomatchSubsystem(subsystem string) (err error) {
e.lock()
defer e.unlock()
s := C.CString(subsystem)
defer freeCharPtr(s)
if C.udev_enumerate_add_nomatch_subsystem(e.ptr, s) != 0 {
err = errors.New("udev: udev_enumerate_add_nomatch_subsystem failed")
}
return
}
// AddMatchSysattr adds a filter for a sys attribute at the device to include in the list.
func (e *Enumerate) AddMatchSysattr(sysattr, value string) (err error) {
e.lock()
defer e.unlock()
s, v := C.CString(sysattr), C.CString(value)
defer freeCharPtr(s)
defer freeCharPtr(v)
if C.udev_enumerate_add_match_sysattr(e.ptr, s, v) != 0 {
err = errors.New("udev: udev_enumerate_add_match_sysattr failed")
}
return
}
// AddNomatchSysattr adds a filter for a sys attribute at the device to exclude from the list.
func (e *Enumerate) AddNomatchSysattr(sysattr, value string) (err error) {
e.lock()
defer e.unlock()
s, v := C.CString(sysattr), C.CString(value)
defer freeCharPtr(s)
defer freeCharPtr(v)
if C.udev_enumerate_add_nomatch_sysattr(e.ptr, s, v) != 0 {
err = errors.New("udev: udev_enumerate_add_nomatch_sysattr failed")
}
return
}
// AddMatchProperty adds a filter for a property of the device to include in the list.
func (e *Enumerate) AddMatchProperty(property, value string) (err error) {
e.lock()
defer e.unlock()
p, v := C.CString(property), C.CString(value)
defer freeCharPtr(p)
defer freeCharPtr(v)
if C.udev_enumerate_add_match_property(e.ptr, p, v) != 0 {
err = errors.New("udev: udev_enumerate_add_match_property failed")
}
return
}
// AddMatchSysname adds a filter for the name of the device to include in the list.
func (e *Enumerate) AddMatchSysname(sysname string) (err error) {
e.lock()
defer e.unlock()
s := C.CString(sysname)
defer freeCharPtr(s)
if C.udev_enumerate_add_match_sysname(e.ptr, s) != 0 {
err = errors.New("udev: udev_enumerate_add_match_sysname failed")
}
return
}
// AddMatchTag adds a filter for a tag of the device to include in the list.
func (e *Enumerate) AddMatchTag(tag string) (err error) {
e.lock()
defer e.unlock()
t := C.CString(tag)
defer freeCharPtr(t)
if C.udev_enumerate_add_match_tag(e.ptr, t) != 0 {
err = errors.New("udev: udev_enumerate_add_match_tag failed")
}
return
}
// AddMatchParent adds a filter for a parent Device to include in the list.
func (e *Enumerate) AddMatchParent(parent *Device) (err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_add_match_parent(e.ptr, parent.ptr) != 0 {
err = errors.New("udev: udev_enumerate_add_match_parent failed")
}
return
}
// AddMatchIsInitialized adds a filter matching only devices which udev has set up already.
// This makes sure, that the device node permissions and context are properly set and that network devices are fully renamed.
// Usually, devices which are found in the kernel but not already handled by udev, have still pending events.
// Services should subscribe to monitor events and wait for these devices to become ready, instead of using uninitialized devices.
// For now, this will not affect devices which do not have a device node and are not network interfaces.
func (e *Enumerate) AddMatchIsInitialized() (err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_add_match_is_initialized(e.ptr) != 0 {
err = errors.New("udev: udev_enumerate_add_match_is_initialized failed")
}
return
}
// AddSyspath adds a device to the list of enumerated devices, to retrieve it back sorted in dependency order.
func (e *Enumerate) AddSyspath(syspath string) (err error) {
e.lock()
defer e.unlock()
s := C.CString(syspath)
defer freeCharPtr(s)
if C.udev_enumerate_add_syspath(e.ptr, s) != 0 {
err = errors.New("udev: udev_enumerate_add_syspath failed")
}
return
}
// DeviceSyspaths retrieves a list of device syspaths matching the filter, sorted in dependency order.
func (e *Enumerate) DeviceSyspaths() (s []string, err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_scan_devices(e.ptr) < 0 {
err = errors.New("udev: udev_enumerate_scan_devices failed")
} else {
s = make([]string, 0)
for l := C.udev_enumerate_get_list_entry(e.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
s = append(s, C.GoString(C.udev_list_entry_get_name(l)))
}
}
return
}
// DeviceSyspathIterator returns an Iterator over the device syspaths matching the filter, sorted in dependency order.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to string.
func (e *Enumerate) DeviceSyspathIterator() (it iter.Iterator, err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_scan_devices(e.ptr) < 0 {
err = errors.New("udev: udev_enumerate_scan_devices failed")
} else {
l := C.udev_enumerate_get_list_entry(e.ptr)
it = iter.Iterator{
Next: func() (item interface{}, err error) {
e.lock()
defer e.unlock()
if l != nil {
item = C.GoString(C.udev_list_entry_get_name(l))
l = C.udev_list_entry_get_next(l)
} else {
err = iter.FINISHED
}
return
},
Close: func() {
},
}
}
return
}
// SubsystemSyspaths retrieves a list of subsystem syspaths matching the filter, sorted in dependency order.
func (e *Enumerate) SubsystemSyspaths() (s []string, err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_scan_subsystems(e.ptr) < 0 {
err = errors.New("udev: udev_enumerate_scan_subsystems failed")
} else {
s = make([]string, 0)
for l := C.udev_enumerate_get_list_entry(e.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
s = append(s, C.GoString(C.udev_list_entry_get_name(l)))
}
}
return
}
// DeviceSubsystemIterator returns an Iterator over the subsystem syspaths matching the filter, sorted in dependency order.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to string.
func (e *Enumerate) DeviceSubsystemIterator() (it iter.Iterator, err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_scan_subsystems(e.ptr) < 0 {
err = errors.New("udev: udev_enumerate_scan_devices failed")
} else {
l := C.udev_enumerate_get_list_entry(e.ptr)
it = iter.Iterator{
Next: func() (item interface{}, err error) {
e.lock()
defer e.unlock()
if l != nil {
item = C.GoString(C.udev_list_entry_get_name(l))
l = C.udev_list_entry_get_next(l)
} else {
err = iter.FINISHED
}
return
},
Close: func() {
},
}
}
return
}
// Devices retrieves a list of Devices matching the filter, sorted in dependency order.
func (e *Enumerate) Devices() (m []*Device, err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_scan_devices(e.ptr) < 0 {
err = errors.New("udev: udev_enumerate_scan_devices failed")
} else {
m = make([]*Device, 0)
for l := C.udev_enumerate_get_list_entry(e.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
s := C.udev_list_entry_get_name(l)
m = append(m, e.u.newDevice(C.udev_device_new_from_syspath(e.u.ptr, s)))
}
}
return
}
// DeviceIterator returns an Iterator over the Devices matching the filter, sorted in dependency order.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to *Device.
func (e *Enumerate) DeviceIterator() (it iter.Iterator, err error) {
e.lock()
defer e.unlock()
if C.udev_enumerate_scan_devices(e.ptr) < 0 {
err = errors.New("udev: udev_enumerate_scan_devices failed")
} else {
l := C.udev_enumerate_get_list_entry(e.ptr)
it = iter.Iterator{
Next: func() (item interface{}, err error) {
e.lock()
defer e.unlock()
if l != nil {
s := C.udev_list_entry_get_name(l)
item = e.u.newDevice(C.udev_device_new_from_syspath(e.u.ptr, s))
l = C.udev_list_entry_get_next(l)
} else {
err = iter.FINISHED
}
return
},
Close: func() {
},
}
}
return
}