| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <poll.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
#include <sys/epoll.h> /* IWYU pragma: keep */ |
| 8 |
#include <time.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include "errno-util.h" |
| 12 |
#include "fiber-ops.h" |
| 13 |
#include "io-util.h" |
| 14 |
#include "time-util.h" |
| 15 |
|
| 16 |
uint32_t poll_events_to_epoll(uint32_t events) { |
| 17 |
return (events & POLLIN ? EPOLLIN : 0) | |
| 18 |
(events & POLLOUT ? EPOLLOUT : 0) | |
| 19 |
(events & POLLPRI ? EPOLLPRI : 0) | |
| 20 |
(events & POLLERR ? EPOLLERR : 0) | |
| 21 |
(events & POLLHUP ? EPOLLHUP : 0) | |
| 22 |
(events & POLLRDHUP ? EPOLLRDHUP : 0); |
| 23 |
} |
| 24 |
|
| 25 |
uint32_t epoll_events_to_poll(uint32_t events) { |
| 26 |
return (events & EPOLLIN ? POLLIN : 0) | |
| 27 |
(events & EPOLLOUT ? POLLOUT : 0) | |
| 28 |
(events & EPOLLPRI ? POLLPRI : 0) | |
| 29 |
(events & EPOLLERR ? POLLERR : 0) | |
| 30 |
(events & EPOLLHUP ? POLLHUP : 0) | |
| 31 |
(events & EPOLLRDHUP ? POLLRDHUP : 0); |
| 32 |
} |
| 33 |
|
| 34 |
int flush_fd(int fd) { |
| 35 |
int count = 0; |
| 36 |
|
| 37 |
/* Read from the specified file descriptor, until POLLIN is not set anymore, throwing away everything |
| 38 |
* read. Note that some file descriptors (notable IP sockets) will trigger POLLIN even when no data can be read |
| 39 |
* (due to IP packet checksum mismatches), hence this function is only safe to be non-blocking if the fd used |
| 40 |
* was set to non-blocking too. */ |
| 41 |
|
| 42 |
for (;;) { |
| 43 |
char buf[LINE_MAX]; |
| 44 |
ssize_t l; |
| 45 |
int r; |
| 46 |
|
| 47 |
r = fd_wait_for_event(fd, POLLIN, 0); |
| 48 |
if (r == -EINTR) |
| 49 |
continue; |
| 50 |
if (r < 0) |
| 51 |
return r; |
| 52 |
if (r == 0) |
| 53 |
return count; |
| 54 |
|
| 55 |
l = read(fd, buf, sizeof(buf)); |
| 56 |
if (l < 0) { |
| 57 |
if (errno == EINTR) |
| 58 |
continue; |
| 59 |
if (errno == EAGAIN) |
| 60 |
return count; |
| 61 |
|
| 62 |
return -errno; |
| 63 |
} else if (l == 0) |
| 64 |
return count; |
| 65 |
|
| 66 |
if (l > INT_MAX-count) /* On overflow terminate */ |
| 67 |
return INT_MAX; |
| 68 |
|
| 69 |
count += (int) l; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { |
| 74 |
uint8_t *p = ASSERT_PTR(buf); |
| 75 |
ssize_t n = 0; |
| 76 |
|
| 77 |
assert(fd >= 0); |
| 78 |
|
| 79 |
/* If called with nbytes == 0, let's call read() at least once, to validate the operation */ |
| 80 |
|
| 81 |
if (nbytes > (size_t) SSIZE_MAX) |
| 82 |
return -EINVAL; |
| 83 |
|
| 84 |
/* do_poll == false means "don't wait, return what we have if EAGAIN". If the fd is already |
| 85 |
* non-blocking, read() can't block the thread, so the non-fiber path satisfies that semantic |
| 86 |
* correctly even from a fiber. Only use the fiber path when the fd is blocking (where read() |
| 87 |
* would otherwise block the entire event loop). */ |
| 88 |
int flags = 0; |
| 89 |
if (fiber_ops_is_set() && !do_poll) { |
| 90 |
flags = fcntl(fd, F_GETFL); |
| 91 |
if (flags < 0) |
| 92 |
return -errno; |
| 93 |
} |
| 94 |
|
| 95 |
do { |
| 96 |
ssize_t k; |
| 97 |
|
| 98 |
if (fiber_ops_is_set() && (do_poll || !FLAGS_SET(flags, O_NONBLOCK))) { |
| 99 |
/* On a fiber the read op suspends on EAGAIN until data is available, so we don't |
| 100 |
* need a separate poll step or the do_poll knob. */ |
| 101 |
k = fiber_ops_read(fd, p, nbytes); |
| 102 |
if (k < 0) |
| 103 |
return n > 0 ? n : k; |
| 104 |
} else { |
| 105 |
k = read(fd, p, nbytes); |
| 106 |
if (k < 0) { |
| 107 |
if (errno == EINTR) |
| 108 |
continue; |
| 109 |
|
| 110 |
if (errno == EAGAIN && do_poll) { |
| 111 |
|
| 112 |
/* We knowingly ignore any return value here, |
| 113 |
* and expect that any error/EOF is reported |
| 114 |
* via read() */ |
| 115 |
|
| 116 |
(void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY); |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
return n > 0 ? n : -errno; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (k == 0) |
| 125 |
return n; |
| 126 |
|
| 127 |
assert((size_t) k <= nbytes); |
| 128 |
assert(k <= SSIZE_MAX - n); |
| 129 |
|
| 130 |
p += k; |
| 131 |
nbytes -= k; |
| 132 |
n += k; |
| 133 |
} while (nbytes > 0); |
| 134 |
|
| 135 |
return n; |
| 136 |
} |
| 137 |
|
| 138 |
int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) { |
| 139 |
ssize_t n; |
| 140 |
|
| 141 |
n = loop_read(fd, buf, nbytes, do_poll); |
| 142 |
if (n < 0) |
| 143 |
return (int) n; |
| 144 |
if ((size_t) n != nbytes) |
| 145 |
return -EIO; |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
int loop_write_full(int fd, const void *buf, size_t nbytes, usec_t timeout) { |
| 151 |
const uint8_t *p; |
| 152 |
usec_t end; |
| 153 |
int r; |
| 154 |
|
| 155 |
assert(fd >= 0); |
| 156 |
assert(buf || nbytes == 0); |
| 157 |
|
| 158 |
if (nbytes == 0) { |
| 159 |
static const dummy_t dummy[0]; |
| 160 |
assert_cc(sizeof(dummy) == 0); |
| 161 |
p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */ |
| 162 |
} else { |
| 163 |
if (nbytes == SIZE_MAX) |
| 164 |
nbytes = strlen(buf); |
| 165 |
else if (_unlikely_(nbytes > (size_t) SSIZE_MAX)) |
| 166 |
return -EINVAL; |
| 167 |
|
| 168 |
p = buf; |
| 169 |
} |
| 170 |
|
| 171 |
/* timeout == 0 means "don't wait, return -EAGAIN if not ready". If the fd is already |
| 172 |
* non-blocking, write() can't block the thread, so the non-fiber path satisfies that |
| 173 |
* semantic correctly even from a fiber. Only use the fiber path when the fd is blocking |
| 174 |
* (where write() would otherwise block the entire event loop). */ |
| 175 |
int flags = 0; |
| 176 |
if (fiber_ops_is_set() && timeout == 0) { |
| 177 |
flags = fcntl(fd, F_GETFL); |
| 178 |
if (flags < 0) |
| 179 |
return -errno; |
| 180 |
} |
| 181 |
|
| 182 |
if (fiber_ops_is_set() && !FLAGS_SET(flags, O_NONBLOCK)) { |
| 183 |
/* On a fiber the write op suspends on EAGAIN until the fd is writable; honor the |
| 184 |
* caller's timeout via a deadline scope. */ |
| 185 |
FIBER_OPS_TIMEOUT(timestamp_is_set(timeout) ? timeout : USEC_INFINITY); |
| 186 |
|
| 187 |
while (nbytes > 0) { |
| 188 |
ssize_t k = fiber_ops_write(fd, p, nbytes); |
| 189 |
if (k < 0) |
| 190 |
return (int) k; |
| 191 |
if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */ |
| 192 |
return -EIO; |
| 193 |
|
| 194 |
assert((size_t) k <= nbytes); |
| 195 |
p += k; |
| 196 |
nbytes -= k; |
| 197 |
} |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
/* When timeout is 0 or USEC_INFINITY this is not used. But we initialize it to a sensible value. */ |
| 203 |
end = timestamp_is_set(timeout) ? usec_add(now(CLOCK_MONOTONIC), timeout) : USEC_INFINITY; |
| 204 |
|
| 205 |
do { |
| 206 |
ssize_t k; |
| 207 |
|
| 208 |
k = write(fd, p, nbytes); |
| 209 |
if (k < 0) { |
| 210 |
if (errno == EINTR) |
| 211 |
continue; |
| 212 |
|
| 213 |
if (errno != EAGAIN || timeout == 0) |
| 214 |
return -errno; |
| 215 |
|
| 216 |
usec_t wait_for; |
| 217 |
|
| 218 |
if (timeout == USEC_INFINITY) |
| 219 |
wait_for = USEC_INFINITY; |
| 220 |
else { |
| 221 |
usec_t t = now(CLOCK_MONOTONIC); |
| 222 |
if (t >= end) |
| 223 |
return -ETIME; |
| 224 |
|
| 225 |
wait_for = usec_sub_unsigned(end, t); |
| 226 |
} |
| 227 |
|
| 228 |
r = fd_wait_for_event(fd, POLLOUT, wait_for); |
| 229 |
if (timeout == USEC_INFINITY || ERRNO_IS_NEG_TRANSIENT(r)) |
| 230 |
/* If timeout == USEC_INFINITY we knowingly ignore any return value |
| 231 |
* here, and expect that any error/EOF is reported via write() */ |
| 232 |
continue; |
| 233 |
if (r < 0) |
| 234 |
return r; |
| 235 |
if (r == 0) |
| 236 |
return -ETIME; |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */ |
| 241 |
return -EIO; |
| 242 |
|
| 243 |
assert((size_t) k <= nbytes); |
| 244 |
|
| 245 |
p += k; |
| 246 |
nbytes -= k; |
| 247 |
} while (nbytes > 0); |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
int pipe_eof(int fd) { |
| 253 |
int r; |
| 254 |
|
| 255 |
r = fd_wait_for_event(fd, POLLIN, 0); |
| 256 |
if (r <= 0) |
| 257 |
return r; |
| 258 |
|
| 259 |
return !!(r & POLLHUP); |
| 260 |
} |
| 261 |
|
| 262 |
int ppoll_usec_full(struct pollfd *fds, size_t n_fds, usec_t timeout, const sigset_t *ss) { |
| 263 |
int r; |
| 264 |
|
| 265 |
assert(fds || n_fds == 0); |
| 266 |
|
| 267 |
/* This is a wrapper around ppoll() that does primarily two things: |
| 268 |
* |
| 269 |
* ✅ Takes a usec_t instead of a struct timespec |
| 270 |
* |
| 271 |
* ✅ Guarantees that if an invalid fd is specified we return EBADF (i.e. converts POLLNVAL to |
| 272 |
* EBADF). This is done because EBADF is a programming error usually, and hence should bubble up |
| 273 |
* as error, and not be eaten up as non-error POLLNVAL event. |
| 274 |
* |
| 275 |
* ⚠️ ⚠️ ⚠️ Note that this function does not add any special handling for EINTR. Don't forget |
| 276 |
* poll()/ppoll() will return with EINTR on any received signal always, there is no automatic |
| 277 |
* restarting via SA_RESTART available. Thus, typically you want to handle EINTR not as an error, |
| 278 |
* but just as reason to restart things, under the assumption you use a more appropriate mechanism |
| 279 |
* to handle signals, such as signalfd() or signal handlers. ⚠️ ⚠️ ⚠️ |
| 280 |
*/ |
| 281 |
|
| 282 |
if (n_fds == 0 && timeout == 0) |
| 283 |
return 0; |
| 284 |
|
| 285 |
r = fiber_ops_ppoll(fds, n_fds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), ss); |
| 286 |
if (r <= 0) |
| 287 |
return r; |
| 288 |
|
| 289 |
for (size_t i = 0, n = r; i < n_fds && n > 0; i++) { |
| 290 |
if (fds[i].revents == 0) |
| 291 |
continue; |
| 292 |
if (fds[i].revents & POLLNVAL) |
| 293 |
return -EBADF; |
| 294 |
n--; |
| 295 |
} |
| 296 |
|
| 297 |
return r; |
| 298 |
} |
| 299 |
|
| 300 |
int fd_wait_for_event(int fd, int event, usec_t timeout) { |
| 301 |
struct pollfd pollfd = { |
| 302 |
.fd = fd, |
| 303 |
.events = event, |
| 304 |
}; |
| 305 |
int r; |
| 306 |
|
| 307 |
/* ⚠️ ⚠️ ⚠️ Keep in mind you almost certainly want to handle -EINTR gracefully in the caller, see |
| 308 |
* ppoll_usec() above! ⚠️ ⚠️ ⚠️ */ |
| 309 |
|
| 310 |
r = ppoll_usec(&pollfd, 1, timeout); |
| 311 |
if (r <= 0) |
| 312 |
return r; |
| 313 |
|
| 314 |
return pollfd.revents; |
| 315 |
} |
| 316 |
|
| 317 |
static size_t nul_length(const uint8_t *p, size_t sz) { |
| 318 |
size_t n = 0; |
| 319 |
|
| 320 |
assert(p); |
| 321 |
|
| 322 |
while (sz > 0) { |
| 323 |
if (*p != 0) |
| 324 |
break; |
| 325 |
|
| 326 |
n++; |
| 327 |
p++; |
| 328 |
sz--; |
| 329 |
} |
| 330 |
|
| 331 |
return n; |
| 332 |
} |
| 333 |
|
| 334 |
ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) { |
| 335 |
const uint8_t *q, *w, *e; |
| 336 |
ssize_t l; |
| 337 |
|
| 338 |
q = w = p; |
| 339 |
e = q + sz; |
| 340 |
while (q < e) { |
| 341 |
size_t n; |
| 342 |
|
| 343 |
n = nul_length(q, e - q); |
| 344 |
|
| 345 |
/* If there are more than the specified run length of |
| 346 |
* NUL bytes, or if this is the beginning or the end |
| 347 |
* of the buffer, then seek instead of write */ |
| 348 |
if ((n > run_length) || |
| 349 |
(n > 0 && q == p) || |
| 350 |
(n > 0 && q + n >= e)) { |
| 351 |
if (q > w) { |
| 352 |
l = write(fd, w, q - w); |
| 353 |
if (l < 0) |
| 354 |
return -errno; |
| 355 |
if (l != q -w) |
| 356 |
return -EIO; |
| 357 |
} |
| 358 |
|
| 359 |
if (lseek(fd, n, SEEK_CUR) < 0) |
| 360 |
return -errno; |
| 361 |
|
| 362 |
q += n; |
| 363 |
w = q; |
| 364 |
} else if (n > 0) |
| 365 |
q += n; |
| 366 |
else |
| 367 |
q++; |
| 368 |
} |
| 369 |
|
| 370 |
if (q > w) { |
| 371 |
l = write(fd, w, q - w); |
| 372 |
if (l < 0) |
| 373 |
return -errno; |
| 374 |
if (l != q - w) |
| 375 |
return -EIO; |
| 376 |
} |
| 377 |
|
| 378 |
return q - (const uint8_t*) p; |
| 379 |
} |