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
+5 -10
View File
@@ -14,6 +14,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "hostname-util.h"
#include "io-util.h"
#include "macro.h"
#include "memory-util.h"
#include "path-util.h"
@@ -297,7 +298,7 @@ int utmp_put_runlevel(int runlevel, int previous) {
return write_entry_both(&store);
}
#define TIMEOUT_MSEC 50
#define TIMEOUT_USEC (50 * USEC_PER_MSEC)
static int write_to_terminal(const char *tty, const char *message) {
_cleanup_close_ int fd = -1;
@@ -315,14 +316,10 @@ static int write_to_terminal(const char *tty, const char *message) {
p = message;
left = strlen(message);
end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
end = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
while (left > 0) {
ssize_t n;
struct pollfd pollfd = {
.fd = fd,
.events = POLLOUT,
};
usec_t t;
int k;
@@ -331,13 +328,11 @@ static int write_to_terminal(const char *tty, const char *message) {
if (t >= end)
return -ETIME;
k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
k = fd_wait_for_event(fd, POLLOUT, end - t);
if (k < 0)
return -errno;
return k;
if (k == 0)
return -ETIME;
if (pollfd.revents & POLLNVAL)
return -EBADF;
n = write(fd, p, left);
if (n < 0) {
+8 -28
View File
@@ -6,6 +6,7 @@
#include "errno-util.h"
#include "fd-util.h"
#include "hashmap.h"
#include "io-util.h"
#include "list.h"
#include "process-util.h"
#include "set.h"
@@ -1003,8 +1004,6 @@ static void handle_revents(Varlink *v, int revents) {
}
int varlink_wait(Varlink *v, usec_t timeout) {
struct timespec ts;
struct pollfd pfd;
int r, fd, events;
usec_t t;
@@ -1038,23 +1037,13 @@ int varlink_wait(Varlink *v, usec_t timeout) {
if (events < 0)
return events;
pfd = (struct pollfd) {
.fd = fd,
.events = events,
};
r = ppoll(&pfd, 1,
t == USEC_INFINITY ? NULL : timespec_store(&ts, t),
NULL);
r = fd_wait_for_event(fd, events, t);
if (r < 0)
return -errno;
return r;
if (r == 0)
return 0;
if (pfd.revents & POLLNVAL)
return -EBADF;
handle_revents(v, pfd.revents);
handle_revents(v, r);
return 1;
}
@@ -1123,8 +1112,6 @@ int varlink_flush(Varlink *v) {
return -ENOTCONN;
for (;;) {
struct pollfd pfd;
if (v->output_buffer_size == 0)
break;
if (v->write_disconnected)
@@ -1138,20 +1125,13 @@ int varlink_flush(Varlink *v) {
continue;
}
pfd = (struct pollfd) {
.fd = v->fd,
.events = POLLOUT,
};
r = poll(&pfd, 1, -1);
r = fd_wait_for_event(v->fd, POLLOUT, USEC_INFINITY);
if (r < 0)
return -errno;
return r;
assert(r > 0);
if (pfd.revents & POLLNVAL)
return -EBADF;
assert(r != 0);
handle_revents(v, pfd.revents);
handle_revents(v, r);
}
return ret;