inotifyd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 tarball for details.
  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 Writtable file was closed
  35. "0" // 0x00000010 Unwrittable 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. ;
  44. extern int inotify_init(void);
  45. extern int inotify_add_watch(int fd, const char *path, uint32_t mask);
  46. int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int inotifyd_main(int argc UNUSED_PARAM, char **argv)
  48. {
  49. int n;
  50. unsigned mask = IN_ALL_EVENTS; // assume we want all events
  51. struct pollfd pfd;
  52. char **watched = ++argv; // watched name list
  53. const char *args[] = { *argv, NULL, NULL, NULL, NULL };
  54. // sanity check: agent and at least one watch must be given
  55. if (!argv[1])
  56. bb_show_usage();
  57. // open inotify
  58. pfd.fd = inotify_init();
  59. if (pfd.fd < 0)
  60. bb_perror_msg_and_die("no kernel support");
  61. // setup watched
  62. while (*++argv) {
  63. char *path = *argv;
  64. char *masks = strchr(path, ':');
  65. // if mask is specified ->
  66. if (masks) {
  67. *masks = '\0'; // split path and mask
  68. // convert mask names to mask bitset
  69. mask = 0;
  70. while (*++masks) {
  71. int i = strchr(mask_names, *masks) - mask_names;
  72. if (i >= 0) {
  73. mask |= (1 << i);
  74. }
  75. }
  76. }
  77. // add watch
  78. n = inotify_add_watch(pfd.fd, path, mask);
  79. if (n < 0)
  80. bb_perror_msg_and_die("add watch (%s) failed", path);
  81. //bb_error_msg("added %d [%s]:%4X", n, path, mask);
  82. }
  83. // setup signals
  84. bb_signals(BB_FATAL_SIGS, record_signo);
  85. // do watch
  86. pfd.events = POLLIN;
  87. while (1) {
  88. ssize_t len;
  89. void *buf;
  90. struct inotify_event *ie;
  91. again:
  92. if (bb_got_signal)
  93. break;
  94. n = poll(&pfd, 1, -1);
  95. /* Signal interrupted us? */
  96. if (n < 0 && errno == EINTR)
  97. goto again;
  98. // Under Linux, above if() is not necessary.
  99. // Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL,
  100. // are not interrupting poll().
  101. // Thus we can just break if n <= 0 (see below),
  102. // because EINTR will happen only on SIGTERM et al.
  103. // But this might be not true under other Unixes,
  104. // and is generally way too subtle to depend on.
  105. if (n <= 0) // strange error?
  106. break;
  107. // read out all pending events
  108. xioctl(pfd.fd, FIONREAD, &len);
  109. #define eventbuf bb_common_bufsiz1
  110. ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
  111. len = full_read(pfd.fd, buf, len);
  112. // process events. N.B. events may vary in length
  113. while (len > 0) {
  114. int i;
  115. char events[sizeof(mask_names)];
  116. char *s = events;
  117. unsigned m = ie->mask;
  118. for (i = 0; i < sizeof(mask_names)-1; ++i, m >>= 1) {
  119. if (m & 1)
  120. *s++ = mask_names[i];
  121. }
  122. *s = '\0';
  123. //bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent,
  124. // ie->mask, events, watched[ie->wd], ie->len ? ie->name : "");
  125. args[1] = events;
  126. args[2] = watched[ie->wd];
  127. args[3] = ie->len ? ie->name : NULL;
  128. wait4pid(xspawn((char **)args));
  129. // next event
  130. i = sizeof(struct inotify_event) + ie->len;
  131. len -= i;
  132. ie = (void*)((char*)ie + i);
  133. }
  134. if (eventbuf != buf)
  135. free(buf);
  136. }
  137. return EXIT_SUCCESS;
  138. }