package libudev

  1. Overview
  2. Docs

Accessing device information.

type t

A Device.t corresponds to a system device.

type descr =
  1. | Syspath of string

    A device path in /sys, including the mount point.

    Example: Device.create (Device.Syspath "/sys/devices/platform").

  2. | Subsystem_sysname of string * string

    A given subsystem, and a given sys name.

    Example: Device.create (Device.Subsystem_sysname ("block", "sda")).

  3. | Device_id of string

    A device id, which is a special string in one of the following four forms:

    • block device major:minor; example: "b8:2"
    • char device major:minor; example: "c128:1"
    • network device ifindex; example: "n3"
    • kernel driver core subsystem:device name; example: "+sound:card29"
  4. | Environment

    The process environment.

    This only works reliably if the current process is called from an udev rule, and is usually used for tools executed from IMPORT= rules. Use this method to create device objects in OCaml programs called from udev rules.


A type describing different ways of creating a Device.t using Device.create.

val create : Context.t -> descr -> t

Create a Device.t from a Device.descr.

General attributes

val context : t -> Context.t

The Context.t to which the device is bound.

val syspath : t -> string

Absolute path of this device in sysfs including the sysfs mount point, as a unicode string.

val sysname : t -> string

Device file name inside sysfs as unicode string.

val sysnum : t -> string option

The trailing number of the sysname as unicode string, if any.

Note: the number is returned as a unicode string to preserve the exact format of the number, especially any leading zeros.

val devpath : t -> string

Kernel device path as a unicode string. This path uniquely identifies a single device.

Unlike Device.syspath, this path does not contain the sysfs mount point. However, the path is absolute and starts with a slash "/".

val tags : t -> string list

The tags attached to the device. Tags are arbitrary classifiers that can be attached to devices by udev scripts and daemons. For instance, systemd uses tags for multi-seat support.

val has_tag : t -> string -> bool

Check is the device has some tag.

Device driver and subsystem

val subsystem : t -> string

Name of the subsystem the device is part of, as a unicode string.

val driver : t -> string option

The driver name as a unicode string, if any.

val devtype : t -> string option

The device type as a unicode string, if it is known.

Device nodes

val devnode : t -> string option

Absolute path to the device node of this device, as a unicode string, if the device has a device node.

This path always points to the actual device node associated with this device, and never to any symbolic links to this device node. See Device.devlinks to get a list of symbolic links to this device node.

The absolute paths of all symbolic links pointing to the Device.devnode of this device. The paths are unicode strings.

UDev can create symlinks to the original device node (see Device.devnode) inside the device directory. This is often used to assign a constant, fixed device node to devices like removeable media, which technically do not have a constant device node, or to map a single device into multiple device hierarchies. This function provides access to all such symbolic links, which were created by UDev for this device.

Device initialization time

val is_initialized : t -> bool

Checks if the device is initialized

A device is initialized if udev has already handled this device and has set up device node permissions and context, or renamed a network device.

Such a distinction is meaningful only for devices with a device node, or for network devices. For all other devices, the function returns true.

Using uninitialized devices is not recommended.

val usec_since_initialized : t -> Stdint.Uint64.t

The time elapsed since initialization, as a number of microseconds.

This is only implemented on devices which need to store properties in the udev database. For all other devices, is returned.

Device hierarchy

val parent : t -> t option

The parent of the device, if any.

val find_parent : t -> ?devtype:string -> subsystem:string -> t option

Find the parent device within the given subsystem and optionally with the given devtype, if any.

subsystem contains the name of the subsystem, in which to search for the parent. devtype optionally holds the expected device type of the parent.

Device events

type action =
  1. | Add
  2. | Remove
  3. | Change
  4. | Online
  5. | Offline
  6. | Other of string

A device event action, for devices received from a Monitor.t.

val string_of_action : action -> string
val action : t -> action option

The device event action. Returns None if the device was not received from a Monitor.t

val seqnum : t -> Stdint.Uint64.t

The device event sequence number, or if this device has no sequence number, i.e. was not received from a Monitor.t

Device properties

val properties : t -> (string * string) list

Return the properties defined for a device, as a (key, value) list.

val property : t -> string -> string option

Return the value of a property, if present.

val int_property : t -> string -> int option
val bool_property : t -> string -> bool option

Sysfs attributes

val sysattrs : t -> (string * string) list

Return the system attributes of the device, as a (key, value) list.

System attributes are basically normal files inside the device directory. These files contain all sorts of information about the device, which may not be reflected by properties. These attributes are commonly used for matching in udev rules, and can be printed using udevadm info --attribute-walk.

val sysattr : t -> string -> string option

Return the value of an attribute, if present.

val int_sysattr : t -> string -> int option
val bool_sysattr : t -> string -> bool option
val set_sysattr : t -> string -> string -> unit

Update the value of a device attribute.


Innovation. Community. Security.