12fdbb076768de39bb55030bbe1f53d9eff6e8ae
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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-watch.h>
37 #include "dir-watch.h"
38
39 #define MAX_DIRS_TO_WATCH 128
40 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
41 #define INOTIFY_BUF_LEN (1024 * (INOTIFY_EVENT_SIZE + 16))
42
43 /* use a static array to avoid handling OOM */
44 static int wds[MAX_DIRS_TO_WATCH];
45 static int num_wds = 0;
46 static int inotify_fd = -1;
47 static DBusWatch *watch = NULL;
48 static DBusLoop *loop = NULL;
49
50 static dbus_bool_t
51 _inotify_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
52 {
53   return dbus_watch_handle (watch, condition);
54 }
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   pid_t pid;
63   dbus_bool_t have_change = FALSE;
64
65   ret = read (inotify_fd, buffer, INOTIFY_BUF_LEN);
66   if (ret < 0)
67     _dbus_verbose ("Error reading inotify event: '%s'\n", _dbus_strerror(errno));
68   else if (!ret)
69     _dbus_verbose ("Error reading inotify event: buffer too small\n");
70
71   while (i < ret)
72     {
73       struct inotify_event *ev;
74       pid = _dbus_getpid ();
75
76       ev = (struct inotify_event *) &buffer[i];
77       i += INOTIFY_EVENT_SIZE + ev->len;
78 #ifdef DBUS_ENABLE_VERBOSE_MODE
79       if (ev->len)
80         _dbus_verbose ("event name: '%s'\n", ev->name);
81       _dbus_verbose ("inotify event: wd=%d mask=%u cookie=%u len=%u\n", ev->wd, ev->mask, ev->cookie, ev->len);
82 #endif
83       _dbus_verbose ("Sending SIGHUP signal on reception of a inotify event\n");
84       have_change = TRUE;
85     }
86   if (have_change)
87     (void) kill (pid, SIGHUP);
88
89   return TRUE;
90 }
91
92 void
93 bus_watch_directory (const char *dir, BusContext *context)
94 {
95   int wd;
96
97   _dbus_assert (dir != NULL);
98
99   if (inotify_fd == -1) {
100      inotify_fd = inotify_init ();
101      if (inotify_fd <= 0) {
102       _dbus_warn ("Cannot initialize inotify\n");
103       goto out;
104      } 
105      loop = bus_context_get_loop (context);
106
107      watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
108                               _handle_inotify_watch, NULL, NULL);
109
110         if (watch == NULL)
111           {
112             _dbus_warn ("Unable to create inotify watch\n");
113             goto out;
114           }
115
116         if (!_dbus_loop_add_watch (loop, watch, _inotify_watch_callback,
117                                    NULL, NULL))
118           {
119             _dbus_warn ("Unable to add reload watch to main loop");
120             _dbus_watch_unref (watch);
121             watch = NULL;
122             goto out;
123           }
124   }
125
126   if (num_wds >= MAX_DIRS_TO_WATCH )
127     {
128       _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH);
129       goto out;
130     }
131
132   wd = inotify_add_watch (inotify_fd, dir, IN_CLOSE_WRITE | IN_DELETE | IN_MOVED_TO | IN_MOVED_FROM);
133   if (wd < 0)
134     {
135       _dbus_warn ("Cannot setup inotify for '%s'; error '%s'\n", dir, _dbus_strerror (errno));
136       goto out;
137     }
138
139   wds[num_wds++] = wd;
140   _dbus_verbose ("Added watch on config directory '%s'\n", dir);
141
142  out:
143   ;
144 }
145
146 void 
147 bus_drop_all_directory_watches (void)
148 {
149   int ret;
150
151   if (watch != NULL)
152     {
153       _dbus_loop_remove_watch (loop, watch, _inotify_watch_callback, NULL);
154       _dbus_watch_unref (watch);
155       watch = NULL;
156     }
157
158   _dbus_verbose ("Dropping all watches on config directories\n");
159   ret = close (inotify_fd);
160   if (ret)
161     _dbus_verbose ("Error dropping watches: '%s'\n",  _dbus_strerror(errno));
162
163   num_wds = 0;
164   inotify_fd = -1;
165 }