add inotify support (FDO Bz#13268)
[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 #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
42 /* use a static array to avoid handling OOM */
43 static int wds[MAX_DIRS_TO_WATCH];
44 static int num_wds = 0;
45 static int inotify_fd = -1;
46 static DBusWatch *watch = NULL;
47 static DBusLoop *loop = NULL;
48
49 static dbus_bool_t
50 _inotify_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
51 {
52   return dbus_watch_handle (watch, condition);
53 }
54
55 static dbus_bool_t
56 _handle_inotify_watch (DBusWatch *watch, unsigned int flags, void *data)
57 {
58   struct inotify_event ev;
59   size_t res;
60   pid_t pid;
61
62   res = read (inotify_fd, &ev, sizeof(ev));
63
64   if (res > 0)
65     {
66       pid = getpid ();
67       _dbus_verbose ("Sending SIGHUP signal on reception of a inotify event\n");
68       (void) kill (pid, SIGHUP);
69     }
70   else if (res < 0 && errno == EBADF)
71     {
72       if (watch != NULL)
73         {
74           _dbus_loop_remove_watch (loop, watch, _inotify_watch_callback, NULL);
75           _dbus_watch_unref (watch);
76           watch = NULL;
77         }
78       pid = getpid ();
79       _dbus_verbose ("Sending SIGHUP signal since inotify fd has been closed\n");
80       (void) kill (pid, SIGHUP);
81     }
82
83   return TRUE;
84 }
85 void
86 bus_watch_directory (const char *dir, BusContext *context)
87 {
88   int wd;
89
90   _dbus_assert (dir != NULL);
91
92   if (inotify_fd == -1) {
93      inotify_fd = inotify_init ();
94      if (inotify_fd <= 0) {
95       _dbus_warn ("Cannot initialize inotify\n");
96       goto out;
97      } 
98      loop = bus_context_get_loop (context);
99
100      watch = _dbus_watch_new (inotify_fd, DBUS_WATCH_READABLE, TRUE,
101                               _handle_inotify_watch, NULL, NULL);
102
103         if (watch == NULL)
104           {
105             _dbus_warn ("Unable to create inotify watch\n");
106             goto out;
107           }
108
109         if (!_dbus_loop_add_watch (loop, watch, _inotify_watch_callback,
110                                    NULL, NULL))
111           {
112             _dbus_warn ("Unable to add reload watch to main loop");
113             _dbus_watch_unref (watch);
114             watch = NULL;
115             goto out;
116           }
117   }
118
119
120   if (num_wds >= MAX_DIRS_TO_WATCH )
121     {
122       _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH);
123       goto out;
124     }
125
126   wd = inotify_add_watch (inotify_fd, dir, IN_MODIFY);
127   if (wd < 0)
128     {
129       _dbus_warn ("Cannot setup inotify for '%s'; error '%s'\n", dir, _dbus_strerror (errno));
130       goto out;
131     }
132
133   wds[num_wds++] = wd;
134   _dbus_verbose ("Added watch on config directory '%s'\n", dir);
135
136  out:
137   ;
138 }
139
140 void 
141 bus_drop_all_directory_watches (void)
142 {
143   int i;
144  
145   _dbus_verbose ("Dropping all watches on config directories\n");
146  
147   for (i = 0; i < num_wds; i++)
148     {
149       if (inotify_rm_watch(inotify_fd, wds[i]) != 0)
150         {
151           _dbus_verbose ("Error closing fd %d for config directory watch\n", wds[i]);
152         }
153     }
154   
155   num_wds = 0;
156 }