[daemon-fix] Fixed kdbus bus address recognition
[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 "dir-watch.h"
42
43 #define MAX_DIRS_TO_WATCH 128
44
45 static int kq = -1;
46 static int fds[MAX_DIRS_TO_WATCH];
47 static char *dirs[MAX_DIRS_TO_WATCH];
48 static int num_fds = 0;
49 static DBusWatch *watch = NULL;
50 static DBusLoop *loop = NULL;
51
52 static dbus_bool_t
53 _handle_kqueue_watch (DBusWatch *watch, unsigned int flags, void *data)
54 {
55   struct kevent ev;
56   struct timespec nullts = { 0, 0 };
57   int res;
58   pid_t pid;
59
60   res = kevent (kq, NULL, 0, &ev, 1, &nullts);
61
62   /* Sleep for half a second to avoid a race when files are install(1)'d
63    * to system.d. */
64   usleep(500000);
65
66   if (res > 0)
67     {
68       pid = getpid ();
69       _dbus_verbose ("Sending SIGHUP signal on reception of a kevent\n");
70       (void) kill (pid, SIGHUP);
71     }
72   else if (res < 0 && errno == EBADF)
73     {
74       kq = -1;
75       if (watch != NULL)
76         {
77           _dbus_loop_remove_watch (loop, watch);
78           _dbus_watch_invalidate (watch);
79           _dbus_watch_unref (watch);
80           watch = NULL;
81         }
82       pid = getpid ();
83       _dbus_verbose ("Sending SIGHUP signal since kqueue has been closed\n");
84       (void) kill (pid, SIGHUP);
85     }
86
87   return TRUE;
88 }
89
90 static int
91 _init_kqueue (BusContext *context)
92 {
93   int ret = 0;
94
95   if (kq < 0)
96     {
97
98       kq = kqueue ();
99       if (kq < 0)
100         {
101           _dbus_warn ("Cannot create kqueue; error '%s'\n", _dbus_strerror (errno));
102           goto out;
103         }
104
105         loop = bus_context_get_loop (context);
106
107         watch = _dbus_watch_new (kq, DBUS_WATCH_READABLE, TRUE,
108                                  _handle_kqueue_watch, NULL, NULL);
109
110         if (watch == NULL)
111           {
112             _dbus_warn ("Unable to create kqueue watch\n");
113             close (kq);
114             kq = -1;
115             goto out;
116           }
117
118         if (!_dbus_loop_add_watch (loop, watch))
119           {
120             _dbus_warn ("Unable to add reload watch to main loop");
121             _dbus_watch_invalidate (watch);
122             _dbus_watch_unref (watch);
123             watch = NULL;
124             close (kq);
125             kq = -1;
126             goto out;
127           }
128     }
129
130   ret = 1;
131
132 out:
133   return ret;
134 }
135
136 void
137 bus_set_watched_dirs (BusContext *context, DBusList **directories)
138 {
139   int new_fds[MAX_DIRS_TO_WATCH];
140   char *new_dirs[MAX_DIRS_TO_WATCH];
141   DBusList *link;
142   int i, j, fd;
143   struct kevent ev;
144
145   if (!_init_kqueue (context))
146     goto out;
147
148   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
149     {
150       new_fds[i] = -1;
151       new_dirs[i] = NULL;
152     }
153
154   i = 0;
155   link = _dbus_list_get_first_link (directories);
156   while (link != NULL)
157     {
158       new_dirs[i++] = (char *)link->data;
159       link = _dbus_list_get_next_link (directories, link);
160     }
161
162   /* Look for directories in both the old and new sets, if
163    * we find one, move its data into the new set.
164    */
165   for (i = 0; new_dirs[i]; i++)
166     {
167       for (j = 0; j < num_fds; j++)
168         {
169           if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0)
170             {
171               new_fds[i] = fds[j];
172               new_dirs[i] = dirs[j];
173               fds[j] = -1;
174               dirs[j] = NULL;
175               break;
176             }
177         }
178     }
179
180   /* Any directory we find in "fds" with a nonzero fd must
181    * not be in the new set, so perform cleanup now.
182    */
183   for (j = 0; j < num_fds; j++)
184     {
185       if (fds[j] != -1)
186         {
187           close (fds[j]);
188           dbus_free (dirs[j]);
189           fds[j] = -1;
190           dirs[j] = NULL;
191         }
192     }
193
194   for (i = 0; new_dirs[i]; i++)
195     {
196       if (new_fds[i] == -1)
197         {
198           /* FIXME - less lame error handling for failing to add a watch;
199            * we may need to sleep.
200            */
201           fd = open (new_dirs[i], O_RDONLY);
202           if (fd < 0)
203             {
204               if (errno != ENOENT)
205                 {
206                   _dbus_warn ("Cannot open directory '%s'; error '%s'\n", new_dirs[i], _dbus_strerror (errno));
207                   goto out;
208                 }
209               else
210                 {
211                   new_fds[i] = -1;
212                   new_dirs[i] = NULL;
213                   continue;
214                 }
215             }
216
217           EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
218                   NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME, 0, 0);
219           if (kevent (kq, &ev, 1, NULL, 0, NULL) == -1)
220             {
221               _dbus_warn ("Cannot setup a kevent for '%s'; error '%s'\n", new_dirs[i], _dbus_strerror (errno));
222               close (fd);
223               goto out;
224             }
225
226           new_fds[i] = fd;
227           new_dirs[i] = _dbus_strdup (new_dirs[i]);
228           if (!new_dirs[i])
229             {
230               /* FIXME have less lame handling for OOM, we just silently fail to
231                * watch.  (In reality though, the whole OOM handling in dbus is
232                * stupid but we won't go into that in this comment =) )
233                */
234               close (fd);
235               new_fds[i] = -1;
236             }
237         }
238     }
239
240   num_fds = i;
241
242   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
243     {
244       fds[i] = new_fds[i];
245       dirs[i] = new_dirs[i];
246     }
247
248  out:
249   ;
250 }