From d0c25a3f23285a1ed17a82d22459a012e53dbae3 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 17 Jul 2013 16:03:23 +0200 Subject: [PATCH] watcher: read multiple notifications if available Use non-blocking I/O on the read end of the notify pipe. This also makes sure the read does not block should select() signal data while there is none. --- src/libstrongswan/processing/watcher.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/libstrongswan/processing/watcher.c b/src/libstrongswan/processing/watcher.c index ee70533962..da7ba759a9 100644 --- a/src/libstrongswan/processing/watcher.c +++ b/src/libstrongswan/processing/watcher.c @@ -25,6 +25,7 @@ #include #include #include +#include typedef struct private_watcher_t private_watcher_t; @@ -286,7 +287,7 @@ static job_requeue_t watch(private_watcher_t *this) if (this->notify[0] != -1 && FD_ISSET(this->notify[0], &rd)) { DBG2(DBG_JOB, "watcher got notification, rebuilding"); - ignore_result(read(this->notify[0], buf, sizeof(buf))); + while (read(this->notify[0], buf, sizeof(buf)) > 0); return JOB_REQUEUE_DIRECT; } @@ -430,6 +431,7 @@ METHOD(watcher_t, destroy, void, watcher_t *watcher_create() { private_watcher_t *this; + int flags; INIT(this, .public = { @@ -444,7 +446,18 @@ watcher_t *watcher_create() .notify[1] = -1, ); - if (pipe(this->notify) != 0) + if (pipe(this->notify) == 0) + { + /* use non-blocking I/O on read-end of notify pipe */ + flags = fcntl(this->notify[0], F_GETFL); + if (flags == -1 || + fcntl(this->notify[0], F_SETFL, flags | O_NONBLOCK) == -1) + { + DBG1(DBG_LIB, "setting watcher notify pipe read-end non-blocking " + "failed: %s", strerror(errno)); + } + } + else { DBG1(DBG_LIB, "creating watcher notify pipe failed: %s", strerror(errno)); -- 2.47.2