Skip to content

Commit c4febde

Browse files
committed
io-util: introduce ppoll_usec() helper function
1 parent 1d61d70 commit c4febde

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/basic/io-util.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <errno.h>
44
#include <limits.h>
5-
#include <poll.h>
65
#include <stdio.h>
76
#include <unistd.h>
87

@@ -159,24 +158,42 @@ int pipe_eof(int fd) {
159158
return !!(r & POLLHUP);
160159
}
161160

162-
int fd_wait_for_event(int fd, int event, usec_t t) {
163-
164-
struct pollfd pollfd = {
165-
.fd = fd,
166-
.events = event,
167-
};
168-
161+
int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
169162
struct timespec ts;
170163
int r;
171164

172-
r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL);
165+
assert(fds || nfds == 0);
166+
167+
if (nfds == 0)
168+
return 0;
169+
170+
r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : timespec_store(&ts, timeout), NULL);
173171
if (r < 0)
174172
return -errno;
175173
if (r == 0)
176174
return 0;
177175

178-
if (pollfd.revents & POLLNVAL)
179-
return -EBADF;
176+
for (size_t i = 0, n = r; i < nfds && n > 0; i++) {
177+
if (fds[i].revents == 0)
178+
continue;
179+
if (fds[i].revents & POLLNVAL)
180+
return -EBADF;
181+
n--;
182+
}
183+
184+
return r;
185+
}
186+
187+
int fd_wait_for_event(int fd, int event, usec_t timeout) {
188+
struct pollfd pollfd = {
189+
.fd = fd,
190+
.events = event,
191+
};
192+
int r;
193+
194+
r = ppoll_usec(&pollfd, 1, timeout);
195+
if (r <= 0)
196+
return r;
180197

181198
return pollfd.revents;
182199
}

src/basic/io-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: LGPL-2.1-or-later */
22
#pragma once
33

4+
#include <poll.h>
45
#include <stdbool.h>
56
#include <stddef.h>
67
#include <stdint.h>
@@ -18,6 +19,7 @@ int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
1819

1920
int pipe_eof(int fd);
2021

22+
int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout);
2123
int fd_wait_for_event(int fd, int event, usec_t timeout);
2224

2325
ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);

0 commit comments

Comments
 (0)