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