gpt4 book ai didi

linux - 让 Inotify 正确发出 IN_UNMOUNT 事件

转载 作者:IT王子 更新时间:2023-10-29 01:24:57 26 4
gpt4 key购买 nike

你好,我一直试图让 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/ 时它会发出一个 忽略了它创建的所有 watch ,但它从不发出 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

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

最佳答案

这似乎是一个内核错误,已根据 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 header ,这是 IN_UNMOUNT 标志的正确事件掩码,这意味着它最终修复了 ~2.6.35 或更高版本...

关于linux - 让 Inotify 正确发出 IN_UNMOUNT 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5360238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com