Merge branch 'my-dbus-1.2'
[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 _inotify_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
54 {
55   return dbus_watch_handle (watch, condition);
56 }
57
58 static dbus_bool_t
59 _handle_inotify_watch (DBusWatch *passed_watch, unsigned int flags, void *data)
60 {
61   char buffer[INOTIFY_BUF_LEN];
62   ssize_t ret = 0;
63   int i = 0;
64   pid_t pid;
65   dbus_bool_t have_change = FALSE;
66
67   ret = read (inotify_fd, buffer, INOTIFY_BUF_LEN);
68   if (ret < 0)
69     _dbus_verbose ("Error reading inotify event: '%s'\n", _dbus_strerror(errno));
70   else if (!ret)
71     _dbus_verbose ("Error reading inotify event: buffer too small\n");
72
73   while (i < ret)
74     {
75       struct inotify_event *ev;
76       pid = _dbus_getpid ();
77
78       ev = (struct inotify_event *) &buffer[i];
79       i += INOTIFY_EVENT_SIZE + ev->len;
80 #ifdef DBUS_ENABLE_VERBOSE_MODE
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 #endif
85       _dbus_verbose ("Sending SIGHUP signal on reception of a inotify event\n");
86       have_change = TRUE;
87     }
88   if (have_change)
89     (void) kill (pid, SIGHUP);
90
91   return TRUE;
92 }
93
94 static int
95 _init_inotify (BusContext *context)
96 {
97   int ret = 0;
98
99   if (inotify_fd == -1) {
100 #ifdef HAVE_INOTIFY_INIT1
101      inotify_fd = inotify_init1 (IN_CLOEXEC);
102 #else
103      inotify_fd = inotify_init ();
104 #endif
105      if (inotify_fd <= 0) {
106       _dbus_warn ("Cannot initialize inotify\n");
107       goto out;
108      } 
109      loop = bus_context_get_loop (context);
110
111      watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
112                               _handle_inotify_watch, NULL, NULL);
113
114      if (watch == NULL)
115        {
116          _dbus_warn ("Unable to create inotify watch\n");
117          goto out;
118        }
119
120      if (!_dbus_loop_add_watch (loop, watch, _inotify_watch_callback,
121                                 NULL, NULL))
122        {
123          _dbus_warn ("Unable to add reload watch to main loop");
124          _dbus_watch_unref (watch);
125          watch = NULL;
126          goto out;
127        }
128   }
129
130   ret = 1;
131
132 out:
133   return ret;
134 }
135
136 void
137 bus_set_watched_dirs (BusContext *context, DBusList **directories)
138 {
139   int new_wds[MAX_DIRS_TO_WATCH];
140   char *new_dirs[MAX_DIRS_TO_WATCH];
141   DBusList *link;
142   int i, j, wd;
143
144   if (!_init_inotify (context))
145     goto out;
146
147   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
148     {
149       new_wds[i] = -1;
150       new_dirs[i] = NULL;
151     }
152
153   i = 0;
154   link = _dbus_list_get_first_link (directories);
155   while (link != NULL)
156     {
157       new_dirs[i++] = (char *)link->data;
158       link = _dbus_list_get_next_link (directories, link);
159     }
160
161   /* Look for directories in both the old and new sets, if
162    * we find one, move its data into the new set.
163    */
164   for (i = 0; new_dirs[i]; i++)
165     {
166       for (j = 0; j < num_wds; j++)
167         {
168           if (dirs[j] && strcmp (new_dirs[i], dirs[j]) == 0)
169             {
170               new_wds[i] = wds[j];
171               new_dirs[i] = dirs[j];
172               wds[j] = -1;
173               dirs[j] = NULL;
174               break;
175             }
176         }
177     }
178
179   /* Any directories we find in "wds" with a nonzero fd must
180    * not be in the new set, so perform cleanup now.
181    */
182   for (j = 0; j < num_wds; j++)
183     {
184       if (wds[j] != -1)
185         {
186           inotify_rm_watch (inotify_fd, wds[j]);
187           dbus_free (dirs[j]);
188           wds[j] = -1;
189           dirs[j] = NULL;
190         }
191     }
192
193   for (i = 0; new_dirs[i]; i++)
194     {
195       if (new_wds[i] == -1)
196         {
197           /* FIXME - less lame error handling for failing to add a watch; we may need to sleep. */
198           wd = inotify_add_watch (inotify_fd, new_dirs[i], IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
199           if (wd < 0)
200             {
201               _dbus_warn ("Cannot setup inotify for '%s'; error '%s'\n", new_dirs[i], _dbus_strerror (errno));
202               goto out;
203             }
204           new_wds[i] = wd;
205           new_dirs[i] = _dbus_strdup (new_dirs[i]);
206           if (!new_dirs[i])
207             {
208               /* FIXME have less lame handling for OOM, we just silently fail to
209                * watch.  (In reality though, the whole OOM handling in dbus is stupid
210                * but we won't go into that in this comment =) )
211                */
212               inotify_rm_watch (inotify_fd, wd);
213               new_wds[i] = -1;
214             }
215         }
216     }
217
218   num_wds = i;
219
220   for (i = 0; i < MAX_DIRS_TO_WATCH; i++)
221     {
222       wds[i] = new_wds[i];
223       dirs[i] = new_dirs[i];
224     }
225
226  out:;
227 }