summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorDaniel Wagner <daniel.wagner@bmw-carit.de>2012-11-02 17:26:12 +0100
committerDaniel Wagner <daniel.wagner@bmw-carit.de>2012-11-23 13:45:10 +0100
commitdb2a311c9f4c77fec8f7baca6cd075ebf4806832 (patch)
tree6d909e45f9e29ee18d98ce4362e749affcf187d2 /src/config.c
parentb49360d9485fc5d003e167f18333ac2e59b8573b (diff)
downloadconnman-db2a311c9f4c77fec8f7baca6cd075ebf4806832.tar.gz
connman-db2a311c9f4c77fec8f7baca6cd075ebf4806832.tar.bz2
connman-db2a311c9f4c77fec8f7baca6cd075ebf4806832.zip
inotify: Move inotify code into its own file
The inotify code will be used by the core (config.c) and the session policy plugin. We introduce a new API for file modifcation notifcation. We move the factored out code part from the last patch into a new file and also change the inotify code so that it allows to monitor not only STORAGEDIR. When registering a new observer, the callee has to tell which directory should be watched. inotify.c will group the observers together.
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c122
1 files changed, 2 insertions, 120 deletions
diff --git a/src/config.c b/src/config.c
index 16fed8da..6a5b12cc 100644
--- a/src/config.c
+++ b/src/config.c
@@ -66,10 +66,6 @@ struct connman_config {
static GHashTable *config_table = NULL;
static GSList *protected_services = NULL;
-static int inotify_wd = -1;
-
-static GIOChannel *inotify_channel = NULL;
-static uint inotify_watch = 0;
static connman_bool_t cleanup = FALSE;
#define INTERNAL_CONFIG_PREFIX "__internal"
@@ -613,120 +609,6 @@ static void config_notify_handler(struct inotify_event *event,
g_hash_table_remove(config_table, ident);
}
-static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
- gpointer user_data)
-{
- char buffer[256];
- char *next_event;
- gsize bytes_read;
- GIOStatus status;
-
- if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
- inotify_watch = 0;
- return FALSE;
- }
-
- status = g_io_channel_read_chars(channel, buffer,
- sizeof(buffer) -1, &bytes_read, NULL);
-
- switch (status) {
- case G_IO_STATUS_NORMAL:
- break;
- case G_IO_STATUS_AGAIN:
- return TRUE;
- default:
- connman_error("Reading from inotify channel failed");
- inotify_watch = 0;
- return FALSE;
- }
-
- next_event = buffer;
-
- while (bytes_read > 0) {
- struct inotify_event *event;
- gchar *ident;
- gsize len;
-
- event = (struct inotify_event *) next_event;
- if (event->len)
- ident = next_event + sizeof(struct inotify_event);
- else
- ident = NULL;
-
- len = sizeof(struct inotify_event) + event->len;
-
- /* check if inotify_event block fit */
- if (len > bytes_read)
- break;
-
- next_event += len;
- bytes_read -= len;
-
- config_notify_handler(event, ident);
- }
-
- return TRUE;
-}
-
-static int create_watch(void)
-{
- int fd;
-
- fd = inotify_init();
- if (fd < 0)
- return -EIO;
-
- inotify_wd = inotify_add_watch(fd, STORAGEDIR,
- IN_MODIFY | IN_CREATE | IN_DELETE);
- if (inotify_wd < 0) {
- connman_error("Creation of STORAGEDIR watch failed");
- close(fd);
- return -EIO;
- }
-
- inotify_channel = g_io_channel_unix_new(fd);
- if (inotify_channel == NULL) {
- connman_error("Creation of inotify channel failed");
- inotify_rm_watch(fd, inotify_wd);
- inotify_wd = 0;
-
- close(fd);
- return -EIO;
- }
-
- g_io_channel_set_close_on_unref(inotify_channel, TRUE);
- g_io_channel_set_encoding(inotify_channel, NULL, NULL);
- g_io_channel_set_buffered(inotify_channel, FALSE);
-
- inotify_watch = g_io_add_watch(inotify_channel,
- G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
- inotify_data, NULL);
-
- return 0;
-}
-
-static void remove_watch(void)
-{
- int fd;
-
- if (inotify_channel == NULL)
- return;
-
- if (inotify_watch > 0) {
- g_source_remove(inotify_watch);
- inotify_watch = 0;
- }
-
- fd = g_io_channel_unix_get_fd(inotify_channel);
-
- if (inotify_wd >= 0) {
- inotify_rm_watch(fd, inotify_wd);
- inotify_wd = 0;
- }
-
- g_io_channel_unref(inotify_channel);
-}
-
int __connman_config_init(void)
{
DBG("");
@@ -734,7 +616,7 @@ int __connman_config_init(void)
config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, unregister_config);
- create_watch();
+ connman_inotify_register(STORAGEDIR, config_notify_handler);
return read_configs();
}
@@ -745,7 +627,7 @@ void __connman_config_cleanup(void)
cleanup = TRUE;
- remove_watch();
+ connman_inotify_unregister(STORAGEDIR, config_notify_handler);
g_hash_table_destroy(config_table);
config_table = NULL;