From f988ca8ef5d957f0f34a71c3e3f4194d8af81b46 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Mon, 24 Jan 2011 16:57:37 -0500 Subject: [PATCH] Move the GSource attach out to the generic watch code, and avoid blocking reads GLib seems to call the read function sometimes even tho the PollFD's revents field is empty. So don't read from the fd if this happens. --- obt/watch.c | 2 ++ obt/watch_inotify.c | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/obt/watch.c b/obt/watch.c index 6e1971ac..487655cd 100644 --- a/obt/watch.c +++ b/obt/watch.c @@ -88,6 +88,8 @@ ObtWatch* obt_watch_new() w->targets_by_path = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, (GDestroyNotify)target_free); w->source = source; + + g_source_attach(source, NULL); } return w; } diff --git a/obt/watch_inotify.c b/obt/watch_inotify.c index f9a5649a..2aff0e39 100644 --- a/obt/watch_inotify.c +++ b/obt/watch_inotify.c @@ -121,7 +121,6 @@ GSource* watch_sys_create_source(ObtWatchNotifyFunc notify) g_str_hash, g_str_equal, NULL, (GDestroyNotify)target_free); ino_source->pfd = (GPollFD){ fd, G_IO_IN, G_IO_IN }; g_source_add_poll(source, &ino_source->pfd); - g_source_attach(source, NULL); } return source; } @@ -158,7 +157,9 @@ static gboolean source_read(GSource *source, GSourceFunc cb, gpointer data) state = READING_EVENT; len = event_len = name_len = 0; - while (TRUE) { + /* sometimes we can end up here even tho no events have been reported by + the kernel. blame glib? but we'll block if we read in that case. */ + while (ino_source->pfd.revents) { if (pos == len || !len || event_len) { /* refill the buffer */ @@ -169,10 +170,16 @@ static gboolean source_read(GSource *source, GSourceFunc cb, gpointer data) len = read(ino_source->pfd.fd, &buffer[pos], BUF_LEN-pos); - if (len < 0 && errno != EINTR) { - gchar *s = strerror(errno); - g_warning("Failed to read from inotify: %s", s); - return FALSE; /* won't read any more */ + if (len < 0) { + if (errno != EINTR) { + gchar *s = strerror(errno); + g_warning("Failed to read from inotify: %s", s); + return FALSE; /* won't read any more */ + } + else { + g_warning("Interrupted reading from inotify"); + return TRUE; + } } if (len == 0) { g_debug("Done reading from inotify"); -- 2.34.1