Path: blob/master/Utilities/cmlibuv/src/unix/linux-inotify.c
3156 views
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.1* Permission is hereby granted, free of charge, to any person obtaining a copy2* of this software and associated documentation files (the "Software"), to3* deal in the Software without restriction, including without limitation the4* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or5* sell copies of the Software, and to permit persons to whom the Software is6* furnished to do so, subject to the following conditions:7*8* The above copyright notice and this permission notice shall be included in9* all copies or substantial portions of the Software.10*11* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING16* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS17* IN THE SOFTWARE.18*/1920#include "uv.h"21#include "uv/tree.h"22#include "internal.h"2324#include <stdint.h>25#include <stdio.h>26#include <stdlib.h>27#include <string.h>28#include <assert.h>29#include <errno.h>3031#include <sys/inotify.h>32#include <sys/types.h>33#include <unistd.h>3435struct watcher_list {36RB_ENTRY(watcher_list) entry;37QUEUE watchers;38int iterating;39char* path;40int wd;41};4243struct watcher_root {44struct watcher_list* rbh_root;45};46#define CAST(p) ((struct watcher_root*)(p))474849static int compare_watchers(const struct watcher_list* a,50const struct watcher_list* b) {51if (a->wd < b->wd) return -1;52if (a->wd > b->wd) return 1;53return 0;54}555657RB_GENERATE_STATIC(watcher_root, watcher_list, entry, compare_watchers)585960static void uv__inotify_read(uv_loop_t* loop,61uv__io_t* w,62unsigned int revents);6364static void maybe_free_watcher_list(struct watcher_list* w,65uv_loop_t* loop);6667static int init_inotify(uv_loop_t* loop) {68int fd;6970if (loop->inotify_fd != -1)71return 0;7273fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);74if (fd < 0)75return UV__ERR(errno);7677loop->inotify_fd = fd;78uv__io_init(&loop->inotify_read_watcher, uv__inotify_read, loop->inotify_fd);79uv__io_start(loop, &loop->inotify_read_watcher, POLLIN);8081return 0;82}838485int uv__inotify_fork(uv_loop_t* loop, void* old_watchers) {86/* Open the inotify_fd, and re-arm all the inotify watchers. */87int err;88struct watcher_list* tmp_watcher_list_iter;89struct watcher_list* watcher_list;90struct watcher_list tmp_watcher_list;91QUEUE queue;92QUEUE* q;93uv_fs_event_t* handle;94char* tmp_path;9596if (old_watchers != NULL) {97/* We must restore the old watcher list to be able to close items98* out of it.99*/100loop->inotify_watchers = old_watchers;101102QUEUE_INIT(&tmp_watcher_list.watchers);103/* Note that the queue we use is shared with the start and stop()104* functions, making QUEUE_FOREACH unsafe to use. So we use the105* QUEUE_MOVE trick to safely iterate. Also don't free the watcher106* list until we're done iterating. c.f. uv__inotify_read.107*/108RB_FOREACH_SAFE(watcher_list, watcher_root,109CAST(&old_watchers), tmp_watcher_list_iter) {110watcher_list->iterating = 1;111QUEUE_MOVE(&watcher_list->watchers, &queue);112while (!QUEUE_EMPTY(&queue)) {113q = QUEUE_HEAD(&queue);114handle = QUEUE_DATA(q, uv_fs_event_t, watchers);115/* It's critical to keep a copy of path here, because it116* will be set to NULL by stop() and then deallocated by117* maybe_free_watcher_list118*/119tmp_path = uv__strdup(handle->path);120assert(tmp_path != NULL);121QUEUE_REMOVE(q);122QUEUE_INSERT_TAIL(&watcher_list->watchers, q);123uv_fs_event_stop(handle);124125QUEUE_INSERT_TAIL(&tmp_watcher_list.watchers, &handle->watchers);126handle->path = tmp_path;127}128watcher_list->iterating = 0;129maybe_free_watcher_list(watcher_list, loop);130}131132QUEUE_MOVE(&tmp_watcher_list.watchers, &queue);133while (!QUEUE_EMPTY(&queue)) {134q = QUEUE_HEAD(&queue);135QUEUE_REMOVE(q);136handle = QUEUE_DATA(q, uv_fs_event_t, watchers);137tmp_path = handle->path;138handle->path = NULL;139err = uv_fs_event_start(handle, handle->cb, tmp_path, 0);140uv__free(tmp_path);141if (err)142return err;143}144}145146return 0;147}148149150static struct watcher_list* find_watcher(uv_loop_t* loop, int wd) {151struct watcher_list w;152w.wd = wd;153return RB_FIND(watcher_root, CAST(&loop->inotify_watchers), &w);154}155156static void maybe_free_watcher_list(struct watcher_list* w, uv_loop_t* loop) {157/* if the watcher_list->watchers is being iterated over, we can't free it. */158if ((!w->iterating) && QUEUE_EMPTY(&w->watchers)) {159/* No watchers left for this path. Clean up. */160RB_REMOVE(watcher_root, CAST(&loop->inotify_watchers), w);161inotify_rm_watch(loop->inotify_fd, w->wd);162uv__free(w);163}164}165166static void uv__inotify_read(uv_loop_t* loop,167uv__io_t* dummy,168unsigned int events) {169const struct inotify_event* e;170struct watcher_list* w;171uv_fs_event_t* h;172QUEUE queue;173QUEUE* q;174const char* path;175ssize_t size;176const char *p;177/* needs to be large enough for sizeof(inotify_event) + strlen(path) */178char buf[4096];179180for (;;) {181do182size = read(loop->inotify_fd, buf, sizeof(buf));183while (size == -1 && errno == EINTR);184185if (size == -1) {186assert(errno == EAGAIN || errno == EWOULDBLOCK);187break;188}189190assert(size > 0); /* pre-2.6.21 thing, size=0 == read buffer too small */191192/* Now we have one or more inotify_event structs. */193for (p = buf; p < buf + size; p += sizeof(*e) + e->len) {194e = (const struct inotify_event*) p;195196events = 0;197if (e->mask & (IN_ATTRIB|IN_MODIFY))198events |= UV_CHANGE;199if (e->mask & ~(IN_ATTRIB|IN_MODIFY))200events |= UV_RENAME;201202w = find_watcher(loop, e->wd);203if (w == NULL)204continue; /* Stale event, no watchers left. */205206/* inotify does not return the filename when monitoring a single file207* for modifications. Repurpose the filename for API compatibility.208* I'm not convinced this is a good thing, maybe it should go.209*/210path = e->len ? (const char*) (e + 1) : uv__basename_r(w->path);211212/* We're about to iterate over the queue and call user's callbacks.213* What can go wrong?214* A callback could call uv_fs_event_stop()215* and the queue can change under our feet.216* So, we use QUEUE_MOVE() trick to safely iterate over the queue.217* And we don't free the watcher_list until we're done iterating.218*219* First,220* tell uv_fs_event_stop() (that could be called from a user's callback)221* not to free watcher_list.222*/223w->iterating = 1;224QUEUE_MOVE(&w->watchers, &queue);225while (!QUEUE_EMPTY(&queue)) {226q = QUEUE_HEAD(&queue);227h = QUEUE_DATA(q, uv_fs_event_t, watchers);228229QUEUE_REMOVE(q);230QUEUE_INSERT_TAIL(&w->watchers, q);231232h->cb(h, path, events, 0);233}234/* done iterating, time to (maybe) free empty watcher_list */235w->iterating = 0;236maybe_free_watcher_list(w, loop);237}238}239}240241242int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle) {243uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);244return 0;245}246247248int uv_fs_event_start(uv_fs_event_t* handle,249uv_fs_event_cb cb,250const char* path,251unsigned int flags) {252struct watcher_list* w;253size_t len;254int events;255int err;256int wd;257258if (uv__is_active(handle))259return UV_EINVAL;260261err = init_inotify(handle->loop);262if (err)263return err;264265events = IN_ATTRIB266| IN_CREATE267| IN_MODIFY268| IN_DELETE269| IN_DELETE_SELF270| IN_MOVE_SELF271| IN_MOVED_FROM272| IN_MOVED_TO;273274wd = inotify_add_watch(handle->loop->inotify_fd, path, events);275if (wd == -1)276return UV__ERR(errno);277278w = find_watcher(handle->loop, wd);279if (w)280goto no_insert;281282len = strlen(path) + 1;283w = uv__malloc(sizeof(*w) + len);284if (w == NULL)285return UV_ENOMEM;286287w->wd = wd;288w->path = memcpy(w + 1, path, len);289QUEUE_INIT(&w->watchers);290w->iterating = 0;291RB_INSERT(watcher_root, CAST(&handle->loop->inotify_watchers), w);292293no_insert:294uv__handle_start(handle);295QUEUE_INSERT_TAIL(&w->watchers, &handle->watchers);296handle->path = w->path;297handle->cb = cb;298handle->wd = wd;299300return 0;301}302303304int uv_fs_event_stop(uv_fs_event_t* handle) {305struct watcher_list* w;306307if (!uv__is_active(handle))308return 0;309310w = find_watcher(handle->loop, handle->wd);311assert(w != NULL);312313handle->wd = -1;314handle->path = NULL;315uv__handle_stop(handle);316QUEUE_REMOVE(&handle->watchers);317318maybe_free_watcher_list(w, handle->loop);319320return 0;321}322323324void uv__fs_event_close(uv_fs_event_t* handle) {325uv_fs_event_stop(handle);326}327328329