1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dir-watch-kqueue.c OS specific directory change notification for message bus
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
27 #include <sys/event.h>
33 #include <dbus/dbus-watch.h>
35 #include <dbus/dbus-internals.h>
36 #include "dir-watch.h"
38 #define MAX_DIRS_TO_WATCH 128
41 static int fds[MAX_DIRS_TO_WATCH];
42 static int num_fds = 0;
43 static DBusWatch *watch = NULL;
44 static DBusLoop *loop = NULL;
47 _kqueue_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
49 return dbus_watch_handle (watch, condition);
53 _handle_kqueue_watch (DBusWatch *watch, unsigned int flags, void *data)
56 struct timespec nullts = { 0, 0 };
60 res = kevent (kq, NULL, 0, &ev, 1, &nullts);
62 /* Sleep for half a second to avoid a race when files are install(1)'d
69 _dbus_verbose ("Sending SIGHUP signal on reception of a kevent\n");
70 (void) kill (pid, SIGHUP);
72 else if (res < 0 && errno == EBADF)
77 _dbus_loop_remove_watch (loop, watch, _kqueue_watch_callback, NULL);
78 _dbus_watch_unref (watch);
82 _dbus_verbose ("Sending SIGHUP signal since kqueue has been closed\n");
83 (void) kill (pid, SIGHUP);
90 bus_watch_directory (const char *dir, BusContext *context)
95 _dbus_assert (dir != NULL);
103 _dbus_warn ("Cannot create kqueue; error '%s'\n", _dbus_strerror (errno));
107 loop = bus_context_get_loop (context);
109 watch = _dbus_watch_new (kq, DBUS_WATCH_READABLE, TRUE,
110 _handle_kqueue_watch, NULL, NULL);
114 _dbus_warn ("Unable to create kqueue watch\n");
120 if (!_dbus_loop_add_watch (loop, watch, _kqueue_watch_callback,
123 _dbus_warn ("Unable to add reload watch to main loop");
126 _dbus_watch_unref (watch);
132 if (num_fds >= MAX_DIRS_TO_WATCH )
134 _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH);
138 fd = open (dir, O_RDONLY);
141 _dbus_warn ("Cannot open directory '%s'; error '%s'\n", dir, _dbus_strerror (errno));
145 EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
146 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME, 0, 0);
147 if (kevent (kq, &ev, 1, NULL, 0, NULL) == -1)
149 _dbus_warn ("Cannot setup a kevent for '%s'; error '%s'\n", dir, _dbus_strerror (errno));
155 _dbus_verbose ("Added kqueue watch on config directory '%s'\n", dir);
162 bus_drop_all_directory_watches (void)
166 _dbus_verbose ("Dropping all watches on config directories\n");
168 for (i = 0; i < num_fds; i++)
170 if (close (fds[i]) != 0)
172 _dbus_verbose ("Error closing fd %d for config directory watch\n", fds[i]);