Class UdevFunctions

java.lang.Object
oshi.ffm.ForeignFunctions
oshi.ffm.linux.UdevFunctions

public final class UdevFunctions extends ForeignFunctions
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.

  • Method Details

    • isAvailable

      public static boolean isAvailable()
      Returns whether libudev was successfully loaded and all symbols bound.
      Returns:
      true if libudev is available
    • udev_new

      public static MemorySegment udev_new() throws Throwable
      Allocates a new udev context. The caller must call udev_unref when done.
      Returns:
      opaque udev context handle, or MemorySegment.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_unref

      public static void udev_unref(MemorySegment udev) throws Throwable
      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

      public static MemorySegment udev_enumerate_new(MemorySegment udev) throws Throwable
      Creates a new udev enumerate object. The caller must call udev_enumerate_unref when done.
      Parameters:
      udev - the udev context handle
      Returns:
      opaque enumerate handle, or MemorySegment.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_enumerate_unref

      public static void udev_enumerate_unref(MemorySegment enumerate) throws Throwable
      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 handle
      subsystem - 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

      public static int udev_enumerate_scan_devices(MemorySegment enumerate) throws Throwable
      Scans /sys for 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

      public static MemorySegment udev_enumerate_get_list_entry(MemorySegment enumerate) throws Throwable
      Gets the first list entry from an enumerate object.
      Parameters:
      enumerate - the enumerate handle
      Returns:
      the first list entry handle, or MemorySegment.NULL if empty
      Throws:
      Throwable - if the native call fails
    • udev_list_entry_get_next

      public static MemorySegment udev_list_entry_get_next(MemorySegment entry) throws Throwable
      Gets the next entry in a list.
      Parameters:
      entry - the current list entry handle
      Returns:
      the next list entry handle, or MemorySegment.NULL if none
      Throws:
      Throwable - if the native call fails
    • udev_list_entry_get_name

      public static MemorySegment udev_list_entry_get_name(MemorySegment entry) throws Throwable
      Gets the name (syspath) of a list entry.
      Parameters:
      entry - the list entry handle
      Returns:
      a native pointer to the name string, or MemorySegment.NULL on 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 call udev_device_unref when done.
      Parameters:
      udev - the udev context handle
      syspath - native string segment for the /sys path
      Returns:
      opaque device handle, or MemorySegment.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_device_unref

      public static void udev_device_unref(MemorySegment device) throws Throwable
      Drops a reference to a udev device.
      Parameters:
      device - the device handle
      Throws:
      Throwable - if the native call fails
    • udev_device_get_parent

      public static MemorySegment udev_device_get_parent(MemorySegment device) throws Throwable
      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.NULL if 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 handle
      subsystem - native string segment for the subsystem to match
      devtype - native string segment for the devtype to match
      Returns:
      the matching parent device handle, or MemorySegment.NULL if none
      Throws:
      Throwable - if the native call fails
    • udev_device_get_syspath

      public static MemorySegment udev_device_get_syspath(MemorySegment device) throws Throwable
      Gets the syspath of a device.
      Parameters:
      device - the device handle
      Returns:
      a native pointer to the syspath string, or MemorySegment.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_device_get_sysname

      public static MemorySegment udev_device_get_sysname(MemorySegment device) throws Throwable
      Gets the sysname of a device.
      Parameters:
      device - the device handle
      Returns:
      a native pointer to the sysname string, or MemorySegment.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_device_get_devnode

      public static MemorySegment udev_device_get_devnode(MemorySegment device) throws Throwable
      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.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_device_get_devtype

      public static MemorySegment udev_device_get_devtype(MemorySegment device) throws Throwable
      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.NULL on failure
      Throws:
      Throwable - if the native call fails
    • udev_device_get_subsystem

      public static MemorySegment udev_device_get_subsystem(MemorySegment device) throws Throwable
      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.NULL on 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 handle
      sysattr - native string segment for the attribute name
      Returns:
      a native pointer to the attribute value string, or MemorySegment.NULL on 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 handle
      key - native string segment for the property key
      Returns:
      a native pointer to the property value string, or MemorySegment.NULL on failure
      Throws:
      Throwable - if the native call fails
    • getString

      public static String getString(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.
      Parameters:
      ptr - the native pointer returned by a udev getter
      arena - the arena to scope the reinterpreted segment to
      Returns:
      the Java string, or null if the pointer is null or MemorySegment.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 calls udev_enumerate_add_match_subsystem(MemorySegment, MemorySegment).
      Parameters:
      enumerate - the enumerate handle
      subsystem - the subsystem name
      arena - 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 calls udev_device_new_from_syspath(MemorySegment, MemorySegment).
      Parameters:
      udev - the udev context handle
      syspath - the syspath string
      arena - the arena to allocate the string in
      Returns:
      opaque device handle, or MemorySegment.NULL on 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 calls udev_device_get_sysattr_value(MemorySegment, MemorySegment).
      Parameters:
      device - the device handle
      sysattr - the attribute name
      arena - the arena to allocate the string in
      Returns:
      the attribute value, or null if 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 calls udev_device_get_property_value(MemorySegment, MemorySegment).
      Parameters:
      device - the device handle
      key - the property key
      arena - the arena to allocate the string in
      Returns:
      the property value, or null if 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 calls udev_device_get_parent_with_subsystem_devtype(MemorySegment, MemorySegment, MemorySegment).
      Parameters:
      device - the device handle
      subsystem - the subsystem to match
      devtype - the devtype to match
      arena - the arena to allocate strings in
      Returns:
      the matching parent device handle, or MemorySegment.NULL if none
      Throws:
      Throwable - if the native call fails