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