0

I'm newbie to fanotify.

I used the example of the fanotify manpage to write any information to a file, while handling events of file open and close. A system call to 'fopen' cause system hangs. When I changed 'FAN_OPEN_PERM' to 'FAN_OPEN', it's all ok, but 'FAN_OPEN_PERM'flag is not allowed to log file.

Are there something I missed to use fanotify tech. or any limitations exist for handling fanotify ?

Or any better ideas to log a file while processing fanotify events ?

I've compiled and tested under 'Ubuntu 14.04.3 64bit' with '3.16.0-70-generic' kernel version.

I added some code like this :

static void PrintToFile(const char *pszMsg) 
{
    int err = 0;
    if( NULL == pszMsg) {
        printf("invalid message\n");
        return ;
    }

    FILE *fp = fopen("/tmp/fanotify.log", "a+");   // <= here, system hangs
    if( NULL == fp ) {
        err = errno;
        printf("file open fail ( %d ) \n", err);
        return ;
    }
    size_t len = strlen(pszMsg);
    feesk(fp, 0L, SEEK_END );
    fwrite(pszMsg, 1, len, fp);
    fclose(fp); 
}

and then, I added the next code to the 'handle_events' fucntion

{
    char strBuf[PATH_MAX];
    sprintf(strBuf, "File %s\n", path);
    PrintToFile(strBuf);
}

See the modifed 'handle_events' function

static void
handle_events(int fd)
{
       const struct fanotify_event_metadata *metadata;
       struct fanotify_event_metadata buf[200];
       ssize_t len;
       char path[PATH_MAX];
       ssize_t path_len;
       char procfd_path[PATH_MAX];
       struct fanotify_response response;

       /* Loop while events can be read from fanotify file descriptor */

       for(;;) {

           /* Read some events */

           len = read(fd, (void *) &buf, sizeof(buf));
           if (len == -1 && errno != EAGAIN) {
               perror("read");
               exit(EXIT_FAILURE);
           }

           /* Check if end of available data reached */

           if (len <= 0)
               break;

           /* Point to the first event in the buffer */

           metadata = buf;

           /* Loop over all events in the buffer */

           while (FAN_EVENT_OK(metadata, len)) {

               /* Check that run-time and compile-time structures match */

               if (metadata->vers != FANOTIFY_METADATA_VERSION) {
                   fprintf(stderr,
                           "Mismatch of fanotify metadata version.\n");
                   exit(EXIT_FAILURE);
               }

               /* metadata->fd contains either FAN_NOFD, indicating a
                  queue overflow, or a file descriptor (a nonnegative
                  integer). Here, we simply ignore queue overflow. */

               if (metadata->fd >= 0) {

                   /* Handle open permission event */

                   if (metadata->mask & FAN_OPEN_PERM) {
                       printf("FAN_OPEN_PERM: ");

                       /* Allow file to be opened */

                       response.fd = metadata->fd;
                       response.response = FAN_ALLOW;
                       write(fd, &response,
                             sizeof(struct fanotify_response));
                   }

                   /* Handle closing of writable file event */

                   if (metadata->mask & FAN_CLOSE_WRITE)
                       printf("FAN_CLOSE_WRITE: ");

                   /* Retrieve and print pathname of the accessed file */

                   snprintf(procfd_path, sizeof(procfd_path),
                            "/proc/self/fd/%d", metadata->fd);
                   path_len = readlink(procfd_path, path,
                                       sizeof(path) - 1);
                   if (path_len == -1) {
                       perror("readlink");
                       exit(EXIT_FAILURE);
                   }

                   path[path_len] = '\0';
                   printf("File %s\n", path);

                   //these code snipptets are added
                   {
                       char strBuf[PATH_MAX];
                       sprintf(strBuf, "File %s\n", path);
                       PrintToFile(strBuf);
                   }

                   /* Close the file descriptor of the event */

                   close(metadata->fd);
               }

               /* Advance to next event */

               metadata = FAN_EVENT_NEXT(metadata, len);
           }
       }
   }
Taesung
  • 1
  • 2

2 Answers2

1

Not sure but opening a file in a "file open" monitor handler may cause infinite loop. Try adding: if (metadata->pid != getpid()) before the block with the call to PrintToFile to ignore printing events cause but your program itself.

Marek
  • 11
  • 1
0

FAN_OPEN_PERM flag request a response from you whether allow to open file nor deny and it blocks events until you answer to kernel that what it should do with that file