1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dir-watch-inotify.c OS specific directory change notification for message bus
4 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/inotify.h>
31 #include <sys/types.h>
35 #include <dbus/dbus-internals.h>
36 #include <dbus/dbus-list.h>
37 #include <dbus/dbus-watch.h>
38 #include "dir-watch.h"
40 #define MAX_DIRS_TO_WATCH 128
41 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
42 #define INOTIFY_BUF_LEN (1024 * (INOTIFY_EVENT_SIZE + 16))
44 /* use a static array to avoid handling OOM */
45 static int wds[MAX_DIRS_TO_WATCH];
46 static char *dirs[MAX_DIRS_TO_WATCH];
47 static int num_wds = 0;
48 static int inotify_fd = -1;
49 static DBusWatch *watch = NULL;
50 static DBusLoop *loop = NULL;
53 _inotify_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
55 return dbus_watch_handle (watch, condition);
59 _handle_inotify_watch (DBusWatch *passed_watch, unsigned int flags, void *data)
61 char buffer[INOTIFY_BUF_LEN];
65 dbus_bool_t have_change = FALSE;
67 ret = read (inotify_fd, buffer, INOTIFY_BUF_LEN);
69 _dbus_verbose ("Error reading inotify event: '%s'\n", _dbus_strerror(errno));
71 _dbus_verbose ("Error reading inotify event: buffer too small\n");
75 struct inotify_event *ev;
76 pid = _dbus_getpid ();
78 ev = (struct inotify_event *) &buffer[i];
79 i += INOTIFY_EVENT_SIZE + ev->len;
80 #ifdef DBUS_ENABLE_VERBOSE_MODE
82 _dbus_verbose ("event name: '%s'\n", ev->name);
83 _dbus_verbose ("inotify event: wd=%d mask=%u cookie=%u len=%u\n", ev->wd, ev->mask, ev->cookie, ev->len);
85 _dbus_verbose ("Sending SIGHUP signal on reception of a inotify event\n");
89 (void) kill (pid, SIGHUP);
97 _set_watched_dirs_internal (DBusList **directories)
99 int new_wds[MAX_DIRS_TO_WATCH];
100 char *new_dirs[MAX_DIRS_TO_WATCH];
104 for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
111 link = _dbus_list_get_first_link (directories);
114 new_dirs[i++] = (char *)link->data;
115 link = _dbus_list_get_next_link (directories, link);
118 /* Look for directories in both the old and new sets, if
119 * we find one, move its data into the new set.
121 for (i = 0; new_dirs[i]; i++)
123 for (j = 0; j < num_wds; j++)
125 if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0)
128 new_dirs[i] = dirs[j];
136 /* Any directories we find in "wds" with a nonzero fd must
137 * not be in the new set, so perform cleanup now.
139 for (j = 0; j < num_wds; j++)
143 inotify_rm_watch (inotify_fd, wds[j]);
150 for (i = 0; new_dirs[i]; i++)
152 if (new_wds[i] == -1)
154 /* FIXME - less lame error handling for failing to add a watch; we may need to sleep. */
155 wd = inotify_add_watch (inotify_fd, new_dirs[i], IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
158 /* Not all service directories need to exist. */
161 _dbus_warn ("Cannot setup inotify for '%s'; error '%s'\n", new_dirs[i], _dbus_strerror (errno));
172 new_dirs[i] = _dbus_strdup (new_dirs[i]);
175 /* FIXME have less lame handling for OOM, we just silently fail to
176 * watch. (In reality though, the whole OOM handling in dbus is stupid
177 * but we won't go into that in this comment =) )
179 inotify_rm_watch (inotify_fd, wd);
187 for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
190 dirs[i] = new_dirs[i];
198 _shutdown_inotify (void *data)
200 DBusList *empty = NULL;
202 if (inotify_fd == -1)
205 _set_watched_dirs_internal (&empty);
211 _dbus_loop_remove_watch (loop, watch, _inotify_watch_callback, NULL);
212 _dbus_watch_unref (watch);
213 _dbus_loop_unref (loop);
220 _init_inotify (BusContext *context)
224 if (inotify_fd == -1)
226 #ifdef HAVE_INOTIFY_INIT1
227 inotify_fd = inotify_init1 (IN_CLOEXEC);
228 /* This ensures we still run on older Linux kernels.
229 * https://bugs.freedesktop.org/show_bug.cgi?id=23957
232 inotify_fd = inotify_init ();
234 inotify_fd = inotify_init ();
238 _dbus_warn ("Cannot initialize inotify\n");
241 loop = bus_context_get_loop (context);
242 _dbus_loop_ref (loop);
244 watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
245 _handle_inotify_watch, NULL, NULL);
249 _dbus_warn ("Unable to create inotify watch\n");
253 if (!_dbus_loop_add_watch (loop, watch, _inotify_watch_callback,
256 _dbus_warn ("Unable to add reload watch to main loop");
257 _dbus_watch_unref (watch);
262 if (!_dbus_register_shutdown_func (_shutdown_inotify, NULL))
264 _dbus_warn ("Unable to register shutdown func");
265 _dbus_watch_unref (watch);
278 bus_set_watched_dirs (BusContext *context, DBusList **directories)
280 if (!_init_inotify (context))
283 _set_watched_dirs_internal (directories);