inotifyd.c 6.2 KB

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