forked from jochenvg/go-udev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
353 lines (320 loc) · 9.98 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
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
// +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"
import "github.com/jkeiser/iter"
// Device wraps a libudev device object
type Device struct {
ptr *C.struct_udev_device
u *Udev
}
// Lock the udev context
func (d *Device) lock() {
d.u.m.Lock()
}
// Unlock the udev context
func (d *Device) unlock() {
d.u.m.Unlock()
}
func deviceUnref(d *Device) {
C.udev_device_unref(d.ptr)
}
// Parent returns the parent Device, or nil if the receiver has no parent Device
func (d *Device) Parent() *Device {
d.lock()
defer d.unlock()
ptr := C.udev_device_get_parent(d.ptr)
if ptr != nil {
C.udev_device_ref(ptr)
}
return d.u.newDevice(ptr)
}
// ParentWithSubsystemDevtype returns the parent Device with the given subsystem and devtype,
// or nil if the receiver has no such parent device
func (d *Device) ParentWithSubsystemDevtype(subsystem, devtype string) *Device {
d.lock()
defer d.unlock()
ss, dt := C.CString(subsystem), C.CString(devtype)
defer freeCharPtr(ss)
defer freeCharPtr(dt)
ptr := C.udev_device_get_parent_with_subsystem_devtype(d.ptr, ss, dt)
if ptr != nil {
C.udev_device_ref(ptr)
}
return d.u.newDevice(ptr)
}
// Devpath returns the kernel devpath value of the udev device.
// The path does not contain the sys mount point, and starts with a '/'.
func (d *Device) Devpath() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_devpath(d.ptr))
}
// Subsystem returns the subsystem string of the udev device.
// The string does not contain any "/".
func (d *Device) Subsystem() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_subsystem(d.ptr))
}
// Devtype returns the devtype string of the udev device.
func (d *Device) Devtype() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_devtype(d.ptr))
}
// Sysname returns the sysname of the udev device (e.g. ttyS3, sda1...).
func (d *Device) Sysname() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_sysname(d.ptr))
}
// Syspath returns the sys path of the udev device.
// The path is an absolute path and starts with the sys mount point.
func (d *Device) Syspath() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_syspath(d.ptr))
}
// Sysnum returns the trailing number of of the device name
func (d *Device) Sysnum() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_sysnum(d.ptr))
}
// Devnode returns the device node file name belonging to the udev device.
// The path is an absolute path, and starts with the device directory.
func (d *Device) Devnode() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_devnode(d.ptr))
}
// IsInitialized checks if udev has already handled the device and has set up
// device node permissions and context, or has renamed a network device.
//
// This is only implemented for devices with a device node or network interfaces.
// All other devices return 1 here.
func (d *Device) IsInitialized() bool {
d.lock()
defer d.unlock()
return C.udev_device_get_is_initialized(d.ptr) != 0
}
// Devlinks retrieves the map of device links pointing to the device file of the udev device.
// The path is an absolute path, and starts with the device directory.
func (d *Device) Devlinks() (r map[string]struct{}) {
d.lock()
defer d.unlock()
r = make(map[string]struct{})
for l := C.udev_device_get_devlinks_list_entry(d.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
r[C.GoString(C.udev_list_entry_get_name(l))] = struct{}{}
}
return
}
// DevlinkIterator returns an Iterator over the device links pointing to the device file of the udev device.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to string.
func (d *Device) DevlinkIterator() iter.Iterator {
d.lock()
defer d.unlock()
l := C.udev_device_get_devlinks_list_entry(d.ptr)
return iter.Iterator{
Next: func() (item interface{}, err error) {
d.lock()
defer d.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() {
},
}
}
// Properties retrieves a map[string]string of key/value device properties of the udev device.
func (d *Device) Properties() (r map[string]string) {
d.lock()
defer d.unlock()
r = make(map[string]string)
for l := C.udev_device_get_properties_list_entry(d.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
r[C.GoString(C.udev_list_entry_get_name(l))] = C.GoString(C.udev_list_entry_get_value(l))
}
return
}
// PropertyIterator returns an Iterator over the key/value device properties of the udev device.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to []string,
// which will have length 2 and represent a Key/Value pair.
func (d *Device) PropertyIterator() iter.Iterator {
d.lock()
defer d.unlock()
l := C.udev_device_get_properties_list_entry(d.ptr)
return iter.Iterator{
Next: func() (item interface{}, err error) {
d.lock()
defer d.unlock()
if l != nil {
item = []string{
C.GoString(C.udev_list_entry_get_name(l)),
C.GoString(C.udev_list_entry_get_value(l)),
}
l = C.udev_list_entry_get_next(l)
} else {
err = iter.FINISHED
}
return
},
Close: func() {
},
}
}
// Tags retrieves the Set of tags attached to the udev device.
func (d *Device) Tags() (r map[string]struct{}) {
d.lock()
defer d.unlock()
r = make(map[string]struct{})
for l := C.udev_device_get_tags_list_entry(d.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
r[C.GoString(C.udev_list_entry_get_name(l))] = struct{}{}
}
return
}
// TagIterator returns an Iterator over the tags attached to the udev device.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to string.
func (d *Device) TagIterator() iter.Iterator {
d.lock()
defer d.unlock()
l := C.udev_device_get_tags_list_entry(d.ptr)
return iter.Iterator{
Next: func() (item interface{}, err error) {
d.lock()
defer d.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() {
},
}
}
// Sysattrs returns a Set with the systems attributes of the udev device.
func (d *Device) Sysattrs() (r map[string]struct{}) {
d.lock()
defer d.unlock()
r = make(map[string]struct{})
for l := C.udev_device_get_sysattr_list_entry(d.ptr); l != nil; l = C.udev_list_entry_get_next(l) {
r[C.GoString(C.udev_list_entry_get_name(l))] = struct{}{}
}
return
}
// SysattrIterator returns an Iterator over the systems attributes of the udev device.
// The Iterator is using the github.com/jkeiser/iter package.
// Values are returned as an interface{} and should be cast to string.
func (d *Device) SysattrIterator() iter.Iterator {
d.lock()
defer d.unlock()
l := C.udev_device_get_sysattr_list_entry(d.ptr)
return iter.Iterator{
Next: func() (item interface{}, err error) {
d.lock()
defer d.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() {
},
}
}
// PropertyValue retrieves the value of a device property
func (d *Device) PropertyValue(key string) string {
d.lock()
defer d.unlock()
k := C.CString(key)
defer freeCharPtr(k)
return C.GoString(C.udev_device_get_property_value(d.ptr, k))
}
// Driver returns the driver for the receiver
func (d *Device) Driver() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_driver(d.ptr))
}
// Devnum returns the device major/minor number.
func (d *Device) Devnum() Devnum {
d.lock()
defer d.unlock()
return Devnum{C.udev_device_get_devnum(d.ptr)}
}
// Action returns the action for the event.
// This is only valid if the device was received through a monitor.
// Devices read from sys do not have an action string.
// Usual actions are: add, remove, change, online, offline.
func (d *Device) Action() string {
d.lock()
defer d.unlock()
return C.GoString(C.udev_device_get_action(d.ptr))
}
// Seqnum returns the sequence number of the event.
// This is only valid if the device was received through a monitor.
// Devices read from sys do not have a sequence number.
func (d *Device) Seqnum() uint64 {
d.lock()
defer d.unlock()
return uint64(C.udev_device_get_seqnum(d.ptr))
}
// UsecSinceInitialized returns the number of microseconds passed since udev set up the device for the first time.
// This is only implemented for devices with need to store properties in the udev database.
// All other devices return 0 here.
func (d *Device) UsecSinceInitialized() uint64 {
d.lock()
defer d.unlock()
return uint64(C.udev_device_get_usec_since_initialized(d.ptr))
}
// SysattrValue retrieves the content of a sys attribute file, and returns an empty string if there is no sys attribute value.
// The retrieved value is cached in the device.
// Repeated calls will return the same value and not open the attribute again.
func (d *Device) SysattrValue(sysattr string) string {
d.lock()
defer d.unlock()
s := C.CString(sysattr)
defer freeCharPtr(s)
return C.GoString(C.udev_device_get_sysattr_value(d.ptr, s))
}
// SetSysattrValue sets the content of a sys attribute file, and returns an error if this fails.
func (d *Device) SetSysattrValue(sysattr, value string) (err error) {
d.lock()
defer d.unlock()
sa, val := C.CString(sysattr), C.CString(value)
defer freeCharPtr(sa)
defer freeCharPtr(val)
if C.udev_device_set_sysattr_value(d.ptr, sa, val) < 0 {
err = errors.New("udev: udev_device_set_sysattr_value failed")
}
return
}
// HasTag checks if the udev device has the tag specified
func (d *Device) HasTag(tag string) bool {
d.lock()
defer d.unlock()
t := C.CString(tag)
defer freeCharPtr(t)
return C.udev_device_has_tag(d.ptr, t) != 0
}