diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-03-03 03:55:22 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-03-04 05:06:43 +0900 |
commit | c4febde9d0f2faf13aff3a5285c8fdff22073dc6 (patch) | |
tree | bfade676cff26b3ec300985c4cc4fe084c83bd55 /src/basic/io-util.c | |
parent | 1d61d70abb7bfef15d8aca140468c95388a42b6b (diff) | |
download | systemd-c4febde9d0f2faf13aff3a5285c8fdff22073dc6.tar.gz |
io-util: introduce ppoll_usec() helper function
Diffstat (limited to 'src/basic/io-util.c')
-rw-r--r-- | src/basic/io-util.c | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/basic/io-util.c b/src/basic/io-util.c index 8ea350dcc3..f0a66da9bf 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -2,7 +2,6 @@ #include <errno.h> #include <limits.h> -#include <poll.h> #include <stdio.h> #include <unistd.h> @@ -159,24 +158,42 @@ int pipe_eof(int fd) { return !!(r & POLLHUP); } -int fd_wait_for_event(int fd, int event, usec_t t) { - - struct pollfd pollfd = { - .fd = fd, - .events = event, - }; - +int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) { struct timespec ts; int r; - r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL); + assert(fds || nfds == 0); + + if (nfds == 0) + return 0; + + r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : timespec_store(&ts, timeout), NULL); if (r < 0) return -errno; if (r == 0) return 0; - if (pollfd.revents & POLLNVAL) - return -EBADF; + for (size_t i = 0, n = r; i < nfds && n > 0; i++) { + if (fds[i].revents == 0) + continue; + if (fds[i].revents & POLLNVAL) + return -EBADF; + n--; + } + + return r; +} + +int fd_wait_for_event(int fd, int event, usec_t timeout) { + struct pollfd pollfd = { + .fd = fd, + .events = event, + }; + int r; + + r = ppoll_usec(&pollfd, 1, timeout); + if (r <= 0) + return r; return pollfd.revents; } |