| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <poll.h> |
| 4 |
#include <sys/epoll.h> /* IWYU pragma: keep */ |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <sys/uio.h> |
| 7 |
#include <time.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "sd-event.h" |
| 11 |
#include "sd-future.h" |
| 12 |
|
| 13 |
#include "alloc-util.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "event-future.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "io-util.h" |
| 18 |
#include "time-util.h" |
| 19 |
|
| 20 |
typedef ssize_t (*FiberIOFunc)(int fd, void *args); |
| 21 |
|
| 22 |
static ssize_t fiber_io_operation( |
| 23 |
int fd, |
| 24 |
uint32_t events, |
| 25 |
FiberIOFunc func, |
| 26 |
void *args) { |
| 27 |
_cleanup_(nonblock_resetp) int reset_fd = -EBADF; |
| 28 |
int r; |
| 29 |
|
| 30 |
assert(fd >= 0); |
| 31 |
assert(func); |
| 32 |
|
| 33 |
if (!sd_fiber_is_running()) |
| 34 |
return func(fd, args); |
| 35 |
|
| 36 |
sd_event *e = sd_fiber_get_event(); |
| 37 |
assert(e); |
| 38 |
|
| 39 |
r = fd_nonblock(fd, true); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
if (r > 0) |
| 43 |
reset_fd = fd; |
| 44 |
|
| 45 |
ssize_t n = func(fd, args); |
| 46 |
if (n >= 0 || !ERRNO_IS_NEG_TRANSIENT(n)) |
| 47 |
return n; |
| 48 |
|
| 49 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *io = NULL; |
| 50 |
r = future_new_io(e, fd, events, &io); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
r = sd_fiber_suspend(); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
return func(fd, args); |
| 59 |
} |
| 60 |
|
| 61 |
typedef struct ReadArgs { |
| 62 |
void *buf; |
| 63 |
size_t count; |
| 64 |
} ReadArgs; |
| 65 |
|
| 66 |
static ssize_t read_callback(int fd, void *args) { |
| 67 |
ReadArgs *a = ASSERT_PTR(args); |
| 68 |
ssize_t n; |
| 69 |
|
| 70 |
n = read(fd, a->buf, a->count); |
| 71 |
return n >= 0 ? n : -errno; |
| 72 |
} |
| 73 |
|
| 74 |
ssize_t sd_fiber_read(int fd, void *buf, size_t count) { |
| 75 |
assert_return(fd >= 0, -EBADF); |
| 76 |
assert_return(buf || count == 0, -EINVAL); |
| 77 |
|
| 78 |
return fiber_io_operation(fd, EPOLLIN, read_callback, &(ReadArgs) { |
| 79 |
.buf = buf, |
| 80 |
.count = count, |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
typedef struct WriteArgs { |
| 85 |
const void *buf; |
| 86 |
size_t count; |
| 87 |
} WriteArgs; |
| 88 |
|
| 89 |
static ssize_t write_callback(int fd, void *args) { |
| 90 |
WriteArgs *a = ASSERT_PTR(args); |
| 91 |
ssize_t n; |
| 92 |
|
| 93 |
n = write(fd, a->buf, a->count); |
| 94 |
return n >= 0 ? n : -errno; |
| 95 |
} |
| 96 |
|
| 97 |
ssize_t sd_fiber_write(int fd, const void *buf, size_t count) { |
| 98 |
assert_return(fd >= 0, -EBADF); |
| 99 |
assert_return(buf || count == 0, -EINVAL); |
| 100 |
|
| 101 |
return fiber_io_operation(fd, EPOLLOUT, write_callback, &(WriteArgs) { |
| 102 |
.buf = buf, |
| 103 |
.count = count, |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
typedef struct ReadvArgs { |
| 108 |
const struct iovec *iov; |
| 109 |
int iovcnt; |
| 110 |
} ReadvArgs; |
| 111 |
|
| 112 |
static ssize_t readv_callback(int fd, void *args) { |
| 113 |
ReadvArgs *a = ASSERT_PTR(args); |
| 114 |
ssize_t n; |
| 115 |
|
| 116 |
n = readv(fd, a->iov, a->iovcnt); |
| 117 |
return n >= 0 ? n : -errno; |
| 118 |
} |
| 119 |
|
| 120 |
ssize_t sd_fiber_readv(int fd, const struct iovec *iov, int iovcnt) { |
| 121 |
assert_return(fd >= 0, -EBADF); |
| 122 |
assert_return(iov || iovcnt == 0, -EINVAL); |
| 123 |
|
| 124 |
return fiber_io_operation(fd, EPOLLIN, readv_callback, &(ReadvArgs) { |
| 125 |
.iov = iov, |
| 126 |
.iovcnt = iovcnt, |
| 127 |
}); |
| 128 |
} |
| 129 |
|
| 130 |
typedef struct WritevArgs { |
| 131 |
const struct iovec *iov; |
| 132 |
int iovcnt; |
| 133 |
} WritevArgs; |
| 134 |
|
| 135 |
static ssize_t writev_callback(int fd, void *args) { |
| 136 |
WritevArgs *a = ASSERT_PTR(args); |
| 137 |
ssize_t n; |
| 138 |
|
| 139 |
n = writev(fd, a->iov, a->iovcnt); |
| 140 |
return n >= 0 ? n : -errno; |
| 141 |
} |
| 142 |
|
| 143 |
ssize_t sd_fiber_writev(int fd, const struct iovec *iov, int iovcnt) { |
| 144 |
assert_return(fd >= 0, -EBADF); |
| 145 |
assert_return(iov || iovcnt == 0, -EINVAL); |
| 146 |
|
| 147 |
return fiber_io_operation(fd, EPOLLOUT, writev_callback, &(WritevArgs) { |
| 148 |
.iov = iov, |
| 149 |
.iovcnt = iovcnt, |
| 150 |
}); |
| 151 |
} |
| 152 |
|
| 153 |
typedef struct RecvArgs { |
| 154 |
void *buf; |
| 155 |
size_t len; |
| 156 |
int flags; |
| 157 |
} RecvArgs; |
| 158 |
|
| 159 |
static ssize_t recv_callback(int fd, void *args) { |
| 160 |
RecvArgs *a = ASSERT_PTR(args); |
| 161 |
ssize_t n; |
| 162 |
|
| 163 |
n = recv(fd, a->buf, a->len, a->flags); |
| 164 |
return n >= 0 ? n : -errno; |
| 165 |
} |
| 166 |
|
| 167 |
ssize_t sd_fiber_recv(int sockfd, void *buf, size_t len, int flags) { |
| 168 |
assert_return(sockfd >= 0, -EBADF); |
| 169 |
assert_return(buf || len == 0, -EINVAL); |
| 170 |
|
| 171 |
return fiber_io_operation(sockfd, EPOLLIN, recv_callback, &(RecvArgs) { |
| 172 |
.buf = buf, |
| 173 |
.len = len, |
| 174 |
.flags = flags, |
| 175 |
}); |
| 176 |
} |
| 177 |
|
| 178 |
typedef struct SendArgs { |
| 179 |
const void *buf; |
| 180 |
size_t len; |
| 181 |
int flags; |
| 182 |
} SendArgs; |
| 183 |
|
| 184 |
static ssize_t send_callback(int fd, void *args) { |
| 185 |
SendArgs *a = ASSERT_PTR(args); |
| 186 |
ssize_t n; |
| 187 |
|
| 188 |
n = send(fd, a->buf, a->len, a->flags); |
| 189 |
return n >= 0 ? n : -errno; |
| 190 |
} |
| 191 |
|
| 192 |
ssize_t sd_fiber_send(int sockfd, const void *buf, size_t len, int flags) { |
| 193 |
assert_return(sockfd >= 0, -EBADF); |
| 194 |
assert_return(buf || len == 0, -EINVAL); |
| 195 |
|
| 196 |
return fiber_io_operation(sockfd, EPOLLOUT, send_callback, &(SendArgs) { |
| 197 |
.buf = buf, |
| 198 |
.len = len, |
| 199 |
.flags = flags, |
| 200 |
}); |
| 201 |
} |
| 202 |
|
| 203 |
int sd_fiber_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { |
| 204 |
_cleanup_(nonblock_resetp) int reset_fd = -EBADF; |
| 205 |
int r; |
| 206 |
|
| 207 |
assert_return(sockfd >= 0, -EBADF); |
| 208 |
assert_return(addr, -EINVAL); |
| 209 |
|
| 210 |
if (!sd_fiber_is_running()) |
| 211 |
return RET_NERRNO(connect(sockfd, addr, addrlen)); |
| 212 |
|
| 213 |
sd_event *e = sd_fiber_get_event(); |
| 214 |
assert(e); |
| 215 |
|
| 216 |
r = fd_nonblock(sockfd, true); |
| 217 |
if (r < 0) |
| 218 |
return r; |
| 219 |
if (r > 0) |
| 220 |
reset_fd = sockfd; |
| 221 |
|
| 222 |
r = RET_NERRNO(connect(sockfd, addr, addrlen)); |
| 223 |
if (r != -EINPROGRESS) |
| 224 |
return r; |
| 225 |
|
| 226 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *io = NULL; |
| 227 |
r = future_new_io(e, sockfd, EPOLLOUT, &io); |
| 228 |
if (r < 0) |
| 229 |
return r; |
| 230 |
|
| 231 |
/* future_new_io resolves with the revents mask on success; translate any positive value |
| 232 |
* (e.g. POLLOUT) back to the connect(2) success status. */ |
| 233 |
r = sd_fiber_suspend(); |
| 234 |
return r > 0 ? 0 : r; |
| 235 |
} |
| 236 |
|
| 237 |
typedef struct RecvmsgArgs { |
| 238 |
struct msghdr *msg; |
| 239 |
int flags; |
| 240 |
} RecvmsgArgs; |
| 241 |
|
| 242 |
static ssize_t recvmsg_callback(int fd, void *args) { |
| 243 |
RecvmsgArgs *a = ASSERT_PTR(args); |
| 244 |
ssize_t n; |
| 245 |
|
| 246 |
n = recvmsg(fd, a->msg, a->flags); |
| 247 |
return n >= 0 ? n : -errno; |
| 248 |
} |
| 249 |
|
| 250 |
ssize_t sd_fiber_recvmsg(int sockfd, struct msghdr *msg, int flags) { |
| 251 |
assert_return(sockfd >= 0, -EBADF); |
| 252 |
assert_return(msg, -EINVAL); |
| 253 |
|
| 254 |
return fiber_io_operation(sockfd, EPOLLIN, recvmsg_callback, &(RecvmsgArgs) { |
| 255 |
.msg = msg, |
| 256 |
.flags = flags, |
| 257 |
}); |
| 258 |
} |
| 259 |
|
| 260 |
typedef struct SendmsgArgs { |
| 261 |
const struct msghdr *msg; |
| 262 |
int flags; |
| 263 |
} SendmsgArgs; |
| 264 |
|
| 265 |
static ssize_t sendmsg_callback(int fd, void *args) { |
| 266 |
SendmsgArgs *a = ASSERT_PTR(args); |
| 267 |
ssize_t n; |
| 268 |
|
| 269 |
n = sendmsg(fd, a->msg, a->flags); |
| 270 |
return n >= 0 ? n : -errno; |
| 271 |
} |
| 272 |
|
| 273 |
ssize_t sd_fiber_sendmsg(int sockfd, const struct msghdr *msg, int flags) { |
| 274 |
assert_return(sockfd >= 0, -EBADF); |
| 275 |
assert_return(msg, -EINVAL); |
| 276 |
|
| 277 |
return fiber_io_operation(sockfd, EPOLLOUT, sendmsg_callback, &(SendmsgArgs) { |
| 278 |
.msg = msg, |
| 279 |
.flags = flags, |
| 280 |
}); |
| 281 |
} |
| 282 |
|
| 283 |
static ssize_t recvfrom_callback(int fd, void *args) { |
| 284 |
RecvmsgArgs *a = ASSERT_PTR(args); |
| 285 |
ssize_t n; |
| 286 |
|
| 287 |
n = recvmsg(fd, a->msg, a->flags); |
| 288 |
return n >= 0 ? n : -errno; |
| 289 |
} |
| 290 |
|
| 291 |
ssize_t sd_fiber_recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { |
| 292 |
ssize_t n; |
| 293 |
|
| 294 |
assert_return(sockfd >= 0, -EBADF); |
| 295 |
assert_return(buf || len == 0, -EINVAL); |
| 296 |
assert_return(!src_addr || addrlen, -EINVAL); |
| 297 |
|
| 298 |
/* io_uring has no direct recvfrom prep helper, so emulate via recvmsg with a single-iovec |
| 299 |
* msghdr. The kernel updates msg_namelen in place; we copy it back to *addrlen below. */ |
| 300 |
struct iovec iov = { .iov_base = buf, .iov_len = len }; |
| 301 |
struct msghdr msg = { |
| 302 |
.msg_name = src_addr, |
| 303 |
.msg_namelen = src_addr ? *addrlen : 0, |
| 304 |
.msg_iov = &iov, |
| 305 |
.msg_iovlen = 1, |
| 306 |
}; |
| 307 |
|
| 308 |
n = fiber_io_operation(sockfd, EPOLLIN, recvfrom_callback, &(RecvmsgArgs) { |
| 309 |
.msg = &msg, |
| 310 |
.flags = flags, |
| 311 |
}); |
| 312 |
if (n < 0) |
| 313 |
return n; |
| 314 |
|
| 315 |
if (addrlen) |
| 316 |
*addrlen = msg.msg_namelen; |
| 317 |
|
| 318 |
return n; |
| 319 |
} |
| 320 |
|
| 321 |
static ssize_t sendto_callback(int fd, void *args) { |
| 322 |
SendmsgArgs *a = ASSERT_PTR(args); |
| 323 |
ssize_t n; |
| 324 |
|
| 325 |
n = sendmsg(fd, a->msg, a->flags); |
| 326 |
return n >= 0 ? n : -errno; |
| 327 |
} |
| 328 |
|
| 329 |
ssize_t sd_fiber_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { |
| 330 |
assert_return(sockfd >= 0, -EBADF); |
| 331 |
assert_return(buf || len == 0, -EINVAL); |
| 332 |
|
| 333 |
struct iovec iov = { .iov_base = (void *) buf, .iov_len = len }; |
| 334 |
struct msghdr msg = { |
| 335 |
.msg_name = (void *) dest_addr, |
| 336 |
.msg_namelen = dest_addr ? addrlen : 0, |
| 337 |
.msg_iov = &iov, |
| 338 |
.msg_iovlen = 1, |
| 339 |
}; |
| 340 |
|
| 341 |
return fiber_io_operation(sockfd, EPOLLOUT, sendto_callback, &(SendmsgArgs) { |
| 342 |
.msg = &msg, |
| 343 |
.flags = flags, |
| 344 |
}); |
| 345 |
} |
| 346 |
|
| 347 |
typedef struct AcceptArgs { |
| 348 |
struct sockaddr *addr; |
| 349 |
socklen_t *addrlen; |
| 350 |
int flags; |
| 351 |
} AcceptArgs; |
| 352 |
|
| 353 |
static ssize_t accept_callback(int fd, void *args) { |
| 354 |
AcceptArgs *a = ASSERT_PTR(args); |
| 355 |
|
| 356 |
return RET_NERRNO(accept4(fd, a->addr, a->addrlen, a->flags)); |
| 357 |
} |
| 358 |
|
| 359 |
int sd_fiber_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { |
| 360 |
assert_return(sockfd >= 0, -EBADF); |
| 361 |
|
| 362 |
return fiber_io_operation(sockfd, EPOLLIN, accept_callback, &(AcceptArgs) { |
| 363 |
.addr = addr, |
| 364 |
.addrlen = addrlen, |
| 365 |
.flags = flags, |
| 366 |
}); |
| 367 |
} |
| 368 |
|
| 369 |
int sd_fiber_ppoll(struct pollfd *fds, size_t n_fds, const struct timespec *timeout, const sigset_t *sigmask) { |
| 370 |
int r; |
| 371 |
|
| 372 |
assert_return(fds || n_fds == 0, -EINVAL); |
| 373 |
|
| 374 |
if (!sd_fiber_is_running()) |
| 375 |
return RET_NERRNO(ppoll(fds, n_fds, timeout, sigmask)); |
| 376 |
|
| 377 |
/* When on a fiber signals are handled via sd-event hence we should never mess around with the |
| 378 |
* signal mask when running on a fiber. */ |
| 379 |
assert_return(!sigmask, -EOPNOTSUPP); |
| 380 |
|
| 381 |
sd_event *e = sd_fiber_get_event(); |
| 382 |
assert(e); |
| 383 |
|
| 384 |
/* No fds to wait on and no timeout means there's nothing that could ever wake the fiber up, |
| 385 |
* since unlike raw ppoll() we cannot use signal delivery as a wakeup. Signals received while |
| 386 |
* the fiber is suspended are handled by sd-event via signalfd, in which case the signal handler |
| 387 |
* is expected to cancel the fiber via sd_future_cancel() if a wakeup is desired. */ |
| 388 |
if (n_fds == 0 && !timeout) |
| 389 |
return -EINVAL; |
| 390 |
|
| 391 |
bool zero_timeout = timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0; |
| 392 |
|
| 393 |
/* Try polling with zero timeout first to see if any are immediately ready. */ |
| 394 |
r = RET_NERRNO(ppoll(fds, n_fds, &(const struct timespec) {}, /* sigmask= */ NULL)); |
| 395 |
if (zero_timeout || r != 0) /* Either error or some fds are ready */ |
| 396 |
return r; |
| 397 |
|
| 398 |
sd_future **futures = NULL; |
| 399 |
CLEANUP_ARRAY(futures, n_fds, sd_future_cancel_wait_unref_array); |
| 400 |
|
| 401 |
futures = new0(sd_future*, n_fds); |
| 402 |
if (!futures) |
| 403 |
return -ENOMEM; |
| 404 |
|
| 405 |
/* Set up I/O event sources for all valid fds. POLL* and EPOLL* share their bit values (see |
| 406 |
* EPOLL_POLL_COMMON_MASK in io-util.h), so we can pass the user-supplied event mask through |
| 407 |
* to either backend without translation. */ |
| 408 |
size_t n_io_futures = 0; |
| 409 |
for (size_t i = 0; i < n_fds; i++) { |
| 410 |
if (fds[i].fd < 0) |
| 411 |
continue; |
| 412 |
|
| 413 |
uint32_t events = poll_events_to_epoll(fds[i].events); |
| 414 |
if (events == 0) |
| 415 |
continue; |
| 416 |
|
| 417 |
r = future_new_io(e, fds[i].fd, events, &futures[i]); |
| 418 |
if (r < 0) |
| 419 |
return r; |
| 420 |
|
| 421 |
n_io_futures++; |
| 422 |
} |
| 423 |
|
| 424 |
/* A timeout that overflows usec_t saturates to USEC_INFINITY in timespec_load(); treat that |
| 425 |
* like "no timeout" (matches sd_fiber_sleep(USEC_INFINITY)) rather than letting |
| 426 |
* sd_event_add_time_relative() reject it with -EOVERFLOW — standard ppoll() would just |
| 427 |
* wait a very long time. */ |
| 428 |
usec_t usec = timeout ? timespec_load(timeout) : USEC_INFINITY; |
| 429 |
|
| 430 |
/* If every fd was skipped (negative or empty event mask) and we'd have no timer, there's |
| 431 |
* nothing that could ever wake the fiber up — same situation as n_fds == 0 && !timeout, |
| 432 |
* just not detectable upfront. Refuse rather than suspend forever. */ |
| 433 |
if (n_io_futures == 0 && usec == USEC_INFINITY) |
| 434 |
return -EINVAL; |
| 435 |
|
| 436 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *timer = NULL; |
| 437 |
if (usec != USEC_INFINITY) { |
| 438 |
r = future_new_time_relative( |
| 439 |
e, |
| 440 |
CLOCK_MONOTONIC, |
| 441 |
usec, |
| 442 |
/* accuracy= */ 1, |
| 443 |
/* result= */ 0, |
| 444 |
&timer); |
| 445 |
if (r < 0) |
| 446 |
return r; |
| 447 |
} |
| 448 |
|
| 449 |
r = sd_fiber_suspend(); |
| 450 |
if (r < 0 && r != -ETIME) |
| 451 |
return r; |
| 452 |
|
| 453 |
/* Always sweep fds with a non-blocking ppoll(): the timer and an fd readiness can resolve in |
| 454 |
* the same event-loop tick (or the fd can become ready between the timer firing and us being |
| 455 |
* scheduled), and ppoll() semantics give events precedence over the timeout in that case. */ |
| 456 |
int n = RET_NERRNO(ppoll(fds, n_fds, &(const struct timespec) {}, /* sigmask= */ NULL)); |
| 457 |
if (n != 0) |
| 458 |
return n; |
| 459 |
|
| 460 |
/* No fds ready: distinguish our own timer from an external -ETIME. */ |
| 461 |
if (timer && sd_future_state(timer) == SD_FUTURE_RESOLVED) |
| 462 |
return 0; |
| 463 |
|
| 464 |
/* An IO future resolved with a revents mask (r > 0) but the readiness was already consumed |
| 465 |
* by the time we swept — report 0 rather than leaking the bitmask as a (bogus) ppoll fd |
| 466 |
* count to the caller. */ |
| 467 |
if (r > 0) |
| 468 |
return 0; |
| 469 |
|
| 470 |
return r; |
| 471 |
} |