udev_monitor: libudev Reference Manual

## udev_monitor udev_monitor — device event source |

## Functions

struct udev_monitor * | udev_monitor_ref ()
struct udev_monitor * | udev_monitor_unref ()
struct udev * | udev_monitor_get_udev ()
struct udev_monitor * | udev_monitor_new_from_netlink ()
int | udev_monitor_enable_receiving ()
int | udev_monitor_set_receive_buffer_size ()
int | udev_monitor_get_fd ()
struct udev_device * | udev_monitor_receive_device ()
int | udev_monitor_filter_add_match_subsystem_devtype ()
int | udev_monitor_filter_add_match_tag ()
int | udev_monitor_filter_update ()
int | udev_monitor_filter_remove ()

## Types and Values

struct | udev_monitor

## Description

Connects to a device event source.

## Functions

### udev_monitor_ref ()

    struct udev_monitor *
    udev_monitor_ref (struct udev_monitor *udev_monitor);

Take a reference of a udev monitor.

#### Parameters

udev_monitor | udev monitor |

#### Returns

the passed udev monitor

### udev_monitor_unref ()

    struct udev_monitor *
    udev_monitor_unref (struct udev_monitor *udev_monitor);

Drop a reference of a udev monitor. If the refcount reaches zero, the bound socket will be closed, and the resources of the monitor will be released.

#### Parameters

udev_monitor | udev monitor |

#### Returns

NULL

### udev_monitor_get_udev ()

    struct udev *
    udev_monitor_get_udev (struct udev_monitor *udev_monitor);

Retrieve the udev library context the monitor was created with.

#### Parameters

udev_monitor | udev monitor |

#### Returns

the udev library context

### udev_monitor_new_from_netlink ()

    struct udev_monitor *
    udev_monitor_new_from_netlink (struct udev *udev,
                                   const char *name);

Create new udev monitor and connect to a specified event source. Valid sources identifiers are "udev" and "kernel".

Applications should usually not connect directly to the "kernel" events, because the devices might not be useable at that time, before udev has configured them, and created device nodes. Accessing devices at the same time as udev, might result in unpredictable behavior. The "udev" events are sent out after udev has finished its event processing, all rules have been processed, and needed device nodes are created.

The initial refcount is 1, and needs to be decremented to release the resources of the udev monitor.

#### Parameters

udev | udev library context |
name | name of event source |

#### Returns

a new udev monitor, or NULL, in case of an error

### udev_monitor_enable_receiving ()

    int
    udev_monitor_enable_receiving (struct udev_monitor *udev_monitor);

Binds the `udev_monitor` socket to the event source.

#### Parameters

udev_monitor | the monitor which should receive events |

#### Returns

0 on success, otherwise a negative error value.

### udev_monitor_set_receive_buffer_size ()

    int
    udev_monitor_set_receive_buffer_size (struct udev_monitor *udev_monitor,
                                          int size);

Set the size of the kernel socket buffer. This call needs the appropriate privileges to succeed.

#### Parameters

udev_monitor | the monitor which should receive events |
size | the size in bytes |

#### Returns

0 on success, otherwise -1 on error.

### udev_monitor_get_fd ()

    int
    udev_monitor_get_fd (struct udev_monitor *udev_monitor);

Retrieve the socket file descriptor associated with the monitor.

#### Parameters

udev_monitor | udev monitor |

#### Returns

the socket file descriptor

### udev_monitor_receive_device ()

    struct udev_device *
    udev_monitor_receive_device (struct udev_monitor *udev_monitor);

Receive data from the udev monitor socket, allocate a new udev device, fill in the received data, and return the device.

Only socket connections with uid=0 are accepted.

The monitor socket is by default set to NONBLOCK. A variant of `poll()` on the file descriptor returned by `udev_monitor_get_fd()` should to be used to wake up when new devices arrive, or alternatively the file descriptor switched into blocking mode.

The initial refcount is 1, and needs to be decremented to release the resources of the udev device.

#### Parameters

udev_monitor | udev monitor |

#### Returns

a new udev device, or NULL, in case of an error

### udev_monitor_filter_add_match_subsystem_devtype ()

    int
    udev_monitor_filter_add_match_subsystem_devtype
                                   (struct udev_monitor *udev_monitor,
                                    const char *subsystem,
                                    const char *devtype);

This filter is efficiently executed inside the kernel, and libudev subscribers will usually not be woken up for devices which do not match.

The filter must be installed before the monitor is switched to listening mode.

#### Parameters

udev_monitor | the monitor |
subsystem | the subsystem value to match the incoming devices against |
devtype | the devtype value to match the incoming devices against |

#### Returns

0 on success, otherwise a negative error value.

### udev_monitor_filter_add_match_tag ()

    int
    udev_monitor_filter_add_match_tag (struct udev_monitor *udev_monitor,
                                       const char *tag);

This filter is efficiently executed inside the kernel, and libudev subscribers will usually not be woken up for devices which do not match.

The filter must be installed before the monitor is switched to listening mode.

#### Parameters

udev_monitor | the monitor |
tag | the name of a tag |

#### Returns

0 on success, otherwise a negative error value.

### udev_monitor_filter_update ()

    int
    udev_monitor_filter_update (struct udev_monitor *udev_monitor);

Update the installed socket filter. This is only needed, if the filter was removed or changed.

#### Parameters

udev_monitor | monitor |

#### Returns

0 on success, otherwise a negative error value.

### udev_monitor_filter_remove ()

    int
    udev_monitor_filter_remove (struct udev_monitor *udev_monitor);

Remove all filters from monitor.

#### Parameters

udev_monitor | monitor |

#### Returns

0 on success, otherwise a negative error value.

## Types and Values

### struct udev_monitor

    struct udev_monitor;

Opaque object handling an event source.