inotify missing events

后端 未结 3 1391
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-25 12:24

I want to monitor USB-Keys on my system. I know they are always mounted in /media so I use inotify to monitor /media. Some USB Keys create a folder (e.g. sda) when plugged which

相关标签:
3条回答
  • 2021-01-25 13:01

    The subfolder problem with inotify is well known and easily reproduced:

    1. Start inotifywait watching an empty tmp directory:

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

    2. In another shell enter:

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

    3. You will most likely only get a notification for the first subdirectory.

      CREATE:ISDIR 0

    The distinct possibility of losing events (particularly subdirectory creation events) between the time a directory is created, your app gets notified, and a new inotify watch is added, makes recursive monitoring too unreliable. The only safe option is to scan contents of newly created directories.

    From the inotify doc under Limitations and caveats:

    If monitoring an entire directory subtree, and a new subdirectory is created in that tree, be aware that by the time you create a watch for the new subdirectory, new files may already have been created in the subdirectory. Therefore, you may want to scan the contents of the subdirectory immediately after adding the watch.

    0 讨论(0)
    1. You can use inotifywait command (from the inotify-tools package) to monitor the /media directory, in order to check whether the inotify events which interest you do occur.
      reference:
      http://www.noah.org/wiki/Inotify,_FAM,_Gamin#Examples_with_inotify-tools

    2. If inotify does miss events, the reason might be:
      Inotify does report some but not all events in sysfs and procfs.
      (Well, I can not say for sure. Just my guess.)

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

    0 讨论(0)
  • 2021-01-25 13:11

    In the meanwhile I found that this is a known issue of inotify. If two events appear virtually at the same time, inotify only catches one of them. My solution: I don't use inotify anymore, but took libudev instead to monitor the devices plugged to the machine...

    0 讨论(0)
提交回复
热议问题