}
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
- uint8_t *p;
+ uint8_t *p = buf;
ssize_t n = 0;
assert(fd >= 0);
assert(buf);
- p = buf;
-
while (nbytes > 0) {
ssize_t k;
- if ((k = read(fd, p, nbytes)) <= 0) {
-
- if (k < 0 && errno == EINTR)
- continue;
-
- if (k < 0 && errno == EAGAIN && do_poll) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLIN,
- };
-
- if (poll(&pollfd, 1, -1) < 0) {
- if (errno == EINTR)
- continue;
+ k = read(fd, p, nbytes);
+ if (k < 0 && errno == EINTR)
+ continue;
- return n > 0 ? n : -errno;
- }
+ if (k < 0 && errno == EAGAIN && do_poll) {
- /* We knowingly ignore the revents value here,
- * and expect that any error/EOF is reported
- * via read()/write()
- */
+ /* We knowingly ignore any return value here,
+ * and expect that any error/EOF is reported
+ * via read() */
- continue;
- }
+ fd_wait_for_event(fd, POLLIN, (usec_t) -1);
+ continue;
+ }
+ if (k <= 0)
return n > 0 ? n : (k < 0 ? -errno : 0);
- }
p += k;
nbytes -= k;
}
ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
- const uint8_t *p;
+ const uint8_t *p = buf;
ssize_t n = 0;
assert(fd >= 0);
assert(buf);
- p = buf;
-
while (nbytes > 0) {
ssize_t k;
k = write(fd, p, nbytes);
- if (k <= 0) {
-
- if (k < 0 && errno == EINTR)
- continue;
-
- if (k < 0 && errno == EAGAIN && do_poll) {
- struct pollfd pollfd = {
- .fd = fd,
- .events = POLLOUT,
- };
-
- if (poll(&pollfd, 1, -1) < 0) {
- if (errno == EINTR)
- continue;
+ if (k < 0 && errno == EINTR)
+ continue;
- return n > 0 ? n : -errno;
- }
+ if (k < 0 && errno == EAGAIN && do_poll) {
- /* We knowingly ignore the revents value here,
- * and expect that any error/EOF is reported
- * via read()/write()
- */
+ /* We knowingly ignore any return value here,
+ * and expect that any error/EOF is reported
+ * via write() */
- continue;
- }
+ fd_wait_for_event(fd, POLLOUT, (usec_t) -1);
+ continue;
+ }
+ if (k <= 0)
return n > 0 ? n : (k < 0 ? -errno : 0);
- }
p += k;
nbytes -= k;