#include <fnmatch.h>
#include <getopt.h>
#include <linux/fs.h>
-#include <poll.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
{ .fd = poll_fd, .events = POLLIN },
{ .fd = STDOUT_FILENO },
};
-
- struct timespec ts;
usec_t timeout;
int r;
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");
#include <endian.h>
#include <netdb.h>
-#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#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"
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);
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;
}
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
-#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
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);
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) {
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);
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;
#include <errno.h>
#include <fcntl.h>
-#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "barrier.h"
#include "fd-util.h"
+#include "io-util.h"
#include "macro.h"
/**
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) {
#include <errno.h>
#include <getopt.h>
-#include <poll.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#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"
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)
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 },
{ .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;
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
-#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/prctl.h>
}
for (;;) {
- int timeout = -1;
+ usec_t timeout = USEC_INFINITY;
r = process_password_files();
if (r < 0) {
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);