inotifyd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. //usage:#define inotifyd_trivial_usage
  29. //usage: "PROG FILE1[:MASK]..."
  30. //usage:#define inotifyd_full_usage "\n\n"
  31. //usage: "Run PROG on filesystem changes."
  32. //usage: "\nWhen a filesystem event matching MASK occurs on FILEn,"
  33. //usage: "\nPROG ACTUAL_EVENTS FILEn [SUBFILE] is run."
  34. //usage: "\nIf PROG is -, events are sent to stdout."
  35. //usage: "\nEvents:"
  36. //usage: "\n a File is accessed"
  37. //usage: "\n c File is modified"
  38. //usage: "\n e Metadata changed"
  39. //usage: "\n w Writable file is closed"
  40. //usage: "\n 0 Unwritable file is closed"
  41. //usage: "\n r File is opened"
  42. //usage: "\n D File is deleted"
  43. //usage: "\n M File is moved"
  44. //usage: "\n u Backing fs is unmounted"
  45. //usage: "\n o Event queue overflowed"
  46. //usage: "\n x File can't be watched anymore"
  47. //usage: "\nIf watching a directory:"
  48. //usage: "\n m Subfile is moved into dir"
  49. //usage: "\n y Subfile is moved out of dir"
  50. //usage: "\n n Subfile is created"
  51. //usage: "\n d Subfile is deleted"
  52. //usage: "\n"
  53. //usage: "\ninotifyd waits for PROG to exit."
  54. //usage: "\nWhen x event happens for all FILEs, inotifyd exits."
  55. #include "libbb.h"
  56. #include <sys/inotify.h>
  57. static const char mask_names[] ALIGN1 =
  58. "a" // 0x00000001 File was accessed
  59. "c" // 0x00000002 File was modified
  60. "e" // 0x00000004 Metadata changed
  61. "w" // 0x00000008 Writable file was closed
  62. "0" // 0x00000010 Unwritable file closed
  63. "r" // 0x00000020 File was opened
  64. "m" // 0x00000040 File was moved from X
  65. "y" // 0x00000080 File was moved to Y
  66. "n" // 0x00000100 Subfile was created
  67. "d" // 0x00000200 Subfile was deleted
  68. "D" // 0x00000400 Self was deleted
  69. "M" // 0x00000800 Self was moved
  70. "\0" // 0x00001000 (unused)
  71. // Kernel events, always reported:
  72. "u" // 0x00002000 Backing fs was unmounted
  73. "o" // 0x00004000 Event queued overflowed
  74. "x" // 0x00008000 File is no longer watched (usually deleted)
  75. ;
  76. enum {
  77. MASK_BITS = sizeof(mask_names) - 1
  78. };
  79. int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  80. int inotifyd_main(int argc, char **argv)
  81. {
  82. int n;
  83. unsigned mask;
  84. struct pollfd pfd;
  85. char **watches; // names of files being watched
  86. const char *args[5];
  87. // sanity check: agent and at least one watch must be given
  88. if (!argv[1] || !argv[2])
  89. bb_show_usage();
  90. argv++;
  91. // inotify_add_watch will number watched files
  92. // starting from 1, thus watches[0] is unimportant,
  93. // and 1st file name is watches[1].
  94. watches = argv;
  95. args[0] = *argv;
  96. args[4] = NULL;
  97. argc -= 2; // number of files we watch
  98. // open inotify
  99. pfd.fd = inotify_init();
  100. if (pfd.fd < 0)
  101. bb_perror_msg_and_die("no kernel support");
  102. // setup watches
  103. while (*++argv) {
  104. char *path = *argv;
  105. char *masks = strchr(path, ':');
  106. mask = 0x0fff; // assuming we want all non-kernel events
  107. // if mask is specified ->
  108. if (masks) {
  109. *masks = '\0'; // split path and mask
  110. // convert mask names to mask bitset
  111. mask = 0;
  112. while (*++masks) {
  113. const char *found;
  114. found = memchr(mask_names, *masks, MASK_BITS);
  115. if (found)
  116. mask |= (1 << (found - mask_names));
  117. }
  118. }
  119. // add watch
  120. n = inotify_add_watch(pfd.fd, path, mask);
  121. if (n < 0)
  122. bb_perror_msg_and_die("add watch (%s) failed", path);
  123. //bb_error_msg("added %d [%s]:%4X", n, path, mask);
  124. }
  125. // setup signals
  126. bb_signals(BB_FATAL_SIGS, record_signo);
  127. // do watch
  128. pfd.events = POLLIN;
  129. while (1) {
  130. int len;
  131. void *buf;
  132. struct inotify_event *ie;
  133. again:
  134. if (bb_got_signal)
  135. break;
  136. n = poll(&pfd, 1, -1);
  137. // Signal interrupted us?
  138. if (n < 0 && errno == EINTR)
  139. goto again;
  140. // Under Linux, above if() is not necessary.
  141. // Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL,
  142. // are not interrupting poll().
  143. // Thus we can just break if n <= 0 (see below),
  144. // because EINTR will happen only on SIGTERM et al.
  145. // But this might be not true under other Unixes,
  146. // and is generally way too subtle to depend on.
  147. if (n <= 0) // strange error?
  148. break;
  149. // read out all pending events
  150. // (NB: len must be int, not ssize_t or long!)
  151. xioctl(pfd.fd, FIONREAD, &len);
  152. #define eventbuf bb_common_bufsiz1
  153. ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
  154. len = full_read(pfd.fd, buf, len);
  155. // process events. N.B. events may vary in length
  156. while (len > 0) {
  157. int i;
  158. // cache relevant events mask
  159. unsigned m = ie->mask & ((1 << MASK_BITS) - 1);
  160. if (m) {
  161. char events[MASK_BITS + 1];
  162. char *s = events;
  163. for (i = 0; i < MASK_BITS; ++i, m >>= 1) {
  164. if ((m & 1) && (mask_names[i] != '\0'))
  165. *s++ = mask_names[i];
  166. }
  167. *s = '\0';
  168. if (LONE_CHAR(args[0], '-')) {
  169. /* "inotifyd - FILE": built-in echo */
  170. printf(ie->len ? "%s\t%s\t%s\n" : "%s\t%s\n", events,
  171. watches[ie->wd],
  172. ie->name);
  173. fflush(stdout);
  174. } else {
  175. // bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0],
  176. // ie->mask, events, watches[ie->wd], ie->len ? ie->name : "");
  177. args[1] = events;
  178. args[2] = watches[ie->wd];
  179. args[3] = ie->len ? ie->name : NULL;
  180. spawn_and_wait((char **)args);
  181. }
  182. // we are done if all files got final x event
  183. if (ie->mask & 0x8000) {
  184. if (--argc <= 0)
  185. goto done;
  186. inotify_rm_watch(pfd.fd, ie->wd);
  187. }
  188. }
  189. // next event
  190. i = sizeof(struct inotify_event) + ie->len;
  191. len -= i;
  192. ie = (void*)((char*)ie + i);
  193. }
  194. if (eventbuf != buf)
  195. free(buf);
  196. } // while (1)
  197. done:
  198. return bb_got_signal;
  199. }