3

One of my udev rules runs a script on removal of USB storage devices. This script performs some general cleanup and removes the mount point directory for the storage device. The issue is that I want to be able to trigger this logic programmatically from my C++ application (as opposed to when a usb flash drive is hot swap removed, which seems to be too late).

The correct way to achieve this seems to be directly adding the event on the udev event queue. I am able to achieve the desired action using udevadm but do not see an equivalent API say in the libudev library.

udevadm trigger --action=remove --property-match=DEVNAME=/dev/sda1

Is there some way to achieve this by accessing udev over dbus? Is there a library which will allow me to do this? Is this ultimately not how udev should work and should I be unmounting & cleaning up block devices in my application instead of having udev do it?

1 Answer 1

1

It seems what you're looking for is the uevent pseudo-file in the /sys sysfs filesystem.

By writing commands to that file (such as "remove"), the kernel will forward those to the userspace handler of the device events, which is udevd.

So doing the equivalent of the following snippet (using shell for the example) should do what you want, requesting udev removal of the /dev/sda1 device:

# echo -n "remove" >/sys/class/block/sda1/uevent

You can use the libudev APIs (for instance, udev_device_get_syspath(3) to help you find the correct sysfs path for a given device.

You can find some documentation from the kernel side under kobject.

You'll also find some mentions of "uevents" in udev's documentation, such as "systemd-udevd listens to kernel uevents" from udevd's man page.

But there doesn't seem to be a lot of documentation on the uevent pseudo-file of sysfs anywhere...

1
  • 1
    Yes, I think this is exactly what I need. That way I can generate events with this and monitor with libudev to execute my callbacks. This seems like the correct place for injection too, so I can let the kernel do its thing. Thanks, I wouldn't have found this on my own. Jan 10, 2019 at 22:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .