Class UdevFunctions
java.lang.Object
oshi.ffm.ForeignFunctions
oshi.ffm.linux.UdevFunctions
FFM bindings for the udev (userspace device management) library.
libudev provides APIs to introspect and enumerate devices on the local Linux system. All udev objects are opaque
MemorySegment handles. Callers must unref context, enumerate, and device handles when done.
Loading can be suppressed via the oshi.os.linux.allowudev configuration property.
-
Field Summary
Fields inherited from class ForeignFunctions
CAPTURE_CALL_STATE, CAPTURED_STATE_LAYOUT, ERRNO_HANDLE, LIBRARY_ARENA, LINKER, NATIVE_LONG_SIZE, NATIVE_POINTER_SIZE, NATIVE_SIZE_T_SIZE, SYMBOL_LOOKUP -
Method Summary
Modifier and TypeMethodDescriptionstatic intaddMatchSubsystem(MemorySegment enumerate, String subsystem, Arena arena) Allocates a native UTF-8 string in the given arena and callsudev_enumerate_add_match_subsystem(MemorySegment, MemorySegment).static MemorySegmentdeviceNewFromSyspath(MemorySegment udev, String syspath, Arena arena) Allocates a native UTF-8 string in the given arena and callsudev_device_new_from_syspath(MemorySegment, MemorySegment).static MemorySegmentgetParentWithSubsystemDevtype(MemorySegment device, String subsystem, String devtype, Arena arena) Allocates native UTF-8 strings in the given arena and callsudev_device_get_parent_with_subsystem_devtype(MemorySegment, MemorySegment, MemorySegment).static StringgetPropertyValue(MemorySegment device, String key, Arena arena) Allocates a native UTF-8 string in the given arena and callsudev_device_get_property_value(MemorySegment, MemorySegment).static StringgetString(MemorySegment ptr, Arena arena) Reads a udev string result (syspath, sysattr, property, etc.) into a Java String, using the provided arena for the reinterpret scope.static StringgetSysattrValue(MemorySegment device, String sysattr, Arena arena) Allocates native UTF-8 strings in the given arena and callsudev_device_get_sysattr_value(MemorySegment, MemorySegment).static booleanReturns whether libudev was successfully loaded and all symbols bound.static MemorySegmentudev_device_get_devnode(MemorySegment device) Gets the devnode of a device (e.g.static MemorySegmentudev_device_get_devtype(MemorySegment device) Gets the devtype of a device (e.g.static MemorySegmentudev_device_get_parent(MemorySegment device) Gets the parent device.static MemorySegmentudev_device_get_parent_with_subsystem_devtype(MemorySegment device, MemorySegment subsystem, MemorySegment devtype) Gets the first parent device matching a subsystem and devtype.static MemorySegmentudev_device_get_property_value(MemorySegment device, MemorySegment key) Retrieves a udev property value from a device.static MemorySegmentGets the subsystem of a device (e.g.static MemorySegmentudev_device_get_sysattr_value(MemorySegment device, MemorySegment sysattr) Retrieves a sysfs attribute value from a device.static MemorySegmentudev_device_get_sysname(MemorySegment device) Gets the sysname of a device.static MemorySegmentudev_device_get_syspath(MemorySegment device) Gets the syspath of a device.static MemorySegmentudev_device_new_from_syspath(MemorySegment udev, MemorySegment syspath) Creates a udev device from a syspath.static voidudev_device_unref(MemorySegment device) Drops a reference to a udev device.static intudev_enumerate_add_match_subsystem(MemorySegment enumerate, MemorySegment subsystem) Adds a subsystem filter to the enumerate object.static MemorySegmentudev_enumerate_get_list_entry(MemorySegment enumerate) Gets the first list entry from an enumerate object.static MemorySegmentCreates a new udev enumerate object.static intudev_enumerate_scan_devices(MemorySegment enumerate) Scans/sysfor devices matching the enumerate filters.static voidudev_enumerate_unref(MemorySegment enumerate) Drops a reference to a udev enumerate object.static MemorySegmentGets the name (syspath) of a list entry.static MemorySegmentGets the next entry in a list.static MemorySegmentudev_new()Allocates a new udev context.static voidudev_unref(MemorySegment udev) Drops a reference to a udev context.Methods inherited from class ForeignFunctions
downcall, getByteArrayFromNativePointer, getErrno, getStringFromNativePointer, getStructFromNativePointer, lib, libraryLookup
-
Method Details
-
isAvailable
public static boolean isAvailable()Returns whether libudev was successfully loaded and all symbols bound.- Returns:
trueif libudev is available
-
udev_new
Allocates a new udev context. The caller must calludev_unrefwhen done.- Returns:
- opaque udev context handle, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_unref
Drops a reference to a udev context. Once the reference count hits 0, the context is destroyed.- Parameters:
udev- the udev context handle- Throws:
Throwable- if the native call fails
-
udev_enumerate_new
Creates a new udev enumerate object. The caller must calludev_enumerate_unrefwhen done.- Parameters:
udev- the udev context handle- Returns:
- opaque enumerate handle, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_enumerate_unref
Drops a reference to a udev enumerate object.- Parameters:
enumerate- the enumerate handle- Throws:
Throwable- if the native call fails
-
udev_enumerate_add_match_subsystem
public static int udev_enumerate_add_match_subsystem(MemorySegment enumerate, MemorySegment subsystem) throws Throwable Adds a subsystem filter to the enumerate object.- Parameters:
enumerate- the enumerate handlesubsystem- the subsystem name as a native string segment- Returns:
- 0 or greater on success
- Throws:
Throwable- if the native call fails
-
udev_enumerate_scan_devices
Scans/sysfor devices matching the enumerate filters.- Parameters:
enumerate- the enumerate handle- Returns:
- 0 or greater on success
- Throws:
Throwable- if the native call fails
-
udev_enumerate_get_list_entry
Gets the first list entry from an enumerate object.- Parameters:
enumerate- the enumerate handle- Returns:
- the first list entry handle, or
MemorySegment.NULLif empty - Throws:
Throwable- if the native call fails
-
udev_list_entry_get_next
Gets the next entry in a list.- Parameters:
entry- the current list entry handle- Returns:
- the next list entry handle, or
MemorySegment.NULLif none - Throws:
Throwable- if the native call fails
-
udev_list_entry_get_name
Gets the name (syspath) of a list entry.- Parameters:
entry- the list entry handle- Returns:
- a native pointer to the name string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_new_from_syspath
public static MemorySegment udev_device_new_from_syspath(MemorySegment udev, MemorySegment syspath) throws Throwable Creates a udev device from a syspath. The caller must calludev_device_unrefwhen done.- Parameters:
udev- the udev context handlesyspath- native string segment for the/syspath- Returns:
- opaque device handle, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_unref
Drops a reference to a udev device.- Parameters:
device- the device handle- Throws:
Throwable- if the native call fails
-
udev_device_get_parent
Gets the parent device. The returned handle is owned by the child; do not unref it separately.- Parameters:
device- the device handle- Returns:
- the parent device handle, or
MemorySegment.NULLif none - Throws:
Throwable- if the native call fails
-
udev_device_get_parent_with_subsystem_devtype
public static MemorySegment udev_device_get_parent_with_subsystem_devtype(MemorySegment device, MemorySegment subsystem, MemorySegment devtype) throws Throwable Gets the first parent device matching a subsystem and devtype. The returned handle is owned by the child; do not unref it separately.- Parameters:
device- the device handlesubsystem- native string segment for the subsystem to matchdevtype- native string segment for the devtype to match- Returns:
- the matching parent device handle, or
MemorySegment.NULLif none - Throws:
Throwable- if the native call fails
-
udev_device_get_syspath
Gets the syspath of a device.- Parameters:
device- the device handle- Returns:
- a native pointer to the syspath string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_get_sysname
Gets the sysname of a device.- Parameters:
device- the device handle- Returns:
- a native pointer to the sysname string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_get_devnode
Gets the devnode of a device (e.g./dev/sda).- Parameters:
device- the device handle- Returns:
- a native pointer to the devnode string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_get_devtype
Gets the devtype of a device (e.g."disk","partition","usb_device").- Parameters:
device- the device handle- Returns:
- a native pointer to the devtype string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_get_subsystem
Gets the subsystem of a device (e.g."block","net","usb").- Parameters:
device- the device handle- Returns:
- a native pointer to the subsystem string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_get_sysattr_value
public static MemorySegment udev_device_get_sysattr_value(MemorySegment device, MemorySegment sysattr) throws Throwable Retrieves a sysfs attribute value from a device.- Parameters:
device- the device handlesysattr- native string segment for the attribute name- Returns:
- a native pointer to the attribute value string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
udev_device_get_property_value
public static MemorySegment udev_device_get_property_value(MemorySegment device, MemorySegment key) throws Throwable Retrieves a udev property value from a device.- Parameters:
device- the device handlekey- native string segment for the property key- Returns:
- a native pointer to the property value string, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
getString
Reads a udev string result (syspath, sysattr, property, etc.) into a Java String, using the provided arena for the reinterpret scope.- Parameters:
ptr- the native pointer returned by a udev getterarena- the arena to scope the reinterpreted segment to- Returns:
- the Java string, or
nullif the pointer is null orMemorySegment.NULL
-
addMatchSubsystem
public static int addMatchSubsystem(MemorySegment enumerate, String subsystem, Arena arena) throws Throwable Allocates a native UTF-8 string in the given arena and callsudev_enumerate_add_match_subsystem(MemorySegment, MemorySegment).- Parameters:
enumerate- the enumerate handlesubsystem- the subsystem namearena- the arena to allocate the string in- Returns:
- 0 or greater on success
- Throws:
Throwable- if the native call fails
-
deviceNewFromSyspath
public static MemorySegment deviceNewFromSyspath(MemorySegment udev, String syspath, Arena arena) throws Throwable Allocates a native UTF-8 string in the given arena and callsudev_device_new_from_syspath(MemorySegment, MemorySegment).- Parameters:
udev- the udev context handlesyspath- the syspath stringarena- the arena to allocate the string in- Returns:
- opaque device handle, or
MemorySegment.NULLon failure - Throws:
Throwable- if the native call fails
-
getSysattrValue
public static String getSysattrValue(MemorySegment device, String sysattr, Arena arena) throws Throwable Allocates native UTF-8 strings in the given arena and callsudev_device_get_sysattr_value(MemorySegment, MemorySegment).- Parameters:
device- the device handlesysattr- the attribute namearena- the arena to allocate the string in- Returns:
- the attribute value, or
nullif not found - Throws:
Throwable- if the native call fails
-
getPropertyValue
public static String getPropertyValue(MemorySegment device, String key, Arena arena) throws Throwable Allocates a native UTF-8 string in the given arena and callsudev_device_get_property_value(MemorySegment, MemorySegment).- Parameters:
device- the device handlekey- the property keyarena- the arena to allocate the string in- Returns:
- the property value, or
nullif not found - Throws:
Throwable- if the native call fails
-
getParentWithSubsystemDevtype
public static MemorySegment getParentWithSubsystemDevtype(MemorySegment device, String subsystem, String devtype, Arena arena) throws Throwable Allocates native UTF-8 strings in the given arena and callsudev_device_get_parent_with_subsystem_devtype(MemorySegment, MemorySegment, MemorySegment).- Parameters:
device- the device handlesubsystem- the subsystem to matchdevtype- the devtype to matcharena- the arena to allocate strings in- Returns:
- the matching parent device handle, or
MemorySegment.NULLif none - Throws:
Throwable- if the native call fails
-