sys_watch_directory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. * The directory is first scanned and "added" events are reported for all files.
  30. * Variables:
  31. * string event_type - what happened with the file: "added", "removed" or "changed"
  32. * string filename - name of the file in the directory the event refers to
  33. * string filepath - "dir/filename"
  34. *
  35. * Synopsis: sys.watch_directory::nextevent()
  36. * Description: makes the watch_directory module transition down in order to report the next event.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <sys/inotify.h>
  42. #include <sys/types.h>
  43. #include <dirent.h>
  44. #include <errno.h>
  45. #include <misc/nonblocking.h>
  46. #include <misc/concat_strings.h>
  47. #include <ncd/NCDModule.h>
  48. #include <generated/blog_channel_ncd_sys_watch_directory.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. #define MAX_INOTIFY_EVENTS 128
  51. struct instance {
  52. NCDModuleInst *i;
  53. char *dir;
  54. DIR *dir_handle;
  55. int inotify_fd;
  56. BFileDescriptor bfd;
  57. struct inotify_event events[MAX_INOTIFY_EVENTS];
  58. int events_count;
  59. int events_index;
  60. int processing;
  61. const char *processing_file;
  62. const char *processing_type;
  63. };
  64. struct nextevent_instance {
  65. NCDModuleInst *i;
  66. };
  67. static void instance_free (struct instance *o, int is_error);
  68. static void next_dir_event (struct instance *o)
  69. {
  70. ASSERT(!o->processing)
  71. ASSERT(o->dir_handle)
  72. struct dirent *entry;
  73. do {
  74. // get next entry
  75. errno = 0;
  76. if (!(entry = readdir(o->dir_handle))) {
  77. if (errno != 0) {
  78. ModuleLog(o->i, BLOG_ERROR, "readdir failed");
  79. instance_free(o, 1);
  80. return;
  81. }
  82. // close directory
  83. if (closedir(o->dir_handle) < 0) {
  84. ModuleLog(o->i, BLOG_ERROR, "closedir failed");
  85. o->dir_handle = NULL;
  86. instance_free(o, 1);
  87. return;
  88. }
  89. // set no dir handle
  90. o->dir_handle = NULL;
  91. // start receiving inotify events
  92. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  93. return;
  94. }
  95. } while (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."));
  96. // set event
  97. o->processing_file = entry->d_name;
  98. o->processing_type = "added";
  99. o->processing = 1;
  100. // signal up
  101. NCDModuleInst_Backend_Up(o->i);
  102. }
  103. static void assert_inotify_event (struct instance *o)
  104. {
  105. ASSERT(o->events_index < o->events_count)
  106. ASSERT(o->events[o->events_index].len % sizeof(o->events[0]) == 0)
  107. ASSERT(o->events[o->events_index].len / sizeof(o->events[0]) <= o->events_count - (o->events_index + 1))
  108. }
  109. static const char * translate_inotify_event (struct instance *o)
  110. {
  111. assert_inotify_event(o);
  112. struct inotify_event *event = &o->events[o->events_index];
  113. if (strlen(event->name) > 0) {
  114. if ((event->mask & (IN_CREATE | IN_MOVED_TO))) {
  115. return "added";
  116. }
  117. if ((event->mask & (IN_DELETE | IN_MOVED_FROM))) {
  118. return "removed";
  119. }
  120. if ((event->mask & IN_MODIFY)) {
  121. return "changed";
  122. }
  123. }
  124. return NULL;
  125. }
  126. static void skip_inotify_event (struct instance *o)
  127. {
  128. assert_inotify_event(o);
  129. o->events_index += 1 + o->events[o->events_index].len / sizeof(o->events[0]);
  130. }
  131. static void next_inotify_event (struct instance *o)
  132. {
  133. ASSERT(!o->processing)
  134. ASSERT(!o->dir_handle)
  135. // skip any bad events
  136. while (o->events_index < o->events_count && !translate_inotify_event(o)) {
  137. ModuleLog(o->i, BLOG_ERROR, "unknown inotify event");
  138. skip_inotify_event(o);
  139. }
  140. if (o->events_index == o->events_count) {
  141. // wait for more events
  142. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, BREACTOR_READ);
  143. return;
  144. }
  145. // set event
  146. o->processing_file = o->events[o->events_index].name;
  147. o->processing_type = translate_inotify_event(o);
  148. o->processing = 1;
  149. // consume this event
  150. skip_inotify_event(o);
  151. // signal up
  152. NCDModuleInst_Backend_Up(o->i);
  153. }
  154. static void inotify_fd_handler (struct instance *o, int events)
  155. {
  156. ASSERT(!o->processing)
  157. ASSERT(!o->dir_handle)
  158. int res = read(o->inotify_fd, o->events, sizeof(o->events));
  159. if (res < 0) {
  160. ModuleLog(o->i, BLOG_ERROR, "read failed");
  161. instance_free(o, 1);
  162. return;
  163. }
  164. // stop waiting for inotify events
  165. BReactor_SetFileDescriptorEvents(o->i->reactor, &o->bfd, 0);
  166. ASSERT(res <= sizeof(o->events))
  167. ASSERT(res % sizeof(o->events[0]) == 0)
  168. // setup buffer state
  169. o->events_count = res / sizeof(o->events[0]);
  170. o->events_index = 0;
  171. // process inotify events
  172. next_inotify_event(o);
  173. }
  174. static void next_event (struct instance *o)
  175. {
  176. ASSERT(o->processing)
  177. // set not processing
  178. o->processing = 0;
  179. // signal down
  180. NCDModuleInst_Backend_Down(o->i);
  181. if (o->dir_handle) {
  182. next_dir_event(o);
  183. return;
  184. } else {
  185. next_inotify_event(o);
  186. return;
  187. }
  188. }
  189. static void func_new (NCDModuleInst *i)
  190. {
  191. // allocate instance
  192. struct instance *o = malloc(sizeof(*o));
  193. if (!o) {
  194. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  195. goto fail0;
  196. }
  197. NCDModuleInst_Backend_SetUser(i, o);
  198. // init arguments
  199. o->i = i;
  200. // check arguments
  201. NCDValue *dir_arg;
  202. if (!NCDValue_ListRead(o->i->args, 1, &dir_arg)) {
  203. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  204. goto fail1;
  205. }
  206. if (NCDValue_Type(dir_arg) != NCDVALUE_STRING) {
  207. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  208. goto fail1;
  209. }
  210. o->dir = NCDValue_StringValue(dir_arg);
  211. // open inotify
  212. if ((o->inotify_fd = inotify_init()) < 0) {
  213. ModuleLog(o->i, BLOG_ERROR, "inotify_init failed");
  214. goto fail1;
  215. }
  216. // add watch
  217. if (inotify_add_watch(o->inotify_fd, o->dir, IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO) < 0) {
  218. ModuleLog(o->i, BLOG_ERROR, "inotify_add_watch failed");
  219. goto fail2;
  220. }
  221. // set non-blocking
  222. if (!badvpn_set_nonblocking(o->inotify_fd)) {
  223. ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
  224. goto fail2;
  225. }
  226. // init BFileDescriptor
  227. BFileDescriptor_Init(&o->bfd, o->inotify_fd, (BFileDescriptor_handler)inotify_fd_handler, o);
  228. if (!BReactor_AddFileDescriptor(o->i->reactor, &o->bfd)) {
  229. ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  230. goto fail2;
  231. }
  232. // open directory
  233. if (!(o->dir_handle = opendir(o->dir))) {
  234. ModuleLog(o->i, BLOG_ERROR, "opendir failed");
  235. goto fail3;
  236. }
  237. // set not processing
  238. o->processing = 0;
  239. // read first directory entry
  240. next_dir_event(o);
  241. return;
  242. fail3:
  243. // free BFileDescriptor
  244. BReactor_RemoveFileDescriptor(o->i->reactor, &o->bfd);
  245. fail2:
  246. ASSERT_FORCE(close(o->inotify_fd) == 0)
  247. fail1:
  248. free(o);
  249. fail0:
  250. NCDModuleInst_Backend_SetError(i);
  251. NCDModuleInst_Backend_Dead(i);
  252. }
  253. void instance_free (struct instance *o, int is_error)
  254. {
  255. NCDModuleInst *i = o->i;
  256. // close directory
  257. if (o->dir_handle) {
  258. if (closedir(o->dir_handle) < 0) {
  259. ModuleLog(o->i, BLOG_ERROR, "closedir failed");
  260. }
  261. }
  262. // free BFileDescriptor
  263. BReactor_RemoveFileDescriptor(o->i->reactor, &o->bfd);
  264. // close inotify
  265. ASSERT_FORCE(close(o->inotify_fd) == 0)
  266. // free instance
  267. free(o);
  268. if (is_error) {
  269. NCDModuleInst_Backend_SetError(i);
  270. }
  271. NCDModuleInst_Backend_Dead(i);
  272. }
  273. static void func_die (void *vo)
  274. {
  275. struct instance *o = vo;
  276. instance_free(o, 0);
  277. }
  278. static int func_getvar (void *vo, const char *name, NCDValue *out)
  279. {
  280. struct instance *o = vo;
  281. ASSERT(o->processing)
  282. if (!strcmp(name, "event_type")) {
  283. if (!NCDValue_InitString(out, o->processing_type)) {
  284. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  285. return 0;
  286. }
  287. return 1;
  288. }
  289. if (!strcmp(name, "filename")) {
  290. if (!NCDValue_InitString(out, o->processing_file)) {
  291. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. if (!strcmp(name, "filepath")) {
  297. char *str = concat_strings(3, o->dir, "/", o->processing_file);
  298. if (!str) {
  299. ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
  300. return 0;
  301. }
  302. if (!NCDValue_InitString(out, str)) {
  303. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  304. free(str);
  305. return 0;
  306. }
  307. free(str);
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. static void nextevent_func_new (NCDModuleInst *i)
  313. {
  314. // allocate instance
  315. struct nextevent_instance *o = malloc(sizeof(*o));
  316. if (!o) {
  317. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  318. goto fail0;
  319. }
  320. NCDModuleInst_Backend_SetUser(i, o);
  321. // init arguments
  322. o->i = i;
  323. // check arguments
  324. if (!NCDValue_ListRead(o->i->args, 0)) {
  325. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  326. goto fail1;
  327. }
  328. // get method object
  329. struct instance *mo = i->method_object->inst_user;
  330. ASSERT(mo->processing)
  331. // signal up.
  332. // Do it before finishing the event so our process does not advance any further if
  333. // we would be killed the event provider going down.
  334. NCDModuleInst_Backend_Up(o->i);
  335. // wait for next event
  336. next_event(mo);
  337. return;
  338. fail1:
  339. free(o);
  340. fail0:
  341. NCDModuleInst_Backend_SetError(i);
  342. NCDModuleInst_Backend_Dead(i);
  343. }
  344. static void nextevent_func_die (void *vo)
  345. {
  346. struct nextevent_instance *o = vo;
  347. NCDModuleInst *i = o->i;
  348. // free instance
  349. free(o);
  350. NCDModuleInst_Backend_Dead(i);
  351. }
  352. static const struct NCDModule modules[] = {
  353. {
  354. .type = "sys.watch_directory",
  355. .func_new = func_new,
  356. .func_die = func_die,
  357. .func_getvar = func_getvar
  358. }, {
  359. .type = "sys.watch_directory::nextevent",
  360. .func_new = nextevent_func_new,
  361. .func_die = nextevent_func_die
  362. }, {
  363. .type = NULL
  364. }
  365. };
  366. const struct NCDModuleGroup ncdmodule_sys_watch_directory = {
  367. .modules = modules
  368. };