inotifyd.c 6.5 KB

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