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