If you need everything in one process, you will have to use libudev to get events from udevd or directly from the kernel.
Seeing that it might be a problem to use libudev in your application (lack of documentation?), An alternative is to use the udevadm program, which can:
udevd device events after udevd processing ( udevadm monitor --udev --property ),udevadm monitor --kernel --property deviation events directly from the kernel ( udevadm monitor --kernel --property ) and- unload the udevd database of current devices (but not the kernel!) (
udevadm info --query all --export-db )
udevadm is part of the udev package, but should not need udevd if you only use it to report kernel events. You can use it if your process spawns it and parses its standard output (but you have to run it through stdbuf -o L ).
In any case, there will probably be a lot of work. I have already implemented a lot of this in my NCD programming language , including USB device monitoring. You might want to take a look at NCDs; This is useful for many configuration tasks and handles hot plugging well. For example, this NCD program will print USB device events to standard output:
process main { sys.watch_usb() watcher; println(watcher.event_type, " ", watcher.devname, " ", watcher.vendor_id, ":", watcher.model_id); watcher->nextevent(); }
This will force NCD to print something similar (with an added start event for any USB device that has already been connected):
added /dev/bus/usb/002/045 0409:0059 added /dev/bus/usb/002/046 046d:c313 added /dev/bus/usb/002/047 046d:c03e added /dev/bus/usb/002/048 0557:2008 removed /dev/bus/usb/002/048 0557:2008
You can also use NCD just for this and analyze this standard output - it is much easier to work with it than directly contact udevadm.
Note that NCD itself uses udevadm , and this requires udevd to work; but why is this a problem anyway? (with some work this dependency can be removed)
Ambroz Bizjak
source share