[systemd-devel] [PATCH 1/2] util: add fd_pollin() and fd_pollout()

chenj at lemote.com chenj at lemote.com
Mon Dec 19 02:23:46 PST 2011


From: cee1 <fykcee1 at gmail.com>

fd_pollin() will do a poll on specified fd, it will return 1, if fd has data
to read. On error, it returns -errno.

fd_pollout() will do a poll on specified fd, it will return 1, if write to fd
will no block. On error, it returns -errno.
---
 src/util.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/util.h |    3 +++
 2 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/src/util.c b/src/util.c
index da71e4d..a46897a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2796,6 +2796,50 @@ int close_pipe(int p[]) {
         return a < 0 ? a : b;
 }
 
+int fd_pollin(int fd, bool do_wait) {
+        struct pollfd pollfd = {0,};
+        int r;
+
+        pollfd.fd = fd;
+        pollfd.events = POLLIN;
+
+redo:
+        if ((r = poll(&pollfd, 1, do_wait ? -1:0) < 0)) {
+                if (errno == EINTR)
+                        goto redo;
+
+                r = -errno;
+                return r;
+        }
+
+        if (pollfd.revents != POLLIN)
+                return -EIO;
+
+        return r;
+}
+
+int fd_pollout(int fd, bool do_wait) {
+        struct pollfd pollfd = {0,};
+        int r;
+
+        pollfd.fd = fd;
+        pollfd.events = POLLOUT;
+
+redo:
+        if ((r = poll(&pollfd, 1, do_wait ? -1:0) < 0)) {
+                if (errno == EINTR)
+                        goto redo;
+
+                r = -errno;
+                return r;
+        }
+
+        if (pollfd.revents != POLLOUT)
+                return -EIO;
+
+        return r;
+}
+
 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
         uint8_t *p;
         ssize_t n = 0;
diff --git a/src/util.h b/src/util.h
index a71a297..23e51b9 100644
--- a/src/util.h
+++ b/src/util.h
@@ -340,6 +340,9 @@ int sigaction_many(const struct sigaction *sa, ...);
 int close_pipe(int p[]);
 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
 
+int fd_pollin(int fd, bool wait);
+int fd_pollout(int fd, bool wait);
+
 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
 ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
 
-- 
1.7.7.4



More information about the systemd-devel mailing list