tree-wide: port to fd_wait_for_event()

Prompted by the discussion on #16110, let's migrate more code to
fd_wait_for_event().

This only leaves 7 places where we call into poll()/poll() directly in
our entire codebase. (one of which is fd_wait_for_event() itself)
This commit is contained in:
Lennart Poettering
2020-06-10 20:06:10 +02:00
committed by Daan De Meyer
parent 24bd74ae03
commit 0f2d351f79
11 changed files with 63 additions and 163 deletions
+6 -23
View File
@@ -11,10 +11,6 @@
#include "time-util.h"
int flush_fd(int fd) {
struct pollfd pollfd = {
.fd = fd,
.events = POLLIN,
};
int count = 0;
/* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything
@@ -27,22 +23,18 @@ int flush_fd(int fd) {
ssize_t l;
int r;
r = poll(&pollfd, 1, 0);
r = fd_wait_for_event(fd, POLLIN, 0);
if (r < 0) {
if (errno == EINTR)
if (r == -EINTR)
continue;
return -errno;
return r;
}
if (r == 0)
return count;
if (pollfd.revents & POLLNVAL)
return -EBADF;
l = read(fd, buf, sizeof(buf));
if (l < 0) {
if (errno == EINTR)
continue;
@@ -158,24 +150,15 @@ int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
}
int pipe_eof(int fd) {
struct pollfd pollfd = {
.fd = fd,
.events = POLLIN|POLLHUP,
};
int r;
r = poll(&pollfd, 1, 0);
r = fd_wait_for_event(fd, POLLIN, 0);
if (r < 0)
return -errno;
return r;
if (r == 0)
return 0;
if (pollfd.revents & POLLNVAL)
return -EBADF;
return pollfd.revents & POLLHUP;
return !!(r & POLLHUP);
}
int fd_wait_for_event(int fd, int event, usec_t t) {
+4 -10
View File
@@ -21,6 +21,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "io-util.h"
#include "log.h"
#include "macro.h"
#include "memory-util.h"
@@ -968,10 +969,6 @@ fallback:
int flush_accept(int fd) {
struct pollfd pollfd = {
.fd = fd,
.events = POLLIN,
};
int r, b;
socklen_t l = sizeof(b);
@@ -992,19 +989,16 @@ int flush_accept(int fd) {
for (unsigned iteration = 0;; iteration++) {
int cfd;
r = poll(&pollfd, 1, 0);
r = fd_wait_for_event(fd, POLLIN, 0);
if (r < 0) {
if (errno == EINTR)
if (r == -EINTR)
continue;
return -errno;
return r;
}
if (r == 0)
return 0;
if (pollfd.revents & POLLNVAL)
return -EBADF;
if (iteration >= MAX_FLUSH_ITERATIONS)
return log_debug_errno(SYNTHETIC_ERRNO(EBUSY),
"Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations.");