Index: bus/dir-watch-dnotify.c =================================================================== --- bus/dir-watch-dnotify.c.orig 2009-09-08 16:42:18.000000000 +0000 +++ bus/dir-watch-dnotify.c 2011-01-12 19:36:51.293682972 +0000 @@ -1,7 +1,7 @@ /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dir-watch-dnotify.c OS specific directory change notification for message bus * - * Copyright (C) 2003 Red Hat, Inc. + * Copyright (C) 2003, 2011 Red Hat, Inc. * * Licensed under the Academic Free License version 2.1 * @@ -32,62 +32,156 @@ #endif #include +#include #include "dir-watch.h" #define MAX_DIRS_TO_WATCH 128 /* use a static array to avoid handling OOM */ static int fds[MAX_DIRS_TO_WATCH]; +static char *dirs[MAX_DIRS_TO_WATCH]; static int num_fds = 0; +static int _dnotify_inited = -1; -void -bus_watch_directory (const char *dir, BusContext *context) +static void +_shutdown_dnotify (void *data) { - int fd; + int i; + for (i = 0; i < num_fds; i++) + { + close (fds[i]); + dbus_free (dirs[i]); + fds[i] = -1; + dirs[i] = NULL; + } + num_fds = 0; +} - _dbus_assert (dir != NULL); +static int +_init_dnotify (void) +{ + if (_dnotify_inited < 0) + { + _dbus_register_shutdown_func (_shutdown_dnotify, NULL); + _dnotify_inited = 0; + } + return _dnotify_inited; +} + +/* This interface was introduced to avoid a race condition where + * "watched" dirs could momentarily be unwatched */ +void +bus_set_watched_dirs (BusContext * context, DBusList ** directories) +{ + DBusList *link = _dbus_list_get_first_link (directories); + int new_fds[MAX_DIRS_TO_WATCH]; + char *new_dirs[MAX_DIRS_TO_WATCH]; + int fd, i, j; - if (num_fds >= MAX_DIRS_TO_WATCH ) + if (_init_dnotify ()) { - _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH); + _dbus_warn ("Cannot init dnotify\n"); goto out; } - fd = open (dir, O_RDONLY); - if (fd < 0) + for (i = 0; i < MAX_DIRS_TO_WATCH; i++) { - _dbus_warn ("Cannot open directory '%s'; error '%s'\n", dir, _dbus_strerror (errno)); - goto out; + new_fds[i] = -1; + new_dirs[i] = NULL; } + i = 0; - if (fcntl (fd, F_NOTIFY, DN_CREATE|DN_DELETE|DN_RENAME|DN_MODIFY) == -1) + while (link != NULL) { - _dbus_warn ("Cannot setup D_NOTIFY for '%s' error '%s'\n", dir, _dbus_strerror (errno)); - close (fd); - goto out; + new_dirs[i++] = (char *) link->data; + link = _dbus_list_get_next_link (directories, link); + } + /* Look for directories in both the old and new sets, if + * we find one, move its data into the new set. + */ + for (i = 0; new_dirs[i]; i++) + { + for (j = 0; j < num_fds; j++) + { + if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0) + { + new_fds[i] = fds[j]; + new_dirs[i] = dirs[j]; + fds[j] = -1; + dirs[j] = NULL; + break; + } + } } - - fds[num_fds++] = fd; - _dbus_verbose ("Added watch on config directory '%s'\n", dir); - out: - ; -} + /* Any directories we find in "fds" with a nonzero fd must + * not be in the new set, so perform cleanup now. + */ + for (j = 0; j < num_fds; j++) + { + if (fds[j] != -1) + { + close (fds[j]); + dbus_free (dirs[j]); + fds[j] = -1; + dirs[j] = NULL; + } + } -void -bus_drop_all_directory_watches (void) -{ - int i; - - _dbus_verbose ("Dropping all watches on config directories\n"); - - for (i = 0; i < num_fds; i++) + for (i = 0; new_dirs[i]; i++) { - if (close (fds[i]) != 0) + if (new_fds[i] == -1) { - _dbus_verbose ("Error closing fd %d for config directory watch\n", fds[i]); + fd = open (new_dirs[i], O_RDONLY); + if (fd < 0) + { + if (errno != ENOENT) + { + _dbus_warn ("Cannot open directory '%s'; error '%s'\n", + new_dirs[i], _dbus_strerror (errno)); + goto out; + } + else + { + new_fds[i] = -1; + new_dirs[i] = NULL; + continue; + } + } + + _dbus_fd_set_close_on_exec (fd); + + if (fcntl + (fd, F_NOTIFY, + DN_CREATE | DN_DELETE | DN_RENAME | DN_MODIFY) == -1) + { + _dbus_warn ("Cannot setup D_NOTIFY for '%s' error '%s'\n", + new_dirs[i], _dbus_strerror (errno)); + close (fd); + goto out; + } + + new_fds[i] = fd; + new_dirs[i] = _dbus_strdup (new_dirs[i]); + if (!new_dirs[i]) + { + close (fd); + new_fds[i] = -1; + } + else + { + _dbus_verbose ("Added watch on config directory '%s'\n", + new_dirs[i]); + } } } - - num_fds = 0; + num_fds = i; + + for (i = 0; i < MAX_DIRS_TO_WATCH; i++) + { + fds[i] = new_fds[i]; + dirs[i] = new_dirs[i]; + } + +out:; }