Best way to monitor disk mounts in Linux using C++? -


I am currently building a carpet front end and should be able to recognize a function that is needed when External media is inserted, such as the USB / SD Memory Stick or iPod on their inclusion, I will then scan the device for music / video / images and add them to the media library. Alternatively, I should know how these devices are removed so that I can remove the items that are currently attached to the available media.

My question is, what is the best way to monitor disk insertion / removal in a Linux environment using C ++?

When I place the / MacDium folder on disk automatically on the disk, is it the best way to accomplish this task? Thank you for any information!

You can read this device provides events about adding / removing, mount / umount . A daemon who listens to the Netlink socket receives a packet of data for every hotplug event, which contains the same information, a user assistant will be found in the environmental variable.

The netlink packet contains a set of empty finished text lines. Netlink adds value to $ action and $ DEVPATH in the first line of the packet, which is different from a @ (on signature). After the first time, a KEYWORD = VALUE pair in each row defines a hotplug event variable.

[...]

Current hotplug action: "add" to add device, "remove" 2.6 to remove it 22 Kernel can also generate "change", "online", "offline" and "move" verbs.

You probably want to monitor mount and umount the actions note that the event does not give you either the device node or the actual mount point , Only the sysfs node of the device. If device node management and mount management are handled by an external process (such as udev), then you have to locate the device node and the mount point must use large and small device numbers yourself and / Proc / mounts .


Comments