filemonitor-inotify.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * QEMU file monitor Linux inotify impl
  3. *
  4. * Copyright (c) 2018 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/filemonitor.h"
  22. #include "qemu/main-loop.h"
  23. #include "qemu/error-report.h"
  24. #include "qapi/error.h"
  25. #include "trace.h"
  26. #include <sys/inotify.h>
  27. struct QFileMonitor {
  28. int fd;
  29. QemuMutex lock; /* protects dirs & idmap */
  30. GHashTable *dirs; /* dirname => QFileMonitorDir */
  31. GHashTable *idmap; /* inotify ID => dirname */
  32. };
  33. typedef struct {
  34. int64_t id; /* watch ID */
  35. char *filename; /* optional filter */
  36. QFileMonitorHandler cb;
  37. void *opaque;
  38. } QFileMonitorWatch;
  39. typedef struct {
  40. char *path;
  41. int inotify_id; /* inotify ID */
  42. int next_file_id; /* file ID counter */
  43. GArray *watches; /* QFileMonitorWatch elements */
  44. } QFileMonitorDir;
  45. static void qemu_file_monitor_watch(void *arg)
  46. {
  47. QFileMonitor *mon = arg;
  48. char buf[4096]
  49. __attribute__ ((aligned(__alignof__(struct inotify_event))));
  50. int used = 0;
  51. int len;
  52. qemu_mutex_lock(&mon->lock);
  53. if (mon->fd == -1) {
  54. qemu_mutex_unlock(&mon->lock);
  55. return;
  56. }
  57. len = read(mon->fd, buf, sizeof(buf));
  58. if (len < 0) {
  59. if (errno != EAGAIN) {
  60. error_report("Failure monitoring inotify FD '%s',"
  61. "disabling events", strerror(errno));
  62. goto cleanup;
  63. }
  64. /* no more events right now */
  65. goto cleanup;
  66. }
  67. /* Loop over all events in the buffer */
  68. while (used < len) {
  69. const char *name;
  70. QFileMonitorDir *dir;
  71. uint32_t iev;
  72. int qev;
  73. gsize i;
  74. struct inotify_event *ev = (struct inotify_event *)(buf + used);
  75. /*
  76. * We trust the kernel to provide valid buffer with complete event
  77. * records.
  78. */
  79. assert(len - used >= sizeof(struct inotify_event));
  80. assert(len - used - sizeof(struct inotify_event) >= ev->len);
  81. name = ev->len ? ev->name : "";
  82. dir = g_hash_table_lookup(mon->idmap, GINT_TO_POINTER(ev->wd));
  83. iev = ev->mask &
  84. (IN_CREATE | IN_MODIFY | IN_DELETE | IN_IGNORED |
  85. IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);
  86. used += sizeof(struct inotify_event) + ev->len;
  87. if (!dir) {
  88. continue;
  89. }
  90. /*
  91. * During a rename operation, the old name gets
  92. * IN_MOVED_FROM and the new name gets IN_MOVED_TO.
  93. * To simplify life for callers, we turn these into
  94. * DELETED and CREATED events
  95. */
  96. switch (iev) {
  97. case IN_CREATE:
  98. case IN_MOVED_TO:
  99. qev = QFILE_MONITOR_EVENT_CREATED;
  100. break;
  101. case IN_MODIFY:
  102. qev = QFILE_MONITOR_EVENT_MODIFIED;
  103. break;
  104. case IN_DELETE:
  105. case IN_MOVED_FROM:
  106. qev = QFILE_MONITOR_EVENT_DELETED;
  107. break;
  108. case IN_ATTRIB:
  109. qev = QFILE_MONITOR_EVENT_ATTRIBUTES;
  110. break;
  111. case IN_IGNORED:
  112. qev = QFILE_MONITOR_EVENT_IGNORED;
  113. break;
  114. default:
  115. g_assert_not_reached();
  116. }
  117. trace_qemu_file_monitor_event(mon, dir->path, name, ev->mask,
  118. dir->inotify_id);
  119. for (i = 0; i < dir->watches->len; i++) {
  120. QFileMonitorWatch *watch = &g_array_index(dir->watches,
  121. QFileMonitorWatch,
  122. i);
  123. if (watch->filename == NULL ||
  124. (name && g_str_equal(watch->filename, name))) {
  125. trace_qemu_file_monitor_dispatch(mon, dir->path, name,
  126. qev, watch->cb,
  127. watch->opaque, watch->id);
  128. watch->cb(watch->id, qev, name, watch->opaque);
  129. }
  130. }
  131. }
  132. cleanup:
  133. qemu_mutex_unlock(&mon->lock);
  134. }
  135. static void
  136. qemu_file_monitor_dir_free(void *data)
  137. {
  138. QFileMonitorDir *dir = data;
  139. gsize i;
  140. for (i = 0; i < dir->watches->len; i++) {
  141. QFileMonitorWatch *watch = &g_array_index(dir->watches,
  142. QFileMonitorWatch, i);
  143. g_free(watch->filename);
  144. }
  145. g_array_unref(dir->watches);
  146. g_free(dir->path);
  147. g_free(dir);
  148. }
  149. QFileMonitor *
  150. qemu_file_monitor_new(Error **errp)
  151. {
  152. int fd;
  153. QFileMonitor *mon;
  154. fd = inotify_init1(IN_NONBLOCK);
  155. if (fd < 0) {
  156. error_setg_errno(errp, errno,
  157. "Unable to initialize inotify");
  158. return NULL;
  159. }
  160. mon = g_new0(QFileMonitor, 1);
  161. qemu_mutex_init(&mon->lock);
  162. mon->fd = fd;
  163. mon->dirs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
  164. qemu_file_monitor_dir_free);
  165. mon->idmap = g_hash_table_new(g_direct_hash, g_direct_equal);
  166. trace_qemu_file_monitor_new(mon, mon->fd);
  167. return mon;
  168. }
  169. static gboolean
  170. qemu_file_monitor_free_idle(void *opaque)
  171. {
  172. QFileMonitor *mon = opaque;
  173. if (!mon) {
  174. return G_SOURCE_REMOVE;
  175. }
  176. qemu_mutex_lock(&mon->lock);
  177. g_hash_table_unref(mon->idmap);
  178. g_hash_table_unref(mon->dirs);
  179. qemu_mutex_unlock(&mon->lock);
  180. qemu_mutex_destroy(&mon->lock);
  181. g_free(mon);
  182. return G_SOURCE_REMOVE;
  183. }
  184. void
  185. qemu_file_monitor_free(QFileMonitor *mon)
  186. {
  187. if (!mon) {
  188. return;
  189. }
  190. qemu_mutex_lock(&mon->lock);
  191. if (mon->fd != -1) {
  192. qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
  193. close(mon->fd);
  194. mon->fd = -1;
  195. }
  196. qemu_mutex_unlock(&mon->lock);
  197. /*
  198. * Can't free it yet, because another thread
  199. * may be running event loop, so the inotify
  200. * callback might be pending. Using an idle
  201. * source ensures we'll only free after the
  202. * pending callback is done
  203. */
  204. g_idle_add((GSourceFunc)qemu_file_monitor_free_idle, mon);
  205. }
  206. int64_t
  207. qemu_file_monitor_add_watch(QFileMonitor *mon,
  208. const char *dirpath,
  209. const char *filename,
  210. QFileMonitorHandler cb,
  211. void *opaque,
  212. Error **errp)
  213. {
  214. QFileMonitorDir *dir;
  215. QFileMonitorWatch watch;
  216. int64_t ret = -1;
  217. qemu_mutex_lock(&mon->lock);
  218. dir = g_hash_table_lookup(mon->dirs, dirpath);
  219. if (!dir) {
  220. int rv = inotify_add_watch(mon->fd, dirpath,
  221. IN_CREATE | IN_DELETE | IN_MODIFY |
  222. IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);
  223. if (rv < 0) {
  224. error_setg_errno(errp, errno, "Unable to watch '%s'", dirpath);
  225. goto cleanup;
  226. }
  227. trace_qemu_file_monitor_enable_watch(mon, dirpath, rv);
  228. dir = g_new0(QFileMonitorDir, 1);
  229. dir->path = g_strdup(dirpath);
  230. dir->inotify_id = rv;
  231. dir->watches = g_array_new(FALSE, TRUE, sizeof(QFileMonitorWatch));
  232. g_hash_table_insert(mon->dirs, dir->path, dir);
  233. g_hash_table_insert(mon->idmap, GINT_TO_POINTER(rv), dir);
  234. if (g_hash_table_size(mon->dirs) == 1) {
  235. qemu_set_fd_handler(mon->fd, qemu_file_monitor_watch, NULL, mon);
  236. }
  237. }
  238. watch.id = (((int64_t)dir->inotify_id) << 32) | dir->next_file_id++;
  239. watch.filename = g_strdup(filename);
  240. watch.cb = cb;
  241. watch.opaque = opaque;
  242. g_array_append_val(dir->watches, watch);
  243. trace_qemu_file_monitor_add_watch(mon, dirpath,
  244. filename ? filename : "<none>",
  245. cb, opaque, watch.id);
  246. ret = watch.id;
  247. cleanup:
  248. qemu_mutex_unlock(&mon->lock);
  249. return ret;
  250. }
  251. void qemu_file_monitor_remove_watch(QFileMonitor *mon,
  252. const char *dirpath,
  253. int64_t id)
  254. {
  255. QFileMonitorDir *dir;
  256. gsize i;
  257. qemu_mutex_lock(&mon->lock);
  258. trace_qemu_file_monitor_remove_watch(mon, dirpath, id);
  259. dir = g_hash_table_lookup(mon->dirs, dirpath);
  260. if (!dir) {
  261. goto cleanup;
  262. }
  263. for (i = 0; i < dir->watches->len; i++) {
  264. QFileMonitorWatch *watch = &g_array_index(dir->watches,
  265. QFileMonitorWatch, i);
  266. if (watch->id == id) {
  267. g_free(watch->filename);
  268. g_array_remove_index(dir->watches, i);
  269. break;
  270. }
  271. }
  272. if (dir->watches->len == 0) {
  273. inotify_rm_watch(mon->fd, dir->inotify_id);
  274. trace_qemu_file_monitor_disable_watch(mon, dir->path, dir->inotify_id);
  275. g_hash_table_remove(mon->idmap, GINT_TO_POINTER(dir->inotify_id));
  276. g_hash_table_remove(mon->dirs, dir->path);
  277. if (g_hash_table_size(mon->dirs) == 0) {
  278. qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
  279. }
  280. }
  281. cleanup:
  282. qemu_mutex_unlock(&mon->lock);
  283. }