Inotify Missing Events

I want to control USB keys on my system. I know that they are always mounted in / media, so I use inotify to monitor / multimedia. Some USB keys create a folder (for example, sda) when connected, which remains until they are disconnected, some create a folder (for example, sda), delete it immediately and create a new one (for example, sda1). This is due to sections on the key.

However, inotify sometimes only captures events to create and delete the first folder, but skips creating the second. When I manually check / media, a second folder exists, but it was not notified inotify.

This happens very rarely, and when this happens, it always happens when you first connect the device after a reboot.

#include <sys/inotify.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> /* size of the event structure, not counting name */ #define EVENT_SIZE (sizeof (struct inotify_event)) /* reasonable guess as to size of 32 events */ #define BUF_LEN (32 * (EVENT_SIZE + 16)) int main(int argc, char **argv) { int fd,wd,len,i; char buf[BUF_LEN]; struct inotify_event *event; fd_set watch_set; fd = inotify_init(); if (fd < 0) { perror("init failed"); exit(EXIT_FAILURE); } wd = inotify_add_watch(fd,"/media",IN_ALL_EVENTS); if (wd < 0) { perror("add watch failed"); exit(EXIT_FAILURE); } /* put the file descriptor to the watch list for select() */ FD_ZERO(&watch_set); FD_SET(fd,&watch_set); while(1) { select(fd+1,&watch_set,NULL,NULL,NULL); len = read(fd,buf,BUF_LEN); i=0; while(i < len) { event = (struct inotify_event *) &buf[i]; if ((event->mask & IN_CREATE) != 0) { printf ("%s created\n",event->name); } else if ((event->mask & IN_DELETE) != 0) { printf ("%s deleted\n",event->name); } else { printf ("wd=%d mask=0x%X cookie=%u len=%u name=%s\n", event->wd, event->mask, event->cookie, event->len, event->name); } i += EVENT_SIZE + event->len; } } } 

Any ideas what is going wrong?

0
source share
3 answers

Meanwhile, I found this to be a known inotify problem. If two events appear almost simultaneously, inotify only catches one of them. My solution: I no longer use inotify, but instead took libudev to control devices connected to the machine ...

0
source

The problem with the inotify subfolder is well known and easy to reproduce:

  • Run inotifywait while viewing the empty tmp directory:

    inotifywait -e create -m -r -format '%: e% f' ./ tmp

  • In another shell, type:

    mkdir tmp / 0 tmp / 0/0 tmp / 0/0/0 tmp / 0/0/0/0

  • Most likely, you will receive a notification only for the first subdirectory.

    CREATE: ISDIR 0

The noted possibility of losing events (especially subdirectory creation events) between creating a directory, notifying your application, and adding new inotify clocks makes recursive monitoring too unreliable. The only safe option is to scan the contents of newly created directories.

From the inotify doc in the Limitations and Disclaimers section:

When monitoring the entire directory subtree and the new subdirectory, remember that by the time you create the watch for the new subdirectory, the new files may have already been created in the subdirectory. Therefore, you can view the contents of a subdirectory immediately after adding the clock.

+3
source
  • You can use the inotifywait command (from the inotify-tools package) to monitor the / media directory to see if the inotify events that interest you are actually occurring.
    link:
    http://www.noah.org/wiki/Inotify,_FAM,_Gamin#Examples_with_inotify-tools

  • If inotify skipping events, the reason could be:
    inotify reports some, but not all, events in sysfs and procfs .
    (Well, I can’t say for sure. Just thinking.)

Reference:
http://en.wikipedia.org/wiki/Inotify#Limitations
http://en.wikipedia.org/wiki/Sysfs
http://en.wikipedia.org/wiki/Procfs

+1
source

Source: https://habr.com/ru/post/987044/


All Articles