monitor device events with udev

Published: July 23, 2022

monitor device events with udev

In this article, we would cover how to monitor device events with udev. Prior to Linux Kernel 2.6, we had DevFS for device management. But, ever since we use udev. One of the advantages of using udev over DevFS - udev mainly runs in userspace while DevFS would run in kernel space. It is also responsible for managing device nodes in /dev directory.

All the device related events are received by udev daemon. When we add/remove or change the state of a device, it leads to a specific device event which is then matched with a set of rules to identify the device. And, these rules are there in /lib/udev/rules.d/ directory. Use the following code to list all the rules files in the directory -

ls -l /lib/udev/rules.d

Now, for instance - we want to check what all rules are there in the file usb_modeswitch file. Use cat command-line utility -

cat /lib/udev/rules.d/40-usb_modeswitch.rules | less

Here, you would find a list of device names along with Vendor ID and Product ID. One thing we must remember here that, rules files should always end with .rules extension. We will cover more about it coming articles. For the purpose of the article, we stick with monitoring device events. Its just that you got to know a bit about how things work in the background.

monitor device events with udev

To monitor device events with udev, we need to use a device management tool - udevadm. Use the following code -

udevadm monitor

And, leave it running in the terminal and then add a USB drive to see the output from udevadm. When we use monitor with udevadm - it displays information about Kernel both uevent and udev event. udev event triggers after it has processed a rule.

We can also use following options with udevadm monitor. First of those is -k

udevadm monitor -k

It will display information related to kernel uevents.

Second, -u to display information after udev has processed a rule.

udevadm monitor -u

Third, to display properties of a device event use -p

udevadm monitor -p

In conclusion, we have covered how to monitor device events with udev here.