| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/wait.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "event-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "fs-util.h" |
| 12 |
#include "log.h" |
| 13 |
#include "parse-util.h" |
| 14 |
#include "path-util.h" |
| 15 |
#include "pidfd-util.h" |
| 16 |
#include "pidref.h" |
| 17 |
#include "process-util.h" |
| 18 |
#include "random-util.h" |
| 19 |
#include "rm-rf.h" |
| 20 |
#include "signal-util.h" |
| 21 |
#include "stdio-util.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "tests.h" |
| 24 |
#include "time-util.h" |
| 25 |
#include "tmpfile-util.h" |
| 26 |
|
| 27 |
static int prepare_handler(sd_event_source *s, void *userdata) { |
| 28 |
log_info("preparing %c", PTR_TO_INT(userdata)); |
| 29 |
return 1; |
| 30 |
} |
| 31 |
|
| 32 |
static bool got_a, got_b, got_c, got_unref; |
| 33 |
static unsigned got_d; |
| 34 |
|
| 35 |
static int unref_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 36 |
sd_event_source_unref(s); |
| 37 |
got_unref = true; |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 42 |
|
| 43 |
log_info("got IO on %c", PTR_TO_INT(userdata)); |
| 44 |
|
| 45 |
if (userdata == INT_TO_PTR('a')) { |
| 46 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_OFF)); |
| 47 |
ASSERT_FALSE(got_a); |
| 48 |
got_a = true; |
| 49 |
} else if (userdata == INT_TO_PTR('b')) { |
| 50 |
ASSERT_FALSE(got_b); |
| 51 |
got_b = true; |
| 52 |
} else if (userdata == INT_TO_PTR('d')) { |
| 53 |
got_d++; |
| 54 |
if (got_d < 2) |
| 55 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT)); |
| 56 |
else |
| 57 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_OFF)); |
| 58 |
} else |
| 59 |
assert_not_reached(); |
| 60 |
|
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 65 |
|
| 66 |
ASSERT_NOT_NULL(s); |
| 67 |
ASSERT_NOT_NULL(si); |
| 68 |
|
| 69 |
ASSERT_EQ(si->si_uid, getuid()); |
| 70 |
ASSERT_EQ(si->si_signo, SIGCHLD); |
| 71 |
ASSERT_EQ(si->si_code, CLD_EXITED); |
| 72 |
ASSERT_EQ(si->si_status, 78); |
| 73 |
|
| 74 |
log_info("got child on %c", PTR_TO_INT(userdata)); |
| 75 |
|
| 76 |
ASSERT_PTR_EQ(userdata, INT_TO_PTR('f')); |
| 77 |
|
| 78 |
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0)); |
| 79 |
sd_event_source_unref(s); |
| 80 |
|
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 85 |
sd_event_source *p = NULL; |
| 86 |
pid_t pid; |
| 87 |
siginfo_t plain_si; |
| 88 |
|
| 89 |
ASSERT_NOT_NULL(s); |
| 90 |
ASSERT_NOT_NULL(si); |
| 91 |
|
| 92 |
log_info("got signal on %c", PTR_TO_INT(userdata)); |
| 93 |
|
| 94 |
ASSERT_PTR_EQ(userdata, INT_TO_PTR('e')); |
| 95 |
|
| 96 |
ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR2)); |
| 97 |
|
| 98 |
ASSERT_OK_ERRNO(pid = fork()); |
| 99 |
|
| 100 |
if (pid == 0) { |
| 101 |
sigset_t ss; |
| 102 |
|
| 103 |
ASSERT_OK_ERRNO(sigemptyset(&ss)); |
| 104 |
ASSERT_OK_ERRNO(sigaddset(&ss, SIGUSR2)); |
| 105 |
|
| 106 |
zero(plain_si); |
| 107 |
ASSERT_OK_ERRNO(sigwaitinfo(&ss, &plain_si)); |
| 108 |
|
| 109 |
ASSERT_EQ(plain_si.si_signo, SIGUSR2); |
| 110 |
ASSERT_EQ(plain_si.si_value.sival_int, 4711); |
| 111 |
|
| 112 |
_exit(78); |
| 113 |
} |
| 114 |
|
| 115 |
ASSERT_OK(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f'))); |
| 116 |
ASSERT_OK(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT)); |
| 117 |
ASSERT_OK(sd_event_source_set_child_process_own(p, true)); |
| 118 |
|
| 119 |
/* We can't use structured initialization here, since the structure contains various unions and these |
| 120 |
* fields lie in overlapping (carefully aligned) unions that LLVM is allergic to allow assignments |
| 121 |
* to */ |
| 122 |
zero(plain_si); |
| 123 |
plain_si.si_signo = SIGUSR2; |
| 124 |
plain_si.si_code = SI_QUEUE; |
| 125 |
plain_si.si_pid = getpid_cached(); |
| 126 |
plain_si.si_uid = getuid(); |
| 127 |
plain_si.si_value.sival_int = 4711; |
| 128 |
|
| 129 |
ASSERT_OK(sd_event_source_send_child_signal(p, SIGUSR2, &plain_si, 0)); |
| 130 |
|
| 131 |
sd_event_source_unref(s); |
| 132 |
|
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
static int defer_handler(sd_event_source *s, void *userdata) { |
| 137 |
sd_event_source *p = NULL; |
| 138 |
|
| 139 |
ASSERT_NOT_NULL(s); |
| 140 |
|
| 141 |
log_info("got defer on %c", PTR_TO_INT(userdata)); |
| 142 |
|
| 143 |
ASSERT_PTR_EQ(userdata, INT_TO_PTR('d')); |
| 144 |
|
| 145 |
ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1)); |
| 146 |
|
| 147 |
ASSERT_OK(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e'))); |
| 148 |
ASSERT_OK(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT)); |
| 149 |
raise(SIGUSR1); |
| 150 |
|
| 151 |
sd_event_source_unref(s); |
| 152 |
|
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
static bool do_quit; |
| 157 |
|
| 158 |
static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
| 159 |
log_info("got timer on %c", PTR_TO_INT(userdata)); |
| 160 |
|
| 161 |
if (userdata == INT_TO_PTR('c')) { |
| 162 |
|
| 163 |
if (do_quit) { |
| 164 |
sd_event_source *p; |
| 165 |
|
| 166 |
ASSERT_OK(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d'))); |
| 167 |
ASSERT_OK(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT)); |
| 168 |
} else { |
| 169 |
ASSERT_FALSE(got_c); |
| 170 |
got_c = true; |
| 171 |
} |
| 172 |
} else |
| 173 |
assert_not_reached(); |
| 174 |
|
| 175 |
return 2; |
| 176 |
} |
| 177 |
|
| 178 |
static bool got_exit = false; |
| 179 |
|
| 180 |
static int exit_handler(sd_event_source *s, void *userdata) { |
| 181 |
log_info("got quit handler on %c", PTR_TO_INT(userdata)); |
| 182 |
|
| 183 |
got_exit = true; |
| 184 |
|
| 185 |
return 3; |
| 186 |
} |
| 187 |
|
| 188 |
static bool got_post = false; |
| 189 |
|
| 190 |
static int post_handler(sd_event_source *s, void *userdata) { |
| 191 |
log_info("got post handler"); |
| 192 |
|
| 193 |
got_post = true; |
| 194 |
|
| 195 |
return 2; |
| 196 |
} |
| 197 |
|
| 198 |
TEST(basic) { |
| 199 |
sd_event *e = NULL; |
| 200 |
sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL; |
| 201 |
static const char ch = 'x'; |
| 202 |
int a[2] = EBADF_PAIR, b[2] = EBADF_PAIR, |
| 203 |
d[2] = EBADF_PAIR, k[2] = EBADF_PAIR; |
| 204 |
uint64_t event_now; |
| 205 |
int64_t priority; |
| 206 |
|
| 207 |
ASSERT_OK_ERRNO(pipe(a)); |
| 208 |
ASSERT_OK_ERRNO(pipe(b)); |
| 209 |
ASSERT_OK_ERRNO(pipe(d)); |
| 210 |
ASSERT_OK_ERRNO(pipe(k)); |
| 211 |
|
| 212 |
ASSERT_OK(sd_event_default(&e)); |
| 213 |
ASSERT_OK_POSITIVE(sd_event_now(e, CLOCK_MONOTONIC, &event_now)); |
| 214 |
|
| 215 |
ASSERT_OK(sd_event_set_watchdog(e, true)); |
| 216 |
|
| 217 |
/* Test whether we cleanly can destroy an io event source from its own handler */ |
| 218 |
got_unref = false; |
| 219 |
ASSERT_OK(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL)); |
| 220 |
ASSERT_OK_EQ_ERRNO(write(k[1], &ch, 1), 1); |
| 221 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 222 |
ASSERT_TRUE(got_unref); |
| 223 |
|
| 224 |
got_a = false, got_b = false, got_c = false, got_d = 0; |
| 225 |
|
| 226 |
/* Add a oneshot handler, trigger it, reenable it, and trigger it again. */ |
| 227 |
ASSERT_OK(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d'))); |
| 228 |
ASSERT_OK(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT)); |
| 229 |
ASSERT_OK_ERRNO(write(d[1], &ch, 1)); |
| 230 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 231 |
ASSERT_EQ(got_d, 1U); |
| 232 |
ASSERT_OK_ERRNO(write(d[1], &ch, 1)); |
| 233 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 234 |
ASSERT_EQ(got_d, 2U); |
| 235 |
|
| 236 |
ASSERT_OK(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a'))); |
| 237 |
ASSERT_OK(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b'))); |
| 238 |
|
| 239 |
do_quit = false; |
| 240 |
ASSERT_OK(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c'))); |
| 241 |
ASSERT_OK(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g'))); |
| 242 |
|
| 243 |
ASSERT_OK(sd_event_source_set_priority(x, 99)); |
| 244 |
ASSERT_OK(sd_event_source_get_priority(x, &priority)); |
| 245 |
ASSERT_EQ(priority, 99); |
| 246 |
ASSERT_OK(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT)); |
| 247 |
ASSERT_OK(sd_event_source_set_prepare(x, prepare_handler)); |
| 248 |
ASSERT_OK(sd_event_source_set_priority(z, 50)); |
| 249 |
ASSERT_OK(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT)); |
| 250 |
ASSERT_OK(sd_event_source_set_prepare(z, prepare_handler)); |
| 251 |
|
| 252 |
/* Test for floating event sources */ |
| 253 |
ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1)); |
| 254 |
ASSERT_OK(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL)); |
| 255 |
|
| 256 |
ASSERT_OK_ERRNO(write(a[1], &ch, 1)); |
| 257 |
ASSERT_OK_ERRNO(write(b[1], &ch, 1)); |
| 258 |
|
| 259 |
ASSERT_FALSE(got_a); |
| 260 |
ASSERT_FALSE(got_b); |
| 261 |
ASSERT_FALSE(got_c); |
| 262 |
|
| 263 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 264 |
|
| 265 |
ASSERT_FALSE(got_a); |
| 266 |
ASSERT_TRUE(got_b); |
| 267 |
ASSERT_FALSE(got_c); |
| 268 |
|
| 269 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 270 |
|
| 271 |
ASSERT_FALSE(got_a); |
| 272 |
ASSERT_TRUE(got_b); |
| 273 |
ASSERT_TRUE(got_c); |
| 274 |
|
| 275 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 276 |
|
| 277 |
ASSERT_TRUE(got_a); |
| 278 |
ASSERT_TRUE(got_b); |
| 279 |
ASSERT_TRUE(got_c); |
| 280 |
|
| 281 |
sd_event_source_unref(x); |
| 282 |
sd_event_source_unref(y); |
| 283 |
|
| 284 |
do_quit = true; |
| 285 |
ASSERT_OK(sd_event_add_post(e, NULL, post_handler, NULL)); |
| 286 |
ASSERT_OK_ZERO(sd_event_now(e, CLOCK_MONOTONIC, &event_now)); |
| 287 |
ASSERT_OK(sd_event_source_set_time(z, event_now + 200 * USEC_PER_MSEC)); |
| 288 |
ASSERT_OK(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT)); |
| 289 |
|
| 290 |
ASSERT_OK(sd_event_loop(e)); |
| 291 |
ASSERT_TRUE(got_post); |
| 292 |
ASSERT_TRUE(got_exit); |
| 293 |
|
| 294 |
sd_event_source_unref(z); |
| 295 |
sd_event_source_unref(q); |
| 296 |
|
| 297 |
sd_event_source_unref(w); |
| 298 |
|
| 299 |
sd_event_unref(e); |
| 300 |
|
| 301 |
safe_close_pair(a); |
| 302 |
safe_close_pair(b); |
| 303 |
safe_close_pair(d); |
| 304 |
safe_close_pair(k); |
| 305 |
} |
| 306 |
|
| 307 |
TEST(sd_event_now) { |
| 308 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 309 |
uint64_t event_now; |
| 310 |
|
| 311 |
ASSERT_OK(sd_event_new(&e)); |
| 312 |
ASSERT_OK_POSITIVE(sd_event_now(e, CLOCK_MONOTONIC, &event_now)); |
| 313 |
ASSERT_OK_POSITIVE(sd_event_now(e, CLOCK_REALTIME, &event_now)); |
| 314 |
ASSERT_OK_POSITIVE(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now)); |
| 315 |
ASSERT_OK_POSITIVE(sd_event_now(e, CLOCK_BOOTTIME, &event_now)); |
| 316 |
ASSERT_OK_POSITIVE(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now)); |
| 317 |
ASSERT_ERROR(sd_event_now(e, -1, &event_now), EOPNOTSUPP); |
| 318 |
ASSERT_ERROR(sd_event_now(e, 900 /* arbitrary big number */, &event_now), EOPNOTSUPP); |
| 319 |
|
| 320 |
ASSERT_OK_ZERO(sd_event_run(e, 0)); |
| 321 |
|
| 322 |
ASSERT_OK_ZERO(sd_event_now(e, CLOCK_MONOTONIC, &event_now)); |
| 323 |
ASSERT_OK_ZERO(sd_event_now(e, CLOCK_REALTIME, &event_now)); |
| 324 |
ASSERT_OK_ZERO(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now)); |
| 325 |
ASSERT_OK_ZERO(sd_event_now(e, CLOCK_BOOTTIME, &event_now)); |
| 326 |
ASSERT_OK_ZERO(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now)); |
| 327 |
ASSERT_ERROR(sd_event_now(e, -1, &event_now), EOPNOTSUPP); |
| 328 |
ASSERT_ERROR(sd_event_now(e, 900 /* arbitrary big number */, &event_now), EOPNOTSUPP); |
| 329 |
} |
| 330 |
|
| 331 |
static int last_rtqueue_sigval = 0; |
| 332 |
static int n_rtqueue = 0; |
| 333 |
|
| 334 |
static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 335 |
last_rtqueue_sigval = si->ssi_int; |
| 336 |
n_rtqueue++; |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
TEST(rtqueue) { |
| 341 |
sd_event_source *u = NULL, *v = NULL, *s = NULL; |
| 342 |
sd_event *e = NULL; |
| 343 |
|
| 344 |
ASSERT_OK(sd_event_default(&e)); |
| 345 |
|
| 346 |
ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2)); |
| 347 |
ASSERT_OK(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL)); |
| 348 |
ASSERT_OK(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL)); |
| 349 |
ASSERT_OK(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL)); |
| 350 |
|
| 351 |
ASSERT_OK(sd_event_source_set_priority(v, -10)); |
| 352 |
|
| 353 |
ASSERT_OK_ERRNO(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 })); |
| 354 |
ASSERT_OK_ERRNO(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 })); |
| 355 |
ASSERT_OK_ERRNO(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 })); |
| 356 |
ASSERT_OK_ERRNO(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 })); |
| 357 |
ASSERT_OK_ERRNO(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 })); |
| 358 |
|
| 359 |
ASSERT_EQ(n_rtqueue, 0); |
| 360 |
ASSERT_EQ(last_rtqueue_sigval, 0); |
| 361 |
|
| 362 |
ASSERT_OK_EQ(sd_event_run(e, UINT64_MAX), 1); |
| 363 |
ASSERT_EQ(n_rtqueue, 1); |
| 364 |
ASSERT_EQ(last_rtqueue_sigval, 2); /* first SIGRTMIN+3 */ |
| 365 |
|
| 366 |
ASSERT_OK_EQ(sd_event_run(e, UINT64_MAX), 1); |
| 367 |
ASSERT_EQ(n_rtqueue, 2); |
| 368 |
ASSERT_EQ(last_rtqueue_sigval, 4); /* second SIGRTMIN+3 */ |
| 369 |
|
| 370 |
ASSERT_OK_EQ(sd_event_run(e, UINT64_MAX), 1); |
| 371 |
ASSERT_EQ(n_rtqueue, 3); |
| 372 |
ASSERT_EQ(last_rtqueue_sigval, 3); /* first SIGUSR2 */ |
| 373 |
|
| 374 |
ASSERT_OK_EQ(sd_event_run(e, UINT64_MAX), 1); |
| 375 |
ASSERT_EQ(n_rtqueue, 4); |
| 376 |
ASSERT_EQ(last_rtqueue_sigval, 1); /* SIGRTMIN+2 */ |
| 377 |
|
| 378 |
ASSERT_OK_ZERO(sd_event_run(e, 0)); /* the other SIGUSR2 is dropped, because the first one was still queued */ |
| 379 |
ASSERT_EQ(n_rtqueue, 4); |
| 380 |
ASSERT_EQ(last_rtqueue_sigval, 1); |
| 381 |
|
| 382 |
sd_event_source_unref(u); |
| 383 |
sd_event_source_unref(v); |
| 384 |
sd_event_source_unref(s); |
| 385 |
|
| 386 |
sd_event_unref(e); |
| 387 |
} |
| 388 |
|
| 389 |
#define CREATE_EVENTS_MAX (70000U) |
| 390 |
|
| 391 |
struct inotify_context { |
| 392 |
bool delete_self_handler_called; |
| 393 |
unsigned create_called[CREATE_EVENTS_MAX]; |
| 394 |
unsigned create_overflow; |
| 395 |
unsigned n_create_events; |
| 396 |
const char *path; |
| 397 |
}; |
| 398 |
|
| 399 |
static void maybe_exit(sd_event_source *s, struct inotify_context *c) { |
| 400 |
unsigned n; |
| 401 |
|
| 402 |
ASSERT_NOT_NULL(s); |
| 403 |
ASSERT_NOT_NULL(c); |
| 404 |
|
| 405 |
if (!c->delete_self_handler_called) |
| 406 |
return; |
| 407 |
|
| 408 |
for (n = 0; n < 3; n++) { |
| 409 |
unsigned i; |
| 410 |
|
| 411 |
if (c->create_overflow & (1U << n)) |
| 412 |
continue; |
| 413 |
|
| 414 |
for (i = 0; i < c->n_create_events; i++) |
| 415 |
if (!(c->create_called[i] & (1U << n))) |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
sd_event_exit(sd_event_source_get_event(s), 0); |
| 420 |
} |
| 421 |
|
| 422 |
static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { |
| 423 |
struct inotify_context *c = ASSERT_PTR(userdata); |
| 424 |
const char *path, *description; |
| 425 |
unsigned bit, n; |
| 426 |
|
| 427 |
ASSERT_OK(sd_event_source_get_inotify_path(s, &path)); |
| 428 |
|
| 429 |
ASSERT_OK(sd_event_source_get_description(s, &description)); |
| 430 |
ASSERT_OK(safe_atou(description, &n)); |
| 431 |
|
| 432 |
ASSERT_LE(n, 3U); |
| 433 |
bit = 1U << n; |
| 434 |
|
| 435 |
if (ev->mask & IN_Q_OVERFLOW) { |
| 436 |
log_info("inotify-handler for %s <%s>: overflow", path, description); |
| 437 |
c->create_overflow |= bit; |
| 438 |
} else if (ev->mask & IN_CREATE) { |
| 439 |
ASSERT_TRUE(path_equal_or_inode_same(path, c->path, 0)); |
| 440 |
if (streq(ev->name, "sub")) |
| 441 |
log_debug("inotify-handler for %s <%s>: create on %s", path, description, ev->name); |
| 442 |
else { |
| 443 |
unsigned i; |
| 444 |
|
| 445 |
ASSERT_OK(safe_atou(ev->name, &i)); |
| 446 |
ASSERT_LT(i, c->n_create_events); |
| 447 |
c->create_called[i] |= bit; |
| 448 |
} |
| 449 |
} else if (ev->mask & IN_DELETE) { |
| 450 |
log_info("inotify-handler for %s <%s>: delete of %s", path, description, ev->name); |
| 451 |
ASSERT_STREQ(ev->name, "sub"); |
| 452 |
} else |
| 453 |
assert_not_reached(); |
| 454 |
|
| 455 |
maybe_exit(s, c); |
| 456 |
return 1; |
| 457 |
} |
| 458 |
|
| 459 |
static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { |
| 460 |
struct inotify_context *c = ASSERT_PTR(userdata); |
| 461 |
const char *path; |
| 462 |
|
| 463 |
ASSERT_OK(sd_event_source_get_inotify_path(s, &path)); |
| 464 |
|
| 465 |
if (ev->mask & IN_Q_OVERFLOW) { |
| 466 |
log_info("delete-self-handler for %s: overflow", path); |
| 467 |
c->delete_self_handler_called = true; |
| 468 |
} else if (ev->mask & IN_DELETE_SELF) { |
| 469 |
log_info("delete-self-handler for %s: delete-self", path); |
| 470 |
c->delete_self_handler_called = true; |
| 471 |
} else if (ev->mask & IN_IGNORED) { |
| 472 |
log_info("delete-self-handler for %s: ignore", path); |
| 473 |
} else |
| 474 |
assert_not_reached(); |
| 475 |
|
| 476 |
maybe_exit(s, c); |
| 477 |
return 1; |
| 478 |
} |
| 479 |
|
| 480 |
static void test_inotify_one(unsigned n_create_events) { |
| 481 |
_cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
| 482 |
sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL; |
| 483 |
struct inotify_context context = { |
| 484 |
.n_create_events = n_create_events, |
| 485 |
}; |
| 486 |
sd_event *e = NULL; |
| 487 |
const char *q, *pp; |
| 488 |
unsigned i; |
| 489 |
|
| 490 |
log_info("/* %s(%u) */", __func__, n_create_events); |
| 491 |
|
| 492 |
ASSERT_OK(sd_event_default(&e)); |
| 493 |
|
| 494 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p)); |
| 495 |
context.path = p; |
| 496 |
|
| 497 |
ASSERT_OK(sd_event_add_inotify(e, &a, p, IN_CREATE|IN_ONLYDIR, inotify_handler, &context)); |
| 498 |
ASSERT_OK(sd_event_add_inotify(e, &b, p, IN_CREATE|IN_DELETE|IN_DONT_FOLLOW, inotify_handler, &context)); |
| 499 |
ASSERT_OK(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_IDLE)); |
| 500 |
ASSERT_OK(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_NORMAL)); |
| 501 |
ASSERT_OK(sd_event_add_inotify(e, &c, p, IN_CREATE|IN_DELETE|IN_EXCL_UNLINK, inotify_handler, &context)); |
| 502 |
ASSERT_OK(sd_event_source_set_priority(c, SD_EVENT_PRIORITY_IDLE)); |
| 503 |
|
| 504 |
ASSERT_OK(sd_event_source_set_description(a, "0")); |
| 505 |
ASSERT_OK(sd_event_source_set_description(b, "1")); |
| 506 |
ASSERT_OK(sd_event_source_set_description(c, "2")); |
| 507 |
|
| 508 |
ASSERT_OK(sd_event_source_get_inotify_path(a, &pp)); |
| 509 |
ASSERT_TRUE(path_equal_or_inode_same(pp, p, 0)); |
| 510 |
ASSERT_OK(sd_event_source_get_inotify_path(b, &pp)); |
| 511 |
ASSERT_TRUE(path_equal_or_inode_same(pp, p, 0)); |
| 512 |
ASSERT_OK(sd_event_source_get_inotify_path(b, &pp)); |
| 513 |
ASSERT_TRUE(path_equal_or_inode_same(pp, p, 0)); |
| 514 |
|
| 515 |
q = strjoina(p, "/sub"); |
| 516 |
ASSERT_OK(touch(q)); |
| 517 |
ASSERT_OK(sd_event_add_inotify(e, &d, q, IN_DELETE_SELF, delete_self_handler, &context)); |
| 518 |
|
| 519 |
for (i = 0; i < n_create_events; i++) { |
| 520 |
char buf[DECIMAL_STR_MAX(unsigned)+1]; |
| 521 |
_cleanup_free_ char *z = NULL; |
| 522 |
|
| 523 |
xsprintf(buf, "%u", i); |
| 524 |
ASSERT_NOT_NULL(z = path_join(p, buf)); |
| 525 |
|
| 526 |
ASSERT_OK(touch(z)); |
| 527 |
} |
| 528 |
|
| 529 |
ASSERT_OK_ERRNO(unlink(q)); |
| 530 |
|
| 531 |
ASSERT_OK(sd_event_loop(e)); |
| 532 |
|
| 533 |
sd_event_source_unref(a); |
| 534 |
sd_event_source_unref(b); |
| 535 |
sd_event_source_unref(c); |
| 536 |
sd_event_source_unref(d); |
| 537 |
|
| 538 |
sd_event_unref(e); |
| 539 |
} |
| 540 |
|
| 541 |
TEST(inotify) { |
| 542 |
test_inotify_one(100); /* should work without overflow */ |
| 543 |
test_inotify_one(33000); /* should trigger a q overflow */ |
| 544 |
} |
| 545 |
|
| 546 |
static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 547 |
ASSERT_NOT_NULL(s); |
| 548 |
ASSERT_NOT_NULL(si); |
| 549 |
|
| 550 |
ASSERT_EQ(si->si_uid, getuid()); |
| 551 |
ASSERT_EQ(si->si_signo, SIGCHLD); |
| 552 |
ASSERT_EQ(si->si_code, CLD_EXITED); |
| 553 |
ASSERT_EQ(si->si_status, 66); |
| 554 |
|
| 555 |
log_info("got pidfd on %c", PTR_TO_INT(userdata)); |
| 556 |
|
| 557 |
ASSERT_PTR_EQ(userdata, INT_TO_PTR('p')); |
| 558 |
|
| 559 |
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0)); |
| 560 |
sd_event_source_unref(s); |
| 561 |
|
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
TEST(pidfd) { |
| 566 |
sd_event_source *s = NULL, *t = NULL; |
| 567 |
sd_event *e = NULL; |
| 568 |
int pidfd; |
| 569 |
pid_t pid, pid2; |
| 570 |
|
| 571 |
ASSERT_OK_ERRNO(pid = fork()); |
| 572 |
if (pid == 0) |
| 573 |
/* child */ |
| 574 |
_exit(66); |
| 575 |
|
| 576 |
ASSERT_OK(pidfd = pidfd_open(pid, 0)); |
| 577 |
|
| 578 |
ASSERT_OK_ERRNO(pid2 = fork()); |
| 579 |
if (pid2 == 0) |
| 580 |
freeze(); |
| 581 |
|
| 582 |
ASSERT_OK(sd_event_default(&e)); |
| 583 |
ASSERT_OK(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED, pidfd_handler, INT_TO_PTR('p'))); |
| 584 |
ASSERT_OK(sd_event_source_set_child_pidfd_own(s, true)); |
| 585 |
|
| 586 |
/* This one should never trigger, since our second child lives forever */ |
| 587 |
ASSERT_OK(sd_event_add_child(e, &t, pid2, WEXITED, pidfd_handler, INT_TO_PTR('q'))); |
| 588 |
ASSERT_OK(sd_event_source_set_child_process_own(t, true)); |
| 589 |
|
| 590 |
ASSERT_OK(sd_event_loop(e)); |
| 591 |
|
| 592 |
/* Child should still be alive */ |
| 593 |
ASSERT_OK_ERRNO(kill(pid2, 0)); |
| 594 |
|
| 595 |
t = sd_event_source_unref(t); |
| 596 |
|
| 597 |
/* Child should now be dead, since we dropped the ref */ |
| 598 |
ASSERT_ERROR_ERRNO(kill(pid2, 0), ESRCH); |
| 599 |
|
| 600 |
sd_event_unref(e); |
| 601 |
} |
| 602 |
|
| 603 |
static int ratelimit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 604 |
unsigned *c = (unsigned*) userdata; |
| 605 |
*c += 1; |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
| 610 |
int r; |
| 611 |
|
| 612 |
r = sd_event_source_set_enabled(s, SD_EVENT_ON); |
| 613 |
if (r < 0) |
| 614 |
log_warning_errno(r, "Failed to turn on notify event source: %m"); |
| 615 |
|
| 616 |
r = sd_event_source_set_time(s, usec + 1000); |
| 617 |
if (r < 0) |
| 618 |
log_error_errno(r, "Failed to restart watchdog event source: %m"); |
| 619 |
|
| 620 |
unsigned *c = (unsigned*) userdata; |
| 621 |
*c += 1; |
| 622 |
|
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
static int expired = -1; |
| 627 |
static int ratelimit_expired(sd_event_source *s, void *userdata) { |
| 628 |
return ++expired; |
| 629 |
} |
| 630 |
|
| 631 |
TEST(ratelimit) { |
| 632 |
_cleanup_close_pair_ int p[2] = EBADF_PAIR; |
| 633 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 634 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 635 |
uint64_t interval; |
| 636 |
unsigned count, burst; |
| 637 |
|
| 638 |
ASSERT_OK(sd_event_default(&e)); |
| 639 |
ASSERT_OK_ERRNO(pipe2(p, O_CLOEXEC|O_NONBLOCK)); |
| 640 |
|
| 641 |
ASSERT_OK(sd_event_add_io(e, &s, p[0], EPOLLIN, ratelimit_io_handler, &count)); |
| 642 |
ASSERT_OK(sd_event_source_set_description(s, "test-ratelimit-io")); |
| 643 |
ASSERT_OK(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5)); |
| 644 |
ASSERT_OK(sd_event_source_get_ratelimit(s, &interval, &burst)); |
| 645 |
ASSERT_EQ(interval, 1 * USEC_PER_SEC); |
| 646 |
ASSERT_EQ(burst, 5U); |
| 647 |
|
| 648 |
ASSERT_OK_EQ_ERRNO(write(p[1], "1", 1), 1); |
| 649 |
|
| 650 |
count = 0; |
| 651 |
for (unsigned i = 0; i < 10; i++) { |
| 652 |
log_debug("slow loop iteration %u", i); |
| 653 |
ASSERT_OK(sd_event_run(e, UINT64_MAX)); |
| 654 |
ASSERT_OK(usleep_safe(250 * USEC_PER_MSEC)); |
| 655 |
} |
| 656 |
|
| 657 |
ASSERT_OK_ZERO(sd_event_source_is_ratelimited(s)); |
| 658 |
ASSERT_EQ(count, 10U); |
| 659 |
log_info("ratelimit_io_handler: called %u times, event source not ratelimited", count); |
| 660 |
|
| 661 |
ASSERT_OK(sd_event_source_set_ratelimit(s, 0, 0)); |
| 662 |
ASSERT_OK(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5)); |
| 663 |
|
| 664 |
count = 0; |
| 665 |
for (unsigned i = 0; i < 10; i++) { |
| 666 |
log_debug("fast event loop iteration %u", i); |
| 667 |
ASSERT_OK(sd_event_run(e, UINT64_MAX)); |
| 668 |
ASSERT_OK(usleep_safe(10)); |
| 669 |
} |
| 670 |
log_info("ratelimit_io_handler: called %u times, event source got ratelimited", count); |
| 671 |
ASSERT_LT(count, 10U); |
| 672 |
|
| 673 |
s = sd_event_source_unref(s); |
| 674 |
safe_close_pair(p); |
| 675 |
|
| 676 |
count = 0; |
| 677 |
ASSERT_OK(sd_event_add_time_relative(e, &s, CLOCK_MONOTONIC, 1000, 1, ratelimit_time_handler, &count)); |
| 678 |
ASSERT_OK_ZERO(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10)); |
| 679 |
|
| 680 |
do { |
| 681 |
ASSERT_OK(sd_event_run(e, UINT64_MAX)); |
| 682 |
} while (!sd_event_source_is_ratelimited(s)); |
| 683 |
|
| 684 |
log_info("ratelimit_time_handler: called %u times, event source got ratelimited", count); |
| 685 |
ASSERT_EQ(count, 10U); |
| 686 |
|
| 687 |
/* In order to get rid of active rate limit client needs to disable it explicitly */ |
| 688 |
ASSERT_OK(sd_event_source_set_ratelimit(s, 0, 0)); |
| 689 |
ASSERT_OK_ZERO(sd_event_source_is_ratelimited(s)); |
| 690 |
|
| 691 |
ASSERT_OK(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10)); |
| 692 |
|
| 693 |
/* Set callback that will be invoked when we leave rate limited state. */ |
| 694 |
ASSERT_OK(sd_event_source_set_ratelimit_expire_callback(s, ratelimit_expired)); |
| 695 |
|
| 696 |
do { |
| 697 |
ASSERT_OK(sd_event_run(e, UINT64_MAX)); |
| 698 |
} while (!sd_event_source_is_ratelimited(s)); |
| 699 |
|
| 700 |
log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited"); |
| 701 |
ASSERT_EQ(count, 20U); |
| 702 |
|
| 703 |
/* Dispatch the event loop once more and check that ratelimit expiration callback got called */ |
| 704 |
ASSERT_OK(sd_event_run(e, UINT64_MAX)); |
| 705 |
ASSERT_EQ(expired, 0); |
| 706 |
} |
| 707 |
|
| 708 |
TEST(simple_timeout) { |
| 709 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 710 |
usec_t f, t, some_time; |
| 711 |
|
| 712 |
some_time = random_u64_range(2 * USEC_PER_SEC); |
| 713 |
|
| 714 |
ASSERT_OK(sd_event_default(&e)); |
| 715 |
|
| 716 |
ASSERT_OK_ZERO(sd_event_prepare(e)); |
| 717 |
|
| 718 |
f = now(CLOCK_MONOTONIC); |
| 719 |
ASSERT_OK(sd_event_wait(e, some_time)); |
| 720 |
t = now(CLOCK_MONOTONIC); |
| 721 |
|
| 722 |
/* The event loop may sleep longer than the specified time (timer accuracy, scheduling latencies, …), |
| 723 |
* but never shorter. Let's check that. */ |
| 724 |
ASSERT_GE(t, usec_add(f, some_time)); |
| 725 |
} |
| 726 |
|
| 727 |
static int inotify_self_destroy_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { |
| 728 |
sd_event_source **p = userdata; |
| 729 |
|
| 730 |
ASSERT_NOT_NULL(ev); |
| 731 |
ASSERT_NOT_NULL(p); |
| 732 |
ASSERT_PTR_EQ(*p, s); |
| 733 |
|
| 734 |
ASSERT_TRUE(FLAGS_SET(ev->mask, IN_ATTRIB)); |
| 735 |
|
| 736 |
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0)); |
| 737 |
|
| 738 |
*p = sd_event_source_unref(*p); /* here's what we actually intend to test: we destroy the event |
| 739 |
* source from inside the event source handler */ |
| 740 |
return 1; |
| 741 |
} |
| 742 |
|
| 743 |
TEST(inotify_self_destroy) { |
| 744 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 745 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 746 |
char path[] = "/tmp/inotifyXXXXXX"; |
| 747 |
_cleanup_close_ int fd = -EBADF; |
| 748 |
|
| 749 |
/* Tests that destroying an inotify event source from its own handler is safe */ |
| 750 |
|
| 751 |
ASSERT_OK(sd_event_default(&e)); |
| 752 |
|
| 753 |
ASSERT_OK(fd = mkostemp_safe(path)); |
| 754 |
ASSERT_OK(sd_event_add_inotify_fd(e, &s, fd, IN_ATTRIB, inotify_self_destroy_handler, &s)); |
| 755 |
fd = safe_close(fd); |
| 756 |
ASSERT_OK_ERRNO(unlink(path)); /* This will trigger IN_ATTRIB because link count goes to zero */ |
| 757 |
ASSERT_OK(sd_event_loop(e)); |
| 758 |
} |
| 759 |
|
| 760 |
struct inotify_process_buffered_data_context { |
| 761 |
const char *path[2]; |
| 762 |
unsigned i; |
| 763 |
}; |
| 764 |
|
| 765 |
static int inotify_process_buffered_data_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { |
| 766 |
struct inotify_process_buffered_data_context *c = ASSERT_PTR(userdata); |
| 767 |
const char *description; |
| 768 |
|
| 769 |
ASSERT_OK(sd_event_source_get_description(s, &description)); |
| 770 |
|
| 771 |
ASSERT_LT(c->i, 2U); |
| 772 |
ASSERT_STREQ(c->path[c->i], description); |
| 773 |
c->i++; |
| 774 |
|
| 775 |
return 1; |
| 776 |
} |
| 777 |
|
| 778 |
TEST(inotify_process_buffered_data) { |
| 779 |
_cleanup_(rm_rf_physical_and_freep) char *p = NULL, *q = NULL; |
| 780 |
_cleanup_(sd_event_source_unrefp) sd_event_source *a = NULL, *b = NULL; |
| 781 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 782 |
_cleanup_free_ char *z = NULL; |
| 783 |
|
| 784 |
/* For issue #23826 */ |
| 785 |
|
| 786 |
ASSERT_OK(sd_event_default(&e)); |
| 787 |
|
| 788 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p)); |
| 789 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &q)); |
| 790 |
|
| 791 |
struct inotify_process_buffered_data_context context = { |
| 792 |
.path = { p, q }, |
| 793 |
}; |
| 794 |
|
| 795 |
ASSERT_OK(sd_event_add_inotify(e, &a, p, IN_CREATE, inotify_process_buffered_data_handler, &context)); |
| 796 |
ASSERT_OK(sd_event_add_inotify(e, &b, q, IN_CREATE, inotify_process_buffered_data_handler, &context)); |
| 797 |
|
| 798 |
ASSERT_NOT_NULL(z = path_join(p, "aaa")); |
| 799 |
ASSERT_OK(touch(z)); |
| 800 |
z = mfree(z); |
| 801 |
ASSERT_NOT_NULL(z = path_join(q, "bbb")); |
| 802 |
ASSERT_OK(touch(z)); |
| 803 |
z = mfree(z); |
| 804 |
|
| 805 |
ASSERT_OK_POSITIVE(sd_event_run(e, 10 * USEC_PER_SEC)); |
| 806 |
ASSERT_OK_POSITIVE(sd_event_prepare(e)); /* issue #23826: this was 0. */ |
| 807 |
ASSERT_OK_POSITIVE(sd_event_dispatch(e)); |
| 808 |
ASSERT_OK_ZERO(sd_event_prepare(e)); |
| 809 |
ASSERT_OK_ZERO(sd_event_wait(e, 0)); |
| 810 |
} |
| 811 |
|
| 812 |
static int inotify_handler_issue_38265(sd_event_source *s, const struct inotify_event *event, void *userdata) { |
| 813 |
log_debug("Inotify event: mask=0x%x name=%s", event->mask, event->name); |
| 814 |
return 0; |
| 815 |
} |
| 816 |
|
| 817 |
TEST(inotify_issue_38265) { |
| 818 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 819 |
_cleanup_(sd_event_source_unrefp) sd_event_source *a = NULL, *b = NULL; |
| 820 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 821 |
_cleanup_free_ char *p = NULL; |
| 822 |
|
| 823 |
/* For issue #38265. */ |
| 824 |
|
| 825 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &t)); |
| 826 |
|
| 827 |
ASSERT_OK(sd_event_default(&e)); |
| 828 |
|
| 829 |
/* Create inode data that watches IN_MODIFY */ |
| 830 |
ASSERT_OK(sd_event_add_inotify(e, &a, t, IN_CREATE | IN_MODIFY, inotify_handler_issue_38265, NULL)); |
| 831 |
ASSERT_OK(sd_event_add_inotify(e, &b, t, IN_CREATE, inotify_handler_issue_38265, NULL)); |
| 832 |
|
| 833 |
/* Then drop the event source that is interested in IN_MODIFY */ |
| 834 |
ASSERT_NULL(a = sd_event_source_unref(a)); |
| 835 |
|
| 836 |
/* Trigger IN_MODIFY (of course with IN_CREATE) */ |
| 837 |
ASSERT_NOT_NULL(p = path_join(t, "hoge")); |
| 838 |
ASSERT_OK(write_string_file(p, "aaa", WRITE_STRING_FILE_CREATE)); |
| 839 |
|
| 840 |
for (unsigned i = 1; i < 5; i++) { |
| 841 |
log_debug("Running event loop cycle %u to process inotify events...", i); |
| 842 |
ASSERT_OK(sd_event_run(e, USEC_PER_MSEC)); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
TEST(fork) { |
| 847 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 848 |
int r; |
| 849 |
|
| 850 |
ASSERT_OK(sd_event_default(&e)); |
| 851 |
ASSERT_OK_ZERO(sd_event_prepare(e)); |
| 852 |
|
| 853 |
/* Check that after a fork the cleanup functions return NULL */ |
| 854 |
r = pidref_safe_fork("(bus-fork-test)", FORK_WAIT|FORK_LOG, NULL); |
| 855 |
if (r == 0) { |
| 856 |
ASSERT_NOT_NULL(e); |
| 857 |
ASSERT_NULL(sd_event_ref(e)); |
| 858 |
ASSERT_NULL(sd_event_unref(e)); |
| 859 |
_exit(EXIT_SUCCESS); |
| 860 |
} |
| 861 |
|
| 862 |
ASSERT_OK(r); |
| 863 |
} |
| 864 |
|
| 865 |
TEST(sd_event_source_set_io_fd) { |
| 866 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 867 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 868 |
_cleanup_close_pair_ int pfd_a[2] = EBADF_PAIR, pfd_b[2] = EBADF_PAIR; |
| 869 |
|
| 870 |
ASSERT_OK(sd_event_default(&e)); |
| 871 |
|
| 872 |
ASSERT_OK_ERRNO(pipe2(pfd_a, O_CLOEXEC)); |
| 873 |
ASSERT_OK_ERRNO(pipe2(pfd_b, O_CLOEXEC)); |
| 874 |
|
| 875 |
ASSERT_OK(sd_event_add_io(e, &s, pfd_a[0], EPOLLIN, NULL, INT_TO_PTR(-ENOANO))); |
| 876 |
ASSERT_OK(sd_event_source_set_io_fd_own(s, true)); |
| 877 |
TAKE_FD(pfd_a[0]); |
| 878 |
|
| 879 |
ASSERT_OK(sd_event_source_set_io_fd(s, pfd_b[0])); |
| 880 |
TAKE_FD(pfd_b[0]); |
| 881 |
} |
| 882 |
|
| 883 |
static int hup_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 884 |
unsigned *c = userdata; |
| 885 |
|
| 886 |
ASSERT_EQ(revents, (uint32_t) EPOLLHUP); |
| 887 |
|
| 888 |
(*c)++; |
| 889 |
return 0; |
| 890 |
} |
| 891 |
|
| 892 |
TEST(leave_ratelimit) { |
| 893 |
bool expect_ratelimit = false, manually_left_ratelimit = false; |
| 894 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 895 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 896 |
_cleanup_close_pair_ int pfd[2] = EBADF_PAIR; |
| 897 |
unsigned c = 0; |
| 898 |
int r; |
| 899 |
|
| 900 |
ASSERT_OK(sd_event_default(&e)); |
| 901 |
|
| 902 |
/* Create an event source that will continuously fire by creating a pipe whose write side is closed, |
| 903 |
* and which hence will only see EOF and constant EPOLLHUP */ |
| 904 |
ASSERT_OK_ERRNO(pipe2(pfd, O_CLOEXEC)); |
| 905 |
ASSERT_OK(sd_event_add_io(e, &s, pfd[0], EPOLLIN, hup_callback, &c)); |
| 906 |
ASSERT_OK(sd_event_source_set_io_fd_own(s, true)); |
| 907 |
ASSERT_OK(sd_event_source_set_ratelimit(s, 5*USEC_PER_MINUTE, 5)); |
| 908 |
|
| 909 |
pfd[0] = -EBADF; |
| 910 |
pfd[1] = safe_close(pfd[1]); /* Trigger continuous EOF */ |
| 911 |
|
| 912 |
for (;;) { |
| 913 |
ASSERT_OK(r = sd_event_prepare(e)); |
| 914 |
|
| 915 |
if (r == 0) |
| 916 |
ASSERT_OK_POSITIVE(sd_event_wait(e, UINT64_MAX)); |
| 917 |
|
| 918 |
ASSERT_OK_POSITIVE(sd_event_dispatch(e)); |
| 919 |
|
| 920 |
ASSERT_OK(r = sd_event_source_is_ratelimited(s)); |
| 921 |
|
| 922 |
if (c < 5) |
| 923 |
/* First four dispatches should just work */ |
| 924 |
ASSERT_FALSE(r); |
| 925 |
else if (c == 5) { |
| 926 |
/* The fifth dispatch should still work, but we now expect the ratelimit to be hit subsequently */ |
| 927 |
if (!expect_ratelimit) { |
| 928 |
ASSERT_FALSE(r); |
| 929 |
ASSERT_OK_ZERO(sd_event_source_leave_ratelimit(s)); /* this should be a NOP, and return 0 hence */ |
| 930 |
expect_ratelimit = true; |
| 931 |
} else { |
| 932 |
/* We expected the ratelimit, let's leave it manually, and verify it */ |
| 933 |
ASSERT_TRUE(r); |
| 934 |
ASSERT_OK_POSITIVE(sd_event_source_leave_ratelimit(s)); /* we are ratelimited, hence should return > 0 */ |
| 935 |
ASSERT_OK_ZERO(sd_event_source_is_ratelimited(s)); |
| 936 |
|
| 937 |
manually_left_ratelimit = true; |
| 938 |
} |
| 939 |
|
| 940 |
} else if (c == 6) |
| 941 |
/* On the sixth iteration let's just exit */ |
| 942 |
break; |
| 943 |
} |
| 944 |
|
| 945 |
/* Verify we definitely hit the ratelimit and left it manually again */ |
| 946 |
ASSERT_TRUE(manually_left_ratelimit); |
| 947 |
} |
| 948 |
|
| 949 |
static int defer_post_handler(sd_event_source *s, void *userdata) { |
| 950 |
bool *dispatched_post = ASSERT_PTR(userdata); |
| 951 |
|
| 952 |
*dispatched_post = true; |
| 953 |
|
| 954 |
return 0; |
| 955 |
} |
| 956 |
|
| 957 |
static int defer_adds_post_handler(sd_event_source *s, void *userdata) { |
| 958 |
sd_event *e = sd_event_source_get_event(s); |
| 959 |
|
| 960 |
/* Add a post event source from within the defer handler */ |
| 961 |
ASSERT_OK(sd_event_add_post(e, NULL, defer_post_handler, userdata)); |
| 962 |
|
| 963 |
return 0; |
| 964 |
} |
| 965 |
|
| 966 |
TEST(defer_add_post) { |
| 967 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 968 |
bool dispatched_post = false; |
| 969 |
|
| 970 |
ASSERT_OK(sd_event_default(&e)); |
| 971 |
|
| 972 |
/* Add a oneshot defer event source that will add a post event source */ |
| 973 |
ASSERT_OK(sd_event_add_defer(e, NULL, defer_adds_post_handler, &dispatched_post)); |
| 974 |
|
| 975 |
/* Run one iteration - this should dispatch the defer handler */ |
| 976 |
ASSERT_OK_POSITIVE(sd_event_run(e, UINT64_MAX)); |
| 977 |
|
| 978 |
/* The post handler should have been added but not yet dispatched */ |
| 979 |
ASSERT_FALSE(dispatched_post); |
| 980 |
|
| 981 |
/* Run another iteration - this should dispatch the post handler */ |
| 982 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 983 |
|
| 984 |
/* Now the post handler should have been dispatched */ |
| 985 |
ASSERT_TRUE(dispatched_post); |
| 986 |
} |
| 987 |
|
| 988 |
static int child_handler_wnowait(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 989 |
int *counter = ASSERT_PTR(userdata); |
| 990 |
|
| 991 |
(*counter)++; |
| 992 |
|
| 993 |
if (*counter == 5) |
| 994 |
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0)); |
| 995 |
|
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
TEST(child_wnowait) { |
| 1000 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1001 |
|
| 1002 |
ASSERT_OK(sd_event_default(&e)); |
| 1003 |
|
| 1004 |
/* Fork a subprocess */ |
| 1005 |
pid_t pid; |
| 1006 |
ASSERT_OK_ERRNO(pid = fork()); |
| 1007 |
|
| 1008 |
if (pid == 0) |
| 1009 |
/* Child process - exit with a specific code */ |
| 1010 |
_exit(42); |
| 1011 |
|
| 1012 |
/* Add a child source with WNOWAIT flag */ |
| 1013 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 1014 |
int counter = 0; |
| 1015 |
ASSERT_OK(sd_event_add_child(e, &s, pid, WEXITED|WNOWAIT, child_handler_wnowait, &counter)); |
| 1016 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ON)); |
| 1017 |
|
| 1018 |
/* Run the event loop - this should call the handler */ |
| 1019 |
ASSERT_OK(sd_event_loop(e)); |
| 1020 |
ASSERT_EQ(counter, 5); |
| 1021 |
|
| 1022 |
/* Since we used WNOWAIT, the child should still be waitable */ |
| 1023 |
siginfo_t si = {}; |
| 1024 |
ASSERT_OK_ERRNO(waitid(P_PID, pid, &si, WEXITED|WNOHANG)); |
| 1025 |
ASSERT_EQ(si.si_pid, pid); |
| 1026 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 1027 |
ASSERT_EQ(si.si_status, 42); |
| 1028 |
} |
| 1029 |
|
| 1030 |
TEST(child_pidfd_wnowait) { |
| 1031 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1032 |
|
| 1033 |
ASSERT_OK(sd_event_default(&e)); |
| 1034 |
|
| 1035 |
/* Fork a subprocess */ |
| 1036 |
pid_t pid; |
| 1037 |
ASSERT_OK_ERRNO(pid = fork()); |
| 1038 |
|
| 1039 |
if (pid == 0) |
| 1040 |
/* Child process - exit with a specific code */ |
| 1041 |
_exit(42); |
| 1042 |
|
| 1043 |
_cleanup_close_ int pidfd = -EBADF; |
| 1044 |
ASSERT_OK_ERRNO(pidfd = pidfd_open(pid, 0)); |
| 1045 |
|
| 1046 |
/* Add a child source with WNOWAIT flag */ |
| 1047 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 1048 |
int counter = 0; |
| 1049 |
ASSERT_OK(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED|WNOWAIT, child_handler_wnowait, &counter)); |
| 1050 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ON)); |
| 1051 |
|
| 1052 |
/* Run the event loop - this should call the handler */ |
| 1053 |
ASSERT_OK(sd_event_loop(e)); |
| 1054 |
ASSERT_EQ(counter, 5); |
| 1055 |
|
| 1056 |
/* Since we used WNOWAIT, the child should still be waitable */ |
| 1057 |
siginfo_t si = {}; |
| 1058 |
ASSERT_OK_ERRNO(waitid(P_PIDFD, pidfd, &si, WEXITED|WNOHANG)); |
| 1059 |
ASSERT_EQ(si.si_pid, pid); |
| 1060 |
ASSERT_EQ(si.si_code, CLD_EXITED); |
| 1061 |
ASSERT_EQ(si.si_status, 42); |
| 1062 |
} |
| 1063 |
|
| 1064 |
static int exit_on_idle_defer_handler(sd_event_source *s, void *userdata) { |
| 1065 |
unsigned *c = ASSERT_PTR(userdata); |
| 1066 |
|
| 1067 |
/* Should not be reached on third call because the event loop should exit before */ |
| 1068 |
ASSERT_LT(*c, 2u); |
| 1069 |
|
| 1070 |
(*c)++; |
| 1071 |
|
| 1072 |
/* Disable ourselves, which should trigger exit-on-idle after the second iteration */ |
| 1073 |
if (*c == 2) |
| 1074 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_OFF)); |
| 1075 |
|
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
static int exit_on_idle_post_handler(sd_event_source *s, void *userdata) { |
| 1080 |
unsigned *c = ASSERT_PTR(userdata); |
| 1081 |
|
| 1082 |
/* Should not be reached on third call because the event loop should exit before */ |
| 1083 |
ASSERT_LT(*c, 2u); |
| 1084 |
|
| 1085 |
(*c)++; |
| 1086 |
return 0; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static int exit_on_idle_exit_handler(sd_event_source *s, void *userdata) { |
| 1090 |
return 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
TEST(exit_on_idle) { |
| 1094 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1095 |
ASSERT_OK(sd_event_new(&e)); |
| 1096 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1097 |
ASSERT_OK_POSITIVE(sd_event_get_exit_on_idle(e)); |
| 1098 |
|
| 1099 |
/* Create a recurring defer event source. */ |
| 1100 |
_cleanup_(sd_event_source_unrefp) sd_event_source *d = NULL; |
| 1101 |
unsigned dc = 0; |
| 1102 |
ASSERT_OK(sd_event_add_defer(e, &d, exit_on_idle_defer_handler, &dc)); |
| 1103 |
ASSERT_OK(sd_event_source_set_enabled(d, SD_EVENT_ON)); |
| 1104 |
|
| 1105 |
/* This post event source should not keep the event loop running after the defer source is disabled. */ |
| 1106 |
_cleanup_(sd_event_source_unrefp) sd_event_source *p = NULL; |
| 1107 |
unsigned pc = 0; |
| 1108 |
ASSERT_OK(sd_event_add_post(e, &p, exit_on_idle_post_handler, &pc)); |
| 1109 |
ASSERT_OK(sd_event_source_set_enabled(p, SD_EVENT_ON)); |
| 1110 |
ASSERT_OK(sd_event_source_set_priority(p, SD_EVENT_PRIORITY_IMPORTANT)); |
| 1111 |
|
| 1112 |
/* And neither should this exit event source. */ |
| 1113 |
ASSERT_OK(sd_event_add_exit(e, NULL, exit_on_idle_exit_handler, NULL)); |
| 1114 |
|
| 1115 |
/* Run the event loop - it should exit after we disable the event source */ |
| 1116 |
ASSERT_OK(sd_event_loop(e)); |
| 1117 |
ASSERT_EQ(dc, 2u); |
| 1118 |
ASSERT_EQ(pc, 2u); |
| 1119 |
} |
| 1120 |
|
| 1121 |
TEST(exit_on_idle_no_sources) { |
| 1122 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1123 |
ASSERT_OK(sd_event_new(&e)); |
| 1124 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1125 |
|
| 1126 |
/* Running loop with no sources should return immediately with success */ |
| 1127 |
ASSERT_OK(sd_event_loop(e)); |
| 1128 |
} |
| 1129 |
|
| 1130 |
static int defer_fair_handler(sd_event_source *s, void *userdata) { |
| 1131 |
unsigned *counter = ASSERT_PTR(userdata); |
| 1132 |
|
| 1133 |
/* If we're about to increment above 5, exit the event loop */ |
| 1134 |
if (*counter >= 5) |
| 1135 |
return sd_event_exit(sd_event_source_get_event(s), 0); |
| 1136 |
|
| 1137 |
(*counter)++; |
| 1138 |
|
| 1139 |
return 0; |
| 1140 |
} |
| 1141 |
|
| 1142 |
TEST(defer_fair_scheduling) { |
| 1143 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1144 |
sd_event_source *sources[5] = {}; |
| 1145 |
unsigned counters[5] = {}; |
| 1146 |
|
| 1147 |
ASSERT_OK(sd_event_new(&e)); |
| 1148 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1149 |
|
| 1150 |
/* Create 5 defer sources with equal priority */ |
| 1151 |
for (unsigned i = 0; i < 5; i++) { |
| 1152 |
ASSERT_OK(sd_event_add_defer(e, &sources[i], defer_fair_handler, &counters[i])); |
| 1153 |
ASSERT_OK(sd_event_source_set_enabled(sources[i], SD_EVENT_ON)); |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* Run the event loop until one of the handlers exits */ |
| 1157 |
ASSERT_OK(sd_event_loop(e)); |
| 1158 |
|
| 1159 |
/* All counters should be equal to 5, demonstrating fair scheduling */ |
| 1160 |
for (unsigned i = 0; i < 5; i++) { |
| 1161 |
ASSERT_EQ(counters[i], 5u); |
| 1162 |
sd_event_source_unref(sources[i]); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
TEST(child_autoreap_ebusy) { |
| 1167 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1168 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 1169 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 1170 |
|
| 1171 |
/* Test that sd_event_add_child() fails with EBUSY when kernel autoreaping is enabled |
| 1172 |
* by setting SIGCHLD disposition to SIG_IGN */ |
| 1173 |
|
| 1174 |
ASSERT_OK(sd_event_new(&e)); |
| 1175 |
|
| 1176 |
/* First, verify that adding a child source works with default signal disposition */ |
| 1177 |
ASSERT_OK_POSITIVE(pidref_safe_fork("(child-autoreaping-ebusy)", FORK_DEATHSIG_SIGKILL|FORK_FREEZE, &pidref)); |
| 1178 |
|
| 1179 |
ASSERT_OK(event_add_child_pidref(e, &s, &pidref, WEXITED, NULL, NULL)); |
| 1180 |
s = sd_event_source_unref(s); |
| 1181 |
|
| 1182 |
/* Now set SIGCHLD to SIG_IGN to enable kernel autoreaping */ |
| 1183 |
struct sigaction old_sa, new_sa = {}; |
| 1184 |
new_sa.sa_handler = SIG_IGN; |
| 1185 |
ASSERT_OK_ERRNO(sigaction(SIGCHLD, &new_sa, &old_sa)); |
| 1186 |
|
| 1187 |
ASSERT_ERROR(event_add_child_pidref(e, &s, &pidref, WEXITED, NULL, NULL), EBUSY); |
| 1188 |
|
| 1189 |
/* Restore original SIGCHLD disposition */ |
| 1190 |
ASSERT_OK_ERRNO(sigaction(SIGCHLD, &old_sa, NULL)); |
| 1191 |
} |
| 1192 |
|
| 1193 |
DEFINE_TEST_MAIN(LOG_DEBUG); |