sys_watch_directory.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * @file sys_watch_directory.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Directory watcher.
  25. *
  26. * Synopsis: sys.watch_directory(string dir)
  27. * Description: reports directory entry events. Transitions up when an event is detected, and
  28. * goes down waiting for the next event when sys.watch_directory::nextevent() is called.
  29. * Variables:
  30. * string event_type - what happened with the file: "added", "removed" or "changed"
  31. * string filename - name of the file in the directory the event refers to
  32. * string filepath - "dir/filename"
  33. *
  34. * Synopsis: sys.watch_directory::nextevent()
  35. * Description: makes the watch_directory module transition down in order to report the next event.
  36. */
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <sys/inotify.h>
  41. #include <misc/nonblocking.h>
  42. #include <misc/concat_strings.h>
  43. #include <ncd/NCDModule.h>
  44. #include <generated/blog_channel_ncd_sys_watch_directory.h>
  45. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. #define MAX_EVENTS 128
  47. struct instance {
  48. NCDModuleInst *i;
  49. char *dir;
  50. int inotify_fd;
  51. BFileDescriptor bfd;
  52. int processing;
  53. struct inotify_event events[MAX_EVENTS];
  54. int events_count;
  55. int events_index;
  56. };
  57. struct nextevent_instance {
  58. NCDModuleInst *i;
  59. };
  60. static void assert_event (struct instance *o)
  61. {
  62. ASSERT(o->events_index < o->events_count)
  63. ASSERT(o->events[o->events_index].len % sizeof(o->events[0]) == 0)
  64. ASSERT(o->events[o->events_index].len / sizeof(o->events[0]) <= o->events_count - (o->events_index + 1))
  65. }
  66. static int check_event (struct instance *o)
  67. {
  68. assert_event(o);
  69. return (
  70. strlen(o->events[o->events_index].name) > 0 &&
  71. (o->events[o->events_index].mask & (IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO))
  72. );
  73. }
  74. static void next_event (struct instance *o)
  75. {
  76. assert_event(o);
  77. o->events_index += 1 + o->events[o->events_index].len / sizeof(o->events[0]);
  78. }
  79. static void skip_bad_events (struct instance *o)
  80. {
  81. while (o->events_index < o->events_count && !check_event(o)) {
  82. ModuleLog(o->i, BLOG_ERROR, "unknown inotify event");
  83. next_event(o);
  84. }
  85. }
  86. static void inotify_fd_handler (struct instance *o, int events)
  87. {
  88. ASSERT(!o->processing)
  89. int res = read(o->inotify_fd, o->events, sizeof(o->events));
  90. if (res < 0) {
  91. ModuleLog(o->i, BLOG_ERROR, "read failed");
  92. return;
  93. }
  94. ASSERT(res <= sizeof(o->events))
  95. ASSERT(res % sizeof(o->events[0]) == 0)
  96. // setup buffer state
  97. o->events_count = res / sizeof(o->events[0]);
  98. o->events_index = 0;
  99. // skip bad events
  100. skip_bad_events(o);
  101. if (o->events_index == o->events_count) {
  102. // keep reading
  103. return;
  104. }
  105. // stop reading
  106. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, 0);
  107. // set processing
  108. o->processing = 1;
  109. // signal up
  110. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  111. }
  112. static void inotify_nextevent (struct instance *o)
  113. {
  114. ASSERT(o->processing)
  115. assert_event(o);
  116. // update index, skip bad events
  117. next_event(o);
  118. skip_bad_events(o);
  119. if (o->events_index == o->events_count) {
  120. // start reading
  121. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  122. // set not processing
  123. o->processing = 0;
  124. // signal down
  125. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  126. return;
  127. }
  128. // signal down and up to process the next event
  129. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  130. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  131. }
  132. static void func_new (NCDModuleInst *i)
  133. {
  134. // allocate instance
  135. struct instance *o = malloc(sizeof(*o));
  136. if (!o) {
  137. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  138. goto fail0;
  139. }
  140. NCDModuleInst_Backend_SetUser(i, o);
  141. // init arguments
  142. o->i = i;
  143. // check arguments
  144. NCDValue *dir_arg;
  145. if (!NCDValue_ListRead(o->i->args, 1, &dir_arg)) {
  146. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  147. goto fail1;
  148. }
  149. if (NCDValue_Type(dir_arg) != NCDVALUE_STRING) {
  150. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  151. goto fail1;
  152. }
  153. o->dir = NCDValue_StringValue(dir_arg);
  154. // open inotify
  155. if ((o->inotify_fd = inotify_init()) < 0) {
  156. ModuleLog(o->i, BLOG_ERROR, "inotify_init failed");
  157. goto fail1;
  158. }
  159. // add watch
  160. if (inotify_add_watch(o->inotify_fd, o->dir, IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO) < 0) {
  161. ModuleLog(o->i, BLOG_ERROR, "inotify_add_watch failed");
  162. goto fail2;
  163. }
  164. // set non-blocking
  165. if (!badvpn_set_nonblocking(o->inotify_fd)) {
  166. ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
  167. goto fail2;
  168. }
  169. // init BFileDescriptor
  170. BFileDescriptor_Init(&o->bfd, o->inotify_fd, (BFileDescriptor_handler)inotify_fd_handler, o);
  171. if (!BReactor_AddFileDescriptor(o->i->reactor, &o->bfd)) {
  172. ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  173. goto fail2;
  174. }
  175. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  176. // set not processing
  177. o->processing = 0;
  178. return;
  179. fail2:
  180. ASSERT_FORCE(close(o->inotify_fd) == 0)
  181. fail1:
  182. free(o);
  183. fail0:
  184. NCDModuleInst_Backend_SetError(i);
  185. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  186. }
  187. static void func_die (void *vo)
  188. {
  189. struct instance *o = vo;
  190. NCDModuleInst *i = o->i;
  191. // free BFileDescriptor
  192. BReactor_RemoveFileDescriptor(o->i->reactor, &o->bfd);
  193. // close inotify
  194. ASSERT_FORCE(close(o->inotify_fd) == 0)
  195. // free instance
  196. free(o);
  197. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  198. }
  199. static int func_getvar (void *vo, const char *name, NCDValue *out)
  200. {
  201. struct instance *o = vo;
  202. ASSERT(o->processing)
  203. assert_event(o);
  204. ASSERT(check_event(o))
  205. struct inotify_event *event = &o->events[o->events_index];
  206. if (!strcmp(name, "event_type")) {
  207. const char *str;
  208. if ((event->mask & (IN_CREATE | IN_MOVED_TO))) {
  209. str = "added";
  210. }
  211. else if ((event->mask & (IN_DELETE | IN_MOVED_FROM))) {
  212. str = "removed";
  213. }
  214. else if ((event->mask & IN_MODIFY)) {
  215. str = "changed";
  216. } else {
  217. ASSERT(0);
  218. }
  219. if (!NCDValue_InitString(out, str)) {
  220. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. if (!strcmp(name, "filename")) {
  226. if (!NCDValue_InitString(out, event->name)) {
  227. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. if (!strcmp(name, "filepath")) {
  233. char *str = concat_strings(3, o->dir, "/", event->name);
  234. if (!str) {
  235. ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
  236. return 0;
  237. }
  238. if (!NCDValue_InitString(out, str)) {
  239. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  240. free(str);
  241. return 0;
  242. }
  243. free(str);
  244. return 1;
  245. }
  246. return 0;
  247. }
  248. static void nextevent_func_new (NCDModuleInst *i)
  249. {
  250. // allocate instance
  251. struct nextevent_instance *o = malloc(sizeof(*o));
  252. if (!o) {
  253. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  254. goto fail0;
  255. }
  256. NCDModuleInst_Backend_SetUser(i, o);
  257. // init arguments
  258. o->i = i;
  259. // check arguments
  260. if (!NCDValue_ListRead(o->i->args, 0)) {
  261. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  262. goto fail1;
  263. }
  264. // get method object
  265. struct instance *mo = i->method_object->inst_user;
  266. ASSERT(mo->processing)
  267. // signal up.
  268. // Do it before finishing the event so our process does not advance any further if
  269. // we would be killed the event provider going down.
  270. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  271. // wait for next event
  272. inotify_nextevent(mo);
  273. return;
  274. fail1:
  275. free(o);
  276. fail0:
  277. NCDModuleInst_Backend_SetError(i);
  278. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  279. }
  280. static void nextevent_func_die (void *vo)
  281. {
  282. struct nextevent_instance *o = vo;
  283. NCDModuleInst *i = o->i;
  284. // free instance
  285. free(o);
  286. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  287. }
  288. static const struct NCDModule modules[] = {
  289. {
  290. .type = "sys.watch_directory",
  291. .func_new = func_new,
  292. .func_die = func_die,
  293. .func_getvar = func_getvar
  294. }, {
  295. .type = "sys.watch_directory::nextevent",
  296. .func_new = nextevent_func_new,
  297. .func_die = nextevent_func_die
  298. }, {
  299. .type = NULL
  300. }
  301. };
  302. const struct NCDModuleGroup ncdmodule_sys_watch_directory = {
  303. .modules = modules
  304. };