2
0

filemonitor-inotify.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 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. struct inotify_event *ev =
  70. (struct inotify_event *)(buf + used);
  71. const char *name = ev->len ? ev->name : "";
  72. QFileMonitorDir *dir = g_hash_table_lookup(mon->idmap,
  73. GINT_TO_POINTER(ev->wd));
  74. uint32_t iev = ev->mask &
  75. (IN_CREATE | IN_MODIFY | IN_DELETE | IN_IGNORED |
  76. IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);
  77. int qev;
  78. gsize i;
  79. used += sizeof(struct inotify_event) + ev->len;
  80. if (!dir) {
  81. continue;
  82. }
  83. /*
  84. * During a rename operation, the old name gets
  85. * IN_MOVED_FROM and the new name gets IN_MOVED_TO.
  86. * To simplify life for callers, we turn these into
  87. * DELETED and CREATED events
  88. */
  89. switch (iev) {
  90. case IN_CREATE:
  91. case IN_MOVED_TO:
  92. qev = QFILE_MONITOR_EVENT_CREATED;
  93. break;
  94. case IN_MODIFY:
  95. qev = QFILE_MONITOR_EVENT_MODIFIED;
  96. break;
  97. case IN_DELETE:
  98. case IN_MOVED_FROM:
  99. qev = QFILE_MONITOR_EVENT_DELETED;
  100. break;
  101. case IN_ATTRIB:
  102. qev = QFILE_MONITOR_EVENT_ATTRIBUTES;
  103. break;
  104. case IN_IGNORED:
  105. qev = QFILE_MONITOR_EVENT_IGNORED;
  106. break;
  107. default:
  108. g_assert_not_reached();
  109. }
  110. trace_qemu_file_monitor_event(mon, dir->path, name, ev->mask,
  111. dir->inotify_id);
  112. for (i = 0; i < dir->watches->len; i++) {
  113. QFileMonitorWatch *watch = &g_array_index(dir->watches,
  114. QFileMonitorWatch,
  115. i);
  116. if (watch->filename == NULL ||
  117. (name && g_str_equal(watch->filename, name))) {
  118. trace_qemu_file_monitor_dispatch(mon, dir->path, name,
  119. qev, watch->cb,
  120. watch->opaque, watch->id);
  121. watch->cb(watch->id, qev, name, watch->opaque);
  122. }
  123. }
  124. }
  125. cleanup:
  126. qemu_mutex_unlock(&mon->lock);
  127. }
  128. static void
  129. qemu_file_monitor_dir_free(void *data)
  130. {
  131. QFileMonitorDir *dir = data;
  132. gsize i;
  133. for (i = 0; i < dir->watches->len; i++) {
  134. QFileMonitorWatch *watch = &g_array_index(dir->watches,
  135. QFileMonitorWatch, i);
  136. g_free(watch->filename);
  137. }
  138. g_array_unref(dir->watches);
  139. g_free(dir->path);
  140. g_free(dir);
  141. }
  142. QFileMonitor *
  143. qemu_file_monitor_new(Error **errp)
  144. {
  145. int fd;
  146. QFileMonitor *mon;
  147. fd = inotify_init1(IN_NONBLOCK);
  148. if (fd < 0) {
  149. error_setg_errno(errp, errno,
  150. "Unable to initialize inotify");
  151. return NULL;
  152. }
  153. mon = g_new0(QFileMonitor, 1);
  154. qemu_mutex_init(&mon->lock);
  155. mon->fd = fd;
  156. mon->dirs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
  157. qemu_file_monitor_dir_free);
  158. mon->idmap = g_hash_table_new(g_direct_hash, g_direct_equal);
  159. trace_qemu_file_monitor_new(mon, mon->fd);
  160. return mon;
  161. }
  162. static gboolean
  163. qemu_file_monitor_free_idle(void *opaque)
  164. {
  165. QFileMonitor *mon = opaque;
  166. if (!mon) {
  167. return G_SOURCE_REMOVE;
  168. }
  169. qemu_mutex_lock(&mon->lock);
  170. g_hash_table_unref(mon->idmap);
  171. g_hash_table_unref(mon->dirs);
  172. qemu_mutex_unlock(&mon->lock);
  173. qemu_mutex_destroy(&mon->lock);
  174. g_free(mon);
  175. return G_SOURCE_REMOVE;
  176. }
  177. void
  178. qemu_file_monitor_free(QFileMonitor *mon)
  179. {
  180. if (!mon) {
  181. return;
  182. }
  183. qemu_mutex_lock(&mon->lock);
  184. if (mon->fd != -1) {
  185. qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
  186. close(mon->fd);
  187. mon->fd = -1;
  188. }
  189. qemu_mutex_unlock(&mon->lock);
  190. /*
  191. * Can't free it yet, because another thread
  192. * may be running event loop, so the inotify
  193. * callback might be pending. Using an idle
  194. * source ensures we'll only free after the
  195. * pending callback is done
  196. */
  197. g_idle_add((GSourceFunc)qemu_file_monitor_free_idle, mon);
  198. }
  199. int64_t
  200. qemu_file_monitor_add_watch(QFileMonitor *mon,
  201. const char *dirpath,
  202. const char *filename,
  203. QFileMonitorHandler cb,
  204. void *opaque,
  205. Error **errp)
  206. {
  207. QFileMonitorDir *dir;
  208. QFileMonitorWatch watch;
  209. int64_t ret = -1;
  210. qemu_mutex_lock(&mon->lock);
  211. dir = g_hash_table_lookup(mon->dirs, dirpath);
  212. if (!dir) {
  213. int rv = inotify_add_watch(mon->fd, dirpath,
  214. IN_CREATE | IN_DELETE | IN_MODIFY |
  215. IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);
  216. if (rv < 0) {
  217. error_setg_errno(errp, errno, "Unable to watch '%s'", dirpath);
  218. goto cleanup;
  219. }
  220. trace_qemu_file_monitor_enable_watch(mon, dirpath, rv);
  221. dir = g_new0(QFileMonitorDir, 1);
  222. dir->path = g_strdup(dirpath);
  223. dir->inotify_id = rv;
  224. dir->watches = g_array_new(FALSE, TRUE, sizeof(QFileMonitorWatch));
  225. g_hash_table_insert(mon->dirs, dir->path, dir);
  226. g_hash_table_insert(mon->idmap, GINT_TO_POINTER(rv), dir);
  227. if (g_hash_table_size(mon->dirs) == 1) {
  228. qemu_set_fd_handler(mon->fd, qemu_file_monitor_watch, NULL, mon);
  229. }
  230. }
  231. watch.id = (((int64_t)dir->inotify_id) << 32) | dir->next_file_id++;
  232. watch.filename = g_strdup(filename);
  233. watch.cb = cb;
  234. watch.opaque = opaque;
  235. g_array_append_val(dir->watches, watch);
  236. trace_qemu_file_monitor_add_watch(mon, dirpath,
  237. filename ? filename : "<none>",
  238. cb, opaque, watch.id);
  239. ret = watch.id;
  240. cleanup:
  241. qemu_mutex_unlock(&mon->lock);
  242. return ret;
  243. }
  244. void qemu_file_monitor_remove_watch(QFileMonitor *mon,
  245. const char *dirpath,
  246. int64_t id)
  247. {
  248. QFileMonitorDir *dir;
  249. gsize i;
  250. qemu_mutex_lock(&mon->lock);
  251. trace_qemu_file_monitor_remove_watch(mon, dirpath, id);
  252. dir = g_hash_table_lookup(mon->dirs, dirpath);
  253. if (!dir) {
  254. goto cleanup;
  255. }
  256. for (i = 0; i < dir->watches->len; i++) {
  257. QFileMonitorWatch *watch = &g_array_index(dir->watches,
  258. QFileMonitorWatch, i);
  259. if (watch->id == id) {
  260. g_free(watch->filename);
  261. g_array_remove_index(dir->watches, i);
  262. break;
  263. }
  264. }
  265. if (dir->watches->len == 0) {
  266. inotify_rm_watch(mon->fd, dir->inotify_id);
  267. trace_qemu_file_monitor_disable_watch(mon, dir->path, dir->inotify_id);
  268. g_hash_table_remove(mon->idmap, GINT_TO_POINTER(dir->inotify_id));
  269. g_hash_table_remove(mon->dirs, dir->path);
  270. if (g_hash_table_size(mon->dirs) == 0) {
  271. qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
  272. }
  273. }
  274. cleanup:
  275. qemu_mutex_unlock(&mon->lock);
  276. }