github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/udev/enumerate.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 // UdevEnumerate wraps a libudev udev_enumerate object 29 type UdevEnumerate struct { 30 ueptr *C.struct_udev_enumerate 31 } 32 33 // AddSubsystemFilter adds filter in UdeviceMon struct. 34 // This filter is efficiently executed inside kernel, and libudev 35 // subscribers will usually not be woken up for devices which do not match. 36 func (ue *UdevEnumerate) AddSubsystemFilter(subSystem string) error { 37 subsystem := C.CString(subSystem) 38 defer freeCharPtr(subsystem) 39 ret := C.udev_enumerate_add_match_subsystem(ue.ueptr, subsystem) 40 if ret < 0 { 41 return errors.New("unable to apply subsystem filter") 42 } 43 return nil 44 } 45 46 // ScanDevices scan devices in system and returns list 47 // of devices present in system 48 func (ue *UdevEnumerate) ScanDevices() error { 49 ret := C.udev_enumerate_scan_devices(ue.ueptr) 50 if ret < 0 { 51 return errors.New("unable to scan device list") 52 } 53 return nil 54 } 55 56 // ListEntry returns UdevListEntry struct from which we can get device. 57 func (ue *UdevEnumerate) ListEntry() *UdevListEntry { 58 return newUdevListEntry(C.udev_enumerate_get_list_entry(ue.ueptr)) 59 } 60 61 // UnrefUdevEnumerate frees udev_enumerate structure. 62 func (ue *UdevEnumerate) UnrefUdevEnumerate() { 63 C.udev_enumerate_unref(ue.ueptr) 64 }