1

I want to monitor a single directory using fanotify, but what I got is monitoring the whole filesystem. This is the code:

fa_fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_CONTENT, O_RDONLY | O_LARGEFILE | O_CLOEXEC);

static uint64_t event_mask = 
(FAN_ACCESS |         /* File accessed */
FAN_MODIFY |         /* File modified */
FAN_CLOSE_WRITE |    /* Writable file closed */
FAN_OPEN |           /* File was opened */
FAN_EVENT_ON_CHILD); /* We want to be reported of events in   files of the directory */

filesystem::path path("some-relative-path");
if (!filesystem::exists(path)){
    filesystem::create_directory(path);
}

if (fanotify_mark(fa_fd,
    FAN_MARK_ADD,
    event_mask,
    AT_FDCWD,
    "some-relative-path") >= 0)
{
    return NO_ERROR;
}

I have read from fanotify's man page if pathname("some-relative-path") is relative and we have set fds to AT_FDCWD then we are asking for mark that relative path.

I'm working with threads but I don't think that's the problem. Maybe I'm using wrong some flag or I'm not using the correct one at all.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • Hi Raydel. For what is worth the fragment you posted seems correct. But since you are not posting a complete program together with example usage and output, it is very difficult for anybody reading your question to actually help. Asere no nos escondas la bola ;-) – dsign Nov 11 '13 at 14:59

1 Answers1

2

I have face this problem, when we use FAN_EVENT_ON_CHILD, fanotify monitors the entire mount point of that specified directory/file. When you monitor only directory, with FAN_ONDIR, fanotify monitors only files in that directory and not sub-directories.

Therefore, you have to monitor either whole mount point or a directory without its sub-directories. Notice that, you can skip some files/directories while monitoring entire mount point(s). I hope this will help you :)

Nitinkumar Ambekar
  • 969
  • 20
  • 39
  • Thanks for your answer. I already figured out what was going on see http://stackoverflow.com/questions/19528432/fanotify-recursivity-does-really-works/19543049#19543049 – Raydel Miranda Jan 07 '14 at 13:03