Advertisement
zmatt

inotify rant

Nov 28th, 2018
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. // Welcome to the inotify API to avoid polling your filesystem for changes!
  2. //
  3. // For each inode you add to your inotify instance you get a `watch descriptor'
  4. // to identify it.  You cannot supply your own pointer-sized user data like you
  5. // can with any sane callback mechanism, so you'll have to keep your own lookup
  6. // table indexed by watch descriptor.
  7. //
  8. // Fortunately, watch descriptors are integers allocated sequentially from 1
  9. // upwards so you can use a simple array.... provided of course you weren't
  10. // hoping to be able to dynamically add and remove watches since -- unlike file
  11. // descriptors -- watch descriptors are never reused, so you'll need a sparse
  12. // array to avoid leaking memory.
  13. //
  14. // Also, you can't add an inode more than once (even via different paths), so
  15. // if different things request events for, what turns out to be, the same inode
  16. // you will have to notice this and demultiplex the events yourself,
  17. //
  18. // Also, after a sufficient number of dynamic changes to the watch list you
  19. // will run out of watch descriptors, get ENOSPC, and need to rebuild
  20. // everything on a fresh inotify instance.  To keep things interesting, the
  21. // same error code is used to indicate you've hit the configured maximum number
  22. // of total watches per user, which is obviously not fixed by rebuilding (but
  23. // may be transient, unlike running out of descriptors).
  24. //
  25. // Luckily, the code to rescan all your watches needed to be written anyway to
  26. // deal with the case where the kernel indicates the event queue overflowed and
  27. // events were dropped.
  28. //
  29. // Oh, and if for some reason you'd wish to support kernel versions older than
  30. // 2.6.36 and therefore can't use IN_EXCL_UNLINK, if any child of a watched dir
  31. // is unlinked you'll probably want to remove, readd, and rescan the directory
  32. // since otherwise you will still get events for the unlinked child but have no
  33. // way of distinguishing these from events for a new child with the same name.
  34. //
  35. // Of course it's also quite possible you can't create the inotify instance at
  36. // all since you hit the per-user limit on inotify instances (128 by default).
  37. //
  38. // Any questions?  Have fun!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement