aboutsummaryrefslogtreecommitdiff
path: root/util/filemonitor-inotify.c
blob: b5f4b93f3ff0639b398ef2ad380ba5b423cc18cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * QEMU file monitor Linux inotify impl
 *
 * Copyright (c) 2018 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "qemu/osdep.h"
#include "qemu/filemonitor.h"
#include "qemu/main-loop.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "trace.h"

#include <sys/inotify.h>

struct QFileMonitor {
    int fd;
    QemuMutex lock; /* protects dirs & idmap */
    GHashTable *dirs; /* dirname => QFileMonitorDir */
    GHashTable *idmap; /* inotify ID => dirname */
};


typedef struct {
    int64_t id; /* watch ID */
    char *filename; /* optional filter */
    QFileMonitorHandler cb;
    void *opaque;
} QFileMonitorWatch;


typedef struct {
    char *path;
    int inotify_id; /* inotify ID */
    int next_file_id; /* file ID counter */
    GArray *watches; /* QFileMonitorWatch elements */
} QFileMonitorDir;


static void qemu_file_monitor_watch(void *arg)
{
    QFileMonitor *mon = arg;
    char buf[4096]
        __attribute__ ((aligned(__alignof__(struct inotify_event))));
    int used = 0;
    int len;

    qemu_mutex_lock(&mon->lock);

    if (mon->fd == -1) {
        qemu_mutex_unlock(&mon->lock);
        return;
    }

    len = read(mon->fd, buf, sizeof(buf));

    if (len < 0) {
        if (errno != EAGAIN) {
            error_report("Failure monitoring inotify FD '%s',"
                         "disabling events", strerror(errno));
            goto cleanup;
        }

        /* no more events right now */
        goto cleanup;
    }

    /* Loop over all events in the buffer */
    while (used < len) {
        struct inotify_event *ev =
            (struct inotify_event *)(buf + used);
        const char *name = ev->len ? ev->name : "";
        QFileMonitorDir *dir = g_hash_table_lookup(mon->idmap,
                                                   GINT_TO_POINTER(ev->wd));
        uint32_t iev = ev->mask &
            (IN_CREATE | IN_MODIFY | IN_DELETE | IN_IGNORED |
             IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);
        int qev;
        gsize i;

        used += sizeof(struct inotify_event) + ev->len;

        if (!dir) {
            continue;
        }

        /*
         * During a rename operation, the old name gets
         * IN_MOVED_FROM and the new name gets IN_MOVED_TO.
         * To simplify life for callers, we turn these into
         * DELETED and CREATED events
         */
        switch (iev) {
        case IN_CREATE:
        case IN_MOVED_TO:
            qev = QFILE_MONITOR_EVENT_CREATED;
            break;
        case IN_MODIFY:
            qev = QFILE_MONITOR_EVENT_MODIFIED;
            break;
        case IN_DELETE:
        case IN_MOVED_FROM:
            qev = QFILE_MONITOR_EVENT_DELETED;
            break;
        case IN_ATTRIB:
            qev = QFILE_MONITOR_EVENT_ATTRIBUTES;
            break;
        case IN_IGNORED:
            qev = QFILE_MONITOR_EVENT_IGNORED;
            break;
        default:
            g_assert_not_reached();
        }

        trace_qemu_file_monitor_event(mon, dir->path, name, ev->mask,
                                      dir->inotify_id);
        for (i = 0; i < dir->watches->len; i++) {
            QFileMonitorWatch *watch = &g_array_index(dir->watches,
                                                      QFileMonitorWatch,
                                                      i);

            if (watch->filename == NULL ||
                (name && g_str_equal(watch->filename, name))) {
                trace_qemu_file_monitor_dispatch(mon, dir->path, name,
                                                 qev, watch->cb,
                                                 watch->opaque, watch->id);
                watch->cb(watch->id, qev, name, watch->opaque);
            }
        }
    }

 cleanup:
    qemu_mutex_unlock(&mon->lock);
}


static void
qemu_file_monitor_dir_free(void *data)
{
    QFileMonitorDir *dir = data;
    gsize i;

    for (i = 0; i < dir->watches->len; i++) {
        QFileMonitorWatch *watch = &g_array_index(dir->watches,
                                                  QFileMonitorWatch, i);
        g_free(watch->filename);
    }
    g_array_unref(dir->watches);
    g_free(dir->path);
    g_free(dir);
}


QFileMonitor *
qemu_file_monitor_new(Error **errp)
{
    int fd;
    QFileMonitor *mon;

    fd = inotify_init1(IN_NONBLOCK);
    if (fd < 0) {
        error_setg_errno(errp, errno,
                         "Unable to initialize inotify");
        return NULL;
    }

    mon = g_new0(QFileMonitor, 1);
    qemu_mutex_init(&mon->lock);
    mon->fd = fd;

    mon->dirs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
                                      qemu_file_monitor_dir_free);
    mon->idmap = g_hash_table_new(g_direct_hash, g_direct_equal);

    trace_qemu_file_monitor_new(mon, mon->fd);

    return mon;
}

static gboolean
qemu_file_monitor_free_idle(void *opaque)
{
    QFileMonitor *mon = opaque;

    if (!mon) {
        return G_SOURCE_REMOVE;
    }

    qemu_mutex_lock(&mon->lock);

    g_hash_table_unref(mon->idmap);
    g_hash_table_unref(mon->dirs);

    qemu_mutex_unlock(&mon->lock);

    qemu_mutex_destroy(&mon->lock);
    g_free(mon);

    return G_SOURCE_REMOVE;
}

void
qemu_file_monitor_free(QFileMonitor *mon)
{
    if (!mon) {
        return;
    }

    qemu_mutex_lock(&mon->lock);
    if (mon->fd != -1) {
        qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
        close(mon->fd);
        mon->fd = -1;
    }
    qemu_mutex_unlock(&mon->lock);

    /*
     * Can't free it yet, because another thread
     * may be running event loop, so the inotify
     * callback might be pending. Using an idle
     * source ensures we'll only free after the
     * pending callback is done
     */
    g_idle_add((GSourceFunc)qemu_file_monitor_free_idle, mon);
}

int64_t
qemu_file_monitor_add_watch(QFileMonitor *mon,
                            const char *dirpath,
                            const char *filename,
                            QFileMonitorHandler cb,
                            void *opaque,
                            Error **errp)
{
    QFileMonitorDir *dir;
    QFileMonitorWatch watch;
    int64_t ret = -1;

    qemu_mutex_lock(&mon->lock);
    dir = g_hash_table_lookup(mon->dirs, dirpath);
    if (!dir) {
        int rv = inotify_add_watch(mon->fd, dirpath,
                                   IN_CREATE | IN_DELETE | IN_MODIFY |
                                   IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);

        if (rv < 0) {
            error_setg_errno(errp, errno, "Unable to watch '%s'", dirpath);
            goto cleanup;
        }

        trace_qemu_file_monitor_enable_watch(mon, dirpath, rv);

        dir = g_new0(QFileMonitorDir, 1);
        dir->path = g_strdup(dirpath);
        dir->inotify_id = rv;
        dir->watches = g_array_new(FALSE, TRUE, sizeof(QFileMonitorWatch));

        g_hash_table_insert(mon->dirs, dir->path, dir);
        g_hash_table_insert(mon->idmap, GINT_TO_POINTER(rv), dir);

        if (g_hash_table_size(mon->dirs) == 1) {
            qemu_set_fd_handler(mon->fd, qemu_file_monitor_watch, NULL, mon);
        }
    }

    watch.id = (((int64_t)dir->inotify_id) << 32) | dir->next_file_id++;
    watch.filename = g_strdup(filename);
    watch.cb = cb;
    watch.opaque = opaque;

    g_array_append_val(dir->watches, watch);

    trace_qemu_file_monitor_add_watch(mon, dirpath,
                                      filename ? filename : "<none>",
                                      cb, opaque, watch.id);

    ret = watch.id;

 cleanup:
    qemu_mutex_unlock(&mon->lock);
    return ret;
}


void qemu_file_monitor_remove_watch(QFileMonitor *mon,
                                    const char *dirpath,
                                    int64_t id)
{
    QFileMonitorDir *dir;
    gsize i;

    qemu_mutex_lock(&mon->lock);

    trace_qemu_file_monitor_remove_watch(mon, dirpath, id);

    dir = g_hash_table_lookup(mon->dirs, dirpath);
    if (!dir) {
        goto cleanup;
    }

    for (i = 0; i < dir->watches->len; i++) {
        QFileMonitorWatch *watch = &g_array_index(dir->watches,
                                                  QFileMonitorWatch, i);
        if (watch->id == id) {
            g_free(watch->filename);
            g_array_remove_index(dir->watches, i);
            break;
        }
    }

    if (dir->watches->len == 0) {
        inotify_rm_watch(mon->fd, dir->inotify_id);
        trace_qemu_file_monitor_disable_watch(mon, dir->path, dir->inotify_id);

        g_hash_table_remove(mon->idmap, GINT_TO_POINTER(dir->inotify_id));
        g_hash_table_remove(mon->dirs, dir->path);

        if (g_hash_table_size(mon->dirs) == 0) {
            qemu_set_fd_handler(mon->fd, NULL, NULL, NULL);
        }
    }

 cleanup:
    qemu_mutex_unlock(&mon->lock);
}