fanotify recursivity does really works?

与世无争的帅哥 提交于 2019-12-02 00:13:52

问题


I'm using code like following to monitor the whole file system:

fanotify_mark(fd,
          FAN_MARK_ADD | FAN_MARK_MOUNT,
          FAN_OPEN | FAN_EVENT_ON_CHILD,
          AT_FDCWD, "/"
)

But I need write some tests, so, I want monitor just a specific dir, let say "/tmp/test_dir". The problem is when I change code this way:

fanotify_mark(fd,
          FAN_MARK_ADD,
          FAN_OPEN | FAN_EVENT_ON_CHILD,
          AT_FDCWD, "/tmp/test_dir"
)

fanotify only watchs to events on "/tmp/test_dir" ignoring whatever happen in deeper folders.

For instance: If I open "/tmp/test_dir/aa/bb/cc/test_file.txt" fanotify detects nothing.

I'm missing some flag?


回答1:


Problem solved.

fanotify isn't recursive. It only works that way when working on mounted directories. I did the following test:

mkdir /tmp/parent
mkdir -p /tmp/other/aa/bb/cc/dd
touch /tmp/other/aa/bb/cc/dd/test.txt
mount --bind /tmp/other /tmp/parent

then in code:

fanotify_mark(fd,
      FAN_MARK_ADD | FAN_MARK_MOUNT,
      FAN_OPEN | FAN_EVENT_ON_CHILD,
      AT_FDCWD, "/tmp/parent"
)

and it's done. Now fanotify fire up events for test.txt file.




回答2:


With fanotify, either monitor entire mount point of specified path (using FAN_MARK_MOUNT), or monitor files in a directory (not its sub-directory, without specifying FAN_MARK_MOUNT). You can set separate monitors for sub-directories to achieve this. see https://stackoverflow.com/a/20965660/2706918



来源:https://stackoverflow.com/questions/19528432/fanotify-recursivity-does-really-works

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!