inotify missing events

后端 未结 3 1390
佛祖请我去吃肉
佛祖请我去吃肉 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.

提交回复
热议问题