]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-event/test-event.c
Merge pull request #18922 from yuwata/sd-event-fix-issue-18190
[thirdparty/systemd.git] / src / libsystemd / sd-event / test-event.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/wait.h>
4
5 #include "sd-event.h"
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fs-util.h"
10 #include "log.h"
11 #include "macro.h"
12 #include "missing_syscall.h"
13 #include "parse-util.h"
14 #include "path-util.h"
15 #include "process-util.h"
16 #include "rm-rf.h"
17 #include "signal-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
20 #include "tests.h"
21 #include "tmpfile-util.h"
22 #include "util.h"
23
24 static int prepare_handler(sd_event_source *s, void *userdata) {
25 log_info("preparing %c", PTR_TO_INT(userdata));
26 return 1;
27 }
28
29 static bool got_a, got_b, got_c, got_unref;
30 static unsigned got_d;
31
32 static int unref_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
33 sd_event_source_unref(s);
34 got_unref = true;
35 return 0;
36 }
37
38 static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
39
40 log_info("got IO on %c", PTR_TO_INT(userdata));
41
42 if (userdata == INT_TO_PTR('a')) {
43 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
44 assert_se(!got_a);
45 got_a = true;
46 } else if (userdata == INT_TO_PTR('b')) {
47 assert_se(!got_b);
48 got_b = true;
49 } else if (userdata == INT_TO_PTR('d')) {
50 got_d++;
51 if (got_d < 2)
52 assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
53 else
54 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
55 } else
56 assert_not_reached("Yuck!");
57
58 return 1;
59 }
60
61 static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
62
63 assert_se(s);
64 assert_se(si);
65
66 assert_se(si->si_uid == getuid());
67 assert_se(si->si_signo == SIGCHLD);
68 assert_se(si->si_code == CLD_EXITED);
69 assert_se(si->si_status == 78);
70
71 log_info("got child on %c", PTR_TO_INT(userdata));
72
73 assert_se(userdata == INT_TO_PTR('f'));
74
75 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
76 sd_event_source_unref(s);
77
78 return 1;
79 }
80
81 static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
82 sd_event_source *p = NULL;
83 pid_t pid;
84 siginfo_t plain_si;
85
86 assert_se(s);
87 assert_se(si);
88
89 log_info("got signal on %c", PTR_TO_INT(userdata));
90
91 assert_se(userdata == INT_TO_PTR('e'));
92
93 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGUSR2, -1) >= 0);
94
95 pid = fork();
96 assert_se(pid >= 0);
97
98 if (pid == 0) {
99 sigset_t ss;
100
101 assert_se(sigemptyset(&ss) >= 0);
102 assert_se(sigaddset(&ss, SIGUSR2) >= 0);
103
104 zero(plain_si);
105 assert_se(sigwaitinfo(&ss, &plain_si) >= 0);
106
107 assert_se(plain_si.si_signo == SIGUSR2);
108 assert_se(plain_si.si_value.sival_int == 4711);
109
110 _exit(78);
111 }
112
113 assert_se(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f')) >= 0);
114 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
115 assert_se(sd_event_source_set_child_process_own(p, true) >= 0);
116
117 /* We can't use structured initialization here, since the structure contains various unions and these
118 * fields lie in overlapping (carefully aligned) unions that LLVM is allergic to allow assignments
119 * to */
120 zero(plain_si);
121 plain_si.si_signo = SIGUSR2;
122 plain_si.si_code = SI_QUEUE;
123 plain_si.si_pid = getpid();
124 plain_si.si_uid = getuid();
125 plain_si.si_value.sival_int = 4711;
126
127 assert_se(sd_event_source_send_child_signal(p, SIGUSR2, &plain_si, 0) >= 0);
128
129 sd_event_source_unref(s);
130
131 return 1;
132 }
133
134 static int defer_handler(sd_event_source *s, void *userdata) {
135 sd_event_source *p = NULL;
136
137 assert_se(s);
138
139 log_info("got defer on %c", PTR_TO_INT(userdata));
140
141 assert_se(userdata == INT_TO_PTR('d'));
142
143 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1, -1) >= 0);
144
145 assert_se(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e')) >= 0);
146 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
147 raise(SIGUSR1);
148
149 sd_event_source_unref(s);
150
151 return 1;
152 }
153
154 static bool do_quit;
155
156 static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
157 log_info("got timer on %c", PTR_TO_INT(userdata));
158
159 if (userdata == INT_TO_PTR('c')) {
160
161 if (do_quit) {
162 sd_event_source *p;
163
164 assert_se(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d')) >= 0);
165 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
166 } else {
167 assert_se(!got_c);
168 got_c = true;
169 }
170 } else
171 assert_not_reached("Huh?");
172
173 return 2;
174 }
175
176 static bool got_exit = false;
177
178 static int exit_handler(sd_event_source *s, void *userdata) {
179 log_info("got quit handler on %c", PTR_TO_INT(userdata));
180
181 got_exit = true;
182
183 return 3;
184 }
185
186 static bool got_post = false;
187
188 static int post_handler(sd_event_source *s, void *userdata) {
189 log_info("got post handler");
190
191 got_post = true;
192
193 return 2;
194 }
195
196 static void test_basic(bool with_pidfd) {
197 sd_event *e = NULL;
198 sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL;
199 static const char ch = 'x';
200 int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 };
201 uint64_t event_now;
202 int64_t priority;
203
204 log_info("/* %s(pidfd=%s) */", __func__, yes_no(with_pidfd));
205
206 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0);
207
208 assert_se(pipe(a) >= 0);
209 assert_se(pipe(b) >= 0);
210 assert_se(pipe(d) >= 0);
211 assert_se(pipe(k) >= 0);
212
213 assert_se(sd_event_default(&e) >= 0);
214 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
215
216 assert_se(sd_event_set_watchdog(e, true) >= 0);
217
218 /* Test whether we cleanly can destroy an io event source from its own handler */
219 got_unref = false;
220 assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
221 assert_se(write(k[1], &ch, 1) == 1);
222 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
223 assert_se(got_unref);
224
225 got_a = false, got_b = false, got_c = false, got_d = 0;
226
227 /* Add a oneshot handler, trigger it, reenable it, and trigger
228 * it again. */
229 assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
230 assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
231 assert_se(write(d[1], &ch, 1) >= 0);
232 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
233 assert_se(got_d == 1);
234 assert_se(write(d[1], &ch, 1) >= 0);
235 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
236 assert_se(got_d == 2);
237
238 assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0);
239 assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0);
240
241 do_quit = false;
242 assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
243 assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);
244
245 assert_se(sd_event_source_set_priority(x, 99) >= 0);
246 assert_se(sd_event_source_get_priority(x, &priority) >= 0);
247 assert_se(priority == 99);
248 assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
249 assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
250 assert_se(sd_event_source_set_priority(z, 50) >= 0);
251 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
252 assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
253
254 /* Test for floating event sources */
255 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1, -1) >= 0);
256 assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);
257
258 assert_se(write(a[1], &ch, 1) >= 0);
259 assert_se(write(b[1], &ch, 1) >= 0);
260
261 assert_se(!got_a && !got_b && !got_c);
262
263 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
264
265 assert_se(!got_a && got_b && !got_c);
266
267 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
268
269 assert_se(!got_a && got_b && got_c);
270
271 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
272
273 assert_se(got_a && got_b && got_c);
274
275 sd_event_source_unref(x);
276 sd_event_source_unref(y);
277
278 do_quit = true;
279 assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0);
280 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
281 assert_se(sd_event_source_set_time(z, event_now + 200 * USEC_PER_MSEC) >= 0);
282 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
283
284 assert_se(sd_event_loop(e) >= 0);
285 assert_se(got_post);
286 assert_se(got_exit);
287
288 sd_event_source_unref(z);
289 sd_event_source_unref(q);
290
291 sd_event_source_unref(w);
292
293 sd_event_unref(e);
294
295 safe_close_pair(a);
296 safe_close_pair(b);
297 safe_close_pair(d);
298 safe_close_pair(k);
299
300 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
301 }
302
303 static void test_sd_event_now(void) {
304 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
305 uint64_t event_now;
306
307 log_info("/* %s */", __func__);
308
309 assert_se(sd_event_new(&e) >= 0);
310 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
311 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
312 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
313 if (clock_boottime_supported()) {
314 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
315 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
316 }
317 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
318 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
319
320 assert_se(sd_event_run(e, 0) == 0);
321
322 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
323 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
324 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
325 if (clock_boottime_supported()) {
326 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0);
327 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0);
328 }
329 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
330 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
331 }
332
333 static int last_rtqueue_sigval = 0;
334 static int n_rtqueue = 0;
335
336 static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
337 last_rtqueue_sigval = si->ssi_int;
338 n_rtqueue++;
339 return 0;
340 }
341
342 static void test_rtqueue(void) {
343 sd_event_source *u = NULL, *v = NULL, *s = NULL;
344 sd_event *e = NULL;
345
346 log_info("/* %s */", __func__);
347
348 assert_se(sd_event_default(&e) >= 0);
349
350 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2, -1) >= 0);
351 assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0);
352 assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0);
353 assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0);
354
355 assert_se(sd_event_source_set_priority(v, -10) >= 0);
356
357 assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
358 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
359 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
360 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
361 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
362
363 assert_se(n_rtqueue == 0);
364 assert_se(last_rtqueue_sigval == 0);
365
366 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
367 assert_se(n_rtqueue == 1);
368 assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */
369
370 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
371 assert_se(n_rtqueue == 2);
372 assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */
373
374 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
375 assert_se(n_rtqueue == 3);
376 assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */
377
378 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
379 assert_se(n_rtqueue == 4);
380 assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */
381
382 assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */
383 assert_se(n_rtqueue == 4);
384 assert_se(last_rtqueue_sigval == 1);
385
386 sd_event_source_unref(u);
387 sd_event_source_unref(v);
388 sd_event_source_unref(s);
389
390 sd_event_unref(e);
391 }
392
393 #define CREATE_EVENTS_MAX (70000U)
394
395 struct inotify_context {
396 bool delete_self_handler_called;
397 unsigned create_called[CREATE_EVENTS_MAX];
398 unsigned create_overflow;
399 unsigned n_create_events;
400 };
401
402 static void maybe_exit(sd_event_source *s, struct inotify_context *c) {
403 unsigned n;
404
405 assert(s);
406 assert(c);
407
408 if (!c->delete_self_handler_called)
409 return;
410
411 for (n = 0; n < 3; n++) {
412 unsigned i;
413
414 if (c->create_overflow & (1U << n))
415 continue;
416
417 for (i = 0; i < c->n_create_events; i++)
418 if (!(c->create_called[i] & (1U << n)))
419 return;
420 }
421
422 sd_event_exit(sd_event_source_get_event(s), 0);
423 }
424
425 static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
426 struct inotify_context *c = userdata;
427 const char *description;
428 unsigned bit, n;
429
430 assert_se(sd_event_source_get_description(s, &description) >= 0);
431 assert_se(safe_atou(description, &n) >= 0);
432
433 assert_se(n <= 3);
434 bit = 1U << n;
435
436 if (ev->mask & IN_Q_OVERFLOW) {
437 log_info("inotify-handler <%s>: overflow", description);
438 c->create_overflow |= bit;
439 } else if (ev->mask & IN_CREATE) {
440 unsigned i;
441
442 log_debug("inotify-handler <%s>: create on %s", description, ev->name);
443
444 if (!streq(ev->name, "sub")) {
445 assert_se(safe_atou(ev->name, &i) >= 0);
446
447 assert_se(i < c->n_create_events);
448 c->create_called[i] |= bit;
449 }
450 } else if (ev->mask & IN_DELETE) {
451 log_info("inotify-handler <%s>: delete of %s", description, ev->name);
452 assert_se(streq(ev->name, "sub"));
453 } else
454 assert_not_reached("unexpected inotify event");
455
456 maybe_exit(s, c);
457 return 1;
458 }
459
460 static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
461 struct inotify_context *c = userdata;
462
463 if (ev->mask & IN_Q_OVERFLOW) {
464 log_info("delete-self-handler: overflow");
465 c->delete_self_handler_called = true;
466 } else if (ev->mask & IN_DELETE_SELF) {
467 log_info("delete-self-handler: delete-self");
468 c->delete_self_handler_called = true;
469 } else if (ev->mask & IN_IGNORED) {
470 log_info("delete-self-handler: ignore");
471 } else
472 assert_not_reached("unexpected inotify event (delete-self)");
473
474 maybe_exit(s, c);
475 return 1;
476 }
477
478 static void test_inotify(unsigned n_create_events) {
479 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
480 sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL;
481 struct inotify_context context = {
482 .n_create_events = n_create_events,
483 };
484 sd_event *e = NULL;
485 const char *q;
486 unsigned i;
487
488 log_info("/* %s(%u) */", __func__, n_create_events);
489
490 assert_se(sd_event_default(&e) >= 0);
491
492 assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0);
493
494 assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE|IN_ONLYDIR, inotify_handler, &context) >= 0);
495 assert_se(sd_event_add_inotify(e, &b, p, IN_CREATE|IN_DELETE|IN_DONT_FOLLOW, inotify_handler, &context) >= 0);
496 assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_IDLE) >= 0);
497 assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_NORMAL) >= 0);
498 assert_se(sd_event_add_inotify(e, &c, p, IN_CREATE|IN_DELETE|IN_EXCL_UNLINK, inotify_handler, &context) >= 0);
499 assert_se(sd_event_source_set_priority(c, SD_EVENT_PRIORITY_IDLE) >= 0);
500
501 assert_se(sd_event_source_set_description(a, "0") >= 0);
502 assert_se(sd_event_source_set_description(b, "1") >= 0);
503 assert_se(sd_event_source_set_description(c, "2") >= 0);
504
505 q = strjoina(p, "/sub");
506 assert_se(touch(q) >= 0);
507 assert_se(sd_event_add_inotify(e, &d, q, IN_DELETE_SELF, delete_self_handler, &context) >= 0);
508
509 for (i = 0; i < n_create_events; i++) {
510 char buf[DECIMAL_STR_MAX(unsigned)+1];
511 _cleanup_free_ char *z;
512
513 xsprintf(buf, "%u", i);
514 assert_se(z = path_join(p, buf));
515
516 assert_se(touch(z) >= 0);
517 }
518
519 assert_se(unlink(q) >= 0);
520
521 assert_se(sd_event_loop(e) >= 0);
522
523 sd_event_source_unref(a);
524 sd_event_source_unref(b);
525 sd_event_source_unref(c);
526 sd_event_source_unref(d);
527
528 sd_event_unref(e);
529 }
530
531 static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
532 assert_se(s);
533 assert_se(si);
534
535 assert_se(si->si_uid == getuid());
536 assert_se(si->si_signo == SIGCHLD);
537 assert_se(si->si_code == CLD_EXITED);
538 assert_se(si->si_status == 66);
539
540 log_info("got pidfd on %c", PTR_TO_INT(userdata));
541
542 assert_se(userdata == INT_TO_PTR('p'));
543
544 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
545 sd_event_source_unref(s);
546
547 return 0;
548 }
549
550 static void test_pidfd(void) {
551 sd_event_source *s = NULL, *t = NULL;
552 sd_event *e = NULL;
553 int pidfd;
554 pid_t pid, pid2;
555
556 log_info("/* %s */", __func__);
557
558 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
559
560 pid = fork();
561 if (pid == 0)
562 /* child */
563 _exit(66);
564
565 assert_se(pid > 1);
566
567 pidfd = pidfd_open(pid, 0);
568 if (pidfd < 0) {
569 /* No pidfd_open() supported or blocked? */
570 assert_se(ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno));
571 (void) wait_for_terminate(pid, NULL);
572 return;
573 }
574
575 pid2 = fork();
576 if (pid2 == 0)
577 freeze();
578
579 assert_se(pid > 2);
580
581 assert_se(sd_event_default(&e) >= 0);
582 assert_se(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED, pidfd_handler, INT_TO_PTR('p')) >= 0);
583 assert_se(sd_event_source_set_child_pidfd_own(s, true) >= 0);
584
585 /* This one should never trigger, since our second child lives forever */
586 assert_se(sd_event_add_child(e, &t, pid2, WEXITED, pidfd_handler, INT_TO_PTR('q')) >= 0);
587 assert_se(sd_event_source_set_child_process_own(t, true) >= 0);
588
589 assert_se(sd_event_loop(e) >= 0);
590
591 /* Child should still be alive */
592 assert_se(kill(pid2, 0) >= 0);
593
594 t = sd_event_source_unref(t);
595
596 /* Child should now be dead, since we dropped the ref */
597 assert_se(kill(pid2, 0) < 0 && errno == ESRCH);
598
599 sd_event_unref(e);
600 }
601
602 static int ratelimit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
603 unsigned *c = (unsigned*) userdata;
604 *c += 1;
605 return 0;
606 }
607
608 static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
609 int r;
610
611 r = sd_event_source_set_enabled(s, SD_EVENT_ON);
612 if (r < 0)
613 log_warning_errno(r, "Failed to turn on notify event source: %m");
614
615 r = sd_event_source_set_time(s, usec + 1000);
616 if (r < 0)
617 log_error_errno(r, "Failed to restart watchdog event source: %m");
618
619 unsigned *c = (unsigned*) userdata;
620 *c += 1;
621
622 return 0;
623 }
624
625 static void test_ratelimit(void) {
626 _cleanup_close_pair_ int p[2] = {-1, -1};
627 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
628 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
629 uint64_t interval;
630 unsigned count, burst;
631
632 log_info("/* %s */", __func__);
633
634 assert_se(sd_event_default(&e) >= 0);
635 assert_se(pipe2(p, O_CLOEXEC|O_NONBLOCK) >= 0);
636
637 assert_se(sd_event_add_io(e, &s, p[0], EPOLLIN, ratelimit_io_handler, &count) >= 0);
638 assert_se(sd_event_source_set_description(s, "test-ratelimit-io") >= 0);
639 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
640 assert_se(sd_event_source_get_ratelimit(s, &interval, &burst) >= 0);
641 assert_se(interval == 1 * USEC_PER_SEC && burst == 5);
642
643 assert_se(write(p[1], "1", 1) == 1);
644
645 count = 0;
646 for (unsigned i = 0; i < 10; i++) {
647 log_debug("slow loop iteration %u", i);
648 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
649 assert_se(usleep(250 * USEC_PER_MSEC) >= 0);
650 }
651
652 assert_se(sd_event_source_is_ratelimited(s) == 0);
653 assert_se(count == 10);
654 log_info("ratelimit_io_handler: called %d times, event source not ratelimited", count);
655
656 assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
657 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
658
659 count = 0;
660 for (unsigned i = 0; i < 10; i++) {
661 log_debug("fast event loop iteration %u", i);
662 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
663 assert_se(usleep(10) >= 0);
664 }
665 log_info("ratelimit_io_handler: called %d times, event source got ratelimited", count);
666 assert_se(count < 10);
667
668 s = sd_event_source_unref(s);
669 safe_close_pair(p);
670
671 count = 0;
672 assert_se(sd_event_add_time_relative(e, &s, CLOCK_MONOTONIC, 1000, 1, ratelimit_time_handler, &count) >= 0);
673 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) == 0);
674
675 do {
676 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
677 } while (!sd_event_source_is_ratelimited(s));
678
679 log_info("ratelimit_time_handler: called %d times, event source got ratelimited", count);
680 assert_se(count == 10);
681
682 /* In order to get rid of active rate limit client needs to disable it explicitly */
683 assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
684 assert_se(!sd_event_source_is_ratelimited(s));
685
686 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) >= 0);
687
688 do {
689 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
690 } while (!sd_event_source_is_ratelimited(s));
691
692 log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited");
693 assert_se(count == 20);
694 }
695
696 int main(int argc, char *argv[]) {
697 test_setup_logging(LOG_DEBUG);
698
699 test_basic(true); /* test with pidfd */
700 test_basic(false); /* test without pidfd */
701
702 test_sd_event_now();
703 test_rtqueue();
704
705 test_inotify(100); /* should work without overflow */
706 test_inotify(33000); /* should trigger a q overflow */
707
708 test_pidfd();
709
710 test_ratelimit();
711
712 return 0;
713 }