dnotify.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Linux Directory Notification
  2. ============================
  3. Stephen Rothwell <sfr@canb.auug.org.au>
  4. The intention of directory notification is to allow user applications
  5. to be notified when a directory, or any of the files in it, are changed.
  6. The basic mechanism involves the application registering for notification
  7. on a directory using a fcntl(2) call and the notifications themselves
  8. being delivered using signals.
  9. The application decides which "events" it wants to be notified about.
  10. The currently defined events are:
  11. DN_ACCESS A file in the directory was accessed (read)
  12. DN_MODIFY A file in the directory was modified (write,truncate)
  13. DN_CREATE A file was created in the directory
  14. DN_DELETE A file was unlinked from directory
  15. DN_RENAME A file in the directory was renamed
  16. DN_ATTRIB A file in the directory had its attributes
  17. changed (chmod,chown)
  18. Usually, the application must reregister after each notification, but
  19. if DN_MULTISHOT is or'ed with the event mask, then the registration will
  20. remain until explicitly removed (by registering for no events).
  21. By default, SIGIO will be delivered to the process and no other useful
  22. information. However, if the F_SETSIG fcntl(2) call is used to let the
  23. kernel know which signal to deliver, a siginfo structure will be passed to
  24. the signal handler and the si_fd member of that structure will contain the
  25. file descriptor associated with the directory in which the event occurred.
  26. Preferably the application will choose one of the real time signals
  27. (SIGRTMIN + <n>) so that the notifications may be queued. This is
  28. especially important if DN_MULTISHOT is specified. Note that SIGRTMIN
  29. is often blocked, so it is better to use (at least) SIGRTMIN + 1.
  30. Implementation expectations (features and bugs :-))
  31. ---------------------------
  32. The notification should work for any local access to files even if the
  33. actual file system is on a remote server. This implies that remote
  34. access to files served by local user mode servers should be notified.
  35. Also, remote accesses to files served by a local kernel NFS server should
  36. be notified.
  37. In order to make the impact on the file system code as small as possible,
  38. the problem of hard links to files has been ignored. So if a file (x)
  39. exists in two directories (a and b) then a change to the file using the
  40. name "a/x" should be notified to a program expecting notifications on
  41. directory "a", but will not be notified to one expecting notifications on
  42. directory "b".
  43. Also, files that are unlinked, will still cause notifications in the
  44. last directory that they were linked to.
  45. Configuration
  46. -------------
  47. Dnotify is controlled via the CONFIG_DNOTIFY configuration option. When
  48. disabled, fcntl(fd, F_NOTIFY, ...) will return -EINVAL.
  49. Example
  50. -------
  51. #define _GNU_SOURCE /* needed to get the defines */
  52. #include <fcntl.h> /* in glibc 2.2 this has the needed
  53. values defined */
  54. #include <signal.h>
  55. #include <stdio.h>
  56. #include <unistd.h>
  57. static volatile int event_fd;
  58. static void handler(int sig, siginfo_t *si, void *data)
  59. {
  60. event_fd = si->si_fd;
  61. }
  62. int main(void)
  63. {
  64. struct sigaction act;
  65. int fd;
  66. act.sa_sigaction = handler;
  67. sigemptyset(&act.sa_mask);
  68. act.sa_flags = SA_SIGINFO;
  69. sigaction(SIGRTMIN + 1, &act, NULL);
  70. fd = open(".", O_RDONLY);
  71. fcntl(fd, F_SETSIG, SIGRTMIN + 1);
  72. fcntl(fd, F_NOTIFY, DN_MODIFY|DN_CREATE|DN_MULTISHOT);
  73. /* we will now be notified if any of the files
  74. in "." is modified or new files are created */
  75. while (1) {
  76. pause();
  77. printf("Got event on fd=%d\n", event_fd);
  78. }
  79. }