您好,我一直试图让 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,但它从不发出卸载事件...
所以看起来我无法获得 IN_UNMOUNT 事件,但我读过的所有 inotify 文档都说,当卸载受监控的文件/目录后备存储时,内核将向事件添加一个 IN_UNMOUNT 位标志......
这是来自 - Inotify 补丁的简单 C 代码
#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
所以在这个时间点上,我不确定问题是出在代码中还是其他什么地方?