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