问题
I need to run some code on storage device mounting and unmounting.
How can i listen for these events on linux?
- I was thinking on adding some udev rules to run some script (any know-how in this matter is appreciated).
- But I would much rather listen for events from the kernel in some netlink socket with my daemon (just like udev does) or something like that.
回答1:
You can use libudev or the more convenient, glib based gudev to monitor udev events in C.
回答2:
You have to listen to the "uevent" signal. Attach a callback to this signal and wait for the notification.
来源:https://stackoverflow.com/questions/3255174/listen-for-harware-change-events-from-the-linux-kernel-or-udev