如何检测目录是否已使用 inotify 挂载?

发布于 2024-07-26 22:10:27 字数 73 浏览 12 评论 0 原文

我正在使用 Linux Inotify 来检测程序上的 FS 事件。

当设备安装在受监控的目录上时,如何通知我?

I'm using Linux Inotify to detect FS events on my program.

How could I be notified when a device is mounted on a monitored directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

数理化全能战士 2024-08-02 22:10:27

我认为你不能用 inotify 来做到这一点。 方法如下:

  1. 通过 uevents from kernel .kernel.org/doc/man-pages/online/pages/man7/netlink.7.html" rel="noreferrer">Netlink 套接字并过滤掉那些 "ACTION"不是“安装”
  2. 读取并解析 "/proc/mounts"您会收到一个带有 "mount" 操作的事件。
  3. 查找刚刚安装的设备的安装点记录,如果不是您正在观看的目录,则将其过滤掉。

I don't think you can do it with inotify. Here is the method though:

  1. Read uevents from kernel via a Netlink socket and filter out those where "ACTION" is not "mount".
  2. Read and parse "/proc/mounts" when you get an event with a "mount" action.
  3. Find a record for a mount point with device that was just mounted and filter it out if it's not the directory you are watching.
复古式 2024-08-02 22:10:27

编辑:更新到过时时间不到 5 年

如果您使用的不是最古老的系统,libudev 是您第一步想要的。

如果您正在使用本十年的某些内容,udisks 将为您完成所有这一切,也。 您需要观看 org.Freedesktop.DBus.ObjectManager /org/freedesktop 上的界面/UDisks2 查看新的文件系统< /a> 出现。

EDIT: Update to be less than 5 years obsolete

If you're on anything but the most ancient of systems, libudev is what you want for the first step.

If you're on something from this decade, udisks will do all of this for you, too. You'd need to watch the org.Freedesktop.DBus.ObjectManager interface on /org/freedesktop/UDisks2 to see when new filesystems turn up.

请叫√我孤独 2024-08-02 22:10:27

在现代 Linux 系统上 /etc/mtab 通常指向 /proc/self/mounts:

$ ls -l /etc/mtab
lrwxrwxrwx 1 root root 12 Sep 5 2013 /etc/mtab -> /proc/挂载
$ ls -l /proc/挂载
lrwxrwxrwx 1 root root 11 Jul 10 14:56 /proc/mounts -> self/mounts

proc(5) 手册页 说您实际上并不需要为此文件使用 inotify,它是可轮询的:

从内核版本 2.6.15 开始,这
文件可轮询:打开文件进行读取后,会发生更改
在此文件中(即文件系统挂载或卸载)会导致
select(2) 将文件描述符标记为可读,poll(2)
epoll_wait(2) 将文件标记为有错误情况。

想知道为什么 inotify 不能在 /etc/mtab 上工作,并找到了这个联机帮助页。

On modern Linux systems /etc/mtab often points to /proc/self/mounts:

$ ls -l /etc/mtab
lrwxrwxrwx 1 root root 12 Sep 5 2013 /etc/mtab -> /proc/mounts
$ ls -l /proc/mounts
lrwxrwxrwx 1 root root 11 Jul 10 14:56 /proc/mounts -> self/mounts

proc(5) manpage says that you don't really need to use inotify for this file, it is pollable:

Since kernel version 2.6.15, this
file is pollable: after opening the file for reading, a change
in this file (i.e., a filesystem mount or unmount) causes
select(2) to mark the file descriptor as readable, and poll(2)
and epoll_wait(2) mark the file as having an error condition.

Was wondered why inotify not works on /etc/mtab and found this manpage.

策马西风 2024-08-02 22:10:27

inotify 只告诉您有关卸载的信息,而 uevents 不再告诉您有关挂载/卸载的信息。

方法是轮询 /proc/mounts,读入内容,并跟踪您所看到的安装,然后在轮询唤醒时重新解析。 当安装或卸载任何文件系统时,轮询将在 ERR/PRI 上唤醒。

#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    int fd;
    struct pollfd ev;
    int ret;
    ssize_t bytesread;
    char buf[8192];

    fd = open("/proc/mounts", O_RDONLY);
    printf("########################################\n");
    while ((bytesread = read(fd, buf, sizeof(buf))) > 0)
        write(1, buf, bytesread);

    do {

        ev.events = POLLERR | POLLPRI;
        ev.fd = fd;
        ev.revents = 0;
        ret = poll(&ev, 1, -1);
        lseek(fd, 0, SEEK_SET);
        if (ev.revents & POLLERR) {
            printf("########################################\n");
            while ((bytesread = read(fd, buf, sizeof(buf))) > 0)
                write(1, buf, bytesread);
        }
    } while (ret >= 0);
    close(fd);

    return 0;
}

上面的代码只是在启动时打印出安装点,然后在任何安装/卸载时打印出安装点。 您可以“区分”它们以找出添加/删除的内容。

请注意,所有这些技术在过去的 Linux 版本中都不稳定和/或被破坏。 在 Linux 2.6.35 末期(或者可能更早一些),一切都变得稳定了。

inotify only tells you about unmounts, and uevents no longer tells you about mount/unmount.

The way to do is to poll on /proc/mounts, read in the contents, and keep track of the mounts you've seen, and then reparse when the poll wakes up. The poll will wake up on ERR/PRI when any filesystem is mounted or unmounted.

#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
    int fd;
    struct pollfd ev;
    int ret;
    ssize_t bytesread;
    char buf[8192];

    fd = open("/proc/mounts", O_RDONLY);
    printf("########################################\n");
    while ((bytesread = read(fd, buf, sizeof(buf))) > 0)
        write(1, buf, bytesread);

    do {

        ev.events = POLLERR | POLLPRI;
        ev.fd = fd;
        ev.revents = 0;
        ret = poll(&ev, 1, -1);
        lseek(fd, 0, SEEK_SET);
        if (ev.revents & POLLERR) {
            printf("########################################\n");
            while ((bytesread = read(fd, buf, sizeof(buf))) > 0)
                write(1, buf, bytesread);
        }
    } while (ret >= 0);
    close(fd);

    return 0;
}

The above code just prints out the mount points on startup, and then on any mount/unmount. It's up to you to "diff" them to find out what got added/removed.

Note, all these techniques has been both unstable and/or broken in past Linux versions. It all got stable around the end of Linux 2.6.35 (or maybe a bit earlier).

怪我入戏太深 2024-08-02 22:10:27

如果您不介意很多误报,您也许可以在/etc/fstab上观察close_nowrite。 。 观看 /etc/mtab/proc/mounts 等对我来说不起作用。

If you don't mind lots of false alarms, you might be able to watch for close_nowrite on /etc/fstab. . Watching /etc/mtab, /proc/mounts, etc. doesn't work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文