简体   繁体   English

如何检测使用inotify挂载目录的时间?

[英]How could I detect when a directory is mounted with inotify?

I'm using Linux Inotify to detect FS events on my program. 我正在使用Linux Inotify来检测程序中的FS事件。

How could I be notified when a device is mounted on a monitored directory? 如何在受监控目录上安装设备时如何通知我?

I don't think you can do it with inotify . 我不认为你可以用inotify做到这一点。 Here is the method though: 这是方法:

  1. Read uevents from kernel via a Netlink socket and filter out those where "ACTION" is not "mount" . 通过Netlink套接字从内核读取uevents并过滤掉"ACTION"不是"mount"
  2. Read and parse "/proc/mounts" when you get an event with a "mount" action. 当您获得具有"mount"操作的事件时,请读取并解析"/proc/mounts"
  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. 使用刚挂载的设备查找挂载点的记录,如果它不是您正在观看的目录,则将其过滤掉。

EDIT: Update to be less than 5 years obsolete 编辑:更新不到5年已过时

If you're on anything but the most ancient of systems, libudev is what you want for the first step. 如果您使用的是最古老的系统, libudev就是您想要的第一步。

If you're on something from this decade, udisks will do all of this for you, too. 如果你正处于这十年的某些事情, udisks也会为你做这一切。 You'd need to watch the org.Freedesktop.DBus.ObjectManager interface on /org/freedesktop/UDisks2 to see when new filesystems turn up. 您需要观察/ org / freedesktop / UDisks2上的org.Freedesktop.DBus.ObjectManager接口,以查看新文件系统何时出现。

On modern Linux systems /etc/mtab often points to /proc/self/mounts: 在现代Linux系统上/ etc / mtab经常指向/ 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: proc(5) manpage说你真的不需要为这个文件使用inotify,它是可轮询的:

Since kernel version 2.6.15, this file is pollable: after opening the file for reading, a change in this file (ie, 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. 从内核版本2.6.15开始,此文件是可轮询的:打开文件进行读取后,此文件中的更改(即文件系统挂载或卸载)会导致select(2)将文件描述符标记为可读,并轮询(2) )和epoll_wait(2)将文件标记为具有错误条件。

Was wondered why inotify not works on /etc/mtab and found this manpage. 想知道为什么inotify不能在/ etc / mtab上工作并找到这个联机帮助页。

If you don't mind lots of false alarms, you might be able to watch for close_nowrite on /etc/fstab . 如果您不介意大量误报,您可以在/etc/fstab上查看close_nowrite . Watching /etc/mtab , /proc/mounts , etc. doesn't work for me. /etc/mtab/proc/mounts等对我来说不起作用。

inotify only tells you about unmounts, and uevents no longer tells you about mount/unmount. inotify只告诉你有关卸载的信息,而uevents不再告诉你有关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. 要做的是在/ proc / mounts上进行轮询,读入内容,并跟踪您看到的挂载,然后在轮询唤醒时重新解析。 The poll will wake up on ERR/PRI when any filesystem is mounted or unmounted. 当挂载或卸载任何文件系统时,轮询将在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;
}

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. 注意,在过去的Linux版本中,所有这些技术都不稳定和/或破坏。 It all got stable around the end of Linux 2.6.35 (or maybe a bit earlier). 它在Linux 2.6.35的末尾(或者可能更早一点)得到了稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM