From: cee1 <fykc...@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 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel