| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <poll.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <sys/un.h> |
| 7 |
#include <unistd.h> |
| 8 |
|
| 9 |
#include "sd-event.h" |
| 10 |
#include "sd-future.h" |
| 11 |
|
| 12 |
#include "fd-util.h" |
| 13 |
#include "tests.h" |
| 14 |
#include "time-util.h" |
| 15 |
|
| 16 |
/* Test: Basic pipe I/O with sd-event */ |
| 17 |
|
| 18 |
typedef struct PipeIOContext { |
| 19 |
int *pipefd; |
| 20 |
int order; |
| 21 |
} PipeIOContext; |
| 22 |
|
| 23 |
static int pipe_read_fiber(void *userdata) { |
| 24 |
PipeIOContext *ctx = ASSERT_PTR(userdata); |
| 25 |
char buf[64]; |
| 26 |
ssize_t n; |
| 27 |
|
| 28 |
n = sd_fiber_read(ctx->pipefd[0], buf, sizeof(buf)); |
| 29 |
if (n < 0) |
| 30 |
return (int) n; |
| 31 |
|
| 32 |
/* Verify we read "hello" */ |
| 33 |
if (n != 5 || memcmp(buf, "hello", 5) != 0) |
| 34 |
return -EIO; |
| 35 |
|
| 36 |
return (int) n; |
| 37 |
} |
| 38 |
|
| 39 |
TEST(fiber_io_basic) { |
| 40 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 41 |
ASSERT_OK(sd_event_new(&e)); |
| 42 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 43 |
|
| 44 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 45 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 46 |
|
| 47 |
PipeIOContext ctx = { .pipefd = pipefd }; |
| 48 |
|
| 49 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 50 |
ASSERT_OK(sd_fiber_new(e, "pipe-read", pipe_read_fiber, &ctx, NULL, &f)); |
| 51 |
|
| 52 |
/* Write data to the pipe */ |
| 53 |
ASSERT_OK_EQ_ERRNO(write(pipefd[1], "hello", 5), 5); |
| 54 |
|
| 55 |
/* Run the scheduler - should process the I/O */ |
| 56 |
ASSERT_OK(sd_event_loop(e)); |
| 57 |
|
| 58 |
/* Verify fiber read the data */ |
| 59 |
ASSERT_OK_EQ(sd_future_result(f), 5); |
| 60 |
} |
| 61 |
|
| 62 |
static int pipe_read_order_fiber(void *userdata) { |
| 63 |
PipeIOContext *ctx = ASSERT_PTR(userdata); |
| 64 |
char buf[64]; |
| 65 |
ssize_t n; |
| 66 |
|
| 67 |
/* Record that the read fiber started before attempting the blocking read */ |
| 68 |
ASSERT_EQ(ctx->order, 0); |
| 69 |
ctx->order = 1; |
| 70 |
|
| 71 |
n = sd_fiber_read(ctx->pipefd[0], buf, sizeof(buf)); |
| 72 |
if (n < 0) |
| 73 |
return (int) n; |
| 74 |
|
| 75 |
/* After resuming, verify the write fiber ran while we were suspended */ |
| 76 |
ASSERT_EQ(ctx->order, 2); |
| 77 |
|
| 78 |
/* Verify we read "hello" */ |
| 79 |
if (n != 5 || memcmp(buf, "hello", 5) != 0) |
| 80 |
return -EIO; |
| 81 |
|
| 82 |
return (int) n; |
| 83 |
} |
| 84 |
|
| 85 |
static int pipe_write_order_fiber(void *userdata) { |
| 86 |
PipeIOContext *ctx = ASSERT_PTR(userdata); |
| 87 |
|
| 88 |
/* Verify the read fiber already ran and suspended before we started */ |
| 89 |
ASSERT_EQ(ctx->order, 1); |
| 90 |
ctx->order = 2; |
| 91 |
|
| 92 |
return sd_fiber_write(ctx->pipefd[1], "hello", STRLEN("hello")); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(fiber_io_read_write) { |
| 96 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 97 |
ASSERT_OK(sd_event_new(&e)); |
| 98 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 99 |
|
| 100 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 101 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 102 |
|
| 103 |
PipeIOContext ctx = { .pipefd = pipefd }; |
| 104 |
|
| 105 |
/* Higher priority for the read fiber, which will run first and then suspend because no data is |
| 106 |
* available. The write fiber will run second, write data to the pipe, causing the read fiber to get |
| 107 |
* resumed. */ |
| 108 |
_cleanup_(sd_future_unrefp) sd_future *fr = NULL, *fw = NULL; |
| 109 |
ASSERT_OK(sd_fiber_new(e, "pipe-read", pipe_read_order_fiber, &ctx, NULL, &fr)); |
| 110 |
ASSERT_OK(sd_future_set_priority(fr, 0)); |
| 111 |
ASSERT_OK(sd_fiber_new(e, "pipe-write", pipe_write_order_fiber, &ctx, NULL, &fw)); |
| 112 |
ASSERT_OK(sd_future_set_priority(fw, 1)); |
| 113 |
|
| 114 |
/* Run the scheduler - should process the I/O */ |
| 115 |
ASSERT_OK(sd_event_loop(e)); |
| 116 |
|
| 117 |
/* Verify both fibers completed and the full read->suspend->write->resume sequence occurred */ |
| 118 |
ASSERT_OK_EQ(sd_future_result(fr), 5); |
| 119 |
ASSERT_OK_EQ(sd_future_result(fw), 5); |
| 120 |
} |
| 121 |
|
| 122 |
/* Test: Multiple concurrent reads */ |
| 123 |
static int concurrent_read_fiber(void *userdata) { |
| 124 |
int *args = userdata; |
| 125 |
int fd = args[0]; |
| 126 |
int expected = args[1]; |
| 127 |
char buf[64]; |
| 128 |
ssize_t n; |
| 129 |
|
| 130 |
n = sd_fiber_read(fd, buf, sizeof buf); |
| 131 |
if (n < 0) |
| 132 |
return (int) n; |
| 133 |
|
| 134 |
if (n != 1 || buf[0] != (char) expected) |
| 135 |
return -EIO; |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
TEST(fiber_io_concurrent) { |
| 141 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 142 |
ASSERT_OK(sd_event_new(&e)); |
| 143 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 144 |
|
| 145 |
sd_future *fibers[3] = {}; |
| 146 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 147 |
|
| 148 |
/* Create 3 pipes and 3 fibers */ |
| 149 |
int pipes[3][2]; |
| 150 |
int args[3][2]; |
| 151 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 152 |
ASSERT_OK_ERRNO(pipe2(pipes[i], O_CLOEXEC | O_NONBLOCK)); |
| 153 |
args[i][0] = pipes[i][0]; |
| 154 |
args[i][1] = 'A' + i; |
| 155 |
ASSERT_OK(sd_fiber_new(e, "concurrent-read", concurrent_read_fiber, args[i], NULL, &fibers[i])); |
| 156 |
} |
| 157 |
|
| 158 |
/* Write data in reverse order */ |
| 159 |
ASSERT_EQ(write(pipes[2][1], "C", 1), 1); |
| 160 |
ASSERT_EQ(write(pipes[1][1], "B", 1), 1); |
| 161 |
ASSERT_EQ(write(pipes[0][1], "A", 1), 1); |
| 162 |
|
| 163 |
/* Run until all complete */ |
| 164 |
ASSERT_OK(sd_event_loop(e)); |
| 165 |
|
| 166 |
/* All should complete successfully */ |
| 167 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 168 |
ASSERT_OK(sd_future_result(fibers[i])); |
| 169 |
safe_close_pair(pipes[i]); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/* Test: Cancel fiber during I/O */ |
| 174 |
static int blocking_read_fiber(void *userdata) { |
| 175 |
int fd = PTR_TO_INT(userdata); |
| 176 |
char buf[64]; |
| 177 |
ssize_t n; |
| 178 |
|
| 179 |
n = sd_fiber_read(fd, buf, sizeof(buf)); |
| 180 |
return (int) n; |
| 181 |
} |
| 182 |
|
| 183 |
TEST(fiber_io_cancel) { |
| 184 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 185 |
ASSERT_OK(sd_event_new(&e)); |
| 186 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 187 |
|
| 188 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 189 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 190 |
|
| 191 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 192 |
ASSERT_OK(sd_fiber_new(e, "blocking-read", blocking_read_fiber, INT_TO_PTR(pipefd[0]), NULL, &f)); |
| 193 |
|
| 194 |
/* Run once - fiber will suspend on read */ |
| 195 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 196 |
|
| 197 |
/* Fiber should be suspended now - add explicit check via state tracking */ |
| 198 |
|
| 199 |
/* Cancel the fiber */ |
| 200 |
ASSERT_OK(sd_future_cancel(f)); |
| 201 |
|
| 202 |
/* Run to completion */ |
| 203 |
ASSERT_OK(sd_event_loop(e)); |
| 204 |
|
| 205 |
/* Should be cancelled */ |
| 206 |
ASSERT_ERROR(sd_future_result(f), ECANCELED); |
| 207 |
} |
| 208 |
|
| 209 |
TEST(fiber_io_fallback) { |
| 210 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 211 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC)); /* Note: blocking pipe */ |
| 212 |
|
| 213 |
char buf[STRLEN("fallback")] = {}; |
| 214 |
ASSERT_OK_EQ(sd_fiber_write(pipefd[1], "fallback", sizeof(buf)), (ssize_t) sizeof(buf)); |
| 215 |
ASSERT_OK_EQ(sd_fiber_read(pipefd[0], buf, sizeof(buf)), (ssize_t) sizeof(buf)); |
| 216 |
} |
| 217 |
|
| 218 |
static int pipe_readv_order_fiber(void *userdata) { |
| 219 |
PipeIOContext *ctx = ASSERT_PTR(userdata); |
| 220 |
char buf1[5], buf2[5]; |
| 221 |
struct iovec iov[] = { |
| 222 |
{ .iov_base = buf1, .iov_len = sizeof(buf1) }, |
| 223 |
{ .iov_base = buf2, .iov_len = sizeof(buf2) }, |
| 224 |
}; |
| 225 |
ssize_t n; |
| 226 |
|
| 227 |
/* Record that the read fiber started before attempting the blocking read */ |
| 228 |
ASSERT_EQ(ctx->order, 0); |
| 229 |
ctx->order = 1; |
| 230 |
|
| 231 |
/* This will initially block since no data is available */ |
| 232 |
n = sd_fiber_readv(ctx->pipefd[0], iov, ELEMENTSOF(iov)); |
| 233 |
if (n < 0) |
| 234 |
return (int) n; |
| 235 |
|
| 236 |
/* After resuming, verify the write fiber ran while we were suspended */ |
| 237 |
ASSERT_EQ(ctx->order, 2); |
| 238 |
|
| 239 |
if (n != 10 || memcmp(buf1, "fiber", 5) != 0 || memcmp(buf2, "readv", 5) != 0) |
| 240 |
return -EIO; |
| 241 |
|
| 242 |
return (int) n; |
| 243 |
} |
| 244 |
|
| 245 |
static int pipe_writev_order_fiber(void *userdata) { |
| 246 |
PipeIOContext *ctx = ASSERT_PTR(userdata); |
| 247 |
const char *part1 = "fiber"; |
| 248 |
const char *part2 = "readv"; |
| 249 |
struct iovec iov[] = { |
| 250 |
{ .iov_base = (void*) part1, .iov_len = 5 }, |
| 251 |
{ .iov_base = (void*) part2, .iov_len = 5 }, |
| 252 |
}; |
| 253 |
|
| 254 |
/* Verify the read fiber already ran and suspended before we started */ |
| 255 |
ASSERT_EQ(ctx->order, 1); |
| 256 |
ctx->order = 2; |
| 257 |
|
| 258 |
return sd_fiber_writev(ctx->pipefd[1], iov, ELEMENTSOF(iov)); |
| 259 |
} |
| 260 |
|
| 261 |
TEST(fiber_io_readv_writev) { |
| 262 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 263 |
ASSERT_OK(sd_event_new(&e)); |
| 264 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 265 |
|
| 266 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 267 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 268 |
|
| 269 |
PipeIOContext ctx = { .pipefd = pipefd }; |
| 270 |
|
| 271 |
/* Higher priority for the read fiber, which will run first and then suspend because no data is |
| 272 |
* available. The write fiber will run second, write data to the pipe, causing the read fiber to get |
| 273 |
* resumed. */ |
| 274 |
_cleanup_(sd_future_unrefp) sd_future *fr = NULL, *fw = NULL; |
| 275 |
ASSERT_OK(sd_fiber_new(e, "pipe-readv", pipe_readv_order_fiber, &ctx, NULL, &fr)); |
| 276 |
ASSERT_OK(sd_future_set_priority(fr, 0)); |
| 277 |
ASSERT_OK(sd_fiber_new(e, "pipe-writev", pipe_writev_order_fiber, &ctx, NULL, &fw)); |
| 278 |
ASSERT_OK(sd_future_set_priority(fw, 1)); |
| 279 |
|
| 280 |
/* Run the scheduler - should process the I/O */ |
| 281 |
ASSERT_OK(sd_event_loop(e)); |
| 282 |
|
| 283 |
/* Verify both fibers completed and the full read->suspend->write->resume sequence occurred */ |
| 284 |
ASSERT_OK_EQ(sd_future_result(fr), 10); |
| 285 |
ASSERT_OK_EQ(sd_future_result(fw), 10); |
| 286 |
} |
| 287 |
|
| 288 |
static int concurrent_readv_fiber(void *userdata) { |
| 289 |
int *args = userdata; |
| 290 |
int fd = args[0]; |
| 291 |
int expected1 = args[1]; |
| 292 |
int expected2 = args[2]; |
| 293 |
char buf1[1], buf2[1]; |
| 294 |
struct iovec iov[] = { |
| 295 |
{ .iov_base = buf1, .iov_len = sizeof(buf1) }, |
| 296 |
{ .iov_base = buf2, .iov_len = sizeof(buf2) }, |
| 297 |
}; |
| 298 |
ssize_t n; |
| 299 |
|
| 300 |
n = sd_fiber_readv(fd, iov, ELEMENTSOF(iov)); |
| 301 |
if (n < 0) |
| 302 |
return (int) n; |
| 303 |
|
| 304 |
if (n != 2 || buf1[0] != (char) expected1 || buf2[0] != (char) expected2) |
| 305 |
return -EIO; |
| 306 |
|
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
TEST(fiber_io_readv_concurrent) { |
| 311 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 312 |
ASSERT_OK(sd_event_new(&e)); |
| 313 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 314 |
|
| 315 |
sd_future *fibers[3] = {}; |
| 316 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 317 |
|
| 318 |
/* Create 3 pipes and 3 fibers */ |
| 319 |
int pipes[3][2]; |
| 320 |
int args[3][3]; |
| 321 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 322 |
ASSERT_OK_ERRNO(pipe2(pipes[i], O_CLOEXEC | O_NONBLOCK)); |
| 323 |
args[i][0] = pipes[i][0]; |
| 324 |
args[i][1] = 'A' + i; |
| 325 |
args[i][2] = 'a' + i; |
| 326 |
ASSERT_OK(sd_fiber_new(e, "concurrent-readv", concurrent_readv_fiber, args[i], NULL, &fibers[i])); |
| 327 |
} |
| 328 |
|
| 329 |
/* Write data in reverse order */ |
| 330 |
ASSERT_EQ(write(pipes[2][1], "Cc", 2), 2); |
| 331 |
ASSERT_EQ(write(pipes[1][1], "Bb", 2), 2); |
| 332 |
ASSERT_EQ(write(pipes[0][1], "Aa", 2), 2); |
| 333 |
|
| 334 |
/* Run until all complete */ |
| 335 |
ASSERT_OK(sd_event_loop(e)); |
| 336 |
|
| 337 |
/* All should complete successfully */ |
| 338 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 339 |
ASSERT_OK(sd_future_result(fibers[i])); |
| 340 |
safe_close_pair(pipes[i]); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
typedef struct SocketIOContext { |
| 345 |
int *sockfd; |
| 346 |
int order; |
| 347 |
} SocketIOContext; |
| 348 |
|
| 349 |
static int socket_send_order_fiber(void *userdata) { |
| 350 |
SocketIOContext *ctx = ASSERT_PTR(userdata); |
| 351 |
|
| 352 |
/* Verify the recv fiber already ran and suspended before we started */ |
| 353 |
ASSERT_EQ(ctx->order, 1); |
| 354 |
ctx->order = 2; |
| 355 |
|
| 356 |
return sd_fiber_send(ctx->sockfd[0], "socket", STRLEN("socket"), 0); |
| 357 |
} |
| 358 |
|
| 359 |
static int socket_recv_order_fiber(void *userdata) { |
| 360 |
SocketIOContext *ctx = ASSERT_PTR(userdata); |
| 361 |
char buf[64]; |
| 362 |
ssize_t n; |
| 363 |
|
| 364 |
/* Record that the recv fiber started before attempting the blocking recv */ |
| 365 |
ASSERT_EQ(ctx->order, 0); |
| 366 |
ctx->order = 1; |
| 367 |
|
| 368 |
n = sd_fiber_recv(ctx->sockfd[1], buf, sizeof(buf), 0); |
| 369 |
if (n < 0) |
| 370 |
return (int) n; |
| 371 |
|
| 372 |
/* After resuming, verify the send fiber ran while we were suspended */ |
| 373 |
ASSERT_EQ(ctx->order, 2); |
| 374 |
|
| 375 |
/* Verify we received "socket" */ |
| 376 |
if (n != 6 || memcmp(buf, "socket", 6) != 0) |
| 377 |
return -EIO; |
| 378 |
|
| 379 |
return (int) n; |
| 380 |
} |
| 381 |
|
| 382 |
TEST(fiber_io_recv_send) { |
| 383 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 384 |
ASSERT_OK(sd_event_new(&e)); |
| 385 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 386 |
|
| 387 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 388 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sockfd)); |
| 389 |
|
| 390 |
SocketIOContext ctx = { .sockfd = sockfd }; |
| 391 |
|
| 392 |
/* Higher priority for the recv fiber, which will run first and suspend */ |
| 393 |
_cleanup_(sd_future_unrefp) sd_future *fs = NULL, *fr = NULL; |
| 394 |
ASSERT_OK(sd_fiber_new(e, "socket-recv", socket_recv_order_fiber, &ctx, NULL, &fr)); |
| 395 |
ASSERT_OK(sd_future_set_priority(fr, 0)); |
| 396 |
ASSERT_OK(sd_fiber_new(e, "socket-send", socket_send_order_fiber, &ctx, NULL, &fs)); |
| 397 |
ASSERT_OK(sd_future_set_priority(fs, 1)); |
| 398 |
|
| 399 |
ASSERT_OK(sd_event_loop(e)); |
| 400 |
|
| 401 |
/* Verify both fibers completed and the full recv->suspend->send->resume sequence occurred */ |
| 402 |
ASSERT_OK_EQ(sd_future_result(fr), 6); |
| 403 |
ASSERT_OK_EQ(sd_future_result(fs), 6); |
| 404 |
} |
| 405 |
|
| 406 |
static int socket_recv_peek_fiber(void *userdata) { |
| 407 |
int sockfd = PTR_TO_INT(userdata); |
| 408 |
char buf1[64], buf2[64]; |
| 409 |
ssize_t n1, n2; |
| 410 |
|
| 411 |
/* First peek at the data */ |
| 412 |
n1 = sd_fiber_recv(sockfd, buf1, sizeof(buf1), MSG_PEEK); |
| 413 |
if (n1 < 0) |
| 414 |
return (int) n1; |
| 415 |
|
| 416 |
/* Then actually read it */ |
| 417 |
n2 = sd_fiber_recv(sockfd, buf2, sizeof(buf2), 0); |
| 418 |
if (n2 < 0) |
| 419 |
return (int) n2; |
| 420 |
|
| 421 |
/* Both should have read the same data */ |
| 422 |
if (n1 != n2 || memcmp(buf1, buf2, n1) != 0) |
| 423 |
return -EIO; |
| 424 |
|
| 425 |
if (n1 != 4 || memcmp(buf1, "peek", 4) != 0) |
| 426 |
return -EIO; |
| 427 |
|
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
TEST(fiber_io_recv_peek) { |
| 432 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 433 |
ASSERT_OK(sd_event_new(&e)); |
| 434 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 435 |
|
| 436 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 437 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sockfd)); |
| 438 |
|
| 439 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 440 |
ASSERT_OK(sd_fiber_new(e, "socket-recv-peek", socket_recv_peek_fiber, INT_TO_PTR(sockfd[1]), NULL, &f)); |
| 441 |
|
| 442 |
/* Write data to the socket */ |
| 443 |
ASSERT_OK_EQ_ERRNO(write(sockfd[0], "peek", 4), 4); |
| 444 |
|
| 445 |
ASSERT_OK(sd_event_loop(e)); |
| 446 |
ASSERT_OK(sd_future_result(f)); |
| 447 |
} |
| 448 |
|
| 449 |
static int socket_connect_fiber(void *userdata) { |
| 450 |
struct sockaddr_un *addr = userdata; |
| 451 |
_cleanup_close_ int sockfd = -EBADF; |
| 452 |
|
| 453 |
sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); |
| 454 |
if (sockfd < 0) |
| 455 |
return -errno; |
| 456 |
|
| 457 |
return sd_fiber_connect(sockfd, (struct sockaddr*) addr, sizeof(*addr)); |
| 458 |
} |
| 459 |
|
| 460 |
TEST(fiber_io_connect) { |
| 461 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 462 |
ASSERT_OK(sd_event_new(&e)); |
| 463 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 464 |
|
| 465 |
/* Create listening socket with abstract namespace */ |
| 466 |
_cleanup_close_ int listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); |
| 467 |
ASSERT_OK(listen_fd); |
| 468 |
|
| 469 |
/* Use abstract socket (starts with null byte) */ |
| 470 |
struct sockaddr_un addr = { |
| 471 |
.sun_family = AF_UNIX, |
| 472 |
}; |
| 473 |
addr.sun_path[0] = '\0'; |
| 474 |
snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, "test-fiber-connect-%d", getpid()); |
| 475 |
|
| 476 |
ASSERT_OK(bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 477 |
ASSERT_OK(listen(listen_fd, 1)); |
| 478 |
|
| 479 |
/* Create fiber to connect */ |
| 480 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 481 |
ASSERT_OK(sd_fiber_new(e, "socket-connect", socket_connect_fiber, &addr, NULL, &f)); |
| 482 |
|
| 483 |
/* Run the event loop - connection should complete */ |
| 484 |
ASSERT_OK(sd_event_loop(e)); |
| 485 |
ASSERT_OK(sd_future_result(f)); |
| 486 |
} |
| 487 |
|
| 488 |
static int socket_sendmsg_fiber(void *userdata) { |
| 489 |
int sockfd = PTR_TO_INT(userdata); |
| 490 |
struct iovec iov = { |
| 491 |
.iov_base = (void*) "message", |
| 492 |
.iov_len = STRLEN("message"), |
| 493 |
}; |
| 494 |
struct msghdr msg = { |
| 495 |
.msg_iov = &iov, |
| 496 |
.msg_iovlen = 1, |
| 497 |
}; |
| 498 |
|
| 499 |
return sd_fiber_sendmsg(sockfd, &msg, 0); |
| 500 |
} |
| 501 |
|
| 502 |
static int socket_recvmsg_fiber(void *userdata) { |
| 503 |
int sockfd = PTR_TO_INT(userdata); |
| 504 |
char buf[64]; |
| 505 |
struct iovec iov = { |
| 506 |
.iov_base = buf, |
| 507 |
.iov_len = sizeof(buf), |
| 508 |
}; |
| 509 |
struct msghdr msg = { |
| 510 |
.msg_iov = &iov, |
| 511 |
.msg_iovlen = 1, |
| 512 |
}; |
| 513 |
ssize_t n; |
| 514 |
|
| 515 |
n = sd_fiber_recvmsg(sockfd, &msg, 0); |
| 516 |
if (n < 0) |
| 517 |
return (int) n; |
| 518 |
|
| 519 |
if (n != 7 || memcmp(buf, "message", 7) != 0) |
| 520 |
return -EIO; |
| 521 |
|
| 522 |
return (int) n; |
| 523 |
} |
| 524 |
|
| 525 |
TEST(fiber_io_recvmsg_sendmsg) { |
| 526 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 527 |
ASSERT_OK(sd_event_new(&e)); |
| 528 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 529 |
|
| 530 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 531 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sockfd)); |
| 532 |
|
| 533 |
_cleanup_(sd_future_unrefp) sd_future *fs = NULL, *fr = NULL; |
| 534 |
ASSERT_OK(sd_fiber_new(e, "socket-recvmsg", socket_recvmsg_fiber, INT_TO_PTR(sockfd[1]), NULL, &fr)); |
| 535 |
ASSERT_OK(sd_future_set_priority(fr, 1)); |
| 536 |
ASSERT_OK(sd_fiber_new(e, "socket-sendmsg", socket_sendmsg_fiber, INT_TO_PTR(sockfd[0]), NULL, &fs)); |
| 537 |
ASSERT_OK(sd_future_set_priority(fs, 0)); |
| 538 |
|
| 539 |
ASSERT_OK(sd_event_loop(e)); |
| 540 |
|
| 541 |
ASSERT_OK_EQ(sd_future_result(fr), 7); |
| 542 |
ASSERT_OK_EQ(sd_future_result(fs), 7); |
| 543 |
} |
| 544 |
|
| 545 |
static int socket_sendto_fiber(void *userdata) { |
| 546 |
int sockfd = PTR_TO_INT(userdata); |
| 547 |
|
| 548 |
/* For socketpair dgram sockets, we can use NULL address since they're connected */ |
| 549 |
return sd_fiber_sendto(sockfd, "datagram", STRLEN("datagram"), 0, NULL, 0); |
| 550 |
} |
| 551 |
|
| 552 |
static int socket_recvfrom_fiber(void *userdata) { |
| 553 |
int sockfd = PTR_TO_INT(userdata); |
| 554 |
char buf[64]; |
| 555 |
struct sockaddr_un addr; |
| 556 |
socklen_t addr_len = sizeof(addr); |
| 557 |
ssize_t n; |
| 558 |
|
| 559 |
n = sd_fiber_recvfrom(sockfd, buf, sizeof(buf), 0, |
| 560 |
(struct sockaddr*) &addr, &addr_len); |
| 561 |
if (n < 0) |
| 562 |
return (int) n; |
| 563 |
|
| 564 |
if (n != 8 || memcmp(buf, "datagram", 8) != 0) |
| 565 |
return -EIO; |
| 566 |
|
| 567 |
return (int) n; |
| 568 |
} |
| 569 |
|
| 570 |
TEST(fiber_io_recvfrom_sendto) { |
| 571 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 572 |
ASSERT_OK(sd_event_new(&e)); |
| 573 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 574 |
|
| 575 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 576 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sockfd)); |
| 577 |
|
| 578 |
_cleanup_(sd_future_unrefp) sd_future *fs = NULL, *fr = NULL; |
| 579 |
ASSERT_OK(sd_fiber_new(e, "socket-recvfrom", socket_recvfrom_fiber, INT_TO_PTR(sockfd[1]), NULL, &fr)); |
| 580 |
ASSERT_OK(sd_future_set_priority(fr, 1)); |
| 581 |
ASSERT_OK(sd_fiber_new(e, "socket-sendto", socket_sendto_fiber, INT_TO_PTR(sockfd[0]), NULL, &fs)); |
| 582 |
ASSERT_OK(sd_future_set_priority(fs, 0)); |
| 583 |
|
| 584 |
ASSERT_OK(sd_event_loop(e)); |
| 585 |
|
| 586 |
ASSERT_OK_EQ(sd_future_result(fr), 8); |
| 587 |
ASSERT_OK_EQ(sd_future_result(fs), 8); |
| 588 |
} |
| 589 |
|
| 590 |
static int socket_sendmsg_fd_fiber(void *userdata) { |
| 591 |
int *args = userdata; |
| 592 |
int sockfd = args[0]; |
| 593 |
int fd_to_send = args[1]; |
| 594 |
struct iovec iov = { |
| 595 |
.iov_base = (void*) "X", |
| 596 |
.iov_len = 1, |
| 597 |
}; |
| 598 |
union { |
| 599 |
struct cmsghdr cmsghdr; |
| 600 |
uint8_t buf[CMSG_SPACE(sizeof(int))]; |
| 601 |
} control = {}; |
| 602 |
struct msghdr msg = { |
| 603 |
.msg_iov = &iov, |
| 604 |
.msg_iovlen = 1, |
| 605 |
.msg_control = &control, |
| 606 |
.msg_controllen = sizeof(control), |
| 607 |
}; |
| 608 |
struct cmsghdr *cmsg; |
| 609 |
|
| 610 |
cmsg = CMSG_FIRSTHDR(&msg); |
| 611 |
cmsg->cmsg_level = SOL_SOCKET; |
| 612 |
cmsg->cmsg_type = SCM_RIGHTS; |
| 613 |
cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 614 |
memcpy(CMSG_DATA(cmsg), &fd_to_send, sizeof(int)); |
| 615 |
|
| 616 |
return sd_fiber_sendmsg(sockfd, &msg, 0); |
| 617 |
} |
| 618 |
|
| 619 |
static int socket_recvmsg_fd_fiber(void *userdata) { |
| 620 |
int sockfd = PTR_TO_INT(userdata); |
| 621 |
char buf[1]; |
| 622 |
struct iovec iov = { |
| 623 |
.iov_base = buf, |
| 624 |
.iov_len = sizeof(buf), |
| 625 |
}; |
| 626 |
union { |
| 627 |
struct cmsghdr cmsghdr; |
| 628 |
uint8_t buf[CMSG_SPACE(sizeof(int))]; |
| 629 |
} control = {}; |
| 630 |
struct msghdr msg = { |
| 631 |
.msg_iov = &iov, |
| 632 |
.msg_iovlen = 1, |
| 633 |
.msg_control = &control, |
| 634 |
.msg_controllen = sizeof(control), |
| 635 |
}; |
| 636 |
struct cmsghdr *cmsg; |
| 637 |
int received_fd; |
| 638 |
ssize_t n; |
| 639 |
|
| 640 |
n = sd_fiber_recvmsg(sockfd, &msg, 0); |
| 641 |
if (n < 0) |
| 642 |
return (int) n; |
| 643 |
|
| 644 |
if (n != 1 || buf[0] != 'X') |
| 645 |
return -EIO; |
| 646 |
|
| 647 |
/* Extract the file descriptor */ |
| 648 |
cmsg = CMSG_FIRSTHDR(&msg); |
| 649 |
if (!cmsg || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) |
| 650 |
return -EIO; |
| 651 |
|
| 652 |
memcpy(&received_fd, CMSG_DATA(cmsg), sizeof(int)); |
| 653 |
|
| 654 |
/* Verify we can use the fd */ |
| 655 |
if (fcntl(received_fd, F_GETFD) < 0) |
| 656 |
return -errno; |
| 657 |
|
| 658 |
close(received_fd); |
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
TEST(fiber_io_sendmsg_recvmsg_fd) { |
| 663 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 664 |
ASSERT_OK(sd_event_new(&e)); |
| 665 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 666 |
|
| 667 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 668 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sockfd)); |
| 669 |
|
| 670 |
/* Create a test file descriptor to send */ |
| 671 |
_cleanup_close_ int test_fd = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 672 |
ASSERT_OK_ERRNO(test_fd); |
| 673 |
|
| 674 |
_cleanup_(sd_future_unrefp) sd_future *fs = NULL, *fr = NULL; |
| 675 |
int args[2] = { sockfd[0], test_fd }; |
| 676 |
ASSERT_OK(sd_fiber_new(e, "socket-recvmsg-fd", socket_recvmsg_fd_fiber, INT_TO_PTR(sockfd[1]), NULL, &fr)); |
| 677 |
ASSERT_OK(sd_future_set_priority(fr, 1)); |
| 678 |
ASSERT_OK(sd_fiber_new(e, "socket-sendmsg-fd", socket_sendmsg_fd_fiber, args, NULL, &fs)); |
| 679 |
ASSERT_OK(sd_future_set_priority(fs, 0)); |
| 680 |
|
| 681 |
ASSERT_OK(sd_event_loop(e)); |
| 682 |
|
| 683 |
ASSERT_OK(sd_future_result(fr)); |
| 684 |
ASSERT_OK_EQ(sd_future_result(fs), 1); |
| 685 |
} |
| 686 |
|
| 687 |
TEST(fiber_io_socket_fallback) { |
| 688 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 689 |
char buf[STRLEN("fallback")] = {}; |
| 690 |
|
| 691 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sockfd)); |
| 692 |
|
| 693 |
/* Test send/recv without fiber context */ |
| 694 |
ASSERT_OK_EQ(sd_fiber_send(sockfd[0], "fallback", sizeof(buf), 0), (ssize_t) sizeof(buf)); |
| 695 |
ASSERT_OK_EQ(sd_fiber_recv(sockfd[1], buf, sizeof(buf), 0), (ssize_t) sizeof(buf)); |
| 696 |
|
| 697 |
/* Test sendto/recvfrom without fiber context */ |
| 698 |
ASSERT_OK_EQ(sd_fiber_sendto(sockfd[0], "fallback", sizeof(buf), 0, NULL, 0), (ssize_t) sizeof(buf)); |
| 699 |
ASSERT_OK_EQ(sd_fiber_recvfrom(sockfd[1], buf, sizeof(buf), 0, NULL, NULL), (ssize_t) sizeof(buf)); |
| 700 |
} |
| 701 |
|
| 702 |
static int blocking_recv_fiber(void *userdata) { |
| 703 |
int sockfd = PTR_TO_INT(userdata); |
| 704 |
char buf[64]; |
| 705 |
|
| 706 |
return sd_fiber_recv(sockfd, buf, sizeof(buf), 0); |
| 707 |
} |
| 708 |
|
| 709 |
TEST(fiber_io_socket_cancel) { |
| 710 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 711 |
ASSERT_OK(sd_event_new(&e)); |
| 712 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 713 |
|
| 714 |
_cleanup_close_pair_ int sockfd[2] = EBADF_PAIR; |
| 715 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sockfd)); |
| 716 |
|
| 717 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 718 |
ASSERT_OK(sd_fiber_new(e, "blocking-recv", blocking_recv_fiber, INT_TO_PTR(sockfd[0]), NULL, &f)); |
| 719 |
|
| 720 |
/* Run once - fiber will suspend on recv */ |
| 721 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 722 |
|
| 723 |
/* Cancel the fiber */ |
| 724 |
ASSERT_OK(sd_future_cancel(f)); |
| 725 |
|
| 726 |
/* Run to completion */ |
| 727 |
ASSERT_OK(sd_event_loop(e)); |
| 728 |
|
| 729 |
/* Should be cancelled */ |
| 730 |
ASSERT_ERROR(sd_future_result(f), ECANCELED); |
| 731 |
} |
| 732 |
|
| 733 |
/* Test: Basic accept operation */ |
| 734 |
static int accept_fiber(void *userdata) { |
| 735 |
int listen_fd = PTR_TO_INT(userdata); |
| 736 |
struct sockaddr_un addr; |
| 737 |
socklen_t addr_len = sizeof(addr); |
| 738 |
int client_fd; |
| 739 |
|
| 740 |
client_fd = sd_fiber_accept(listen_fd, (struct sockaddr*) &addr, &addr_len, SOCK_CLOEXEC); |
| 741 |
if (client_fd < 0) |
| 742 |
return client_fd; |
| 743 |
|
| 744 |
close(client_fd); |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
TEST(fiber_io_accept_basic) { |
| 749 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 750 |
ASSERT_OK(sd_event_new(&e)); |
| 751 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 752 |
|
| 753 |
/* Create listening socket with abstract namespace */ |
| 754 |
_cleanup_close_ int listen_fd = -EBADF; |
| 755 |
ASSERT_OK_ERRNO(listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
| 756 |
|
| 757 |
struct sockaddr_un addr = { |
| 758 |
.sun_family = AF_UNIX, |
| 759 |
}; |
| 760 |
addr.sun_path[0] = '\0'; |
| 761 |
snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, "test-fiber-accept-%d", getpid()); |
| 762 |
|
| 763 |
ASSERT_OK_ERRNO(bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 764 |
ASSERT_OK_ERRNO(listen(listen_fd, 1)); |
| 765 |
|
| 766 |
/* Create fiber to accept connection */ |
| 767 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 768 |
ASSERT_OK(sd_fiber_new(e, "accept", accept_fiber, INT_TO_PTR(listen_fd), NULL, &f)); |
| 769 |
|
| 770 |
/* Connect from outside fiber context */ |
| 771 |
_cleanup_close_ int connect_fd = -EBADF; |
| 772 |
ASSERT_OK(connect_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
| 773 |
ASSERT_OK(connect(connect_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 774 |
|
| 775 |
/* Run the event loop - accept should complete */ |
| 776 |
ASSERT_OK(sd_event_loop(e)); |
| 777 |
ASSERT_OK(sd_future_result(f)); |
| 778 |
} |
| 779 |
|
| 780 |
/* Test: Multiple sequential accepts */ |
| 781 |
static int accept_multiple_fiber(void *userdata) { |
| 782 |
int listen_fd = PTR_TO_INT(userdata); |
| 783 |
struct sockaddr_un addr; |
| 784 |
socklen_t addr_len; |
| 785 |
int count = 0; |
| 786 |
|
| 787 |
for (int i = 0; i < 3; i++) { |
| 788 |
_cleanup_close_ int client_fd = -EBADF; |
| 789 |
|
| 790 |
addr_len = sizeof(addr); |
| 791 |
client_fd = sd_fiber_accept(listen_fd, (struct sockaddr*) &addr, &addr_len, SOCK_CLOEXEC); |
| 792 |
if (client_fd < 0) |
| 793 |
return client_fd; |
| 794 |
|
| 795 |
count++; |
| 796 |
} |
| 797 |
|
| 798 |
return count; |
| 799 |
} |
| 800 |
|
| 801 |
TEST(fiber_io_accept_multiple) { |
| 802 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 803 |
ASSERT_OK(sd_event_new(&e)); |
| 804 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 805 |
|
| 806 |
/* Create listening socket */ |
| 807 |
_cleanup_close_ int listen_fd = -EBADF; |
| 808 |
ASSERT_OK(listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
| 809 |
|
| 810 |
struct sockaddr_un addr = { |
| 811 |
.sun_family = AF_UNIX, |
| 812 |
}; |
| 813 |
addr.sun_path[0] = '\0'; |
| 814 |
snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, "test-fiber-accept-multi-%d", getpid()); |
| 815 |
|
| 816 |
ASSERT_OK(bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 817 |
ASSERT_OK(listen(listen_fd, 5)); |
| 818 |
|
| 819 |
/* Create fiber to accept multiple connections */ |
| 820 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 821 |
ASSERT_OK(sd_fiber_new(e, "accept-multiple", accept_multiple_fiber, INT_TO_PTR(listen_fd), NULL, &f)); |
| 822 |
|
| 823 |
/* Connect multiple times */ |
| 824 |
int connect_fds[3] = { -EBADF, -EBADF, -EBADF }; |
| 825 |
for (size_t i = 0; i < 3; i++) { |
| 826 |
connect_fds[i] = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); |
| 827 |
ASSERT_OK(connect_fds[i]); |
| 828 |
ASSERT_OK(connect(connect_fds[i], (struct sockaddr*) &addr, sizeof(addr))); |
| 829 |
} |
| 830 |
|
| 831 |
/* Run the event loop */ |
| 832 |
ASSERT_OK(sd_event_loop(e)); |
| 833 |
ASSERT_OK_EQ(sd_future_result(f), 3); |
| 834 |
|
| 835 |
/* Clean up connection fds */ |
| 836 |
for (size_t i = 0; i < 3; i++) |
| 837 |
safe_close(connect_fds[i]); |
| 838 |
} |
| 839 |
|
| 840 |
/* Test: Accept and exchange data */ |
| 841 |
static int accept_and_read_fiber(void *userdata) { |
| 842 |
int listen_fd = PTR_TO_INT(userdata); |
| 843 |
_cleanup_close_ int client_fd = -EBADF; |
| 844 |
char buf[64]; |
| 845 |
ssize_t n; |
| 846 |
|
| 847 |
client_fd = sd_fiber_accept(listen_fd, NULL, NULL, SOCK_CLOEXEC); |
| 848 |
if (client_fd < 0) |
| 849 |
return client_fd; |
| 850 |
|
| 851 |
n = sd_fiber_read(client_fd, buf, sizeof(buf)); |
| 852 |
if (n < 0) |
| 853 |
return (int) n; |
| 854 |
|
| 855 |
if (n != 5 || memcmp(buf, "hello", 5) != 0) |
| 856 |
return -EIO; |
| 857 |
|
| 858 |
return 0; |
| 859 |
} |
| 860 |
|
| 861 |
TEST(fiber_io_accept_and_read) { |
| 862 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 863 |
ASSERT_OK(sd_event_new(&e)); |
| 864 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 865 |
|
| 866 |
/* Create listening socket */ |
| 867 |
_cleanup_close_ int listen_fd = -EBADF; |
| 868 |
ASSERT_OK(listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
| 869 |
|
| 870 |
struct sockaddr_un addr = { |
| 871 |
.sun_family = AF_UNIX, |
| 872 |
}; |
| 873 |
addr.sun_path[0] = '\0'; |
| 874 |
snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, "test-fiber-accept-read-%d", getpid()); |
| 875 |
|
| 876 |
ASSERT_OK(bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 877 |
ASSERT_OK(listen(listen_fd, 1)); |
| 878 |
|
| 879 |
/* Create fiber to accept and read */ |
| 880 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 881 |
ASSERT_OK(sd_fiber_new(e, "accept-and-read", accept_and_read_fiber, INT_TO_PTR(listen_fd), NULL, &f)); |
| 882 |
|
| 883 |
/* Connect and send data */ |
| 884 |
_cleanup_close_ int connect_fd = -EBADF; |
| 885 |
ASSERT_OK(connect_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
| 886 |
ASSERT_OK(connect(connect_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 887 |
ASSERT_OK_EQ_ERRNO(write(connect_fd, "hello", 5), 5); |
| 888 |
|
| 889 |
/* Run the event loop */ |
| 890 |
ASSERT_OK(sd_event_loop(e)); |
| 891 |
ASSERT_OK(sd_future_result(f)); |
| 892 |
} |
| 893 |
|
| 894 |
/* Test: poll with single fd ready immediately */ |
| 895 |
static int poll_immediate_fiber(void *userdata) { |
| 896 |
int *pipefd = userdata; |
| 897 |
struct pollfd fds[] = { |
| 898 |
{ .fd = pipefd[0], .events = POLLIN }, |
| 899 |
}; |
| 900 |
int r; |
| 901 |
|
| 902 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL); |
| 903 |
if (r < 0) |
| 904 |
return r; |
| 905 |
|
| 906 |
/* Should have one fd ready */ |
| 907 |
if (r != 1) |
| 908 |
return -EIO; |
| 909 |
|
| 910 |
if (!(fds[0].revents & POLLIN)) |
| 911 |
return -EIO; |
| 912 |
|
| 913 |
return 0; |
| 914 |
} |
| 915 |
|
| 916 |
TEST(fiber_poll_immediate) { |
| 917 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 918 |
ASSERT_OK(sd_event_new(&e)); |
| 919 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 920 |
|
| 921 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 922 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 923 |
|
| 924 |
/* Write data before creating fiber */ |
| 925 |
ASSERT_OK_EQ_ERRNO(write(pipefd[1], "X", 1), 1); |
| 926 |
|
| 927 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 928 |
ASSERT_OK(sd_fiber_new(e, "poll-immediate", poll_immediate_fiber, pipefd, NULL, &f)); |
| 929 |
|
| 930 |
ASSERT_OK(sd_event_loop(e)); |
| 931 |
ASSERT_OK(sd_future_result(f)); |
| 932 |
} |
| 933 |
|
| 934 |
/* Test: poll with fd that becomes ready after suspension */ |
| 935 |
static int poll_fiber(void *userdata) { |
| 936 |
int *pipefd = userdata; |
| 937 |
struct pollfd fds[] = { |
| 938 |
{ .fd = pipefd[0], .events = POLLIN }, |
| 939 |
}; |
| 940 |
int r; |
| 941 |
|
| 942 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL); |
| 943 |
if (r < 0) |
| 944 |
return r; |
| 945 |
|
| 946 |
if (r != 1 || !(fds[0].revents & POLLIN)) |
| 947 |
return -EIO; |
| 948 |
|
| 949 |
/* Read the data */ |
| 950 |
char buf[1]; |
| 951 |
if (read(pipefd[0], buf, 1) != 1 || buf[0] != 'Y') |
| 952 |
return -EIO; |
| 953 |
|
| 954 |
return 0; |
| 955 |
} |
| 956 |
|
| 957 |
TEST(fiber_poll) { |
| 958 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 959 |
ASSERT_OK(sd_event_new(&e)); |
| 960 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 961 |
|
| 962 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 963 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 964 |
|
| 965 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 966 |
ASSERT_OK(sd_fiber_new(e, "poll-suspend", poll_fiber, pipefd, NULL, &f)); |
| 967 |
|
| 968 |
/* Run once - fiber will suspend on poll */ |
| 969 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 970 |
|
| 971 |
/* Write data to wake it up */ |
| 972 |
ASSERT_OK_EQ_ERRNO(write(pipefd[1], "Y", 1), 1); |
| 973 |
|
| 974 |
/* Complete execution */ |
| 975 |
ASSERT_OK(sd_event_loop(e)); |
| 976 |
ASSERT_OK(sd_future_result(f)); |
| 977 |
} |
| 978 |
|
| 979 |
/* Test: poll with multiple fds */ |
| 980 |
static int poll_multiple_fiber(void *userdata) { |
| 981 |
int (*pipes)[2] = userdata; |
| 982 |
struct pollfd fds[] = { |
| 983 |
{ .fd = pipes[0][0], .events = POLLIN }, |
| 984 |
{ .fd = pipes[1][0], .events = POLLIN }, |
| 985 |
{ .fd = pipes[2][0], .events = POLLIN }, |
| 986 |
}; |
| 987 |
int r; |
| 988 |
|
| 989 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL); |
| 990 |
if (r < 0) |
| 991 |
return r; |
| 992 |
|
| 993 |
/* Should have all three ready */ |
| 994 |
if (r != 3) |
| 995 |
return -EIO; |
| 996 |
|
| 997 |
for (size_t i = 0; i < 3; i++) { |
| 998 |
if (!(fds[i].revents & POLLIN)) |
| 999 |
return -EIO; |
| 1000 |
|
| 1001 |
char buf[1]; |
| 1002 |
if (read(fds[i].fd, buf, 1) != 1 || buf[0] != (char) ('A' + i)) |
| 1003 |
return -EIO; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return 0; |
| 1007 |
} |
| 1008 |
|
| 1009 |
TEST(fiber_poll_multiple) { |
| 1010 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1011 |
ASSERT_OK(sd_event_new(&e)); |
| 1012 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1013 |
|
| 1014 |
/* Create three pipes */ |
| 1015 |
int pipes[3][2]; |
| 1016 |
for (size_t i = 0; i < 3; i++) |
| 1017 |
ASSERT_OK_ERRNO(pipe2(pipes[i], O_CLOEXEC | O_NONBLOCK)); |
| 1018 |
|
| 1019 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1020 |
ASSERT_OK(sd_fiber_new(e, "poll-multiple", poll_multiple_fiber, pipes, NULL, &f)); |
| 1021 |
|
| 1022 |
/* Run once - fiber will suspend waiting for data */ |
| 1023 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 1024 |
|
| 1025 |
/* Write to all three pipes in different order */ |
| 1026 |
ASSERT_OK_EQ_ERRNO(write(pipes[2][1], "C", 1), 1); |
| 1027 |
ASSERT_OK_EQ_ERRNO(write(pipes[0][1], "A", 1), 1); |
| 1028 |
ASSERT_OK_EQ_ERRNO(write(pipes[1][1], "B", 1), 1); |
| 1029 |
|
| 1030 |
ASSERT_OK(sd_event_loop(e)); |
| 1031 |
ASSERT_OK(sd_future_result(f)); |
| 1032 |
|
| 1033 |
for (size_t i = 0; i < 3; i++) |
| 1034 |
safe_close_pair(pipes[i]); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* Test: poll with POLLOUT (write readiness) */ |
| 1038 |
static int poll_pollout_fiber(void *userdata) { |
| 1039 |
int *pipefd = userdata; |
| 1040 |
struct pollfd fds[] = { |
| 1041 |
{ .fd = pipefd[1], .events = POLLOUT }, |
| 1042 |
}; |
| 1043 |
int r; |
| 1044 |
|
| 1045 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL); |
| 1046 |
if (r < 0) |
| 1047 |
return r; |
| 1048 |
|
| 1049 |
if (r != 1 || !(fds[0].revents & POLLOUT)) |
| 1050 |
return -EIO; |
| 1051 |
|
| 1052 |
/* Pipe should be writable */ |
| 1053 |
if (write(pipefd[1], "Z", 1) != 1) |
| 1054 |
return -errno; |
| 1055 |
|
| 1056 |
return 0; |
| 1057 |
} |
| 1058 |
|
| 1059 |
TEST(fiber_poll_pollout) { |
| 1060 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1061 |
ASSERT_OK(sd_event_new(&e)); |
| 1062 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1063 |
|
| 1064 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 1065 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 1066 |
|
| 1067 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1068 |
ASSERT_OK(sd_fiber_new(e, "poll-pollout", poll_pollout_fiber, pipefd, NULL, &f)); |
| 1069 |
|
| 1070 |
ASSERT_OK(sd_event_loop(e)); |
| 1071 |
ASSERT_OK(sd_future_result(f)); |
| 1072 |
|
| 1073 |
/* Verify data was written */ |
| 1074 |
char buf[1]; |
| 1075 |
ASSERT_OK_EQ_ERRNO(read(pipefd[0], buf, 1), 1); |
| 1076 |
ASSERT_EQ(buf[0], 'Z'); |
| 1077 |
} |
| 1078 |
|
| 1079 |
/* Test: poll with timeout that expires */ |
| 1080 |
static int poll_timeout_fiber(void *userdata) { |
| 1081 |
int *pipefd = userdata; |
| 1082 |
struct pollfd fds[] = { |
| 1083 |
{ .fd = pipefd[0], .events = POLLIN }, |
| 1084 |
}; |
| 1085 |
int r; |
| 1086 |
|
| 1087 |
/* Poll with 100ms timeout - no data will arrive */ |
| 1088 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), &(struct timespec) { .tv_nsec = 100 * NSEC_PER_MSEC }, NULL); |
| 1089 |
if (r < 0) |
| 1090 |
return r; |
| 1091 |
|
| 1092 |
/* Should timeout with no fds ready */ |
| 1093 |
if (r != 0) |
| 1094 |
return -EIO; |
| 1095 |
|
| 1096 |
return 0; |
| 1097 |
} |
| 1098 |
|
| 1099 |
TEST(fiber_poll_timeout) { |
| 1100 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1101 |
ASSERT_OK(sd_event_new(&e)); |
| 1102 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1103 |
|
| 1104 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 1105 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 1106 |
|
| 1107 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1108 |
ASSERT_OK(sd_fiber_new(e, "poll-timeout", poll_timeout_fiber, pipefd, NULL, &f)); |
| 1109 |
|
| 1110 |
ASSERT_OK(sd_event_loop(e)); |
| 1111 |
ASSERT_OK(sd_future_result(f)); |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Test: poll with zero timeout (should not block) */ |
| 1115 |
static int poll_zero_timeout_fiber(void *userdata) { |
| 1116 |
int *pipefd = userdata; |
| 1117 |
struct pollfd fds[] = { |
| 1118 |
{ .fd = pipefd[0], .events = POLLIN }, |
| 1119 |
}; |
| 1120 |
int r; |
| 1121 |
|
| 1122 |
/* Poll with zero timeout - should return immediately */ |
| 1123 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), &(struct timespec) {}, NULL); |
| 1124 |
if (r < 0) |
| 1125 |
return r; |
| 1126 |
|
| 1127 |
/* No data available, so should return 0 */ |
| 1128 |
if (r != 0) |
| 1129 |
return -EIO; |
| 1130 |
|
| 1131 |
/* Now write data */ |
| 1132 |
if (write(pipefd[1], "Q", 1) != 1) |
| 1133 |
return -errno; |
| 1134 |
|
| 1135 |
/* Poll again with zero timeout - should see data */ |
| 1136 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL); |
| 1137 |
if (r < 0) |
| 1138 |
return r; |
| 1139 |
|
| 1140 |
if (r != 1 || !(fds[0].revents & POLLIN)) |
| 1141 |
return -EIO; |
| 1142 |
|
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
|
| 1146 |
TEST(fiber_poll_zero_timeout) { |
| 1147 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1148 |
ASSERT_OK(sd_event_new(&e)); |
| 1149 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1150 |
|
| 1151 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 1152 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 1153 |
|
| 1154 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1155 |
ASSERT_OK(sd_fiber_new(e, "poll-zero-timeout", poll_zero_timeout_fiber, pipefd, NULL, &f)); |
| 1156 |
|
| 1157 |
ASSERT_OK(sd_event_loop(e)); |
| 1158 |
ASSERT_OK(sd_future_result(f)); |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* Test: poll with zero fds and zero timeout (should return immediately) */ |
| 1162 |
static int poll_zero_fds_fiber(void *userdata) { |
| 1163 |
return sd_fiber_ppoll(NULL, 0, &(struct timespec) {}, NULL); |
| 1164 |
} |
| 1165 |
|
| 1166 |
TEST(fiber_poll_zero_fds) { |
| 1167 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1168 |
ASSERT_OK(sd_event_new(&e)); |
| 1169 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1170 |
|
| 1171 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1172 |
ASSERT_OK(sd_fiber_new(e, "poll-zero-fds", poll_zero_fds_fiber, NULL, NULL, &f)); |
| 1173 |
|
| 1174 |
ASSERT_OK(sd_event_loop(e)); |
| 1175 |
ASSERT_OK_EQ(sd_future_result(f), 0); |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* Test: poll with zero fds and no timeout has no possible wakeup, must reject with -EINVAL */ |
| 1179 |
static int poll_zero_fds_no_timeout_fiber(void *userdata) { |
| 1180 |
return sd_fiber_ppoll(NULL, 0, NULL, NULL); |
| 1181 |
} |
| 1182 |
|
| 1183 |
TEST(fiber_poll_zero_fds_no_timeout) { |
| 1184 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1185 |
ASSERT_OK(sd_event_new(&e)); |
| 1186 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1187 |
|
| 1188 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1189 |
ASSERT_OK(sd_fiber_new(e, "poll-zero-fds-no-timeout", poll_zero_fds_no_timeout_fiber, NULL, NULL, &f)); |
| 1190 |
|
| 1191 |
ASSERT_OK(sd_event_loop(e)); |
| 1192 |
ASSERT_ERROR(sd_future_result(f), EINVAL); |
| 1193 |
} |
| 1194 |
|
| 1195 |
/* Test: poll with negative fd (should be ignored) */ |
| 1196 |
static int poll_negative_fd_fiber(void *userdata) { |
| 1197 |
int *pipefd = userdata; |
| 1198 |
struct pollfd fds[] = { |
| 1199 |
{ .fd = -1, .events = POLLIN }, |
| 1200 |
{ .fd = pipefd[0], .events = POLLIN }, |
| 1201 |
}; |
| 1202 |
int r; |
| 1203 |
|
| 1204 |
r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL); |
| 1205 |
if (r < 0) |
| 1206 |
return r; |
| 1207 |
|
| 1208 |
/* Only the second fd should be ready */ |
| 1209 |
if (r != 1 || !(fds[1].revents & POLLIN)) |
| 1210 |
return -EIO; |
| 1211 |
|
| 1212 |
/* First fd should have no events */ |
| 1213 |
if (fds[0].revents != 0) |
| 1214 |
return -EIO; |
| 1215 |
|
| 1216 |
return 0; |
| 1217 |
} |
| 1218 |
|
| 1219 |
TEST(fiber_poll_negative_fd) { |
| 1220 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1221 |
ASSERT_OK(sd_event_new(&e)); |
| 1222 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1223 |
|
| 1224 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 1225 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 1226 |
|
| 1227 |
/* Write data before creating fiber */ |
| 1228 |
ASSERT_OK_EQ_ERRNO(write(pipefd[1], "N", 1), 1); |
| 1229 |
|
| 1230 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1231 |
ASSERT_OK(sd_fiber_new(e, "poll-negative-fd", poll_negative_fd_fiber, pipefd, NULL, &f)); |
| 1232 |
|
| 1233 |
ASSERT_OK(sd_event_loop(e)); |
| 1234 |
ASSERT_OK(sd_future_result(f)); |
| 1235 |
} |
| 1236 |
|
| 1237 |
/* Test: Multiple fibers waiting on the same fd */ |
| 1238 |
typedef struct SharedFdArgs { |
| 1239 |
int pipefd; |
| 1240 |
int *counter; |
| 1241 |
} SharedFdArgs; |
| 1242 |
|
| 1243 |
static int shared_fd_read_fiber(void *userdata) { |
| 1244 |
SharedFdArgs *args = ASSERT_PTR(userdata); |
| 1245 |
char buf[1]; |
| 1246 |
ssize_t n; |
| 1247 |
|
| 1248 |
n = sd_fiber_read(args->pipefd, buf, sizeof(buf)); |
| 1249 |
if (n < 0) |
| 1250 |
return (int) n; |
| 1251 |
|
| 1252 |
if (n != 1) |
| 1253 |
return -EIO; |
| 1254 |
|
| 1255 |
/* Increment counter to track successful reads */ |
| 1256 |
(*args->counter)++; |
| 1257 |
|
| 1258 |
return 0; |
| 1259 |
} |
| 1260 |
|
| 1261 |
TEST(fiber_io_same_fd_multiple_fibers) { |
| 1262 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1263 |
ASSERT_OK(sd_event_new(&e)); |
| 1264 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1265 |
|
| 1266 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 1267 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK)); |
| 1268 |
|
| 1269 |
/* Create 3 fibers all waiting on the same pipe read end */ |
| 1270 |
sd_future *fibers[3] = {}; |
| 1271 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 1272 |
SharedFdArgs args[3]; |
| 1273 |
int counter = 0; |
| 1274 |
|
| 1275 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 1276 |
args[i].pipefd = pipefd[0]; |
| 1277 |
args[i].counter = &counter; |
| 1278 |
ASSERT_OK(sd_fiber_new(e, "shared-fd-read", shared_fd_read_fiber, &args[i], NULL, &fibers[i])); |
| 1279 |
} |
| 1280 |
|
| 1281 |
/* All fibers should suspend waiting for data */ |
| 1282 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 1283 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 1284 |
|
| 1285 |
/* Write 3 bytes - each byte will wake one fiber */ |
| 1286 |
ASSERT_OK_EQ_ERRNO(write(pipefd[1], "ABC", 3), 3); |
| 1287 |
|
| 1288 |
/* Run until all fibers complete */ |
| 1289 |
ASSERT_OK(sd_event_loop(e)); |
| 1290 |
|
| 1291 |
/* All should complete successfully and each should have read one byte */ |
| 1292 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 1293 |
ASSERT_OK(sd_future_result(fibers[i])); |
| 1294 |
|
| 1295 |
ASSERT_EQ(counter, 3); |
| 1296 |
} |
| 1297 |
|
| 1298 |
static int blocking_fd_preserve_fiber(void *userdata) { |
| 1299 |
int *pipefd = ASSERT_PTR(userdata); |
| 1300 |
char buf[8] = {}; |
| 1301 |
ssize_t n; |
| 1302 |
|
| 1303 |
/* The pipe has data pre-filled, so this should succeed immediately on the fast path. |
| 1304 |
* This exercises the fd blocking state restore: fiber_io_operation() temporarily sets the fd |
| 1305 |
* to nonblocking, and must restore it to blocking on the success path. */ |
| 1306 |
n = sd_fiber_read(pipefd[0], buf, sizeof(buf)); |
| 1307 |
if (n < 0) |
| 1308 |
return (int) n; |
| 1309 |
|
| 1310 |
if ((size_t) n != sizeof(buf) || memcmp(buf, "blocking", sizeof(buf)) != 0) |
| 1311 |
return -EIO; |
| 1312 |
|
| 1313 |
return 0; |
| 1314 |
} |
| 1315 |
|
| 1316 |
TEST(fiber_io_blocking_fd_preserved) { |
| 1317 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1318 |
ASSERT_OK(sd_event_new(&e)); |
| 1319 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1320 |
|
| 1321 |
/* Create a blocking pipe (no O_NONBLOCK) */ |
| 1322 |
_cleanup_close_pair_ int pipefd[2] = EBADF_PAIR; |
| 1323 |
ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC)); |
| 1324 |
|
| 1325 |
/* Pre-fill the pipe so the read will succeed immediately */ |
| 1326 |
ASSERT_OK_EQ_ERRNO(write(pipefd[1], "blocking", 8), 8); |
| 1327 |
|
| 1328 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1329 |
ASSERT_OK(sd_fiber_new(e, "blocking-fd-preserve", blocking_fd_preserve_fiber, pipefd, NULL, &f)); |
| 1330 |
|
| 1331 |
ASSERT_OK(sd_event_loop(e)); |
| 1332 |
ASSERT_OK(sd_future_result(f)); |
| 1333 |
|
| 1334 |
/* Verify the read end is still in blocking mode after the fiber completed */ |
| 1335 |
ASSERT_OK_ZERO(fd_nonblock(pipefd[0], false)); |
| 1336 |
} |
| 1337 |
|
| 1338 |
static int socket_connect_blocking_fiber(void *userdata) { |
| 1339 |
struct sockaddr_un *addr = userdata; |
| 1340 |
_cleanup_close_ int sockfd = -EBADF; |
| 1341 |
|
| 1342 |
/* Use a blocking socket (no SOCK_NONBLOCK). sd_fiber_connect() should temporarily set it |
| 1343 |
* to nonblocking, handle the EINPROGRESS path with getsockopt(SO_ERROR), and restore |
| 1344 |
* the blocking state. */ |
| 1345 |
sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 1346 |
if (sockfd < 0) |
| 1347 |
return -errno; |
| 1348 |
|
| 1349 |
int r = sd_fiber_connect(sockfd, (struct sockaddr*) addr, sizeof(*addr)); |
| 1350 |
if (r < 0) |
| 1351 |
return r; |
| 1352 |
|
| 1353 |
/* Verify the socket is back in blocking mode */ |
| 1354 |
r = fd_nonblock(sockfd, false); |
| 1355 |
if (r < 0) |
| 1356 |
return r; |
| 1357 |
if (r > 0) |
| 1358 |
return -EBUSY; /* fd was nonblocking, but should have been restored to blocking */ |
| 1359 |
|
| 1360 |
return 0; |
| 1361 |
} |
| 1362 |
|
| 1363 |
TEST(fiber_io_connect_blocking) { |
| 1364 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1365 |
ASSERT_OK(sd_event_new(&e)); |
| 1366 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1367 |
|
| 1368 |
/* Create listening socket */ |
| 1369 |
_cleanup_close_ int listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); |
| 1370 |
ASSERT_OK(listen_fd); |
| 1371 |
|
| 1372 |
struct sockaddr_un addr = { |
| 1373 |
.sun_family = AF_UNIX, |
| 1374 |
}; |
| 1375 |
addr.sun_path[0] = '\0'; |
| 1376 |
snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, "test-fiber-connect-blocking-%d", getpid()); |
| 1377 |
|
| 1378 |
ASSERT_OK(bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr))); |
| 1379 |
ASSERT_OK(listen(listen_fd, 1)); |
| 1380 |
|
| 1381 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1382 |
ASSERT_OK(sd_fiber_new(e, "connect-blocking", socket_connect_blocking_fiber, &addr, NULL, &f)); |
| 1383 |
|
| 1384 |
ASSERT_OK(sd_event_loop(e)); |
| 1385 |
ASSERT_OK(sd_future_result(f)); |
| 1386 |
} |
| 1387 |
|
| 1388 |
DEFINE_TEST_MAIN(LOG_DEBUG); |