3
0

inotifyd.c 3.8 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 <linux/inotify.h>
  30. static volatile smallint signalled;
  31. static void signal_handler(int signo)
  32. {
  33. signalled = signo;
  34. }
  35. static const char mask_names[] ALIGN1 =
  36. "a" // 0x00000001 File was accessed
  37. "c" // 0x00000002 File was modified
  38. "e" // 0x00000004 Metadata changed
  39. "w" // 0x00000008 Writtable file was closed
  40. "0" // 0x00000010 Unwrittable file closed
  41. "r" // 0x00000020 File was opened
  42. "m" // 0x00000040 File was moved from X
  43. "y" // 0x00000080 File was moved to Y
  44. "n" // 0x00000100 Subfile was created
  45. "d" // 0x00000200 Subfile was deleted
  46. "D" // 0x00000400 Self was deleted
  47. "M" // 0x00000800 Self was moved
  48. ;
  49. extern int inotify_init(void);
  50. extern int inotify_add_watch(int fd, const char *path, uint32_t mask);
  51. int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  52. int inotifyd_main(int argc UNUSED_PARAM, char **argv)
  53. {
  54. unsigned mask = IN_ALL_EVENTS; // assume we want all events
  55. struct pollfd pfd;
  56. char **watched = ++argv; // watched name list
  57. const char *args[] = { *argv, NULL, NULL, NULL, NULL };
  58. // sanity check: agent and at least one watch must be given
  59. if (!argv[1])
  60. bb_show_usage();
  61. // open inotify
  62. pfd.fd = inotify_init();
  63. if (pfd.fd < 0)
  64. bb_perror_msg_and_die("no kernel support");
  65. // setup watched
  66. while (*++argv) {
  67. char *path = *argv;
  68. char *masks = strchr(path, ':');
  69. int wd; // watch descriptor
  70. // if mask is specified ->
  71. if (masks) {
  72. *masks = '\0'; // split path and mask
  73. // convert mask names to mask bitset
  74. mask = 0;
  75. while (*++masks) {
  76. int i = strchr(mask_names, *masks) - mask_names;
  77. if (i >= 0) {
  78. mask |= (1 << i);
  79. }
  80. }
  81. }
  82. // add watch
  83. wd = inotify_add_watch(pfd.fd, path, mask);
  84. if (wd < 0) {
  85. bb_perror_msg_and_die("add watch (%s) failed", path);
  86. // } else {
  87. // bb_error_msg("added %d [%s]:%4X", wd, path, mask);
  88. }
  89. }
  90. // setup signals
  91. bb_signals(0
  92. + (1 << SIGHUP)
  93. + (1 << SIGINT)
  94. + (1 << SIGTERM)
  95. + (1 << SIGPIPE)
  96. , signal_handler);
  97. // do watch
  98. // pfd.fd = fd;
  99. pfd.events = POLLIN;
  100. while (!signalled && poll(&pfd, 1, -1) > 0) {
  101. ssize_t len;
  102. void *buf;
  103. struct inotify_event *ie;
  104. // read out all pending events
  105. xioctl(pfd.fd, FIONREAD, &len);
  106. #define eventbuf bb_common_bufsiz1
  107. ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
  108. len = full_read(pfd.fd, buf, len);
  109. // process events. N.B. events may vary in length
  110. while (len > 0) {
  111. int i;
  112. char events[12];
  113. char *s = events;
  114. unsigned m = ie->mask;
  115. for (i = 0; i < 12; ++i, m >>= 1) {
  116. if (m & 1) {
  117. *s++ = mask_names[i];
  118. }
  119. }
  120. *s = '\0';
  121. // bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent, ie->mask, events, watched[ie->wd], ie->len ? ie->name : "");
  122. args[1] = events;
  123. args[2] = watched[ie->wd];
  124. args[3] = ie->len ? ie->name : NULL;
  125. xspawn((char **)args);
  126. // next event
  127. i = sizeof(struct inotify_event) + ie->len;
  128. len -= i;
  129. ie = (void*)((char*)ie + i);
  130. }
  131. if (eventbuf != buf)
  132. free(buf);
  133. }
  134. return EXIT_SUCCESS;
  135. }