github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/udev/device.go (about) 1 /* 2 Copyright 2018 OpenEBS Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // +build linux,cgo 18 19 package udev 20 21 /* 22 #cgo LDFLAGS: -ludev 23 #include <libudev.h> 24 */ 25 import "C" 26 import "errors" 27 28 // UdevDevice wraps a libudev device object 29 type UdevDevice struct { 30 udptr *C.struct_udev_device 31 } 32 33 // newUdevDevice is a private helper function and returns a pointer to a new device 34 // it returns nil if the pointer passed is NULL. 35 func newUdevDevice(ptr *C.struct_udev_device) (*UdevDevice, error) { 36 if ptr == nil { 37 return nil, errors.New("unable to create Udevice object for null struct struct_udev_device") 38 } 39 ud := &UdevDevice{ 40 udptr: ptr, 41 } 42 return ud, nil 43 } 44 45 // GetPropertyValue retrieves the value of a device property 46 func (ud *UdevDevice) GetPropertyValue(key string) string { 47 k := C.CString(key) 48 defer freeCharPtr(k) 49 return C.GoString(C.udev_device_get_property_value(ud.udptr, k)) 50 } 51 52 // GetSysattrValue retrieves the content of a sys attribute file 53 // returns an empty string if there is no sys attribute value. 54 // The retrieved value is cached in the device. Repeated calls 55 // will return the same value and not open the attribute again. 56 func (ud *UdevDevice) GetSysattrValue(sysattr string) string { 57 k := C.CString(sysattr) 58 defer freeCharPtr(k) 59 return C.GoString(C.udev_device_get_sysattr_value(ud.udptr, k)) 60 } 61 62 // GetDevtype returns the devtype string of the udev device. 63 func (ud *UdevDevice) GetDevtype() string { 64 return C.GoString(C.udev_device_get_devtype(ud.udptr)) 65 } 66 67 // GetDevnode returns the device node file name belonging to the udev device. 68 // The path is an absolute path, and starts with the device directory. 69 func (ud *UdevDevice) GetDevnode() string { 70 return C.GoString(C.udev_device_get_devnode(ud.udptr)) 71 } 72 73 // GetAction returns device action when it is monitored. 74 // It can be add,remove,online,offline,change 75 func (ud *UdevDevice) GetAction() string { 76 return C.GoString(C.udev_device_get_action(ud.udptr)) 77 } 78 79 // UdevDeviceUnref frees udev_device structure. 80 func (ud *UdevDevice) UdevDeviceUnref() { 81 C.udev_device_unref(ud.udptr) 82 }