I am currently constructing a Carputer front end and one function that it needs is to be able to recognize when external media is inserted, such as USB/SD memory sticks or iPods. Upon their insertion, I will then scan the device for music/video/images and add them to the media library. Alternately, I need to know when these devices are removed so that I can remove the added items from the currently available media.
My question is, what is the best way to monitor disk insertions/removals in a Linux environment using C++?
I could monitor the /media folder for when Linux mounts the disks automagically, but is this the best way to accomplish the task? Thanks for any insight!
You can read kernel uevents from a NetLink socket. It provides events about device adding/removal, mount/umount.
You probably want to monitor
mountandumountactions. Note that event does not give you either device node or the actual mount point, only device’s sysfs node. If device nodes management and mounts management are handled by an external process (e.g. udev), you’ll have to find out the device node and a mount point yourself using major and minor device numbers and/proc/mounts.