Bug 21161 - Update the FSF address
[platform/upstream/dbus.git] / bus / dir-watch-kqueue.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dir-watch-kqueue.c  OS specific directory change notification for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25
26 #include <sys/types.h>
27 #include <sys/event.h>
28 #include <sys/time.h>
29 #include <signal.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35
36 #include "bus.h"
37 #include <dbus/dbus-watch.h>
38
39 #include <dbus/dbus-internals.h>
40 #include "dir-watch.h"
41
42 #define MAX_DIRS_TO_WATCH 128
43
44 static int kq = -1;
45 static int fds[MAX_DIRS_TO_WATCH];
46 static int num_fds = 0;
47 static DBusWatch *watch = NULL;
48 static DBusLoop *loop = NULL;
49
50 static dbus_bool_t
51 _kqueue_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
52 {
53   return dbus_watch_handle (watch, condition);
54 }
55
56 static dbus_bool_t
57 _handle_kqueue_watch (DBusWatch *watch, unsigned int flags, void *data)
58 {
59   struct kevent ev;
60   struct timespec nullts = { 0, 0 };
61   int res;
62   pid_t pid;
63
64   res = kevent (kq, NULL, 0, &ev, 1, &nullts);
65
66   /* Sleep for half a second to avoid a race when files are install(1)'d
67    * to system.d. */
68   usleep(500000);
69
70   if (res > 0)
71     {
72       pid = getpid ();
73       _dbus_verbose ("Sending SIGHUP signal on reception of a kevent\n");
74       (void) kill (pid, SIGHUP);
75     }
76   else if (res < 0 && errno == EBADF)
77     {
78       kq = -1;
79       if (watch != NULL)
80         {
81           _dbus_loop_remove_watch (loop, watch, _kqueue_watch_callback, NULL);
82           _dbus_watch_unref (watch);
83           watch = NULL;
84         }
85       pid = getpid ();
86       _dbus_verbose ("Sending SIGHUP signal since kqueue has been closed\n");
87       (void) kill (pid, SIGHUP);
88     }
89
90   return TRUE;
91 }
92
93 void
94 bus_watch_directory (const char *dir, BusContext *context)
95 {
96   int fd;
97   struct kevent ev;
98
99   _dbus_assert (dir != NULL);
100
101   if (kq < 0)
102     {
103
104       kq = kqueue ();
105       if (kq < 0)
106         {
107           _dbus_warn ("Cannot create kqueue; error '%s'\n", _dbus_strerror (errno));
108           goto out;
109         }
110
111         loop = bus_context_get_loop (context);
112
113         watch = _dbus_watch_new (kq, DBUS_WATCH_READABLE, TRUE,
114                                  _handle_kqueue_watch, NULL, NULL);
115
116         if (watch == NULL)
117           {
118             _dbus_warn ("Unable to create kqueue watch\n");
119             close (kq);
120             kq = -1;
121             goto out;
122           }
123
124         if (!_dbus_loop_add_watch (loop, watch, _kqueue_watch_callback,
125                                    NULL, NULL))
126           {
127             _dbus_warn ("Unable to add reload watch to main loop");
128             close (kq);
129             kq = -1;
130             _dbus_watch_unref (watch);
131             watch = NULL;
132             goto out;
133           }
134     }
135
136   if (num_fds >= MAX_DIRS_TO_WATCH )
137     {
138       _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH);
139       goto out;
140     }
141
142   fd = open (dir, O_RDONLY);
143   if (fd < 0)
144     {
145       _dbus_warn ("Cannot open directory '%s'; error '%s'\n", dir, _dbus_strerror (errno));
146       goto out;
147     }
148
149   EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
150           NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME, 0, 0);
151   if (kevent (kq, &ev, 1, NULL, 0, NULL) == -1)
152     {
153       _dbus_warn ("Cannot setup a kevent for '%s'; error '%s'\n", dir, _dbus_strerror (errno));
154       close (fd);
155       goto out;
156     }
157
158   fds[num_fds++] = fd;
159   _dbus_verbose ("Added kqueue watch on config directory '%s'\n", dir);
160
161  out:
162   ;
163 }
164
165 void
166 bus_drop_all_directory_watches (void)
167 {
168   int i;
169
170   _dbus_verbose ("Dropping all watches on config directories\n");
171
172   for (i = 0; i < num_fds; i++)
173     {
174       if (close (fds[i]) != 0)
175         {
176           _dbus_verbose ("Error closing fd %d for config directory watch\n", fds[i]);
177         }
178     }
179
180   num_fds = 0;
181 }