5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2012 BMW Car IT GmbH. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/inotify.h>
33 #include <connman/storage.h>
37 struct connman_inotify {
45 static GHashTable *inotify_hash;
47 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
50 struct connman_inotify *inotify = user_data;
57 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
62 status = g_io_channel_read_chars(channel, buffer,
63 sizeof(buffer) -1, &bytes_read, NULL);
66 case G_IO_STATUS_NORMAL:
68 case G_IO_STATUS_AGAIN:
71 connman_error("Reading from inotify channel failed");
78 while (bytes_read > 0) {
79 struct inotify_event *event;
83 event = (struct inotify_event *) next_event;
85 ident = next_event + sizeof(struct inotify_event);
89 len = sizeof(struct inotify_event) + event->len;
91 /* check if inotify_event block fit */
98 for (list = inotify->list; list != NULL; list = list->next) {
99 inotify_event_cb callback = list->data;
101 (*callback)(event, ident);
108 static int create_watch(const char *path, struct connman_inotify *inotify)
112 DBG("Add directory watch for %s", path);
118 inotify->wd = inotify_add_watch(fd, path,
119 IN_MODIFY | IN_CREATE | IN_DELETE |
120 IN_MOVED_TO | IN_MOVED_FROM);
121 if (inotify->wd < 0) {
122 connman_error("Creation of %s watch failed", path);
127 inotify->channel = g_io_channel_unix_new(fd);
128 if (inotify->channel == NULL) {
129 connman_error("Creation of inotify channel failed");
130 inotify_rm_watch(fd, inotify->wd);
137 g_io_channel_set_close_on_unref(inotify->channel, TRUE);
138 g_io_channel_set_encoding(inotify->channel, NULL, NULL);
139 g_io_channel_set_buffered(inotify->channel, FALSE);
141 inotify->watch = g_io_add_watch(inotify->channel,
142 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
143 inotify_data, inotify);
148 static void remove_watch(struct connman_inotify *inotify)
152 if (inotify->channel == NULL)
155 if (inotify->watch > 0)
156 g_source_remove(inotify->watch);
158 fd = g_io_channel_unix_get_fd(inotify->channel);
160 if (inotify->wd >= 0)
161 inotify_rm_watch(fd, inotify->wd);
163 g_io_channel_unref(inotify->channel);
166 int connman_inotify_register(const char *path, inotify_event_cb callback)
168 struct connman_inotify *inotify;
171 if (callback == NULL)
174 inotify = g_hash_table_lookup(inotify_hash, path);
178 inotify = g_try_new0(struct connman_inotify, 1);
184 err = create_watch(path, inotify);
190 g_hash_table_replace(inotify_hash, g_strdup(path), inotify);
193 inotify->list = g_slist_prepend(inotify->list, callback);
198 static void cleanup_inotify(gpointer user_data)
200 struct connman_inotify *inotify = user_data;
202 g_slist_free(inotify->list);
204 remove_watch(inotify);
208 void connman_inotify_unregister(const char *path, inotify_event_cb callback)
210 struct connman_inotify *inotify;
212 inotify = g_hash_table_lookup(inotify_hash, path);
216 inotify->list = g_slist_remove(inotify->list, callback);
217 if (inotify->list != NULL)
220 g_hash_table_remove(inotify_hash, path);
223 int __connman_inotify_init(void)
227 inotify_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
228 g_free, cleanup_inotify);
232 void __connman_inotify_cleanup(void)
236 g_hash_table_destroy(inotify_hash);