inotifyd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * simple inotify daemon
  4. * reports filesystem changes via userspace agent
  5. *
  6. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. /*
  11. * Use as follows:
  12. * # inotifyd /user/space/agent dir/or/file/being/watched[:mask] ...
  13. *
  14. * When a filesystem event matching the specified mask is occured on specified file (or directory)
  15. * a userspace agent is spawned and given the following parameters:
  16. * $1. actual event(s)
  17. * $2. file (or directory) name
  18. * $3. name of subfile (if any), in case of watching a directory
  19. *
  20. * E.g. inotifyd ./dev-watcher /dev:n
  21. *
  22. * ./dev-watcher can be, say:
  23. * #!/bin/sh
  24. * echo "We have new device in here! Hello, $3!"
  25. *
  26. * See below for mask names explanation.
  27. */
  28. #include "libbb.h"
  29. #include <sys/inotify.h>
  30. static const char mask_names[] ALIGN1 =
  31. "a" // 0x00000001 File was accessed
  32. "c" // 0x00000002 File was modified
  33. "e" // 0x00000004 Metadata changed
  34. "w" // 0x00000008 Writable file was closed
  35. "0" // 0x00000010 Unwritable file closed
  36. "r" // 0x00000020 File was opened
  37. "m" // 0x00000040 File was moved from X
  38. "y" // 0x00000080 File was moved to Y
  39. "n" // 0x00000100 Subfile was created
  40. "d" // 0x00000200 Subfile was deleted
  41. "D" // 0x00000400 Self was deleted
  42. "M" // 0x00000800 Self was moved
  43. "\0" // 0x00001000 (unused)
  44. // Kernel events, always reported:
  45. "u" // 0x00002000 Backing fs was unmounted
  46. "o" // 0x00004000 Event queued overflowed
  47. "x" // 0x00008000 File is no longer watched (usually deleted)
  48. ;
  49. enum {
  50. MASK_BITS = sizeof(mask_names) - 1
  51. };
  52. int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  53. int inotifyd_main(int argc, char **argv)
  54. {
  55. int n;
  56. unsigned mask;
  57. struct pollfd pfd;
  58. char **watches; // names of files being watched
  59. const char *args[5];
  60. // sanity check: agent and at least one watch must be given
  61. if (!argv[1] || !argv[2])
  62. bb_show_usage();
  63. argv++;
  64. // inotify_add_watch will number watched files
  65. // starting from 1, thus watches[0] is unimportant,
  66. // and 1st file name is watches[1].
  67. watches = argv;
  68. args[0] = *argv;
  69. args[4] = NULL;
  70. argc -= 2; // number of files we watch
  71. // open inotify
  72. pfd.fd = inotify_init();
  73. if (pfd.fd < 0)
  74. bb_perror_msg_and_die("no kernel support");
  75. // setup watches
  76. while (*++argv) {
  77. char *path = *argv;
  78. char *masks = strchr(path, ':');
  79. mask = 0x0fff; // assuming we want all non-kernel events
  80. // if mask is specified ->
  81. if (masks) {
  82. *masks = '\0'; // split path and mask
  83. // convert mask names to mask bitset
  84. mask = 0;
  85. while (*++masks) {
  86. const char *found;
  87. found = memchr(mask_names, *masks, MASK_BITS);
  88. if (found)
  89. mask |= (1 << (found - mask_names));
  90. }
  91. }
  92. // add watch
  93. n = inotify_add_watch(pfd.fd, path, mask);
  94. if (n < 0)
  95. bb_perror_msg_and_die("add watch (%s) failed", path);
  96. //bb_error_msg("added %d [%s]:%4X", n, path, mask);
  97. }
  98. // setup signals
  99. bb_signals(BB_FATAL_SIGS, record_signo);
  100. // do watch
  101. pfd.events = POLLIN;
  102. while (1) {
  103. int len;
  104. void *buf;
  105. struct inotify_event *ie;
  106. again:
  107. if (bb_got_signal)
  108. break;
  109. n = poll(&pfd, 1, -1);
  110. // Signal interrupted us?
  111. if (n < 0 && errno == EINTR)
  112. goto again;
  113. // Under Linux, above if() is not necessary.
  114. // Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL,
  115. // are not interrupting poll().
  116. // Thus we can just break if n <= 0 (see below),
  117. // because EINTR will happen only on SIGTERM et al.
  118. // But this might be not true under other Unixes,
  119. // and is generally way too subtle to depend on.
  120. if (n <= 0) // strange error?
  121. break;
  122. // read out all pending events
  123. // (NB: len must be int, not ssize_t or long!)
  124. xioctl(pfd.fd, FIONREAD, &len);
  125. #define eventbuf bb_common_bufsiz1
  126. ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
  127. len = full_read(pfd.fd, buf, len);
  128. // process events. N.B. events may vary in length
  129. while (len > 0) {
  130. int i;
  131. // cache relevant events mask
  132. unsigned m = ie->mask & ((1 << MASK_BITS) - 1);
  133. if (m) {
  134. char events[MASK_BITS + 1];
  135. char *s = events;
  136. for (i = 0; i < MASK_BITS; ++i, m >>= 1) {
  137. if ((m & 1) && (mask_names[i] != '\0'))
  138. *s++ = mask_names[i];
  139. }
  140. *s = '\0';
  141. // bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0],
  142. // ie->mask, events, watches[ie->wd], ie->len ? ie->name : "");
  143. args[1] = events;
  144. args[2] = watches[ie->wd];
  145. args[3] = ie->len ? ie->name : NULL;
  146. spawn_and_wait((char **)args);
  147. // we are done if all files got final x event
  148. if (ie->mask & 0x8000) {
  149. if (--argc <= 0)
  150. goto done;
  151. inotify_rm_watch(pfd.fd, ie->wd);
  152. }
  153. }
  154. // next event
  155. i = sizeof(struct inotify_event) + ie->len;
  156. len -= i;
  157. ie = (void*)((char*)ie + i);
  158. }
  159. if (eventbuf != buf)
  160. free(buf);
  161. } // while (1)
  162. done:
  163. return bb_got_signal;
  164. }