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