bus: Assign a serial number for messages from the driver
[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 <dbus/dbus-list.h>
41 #include <dbus/dbus-sysdeps-unix.h>
42 #include "dir-watch.h"
43
44 #define MAX_DIRS_TO_WATCH 128
45
46 static int kq = -1;
47 static int fds[MAX_DIRS_TO_WATCH];
48 static char *dirs[MAX_DIRS_TO_WATCH];
49 static int num_fds = 0;
50 static DBusWatch *watch = NULL;
51 static DBusLoop *loop = NULL;
52
53 static dbus_bool_t
54 _handle_kqueue_watch (DBusWatch *watch, unsigned int flags, void *data)
55 {
56   struct kevent ev;
57   struct timespec nullts = { 0, 0 };
58   int res;
59   pid_t pid;
60
61   res = kevent (kq, NULL, 0, &ev, 1, &nullts);
62
63   /* Sleep for half a second to avoid a race when files are install(1)'d
64    * to system.d. */
65   usleep(500000);
66
67   if (res > 0)
68     {
69       pid = getpid ();
70       _dbus_verbose ("Sending SIGHUP signal on reception of a kevent\n");
71       (void) kill (pid, SIGHUP);
72     }
73   else if (res < 0 && errno == EBADF)
74     {
75       kq = -1;
76       if (watch != NULL)
77         {
78           _dbus_loop_remove_watch (loop, watch);
79           _dbus_watch_invalidate (watch);
80           _dbus_watch_unref (watch);
81           watch = NULL;
82         }
83       pid = getpid ();
84       _dbus_verbose ("Sending SIGHUP signal since kqueue has been closed\n");
85       (void) kill (pid, SIGHUP);
86     }
87
88   return TRUE;
89 }
90
91 static void _shutdown_kqueue (void *data)
92 {
93   int i;
94
95   if (kq < 0)
96     return;
97
98   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
99     {
100       if (fds[i] >= 0)
101         {
102           close (fds[i]);
103           fds[i] = -1;
104         }
105       if (dirs[i] != NULL)
106         {
107           /* dbus_free() is necessary to pass memleak check */
108           dbus_free (dirs[i]);
109           dirs[i] = NULL;
110         }
111     }
112
113   if (loop)
114     {
115       _dbus_loop_remove_watch (loop, watch);
116       _dbus_loop_unref (loop);
117       loop = NULL;
118     }
119
120   if (watch)
121     {
122       _dbus_watch_invalidate (watch);
123       _dbus_watch_unref (watch);
124       watch = NULL;
125     }
126
127     close (kq);
128     kq = -1;
129 }
130
131 static int
132 _init_kqueue (BusContext *context)
133 {
134   if (kq < 0)
135     {
136
137       kq = kqueue ();
138       if (kq < 0)
139         {
140           _dbus_warn ("Cannot create kqueue; error '%s'", _dbus_strerror (errno));
141           goto out;
142         }
143
144       loop = bus_context_get_loop (context);
145       _dbus_loop_ref (loop);
146
147       watch = _dbus_watch_new (kq, DBUS_WATCH_READABLE, TRUE,
148                                _handle_kqueue_watch, NULL, NULL);
149
150       if (watch == NULL)
151         {
152           _dbus_warn ("Unable to create kqueue watch");
153           goto out1;
154         }
155
156       if (!_dbus_loop_add_watch (loop, watch))
157         {
158           _dbus_warn ("Unable to add reload watch to main loop");
159           goto out2;
160         }
161
162       if (!_dbus_register_shutdown_func (_shutdown_kqueue, NULL))
163         {
164           _dbus_warn ("Unable to register shutdown function");
165           goto out3;
166         }
167     }
168
169   return 1;
170
171 out3:
172   _dbus_loop_remove_watch (loop, watch);
173
174 out2:
175   if (watch)
176     {
177       _dbus_watch_invalidate (watch);
178       _dbus_watch_unref (watch);
179       watch = NULL;
180     }
181
182 out1:
183   if (kq >= 0)
184     {
185       close (kq);
186       kq = -1;
187     }
188   if (loop)
189     {
190       _dbus_loop_unref (loop);
191       loop = NULL;
192     }
193
194 out:
195   return 0;
196 }
197
198 void
199 bus_set_watched_dirs (BusContext *context, DBusList **directories)
200 {
201   int new_fds[MAX_DIRS_TO_WATCH];
202   char *new_dirs[MAX_DIRS_TO_WATCH];
203   DBusList *link;
204   int i, j, fd;
205   struct kevent ev;
206 #ifdef O_CLOEXEC
207   dbus_bool_t cloexec_done = 0;
208 #endif
209
210   if (!_init_kqueue (context))
211     goto out;
212
213   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
214     {
215       new_fds[i] = -1;
216       new_dirs[i] = NULL;
217     }
218
219   i = 0;
220   link = _dbus_list_get_first_link (directories);
221   while (link != NULL)
222     {
223       new_dirs[i++] = (char *)link->data;
224       link = _dbus_list_get_next_link (directories, link);
225     }
226
227   /* Look for directories in both the old and new sets, if
228    * we find one, move its data into the new set.
229    */
230   for (i = 0; new_dirs[i]; i++)
231     {
232       for (j = 0; j < num_fds; j++)
233         {
234           if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0)
235             {
236               new_fds[i] = fds[j];
237               new_dirs[i] = dirs[j];
238               fds[j] = -1;
239               dirs[j] = NULL;
240               break;
241             }
242         }
243     }
244
245   /* Any directory we find in "fds" with a nonzero fd must
246    * not be in the new set, so perform cleanup now.
247    */
248   for (j = 0; j < num_fds; j++)
249     {
250       if (fds[j] != -1)
251         {
252           close (fds[j]);
253           dbus_free (dirs[j]);
254           fds[j] = -1;
255           dirs[j] = NULL;
256         }
257     }
258
259   for (i = 0; new_dirs[i]; i++)
260     {
261       if (new_fds[i] == -1)
262         {
263           /* FIXME - less lame error handling for failing to add a watch;
264            * we may need to sleep.
265            */
266 #ifdef O_CLOEXEC
267           fd = open (new_dirs[i], O_RDONLY | O_CLOEXEC);
268           cloexec_done = (fd >= 0);
269
270           if (fd < 0 && errno == EINVAL)
271 #endif
272             {
273               fd = open (new_dirs[i], O_RDONLY);
274             }
275           if (fd < 0)
276             {
277               if (errno != ENOENT)
278                 {
279                   _dbus_warn ("Cannot open directory '%s'; error '%s'", new_dirs[i], _dbus_strerror (errno));
280                   goto out;
281                 }
282               else
283                 {
284                   new_fds[i] = -1;
285                   new_dirs[i] = NULL;
286                   continue;
287                 }
288             }
289 #ifdef O_CLOEXEC
290           if (!cloexec_done)
291 #endif
292             {
293               _dbus_fd_set_close_on_exec (fd);
294             }
295
296           EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
297                   NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME, 0, 0);
298           if (kevent (kq, &ev, 1, NULL, 0, NULL) == -1)
299             {
300               _dbus_warn ("Cannot setup a kevent for '%s'; error '%s'", new_dirs[i], _dbus_strerror (errno));
301               close (fd);
302               goto out;
303             }
304
305           new_fds[i] = fd;
306           new_dirs[i] = _dbus_strdup (new_dirs[i]);
307           if (!new_dirs[i])
308             {
309               /* FIXME have less lame handling for OOM, we just silently fail to
310                * watch.  (In reality though, the whole OOM handling in dbus is
311                * stupid but we won't go into that in this comment =) )
312                */
313               close (fd);
314               new_fds[i] = -1;
315             }
316         }
317     }
318
319   num_fds = i;
320
321   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
322     {
323       fds[i] = new_fds[i];
324       dirs[i] = new_dirs[i];
325     }
326
327  out:
328   ;
329 }