让 Inotify 正确发出 IN_UNMOUNT 事件

发布于 2024-10-25 00:18:23 字数 1639 浏览 4 评论 0原文

你好,我一直在尝试让 Inotify 产生一个 IN_UNMOUNT 事件,但它根本不与我合作,所以我用 inotifywait 做了一个简单的实验,结果如下:

paul@imaskar ~ $ inotifywait -r /storage/test/ -m
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/storage/test/ CREATE,ISDIR a
/storage/test/ OPEN,ISDIR a
/storage/test/ CLOSE_NOWRITE,CLOSE,ISDIR a
/storage/test/ DELETE,ISDIR a
/storage/test/a/ DELETE_SELF 
/storage/test/a/ IGNORED 
/storage/test/ IGNORED 

基本上发生的情况是它将拾取所有其他事件,例如创建、打开等......但是当我卸载/storage/test/时,它会为其创建的所有手表发出IGNORED,但是它从不发出 UNMOUNT 事件...

所以看起来我无法获得 IN_UNMOUNT 事件,但我读过的所有 inotify 文档都说内核会在受监视的文件/时向该事件添加 IN_UNMOUNT 位标志目录后备存储已卸载...

这是来自 - 的简单 C 代码Inotify patch

#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>

int main(int argc, char **argv)
{
        char buf[1024];
        struct inotify_event *ie;
        char *p;
        int i;
        ssize_t l;

        p = argv[1];
        i = inotify_init();
        inotify_add_watch(i, p, ~0);

        l = read(i, buf, sizeof(buf));
        printf("read %d bytes\n", l);
        ie = (struct inotify_event *) buf;
        printf("event mask: %x\n", ie->mask);
    return 0;
}

无论如何,我做了以下步骤:

gcc -oinotify inotify.c
mkdir mnt
sudo mount -ttmpfs none mnt
mkdir mnt/d
./inotify mnt/d/

# Different shell
sudo umount mnt

最后,这是它发出的内容

read 16 bytes
event mask: 8000

所以此时我不确定问题是否出在代码中还是其他什么地方?

Hello I have been trying to get Inotify to yield up a IN_UNMOUNT event but its not cooperating with me at all so I went and did a simple experiment with inotifywait and this is the result below:

paul@imaskar ~ $ inotifywait -r /storage/test/ -m
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/storage/test/ CREATE,ISDIR a
/storage/test/ OPEN,ISDIR a
/storage/test/ CLOSE_NOWRITE,CLOSE,ISDIR a
/storage/test/ DELETE,ISDIR a
/storage/test/a/ DELETE_SELF 
/storage/test/a/ IGNORED 
/storage/test/ IGNORED 

Basically what happens is It will pick up all of the other events such as create, open, etc.... but when I unmounted /storage/test/ it will emit a IGNORED for all watches that it had created, but it never emits a UNMOUNT event...

So it seems like I'm unable to get a IN_UNMOUNT event but all of the inotify documentation that I've read said that the kernel will add a IN_UNMOUNT bitflag to the event when a monitored file/directory backing storage was unmounted...

Here's a simple C code from - Inotify patch

#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>

int main(int argc, char **argv)
{
        char buf[1024];
        struct inotify_event *ie;
        char *p;
        int i;
        ssize_t l;

        p = argv[1];
        i = inotify_init();
        inotify_add_watch(i, p, ~0);

        l = read(i, buf, sizeof(buf));
        printf("read %d bytes\n", l);
        ie = (struct inotify_event *) buf;
        printf("event mask: %x\n", ie->mask);
    return 0;
}

Anyway I did the following steps:

gcc -oinotify inotify.c
mkdir mnt
sudo mount -ttmpfs none mnt
mkdir mnt/d
./inotify mnt/d/

# Different shell
sudo umount mnt

And finally here is what it emits

read 16 bytes
event mask: 8000

So at this point in time I'm not sure if the problem is in the code or something else?

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

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

发布评论

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

评论(1

奢华的一滴泪 2024-11-01 00:18:23

这似乎是一个内核错误,已根据 LKML 进行修复。大约从内核 2.6.31 开始,卸载 inode 时就不会发送 IN_UNMOUNT 事件...此补丁适用于“34-longterm”,又名内核 2.6.35(?)。

无论如何,我能够升级到内核 2.6.37 并重新运行上述测试,结果如下: 这

mkdir mnt
sudo mount -ttmpfs none mnt
mkdir mnt/d
inotifywait -r mnt/d/ -m


# Different shell
sudo umount mnt

是输出:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/tmp/test/d/ UNMOUNT 
/tmp/test/d/ IGNORED 
/tmp/test/ UNMOUNT 
/tmp/test/ IGNORED 

根据示例 C 代码,这是输出:

read 32 bytes
event mask: 2000

查看 inotify.h 标头,这是IN_UNMOUNT 标志的正确事件掩码,这意味着它最终修复了 ~2.6.35 或更高版本...

This appears to be a kernel bug which has been fixed as per LKML. Roughly since Kernel 2.6.31 the IN_UNMOUNT event has not been sent when inodes are umounted... This patch was for "34-longterm" aka Kernel 2.6.35(?).

Anyway I was able to upgrade to Kernel 2.6.37 and re-ran the above tests and here's the results:

mkdir mnt
sudo mount -ttmpfs none mnt
mkdir mnt/d
inotifywait -r mnt/d/ -m


# Different shell
sudo umount mnt

And here's the output:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
/tmp/test/d/ UNMOUNT 
/tmp/test/d/ IGNORED 
/tmp/test/ UNMOUNT 
/tmp/test/ IGNORED 

And as per the sample C code here's the output:

read 32 bytes
event mask: 2000

And looking at the inotify.h headers, this is the correct event mask for the IN_UNMOUNT flag so that means its finally fixed ~2.6.35 or latter...

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