]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
io-util: introduce ppoll_usec() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 2 Mar 2021 18:55:22 +0000 (03:55 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 3 Mar 2021 20:06:43 +0000 (05:06 +0900)
src/basic/io-util.c
src/basic/io-util.h

index 8ea350dcc396155f3960b8c04a1d066f00770b29..f0a66da9bfc983c24373a072157d8a64d2dbbc8f 100644 (file)
@@ -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;
 }
index d817714b050741c3c23b26bc618beaba76024a4b..0cd26d4c2c1b5ec66577c226b576f268e9a7d1a1 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 #pragma once
 
+#include <poll.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -18,6 +19,7 @@ int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
 
 int pipe_eof(int fd);
 
+int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout);
 int fd_wait_for_event(int fd, int event, usec_t timeout);
 
 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);