I'm using libudev to detect when a USB drive is attached in a C program. Once I detect that it has been attached via a signal from the kernel (through a udev_monitor structure), can I assume that systemd has mounted the drive?

If not, how can I deterministically wait until the drive has mounted i.e. not wait an arbitrary time X seconds, but wait on a specific event.

  • The udev signal from the kernel is given both to systemd and your C program, so that's a race condition, and you can't assume it is mounted by the time you receive the signal. I don't know if there's a standard mechanism to get a "mounted" signal, or have systemd deliver a custom signal to you. – dirkt Sep 28 '19 at 20:01

It turns out that it's best not to listen for USB device attach events, but mount point attach events instead. In other words, instead of filtering on "usb" subsystem and "usb_device" devtype, use "block" subsystem and "partition" devtype, then check that the event contains ID_BUS="usb".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.