在Linux中使用C ++监视磁盘安装的最佳方法? [英] Best way to monitor disk mounts in Linux using C++?

查看:147
本文介绍了在Linux中使用C ++监视磁盘安装的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个Carputer前端,它需要的一个功能是能够识别何时插入外部媒体,如USB / SD记忆棒或iPod。插入后,我将扫描设备的音乐/视频/图像,并将其添加到媒体库。或者,我需要知道这些设备何时被删除,以便我可以从当前可用的媒体中删除添加的项目。

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.

我的问题是,在Linux环境中使用C ++监视磁盘插入/删除的最佳方式?

当Linux自动安装磁盘时,我可以监视/ media文件夹,这是完成任务的最好方法吗?感谢任何洞察!

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!

推荐答案

您可以阅读从NetLink套接字的内核uevent 。它提供关于设备添加/删除,mount / umount的事件。

You can read kernel uevents from a NetLink socket. It provides events about device adding/removal, mount/umount.


- Netlink

-- Netlink

一个守护进程监听netlink
socket接收
每个hotplug事件的数据包,包含
相同的信息usermode helper
将在环境$ b $中接收b变量。

A daemon listening to the netlink socket receives a packet of data for each hotplug event, containing the same information a usermode helper would receive in environment variables.

netlink数据包包含一组
空值终止的文本行。 netlink包的第一个
行组合
$ ACTION $ DEVPATH
由@(at符号)分隔。每个
行之后包含定义热插拔
事件变量的
KEYWORD = VALUE 对。

The netlink packet contains a set of null terminated text lines. The first line of the netlink packet combines the $ACTION and $DEVPATH values, separated by an @ (at sign). Each line after the first contains a KEYWORD=VALUE pair defining a hotplug event variable.

[...]

ACTION

目前的热插拔动作: 添加设备,删除删除
。 2.6.22内核还可以生成更改,在线,
离线和移动操作。

The current hotplug action: "add" to add the device, "remove" to remove it. The 2.6.22 kernel can also generate "change", "online", "offline", and "move" actions.

您可能想要监控 mount umount 操作。请注意,事件不会给您设备节点或实际装载点,而只给出装置的sysfs节点。如果设备节点管理和安装管理由外部进程(例如udev)处理,则必须使用主要和次要设备号和 / proc / mounts来查找设备节点和安装点

You probably want to monitor mount and umount actions. 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.

这篇关于在Linux中使用C ++监视磁盘安装的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆