diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-03-03 04:11:45 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-03-04 05:06:48 +0900 |
commit | d9e2af0ae8b0fb4eff4a06447cde99002712dd3e (patch) | |
tree | 27565b953767b44f1ba4ec535ebed6c96738ef1e | |
parent | c4febde9d0f2faf13aff3a5285c8fdff22073dc6 (diff) | |
download | systemd-d9e2af0ae8b0fb4eff4a06447cde99002712dd3e.tar.gz |
tree-wide: use ppoll_usec()
-rw-r--r-- | src/journal/journalctl.c | 20 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/sd-bus.c | 18 | ||||
-rw-r--r-- | src/shared/ask-password-api.c | 107 | ||||
-rw-r--r-- | src/shared/barrier.c | 14 | ||||
-rw-r--r-- | src/stdio-bridge/stdio-bridge.c | 33 | ||||
-rw-r--r-- | src/tty-ask-password-agent/tty-ask-password-agent.c | 18 |
6 files changed, 59 insertions, 151 deletions
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 823cf0a25f..6b06320d78 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -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"); diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 6b1f25cc02..d593002712 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -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; } diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index bd33bdd2c5..7137eb3130 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -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; diff --git a/src/shared/barrier.c b/src/shared/barrier.c index 271bba8cde..2864c1b8f9 100644 --- a/src/shared/barrier.c +++ b/src/shared/barrier.c @@ -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) { diff --git a/src/stdio-bridge/stdio-bridge.c b/src/stdio-bridge/stdio-bridge.c index 5d8b514874..217bd97ea5 100644 --- a/src/stdio-bridge/stdio-bridge.c +++ b/src/stdio-bridge/stdio-bridge.c @@ -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; diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index ee66a3c312..5ee82c708b 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -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); |