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