Merge branch 'dbus-1.6'
[platform/upstream/dbus.git] / bus / dir-watch-inotify.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dir-watch-inotify.c  OS specific directory change notification for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *           (c) 2006 Mandriva
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
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.
13  *
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.
18  * 
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
22  *
23  */
24
25 /* Be careful, this file is not Linux-only: QNX also uses it */
26
27 #include <config.h>
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 /* QNX's inotify is broken, and requires stdint.h to be manually included first */
33 #include <stdint.h>
34 #include <sys/inotify.h>
35 #include <sys/types.h>
36 #include <signal.h>
37 #include <errno.h>
38
39 #include <dbus/dbus-internals.h>
40 #include <dbus/dbus-list.h>
41 #include <dbus/dbus-watch.h>
42 #include "dir-watch.h"
43
44 #define MAX_DIRS_TO_WATCH 128
45 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
46 #define INOTIFY_BUF_LEN (1024 * (INOTIFY_EVENT_SIZE + 16))
47
48 /* use a static array to avoid handling OOM */
49 static int wds[MAX_DIRS_TO_WATCH];
50 static char *dirs[MAX_DIRS_TO_WATCH];
51 static int num_wds = 0;
52 static int inotify_fd = -1;
53 static DBusWatch *watch = NULL;
54 static DBusLoop *loop = NULL;
55
56 static dbus_bool_t
57 _handle_inotify_watch (DBusWatch *passed_watch, unsigned int flags, void *data)
58 {
59   char buffer[INOTIFY_BUF_LEN];
60   ssize_t ret = 0;
61   int i = 0;
62   pid_t pid;
63   dbus_bool_t have_change = FALSE;
64
65   ret = read (inotify_fd, buffer, INOTIFY_BUF_LEN);
66   if (ret < 0)
67     _dbus_verbose ("Error reading inotify event: '%s'\n", _dbus_strerror(errno));
68   else if (!ret)
69     _dbus_verbose ("Error reading inotify event: buffer too small\n");
70
71   while (i < ret)
72     {
73       struct inotify_event *ev;
74       pid = _dbus_getpid ();
75
76       ev = (struct inotify_event *) &buffer[i];
77       i += INOTIFY_EVENT_SIZE + ev->len;
78 #ifdef DBUS_ENABLE_VERBOSE_MODE
79       if (ev->len)
80         _dbus_verbose ("event name: '%s'\n", ev->name);
81       _dbus_verbose ("inotify event: wd=%d mask=%u cookie=%u len=%u\n", ev->wd, ev->mask, ev->cookie, ev->len);
82 #endif
83       _dbus_verbose ("Sending SIGHUP signal on reception of a inotify event\n");
84       have_change = TRUE;
85     }
86   if (have_change)
87     (void) kill (pid, SIGHUP);
88
89   return TRUE;
90 }
91
92 #include <stdio.h>
93
94 static void
95 _set_watched_dirs_internal (DBusList **directories)
96 {
97   int new_wds[MAX_DIRS_TO_WATCH];
98   char *new_dirs[MAX_DIRS_TO_WATCH];
99   DBusList *link;
100   int i, j, wd;
101
102   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
103     {
104       new_wds[i] = -1;
105       new_dirs[i] = NULL;
106     }
107
108   i = 0;
109   link = _dbus_list_get_first_link (directories);
110   while (link != NULL)
111     {
112       new_dirs[i++] = (char *)link->data;
113       link = _dbus_list_get_next_link (directories, link);
114     }
115
116   /* Look for directories in both the old and new sets, if
117    * we find one, move its data into the new set.
118    */
119   for (i = 0; new_dirs[i]; i++)
120     {
121       for (j = 0; j < num_wds; j++)
122         {
123           if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0)
124             {
125               new_wds[i] = wds[j];
126               new_dirs[i] = dirs[j];
127               wds[j] = -1;
128               dirs[j] = NULL;
129               break;
130             }
131         }
132     }
133
134   /* Any directories we find in "wds" with a nonzero fd must
135    * not be in the new set, so perform cleanup now.
136    */
137   for (j = 0; j < num_wds; j++)
138     {
139       if (wds[j] != -1)
140         {
141           inotify_rm_watch (inotify_fd, wds[j]);
142           dbus_free (dirs[j]);
143           wds[j] = -1;
144           dirs[j] = NULL;
145         }
146     }
147
148   for (i = 0; new_dirs[i]; i++)
149     {
150       if (new_wds[i] == -1)
151         {
152           /* FIXME - less lame error handling for failing to add a watch; we may need to sleep. */
153           wd = inotify_add_watch (inotify_fd, new_dirs[i], IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
154           if (wd < 0)
155             {
156               /* Not all service directories need to exist. */
157               if (errno != ENOENT)
158                 {
159                   _dbus_warn ("Cannot setup inotify for '%s'; error '%s'\n", new_dirs[i], _dbus_strerror (errno));
160                   goto out;
161                 }
162               else
163                 {
164                   new_wds[i] = -1;
165                   new_dirs[i] = NULL;
166                   continue;
167                 }
168             }
169           new_wds[i] = wd;
170           new_dirs[i] = _dbus_strdup (new_dirs[i]);
171           if (!new_dirs[i])
172             {
173               /* FIXME have less lame handling for OOM, we just silently fail to
174                * watch.  (In reality though, the whole OOM handling in dbus is stupid
175                * but we won't go into that in this comment =) )
176                */
177               inotify_rm_watch (inotify_fd, wd);
178               new_wds[i] = -1;
179             }
180         }
181     }
182
183   num_wds = i;
184
185   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
186     {
187       wds[i] = new_wds[i];
188       dirs[i] = new_dirs[i];
189     }
190
191  out:;
192 }
193
194 #include <stdio.h>
195 static void
196 _shutdown_inotify (void *data)
197 {
198   DBusList *empty = NULL;
199
200   if (inotify_fd == -1)
201     return;
202
203   _set_watched_dirs_internal (&empty);
204
205   if (watch != NULL)
206     {
207       _dbus_loop_remove_watch (loop, watch);
208       _dbus_watch_invalidate (watch);
209       _dbus_watch_unref (watch);
210       _dbus_loop_unref (loop);
211     }
212   watch = NULL;
213   loop = NULL;
214
215   close (inotify_fd);
216   inotify_fd = -1;
217 }
218
219 static int
220 _init_inotify (BusContext *context)
221 {
222   int ret = 0;
223
224   if (inotify_fd == -1)
225     {
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
230        */
231       if (inotify_fd < 0)
232         inotify_fd = inotify_init ();
233 #else
234       inotify_fd = inotify_init ();
235 #endif
236       if (inotify_fd <= 0)
237         {
238           _dbus_warn ("Cannot initialize inotify\n");
239           goto out;
240         }
241       loop = bus_context_get_loop (context);
242       _dbus_loop_ref (loop);
243
244       watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
245                                _handle_inotify_watch, NULL, NULL);
246
247       if (watch == NULL)
248         {
249           _dbus_warn ("Unable to create inotify watch\n");
250           goto out;
251         }
252
253       if (!_dbus_loop_add_watch (loop, watch))
254         {
255           _dbus_warn ("Unable to add reload watch to main loop");
256           _dbus_watch_unref (watch);
257           watch = NULL;
258           goto out;
259         }
260
261       if (!_dbus_register_shutdown_func (_shutdown_inotify, NULL))
262       {
263           _dbus_warn ("Unable to register shutdown func");
264           _dbus_watch_unref (watch);
265           watch = NULL;
266           goto out;
267       }
268     }
269
270   ret = 1;
271
272 out:
273   return ret;
274 }
275
276 void
277 bus_set_watched_dirs (BusContext *context, DBusList **directories)
278 {
279   if (!_init_inotify (context))
280     return;
281
282   _set_watched_dirs_internal (directories);
283 }