5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2012-2014 BMW Car IT GmbH.
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 {
38 unsigned int refcount;
47 static void cleanup_inotify(gpointer user_data);
49 static void connman_inotify_ref(struct connman_inotify *i)
51 __sync_fetch_and_add(&i->refcount, 1);
54 static void connman_inotify_unref(gpointer data)
56 struct connman_inotify *i = data;
58 if (__sync_fetch_and_sub(&i->refcount, 1) != 1)
61 cleanup_inotify(data);
64 static GHashTable *inotify_hash;
66 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
69 struct connman_inotify *inotify = user_data;
70 char buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
76 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
81 status = g_io_channel_read_chars(channel, buffer,
82 sizeof(buffer), &bytes_read, NULL);
85 case G_IO_STATUS_NORMAL:
87 case G_IO_STATUS_AGAIN:
90 connman_error("Reading from inotify channel failed");
97 connman_inotify_ref(inotify);
99 while (bytes_read > 0) {
100 struct inotify_event *event;
104 event = (struct inotify_event *) next_event;
106 ident = next_event + sizeof(struct inotify_event);
110 len = sizeof(struct inotify_event) + event->len;
112 /* check if inotify_event block fit */
113 if (len > bytes_read)
119 for (list = inotify->list; list; list = list->next) {
120 inotify_event_cb callback = list->data;
122 (*callback)(event, ident);
126 connman_inotify_unref(inotify);
131 static int create_watch(const char *path, struct connman_inotify *inotify)
135 DBG("Add directory watch for %s", path);
141 inotify->wd = inotify_add_watch(fd, path,
142 IN_MODIFY | IN_CREATE | IN_DELETE |
143 IN_MOVED_TO | IN_MOVED_FROM);
144 if (inotify->wd < 0) {
145 connman_error("Creation of %s watch failed", path);
150 inotify->channel = g_io_channel_unix_new(fd);
151 if (!inotify->channel) {
152 connman_error("Creation of inotify channel failed");
153 inotify_rm_watch(fd, inotify->wd);
160 g_io_channel_set_close_on_unref(inotify->channel, TRUE);
161 g_io_channel_set_encoding(inotify->channel, NULL, NULL);
162 g_io_channel_set_buffered(inotify->channel, FALSE);
164 inotify->watch = g_io_add_watch(inotify->channel,
165 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
166 inotify_data, inotify);
171 static void remove_watch(struct connman_inotify *inotify)
175 if (!inotify->channel)
178 if (inotify->watch > 0)
179 g_source_remove(inotify->watch);
181 fd = g_io_channel_unix_get_fd(inotify->channel);
183 if (inotify->wd >= 0)
184 inotify_rm_watch(fd, inotify->wd);
186 g_io_channel_unref(inotify->channel);
189 int connman_inotify_register(const char *path, inotify_event_cb callback)
191 struct connman_inotify *inotify;
197 inotify = g_hash_table_lookup(inotify_hash, path);
201 inotify = g_try_new0(struct connman_inotify, 1);
205 inotify->refcount = 1;
208 err = create_watch(path, inotify);
214 g_hash_table_replace(inotify_hash, g_strdup(path), inotify);
217 inotify->list = g_slist_prepend(inotify->list, callback);
222 static void cleanup_inotify(gpointer user_data)
224 struct connman_inotify *inotify = user_data;
226 g_slist_free(inotify->list);
228 remove_watch(inotify);
232 void connman_inotify_unregister(const char *path, inotify_event_cb callback)
234 struct connman_inotify *inotify;
236 inotify = g_hash_table_lookup(inotify_hash, path);
240 inotify->list = g_slist_remove(inotify->list, callback);
244 g_hash_table_remove(inotify_hash, path);
247 int __connman_inotify_init(void)
251 inotify_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
252 g_free, connman_inotify_unref);
256 void __connman_inotify_cleanup(void)
260 g_hash_table_destroy(inotify_hash);