]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use ppoll_usec() 18840/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 2 Mar 2021 19:11:45 +0000 (04:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 3 Mar 2021 20:06:48 +0000 (05:06 +0900)
src/journal/journalctl.c
src/libsystemd/sd-bus/sd-bus.c
src/shared/ask-password-api.c
src/shared/barrier.c
src/stdio-bridge/stdio-bridge.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index 823cf0a25f42e3a2b581e518d0026a3265732b3e..6b06320d78285fd53119cd3fc2be9225a0e26be3 100644 (file)
@@ -5,7 +5,6 @@
 #include <fnmatch.h>
 #include <getopt.h>
 #include <linux/fs.h>
-#include <poll.h>
 #include <signal.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -2095,8 +2094,6 @@ static int wait_for_change(sd_journal *j, int poll_fd) {
                 { .fd = poll_fd, .events = POLLIN },
                 { .fd = STDOUT_FILENO },
         };
-
-        struct timespec ts;
         usec_t timeout;
         int r;
 
@@ -2110,21 +2107,16 @@ static int wait_for_change(sd_journal *j, int poll_fd) {
         if (r < 0)
                 return log_error_errno(r, "Failed to determine journal waiting time: %m");
 
-        if (ppoll(pollfds, ELEMENTSOF(pollfds),
-                  timeout == USEC_INFINITY ? NULL : timespec_store(&ts, timeout), NULL) < 0) {
-                if (errno == EINTR)
-                        return 0;
-
-                return log_error_errno(errno, "Couldn't wait for journal event: %m");
-        }
+        r = ppoll_usec(pollfds, ELEMENTSOF(pollfds), timeout);
+        if (r == -EINTR)
+                return 0;
+        if (r < 0)
+                return log_error_errno(r, "Couldn't wait for journal event: %m");
 
-        if (pollfds[1].revents & (POLLHUP|POLLERR|POLLNVAL)) /* STDOUT has been closed? */
+        if (pollfds[1].revents & (POLLHUP|POLLERR)) /* STDOUT has been closed? */
                 return log_debug_errno(SYNTHETIC_ERRNO(ECANCELED),
                                        "Standard output has been closed.");
 
-        if (pollfds[0].revents & POLLNVAL)
-                return log_debug_errno(SYNTHETIC_ERRNO(EBADF), "Change fd closed?");
-
         r = sd_journal_process(j);
         if (r < 0)
                 return log_error_errno(r, "Failed to process journal events: %m");
index 6b1f25cc0286a75866f005c5502eab01ff743312..d593002712998310d023c8b318d2a26830e2c11f 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <endian.h>
 #include <netdb.h>
-#include <poll.h>
 #include <pthread.h>
 #include <signal.h>
 #include <stdlib.h>
@@ -32,6 +31,7 @@
 #include "fd-util.h"
 #include "hexdecoct.h"
 #include "hostname-util.h"
+#include "io-util.h"
 #include "macro.h"
 #include "memory-util.h"
 #include "missing_syscall.h"
@@ -3255,9 +3255,8 @@ _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_messa
 
 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
         struct pollfd p[2] = {};
-        int r, n;
-        struct timespec ts;
         usec_t m = USEC_INFINITY;
+        int r, n;
 
         assert(bus);
 
@@ -3312,16 +3311,9 @@ static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
         if (timeout_usec != (uint64_t) -1 && (m == USEC_INFINITY || timeout_usec < m))
                 m = timeout_usec;
 
-        r = ppoll(p, n, m == USEC_INFINITY ? NULL : timespec_store(&ts, m), NULL);
-        if (r < 0)
-                return -errno;
-        if (r == 0)
-                return 0;
-
-        if (p[0].revents & POLLNVAL)
-                return -EBADF;
-        if (n >= 2 && (p[1].revents & POLLNVAL))
-                return -EBADF;
+        r = ppoll_usec(p, n, m);
+        if (r <= 0)
+                return r;
 
         return 1;
 }
index bd33bdd2c5245d96bf0432603fbc7c6762784d7d..7137eb31306bfb9705ff3ab4205323570ed7f5a1 100644 (file)
@@ -4,7 +4,6 @@
 #include <fcntl.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <poll.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -276,44 +275,28 @@ int ask_password_plymouth(
         pollfd[POLL_INOTIFY].events = POLLIN;
 
         for (;;) {
-                int sleep_for = -1, j;
+                usec_t timeout;
 
-                if (until > 0) {
-                        usec_t y;
-
-                        y = now(CLOCK_MONOTONIC);
-
-                        if (y > until) {
-                                r = -ETIME;
-                                goto finish;
-                        }
-
-                        sleep_for = (int) ((until - y) / USEC_PER_MSEC);
-                }
+                if (until > 0)
+                        timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
+                else
+                        timeout = USEC_INFINITY;
 
                 if (flag_file && access(flag_file, F_OK) < 0) {
                         r = -errno;
                         goto finish;
                 }
 
-                j = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for);
-                if (j < 0) {
-                        if (errno == EINTR)
-                                continue;
-
-                        r = -errno;
+                r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout);
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
                         goto finish;
-                } else if (j == 0) {
+                if (r == 0) {
                         r = -ETIME;
                         goto finish;
                 }
 
-                if (pollfd[POLL_SOCKET].revents & POLLNVAL ||
-                    (notify >= 0 && pollfd[POLL_INOTIFY].revents & POLLNVAL)) {
-                        r = -EBADF;
-                        goto finish;
-                }
-
                 if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0)
                         (void) flush_fd(notify);
 
@@ -513,21 +496,13 @@ int ask_password_tty(
 
         for (;;) {
                 _cleanup_(erase_char) char c;
-                int sleep_for = -1, k;
+                usec_t timeout;
                 ssize_t n;
 
-                if (until > 0) {
-                        usec_t y;
-
-                        y = now(CLOCK_MONOTONIC);
-
-                        if (y > until) {
-                                r = -ETIME;
-                                goto finish;
-                        }
-
-                        sleep_for = (int) DIV_ROUND_UP(until - y, USEC_PER_MSEC);
-                }
+                if (until > 0)
+                        timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
+                else
+                        timeout = USEC_INFINITY;
 
                 if (flag_file)
                         if (access(flag_file, F_OK) < 0) {
@@ -535,24 +510,16 @@ int ask_password_tty(
                                 goto finish;
                         }
 
-                k = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for);
-                if (k < 0) {
-                        if (errno == EINTR)
-                                continue;
-
-                        r = -errno;
+                r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout);
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
                         goto finish;
-                } else if (k == 0) {
+                if (r == 0) {
                         r = -ETIME;
                         goto finish;
                 }
 
-                if ((pollfd[POLL_TTY].revents & POLLNVAL) ||
-                    (notify >= 0 && (pollfd[POLL_INOTIFY].revents & POLLNVAL))) {
-                        r = -EBADF;
-                        goto finish;
-                }
-
                 if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0 && keyname) {
                         (void) flush_fd(notify);
 
@@ -875,38 +842,24 @@ int ask_password_agent(
                 char passphrase[LINE_MAX+1];
                 struct iovec iovec;
                 struct ucred *ucred;
+                usec_t timeout;
                 ssize_t n;
-                int k;
-                usec_t t;
-
-                t = now(CLOCK_MONOTONIC);
-
-                if (until > 0 && until <= t) {
-                        r = -ETIME;
-                        goto finish;
-                }
 
-                k = poll(pollfd, notify >= 0 ? _FD_MAX : _FD_MAX - 1, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1);
-                if (k < 0) {
-                        if (errno == EINTR)
-                                continue;
+                if (until > 0)
+                        timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
+                else
+                        timeout = USEC_INFINITY;
 
-                        r = -errno;
+                r = ppoll_usec(pollfd, notify >= 0 ? _FD_MAX : _FD_MAX - 1, timeout);
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
                         goto finish;
-                }
-
-                if (k <= 0) {
+                if (r == 0) {
                         r = -ETIME;
                         goto finish;
                 }
 
-                if (pollfd[FD_SOCKET].revents & POLLNVAL ||
-                    pollfd[FD_SIGNAL].revents & POLLNVAL ||
-                    (notify >= 0 && pollfd[FD_INOTIFY].revents & POLLNVAL)) {
-                        r = -EBADF;
-                        goto finish;
-                }
-
                 if (pollfd[FD_SIGNAL].revents & POLLIN) {
                         r = -EINTR;
                         goto finish;
index 271bba8cde94fca51fb8aa1cc44b8e03f5f053b8..2864c1b8f9026051c9e8f9d952874ce98cdd939f 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#include <poll.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -12,6 +11,7 @@
 
 #include "barrier.h"
 #include "fd-util.h"
+#include "io-util.h"
 #include "macro.h"
 
 /**
@@ -219,14 +219,10 @@ static bool barrier_read(Barrier *b, int64_t comp) {
                 uint64_t buf;
                 int r;
 
-                r = poll(pfd, ELEMENTSOF(pfd), -1);
-                if (r < 0) {
-                        if (IN_SET(errno, EAGAIN, EINTR))
-                                continue;
-                        goto error;
-                }
-                if (pfd[0].revents & POLLNVAL ||
-                    pfd[1].revents & POLLNVAL)
+                r = ppoll_usec(pfd, ELEMENTSOF(pfd), USEC_INFINITY);
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
                         goto error;
 
                 if (pfd[1].revents) {
index 5d8b514874b7ca7895d652be8f77c5b005b71a56..217bd97ea5e4049acab607b1104b9a4b7ab12d66 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <errno.h>
 #include <getopt.h>
-#include <poll.h>
 #include <stddef.h>
 #include <string.h>
 #include <unistd.h>
@@ -15,6 +14,7 @@
 #include "bus-internal.h"
 #include "bus-util.h"
 #include "errno-util.h"
+#include "io-util.h"
 #include "log.h"
 #include "main-func.h"
 #include "util.h"
@@ -181,8 +181,9 @@ static int run(int argc, char *argv[]) {
         for (;;) {
                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
                 int events_a, events_b, fd;
-                uint64_t timeout_a, timeout_b, t;
-                struct timespec _ts, *ts;
+                usec_t timeout_a, timeout_b, t;
+
+                assert_cc(sizeof(usec_t) == sizeof(uint64_t));
 
                 r = sd_bus_process(a, &m);
                 if (r < 0)
@@ -235,23 +236,7 @@ static int run(int argc, char *argv[]) {
                 if (r < 0)
                         return log_error_errno(r, "Failed to get timeout: %m");
 
-                t = timeout_a;
-                if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a))
-                        t = timeout_b;
-
-                if (t == (uint64_t) -1)
-                        ts = NULL;
-                else {
-                        usec_t nw;
-
-                        nw = now(CLOCK_MONOTONIC);
-                        if (t > nw)
-                                t -= nw;
-                        else
-                                t = 0;
-
-                        ts = timespec_store(&_ts, t);
-                }
+                t = usec_sub_unsigned(MIN(timeout_a, timeout_b), now(CLOCK_MONOTONIC));
 
                 struct pollfd p[3] = {
                         { .fd = fd,            .events = events_a           },
@@ -259,13 +244,9 @@ static int run(int argc, char *argv[]) {
                         { .fd = STDOUT_FILENO, .events = events_b & POLLOUT },
                 };
 
-                r = ppoll(p, ELEMENTSOF(p), ts, NULL);
+                r = ppoll_usec(p, ELEMENTSOF(p), t);
                 if (r < 0)
-                        return log_error_errno(errno, "ppoll() failed: %m");
-                if (p[0].revents & POLLNVAL ||
-                    p[1].revents & POLLNVAL ||
-                    p[2].revents & POLLNVAL)
-                        return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Invalid file descriptor to poll on?");
+                        return log_error_errno(r, "ppoll() failed: %m");
         }
 
         return 0;
index ee66a3c312a1e4ee3d1768b408302e3cacdf313f..5ee82c708b709ab36f93579b3fce3d86729ede10 100644 (file)
@@ -6,7 +6,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#include <poll.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <sys/prctl.h>
@@ -366,7 +365,7 @@ static int process_and_watch_password_files(bool watch) {
         }
 
         for (;;) {
-                int timeout = -1;
+                usec_t timeout = USEC_INFINITY;
 
                 r = process_password_files();
                 if (r < 0) {
@@ -385,16 +384,11 @@ static int process_and_watch_password_files(bool watch) {
                 if (!watch)
                         break;
 
-                if (poll(pollfd, _FD_MAX, timeout) < 0) {
-                        if (errno == EINTR)
-                                continue;
-
-                        return -errno;
-                }
-
-                if (pollfd[FD_SIGNAL].revents & POLLNVAL ||
-                    pollfd[FD_INOTIFY].revents & POLLNVAL)
-                        return -EBADF;
+                r = ppoll_usec(pollfd, _FD_MAX, timeout);
+                if (r == -EINTR)
+                        continue;
+                if (r < 0)
+                        return r;
 
                 if (pollfd[FD_INOTIFY].revents != 0)
                         (void) flush_fd(notify);