]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-event/test-event.c
tree-wide: fix typos
[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 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0);
205
206 assert_se(pipe(a) >= 0);
207 assert_se(pipe(b) >= 0);
208 assert_se(pipe(d) >= 0);
209 assert_se(pipe(k) >= 0);
210
211 assert_se(sd_event_default(&e) >= 0);
212 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
213
214 assert_se(sd_event_set_watchdog(e, true) >= 0);
215
216 /* Test whether we cleanly can destroy an io event source from its own handler */
217 got_unref = false;
218 assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
219 assert_se(write(k[1], &ch, 1) == 1);
220 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
221 assert_se(got_unref);
222
223 got_a = false, got_b = false, got_c = false, got_d = 0;
224
225 /* Add a oneshot handler, trigger it, reenable it, and trigger
226 * it again. */
227 assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
228 assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
229 assert_se(write(d[1], &ch, 1) >= 0);
230 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
231 assert_se(got_d == 1);
232 assert_se(write(d[1], &ch, 1) >= 0);
233 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
234 assert_se(got_d == 2);
235
236 assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0);
237 assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0);
238
239 do_quit = false;
240 assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
241 assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);
242
243 assert_se(sd_event_source_set_priority(x, 99) >= 0);
244 assert_se(sd_event_source_get_priority(x, &priority) >= 0);
245 assert_se(priority == 99);
246 assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
247 assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
248 assert_se(sd_event_source_set_priority(z, 50) >= 0);
249 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
250 assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
251
252 /* Test for floating event sources */
253 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1, -1) >= 0);
254 assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);
255
256 assert_se(write(a[1], &ch, 1) >= 0);
257 assert_se(write(b[1], &ch, 1) >= 0);
258
259 assert_se(!got_a && !got_b && !got_c);
260
261 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
262
263 assert_se(!got_a && got_b && !got_c);
264
265 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
266
267 assert_se(!got_a && got_b && got_c);
268
269 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
270
271 assert_se(got_a && got_b && got_c);
272
273 sd_event_source_unref(x);
274 sd_event_source_unref(y);
275
276 do_quit = true;
277 assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0);
278 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
279 assert_se(sd_event_source_set_time(z, event_now + 200 * USEC_PER_MSEC) >= 0);
280 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
281
282 assert_se(sd_event_loop(e) >= 0);
283 assert_se(got_post);
284 assert_se(got_exit);
285
286 sd_event_source_unref(z);
287 sd_event_source_unref(q);
288
289 sd_event_source_unref(w);
290
291 sd_event_unref(e);
292
293 safe_close_pair(a);
294 safe_close_pair(b);
295 safe_close_pair(d);
296 safe_close_pair(k);
297
298 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
299 }
300
301 static void test_sd_event_now(void) {
302 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
303 uint64_t event_now;
304
305 assert_se(sd_event_new(&e) >= 0);
306 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
307 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
308 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
309 if (clock_boottime_supported()) {
310 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
311 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
312 }
313 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
314 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
315
316 assert_se(sd_event_run(e, 0) == 0);
317
318 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
319 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
320 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
321 if (clock_boottime_supported()) {
322 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0);
323 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0);
324 }
325 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
326 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
327 }
328
329 static int last_rtqueue_sigval = 0;
330 static int n_rtqueue = 0;
331
332 static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
333 last_rtqueue_sigval = si->ssi_int;
334 n_rtqueue++;
335 return 0;
336 }
337
338 static void test_rtqueue(void) {
339 sd_event_source *u = NULL, *v = NULL, *s = NULL;
340 sd_event *e = NULL;
341
342 assert_se(sd_event_default(&e) >= 0);
343
344 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2, -1) >= 0);
345 assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0);
346 assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0);
347 assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0);
348
349 assert_se(sd_event_source_set_priority(v, -10) >= 0);
350
351 assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
352 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
353 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
354 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
355 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
356
357 assert_se(n_rtqueue == 0);
358 assert_se(last_rtqueue_sigval == 0);
359
360 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
361 assert_se(n_rtqueue == 1);
362 assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */
363
364 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
365 assert_se(n_rtqueue == 2);
366 assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */
367
368 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
369 assert_se(n_rtqueue == 3);
370 assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */
371
372 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
373 assert_se(n_rtqueue == 4);
374 assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */
375
376 assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */
377 assert_se(n_rtqueue == 4);
378 assert_se(last_rtqueue_sigval == 1);
379
380 sd_event_source_unref(u);
381 sd_event_source_unref(v);
382 sd_event_source_unref(s);
383
384 sd_event_unref(e);
385 }
386
387 #define CREATE_EVENTS_MAX (70000U)
388
389 struct inotify_context {
390 bool delete_self_handler_called;
391 unsigned create_called[CREATE_EVENTS_MAX];
392 unsigned create_overflow;
393 unsigned n_create_events;
394 };
395
396 static void maybe_exit(sd_event_source *s, struct inotify_context *c) {
397 unsigned n;
398
399 assert(s);
400 assert(c);
401
402 if (!c->delete_self_handler_called)
403 return;
404
405 for (n = 0; n < 3; n++) {
406 unsigned i;
407
408 if (c->create_overflow & (1U << n))
409 continue;
410
411 for (i = 0; i < c->n_create_events; i++)
412 if (!(c->create_called[i] & (1U << n)))
413 return;
414 }
415
416 sd_event_exit(sd_event_source_get_event(s), 0);
417 }
418
419 static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
420 struct inotify_context *c = userdata;
421 const char *description;
422 unsigned bit, n;
423
424 assert_se(sd_event_source_get_description(s, &description) >= 0);
425 assert_se(safe_atou(description, &n) >= 0);
426
427 assert_se(n <= 3);
428 bit = 1U << n;
429
430 if (ev->mask & IN_Q_OVERFLOW) {
431 log_info("inotify-handler <%s>: overflow", description);
432 c->create_overflow |= bit;
433 } else if (ev->mask & IN_CREATE) {
434 unsigned i;
435
436 log_debug("inotify-handler <%s>: create on %s", description, ev->name);
437
438 if (!streq(ev->name, "sub")) {
439 assert_se(safe_atou(ev->name, &i) >= 0);
440
441 assert_se(i < c->n_create_events);
442 c->create_called[i] |= bit;
443 }
444 } else if (ev->mask & IN_DELETE) {
445 log_info("inotify-handler <%s>: delete of %s", description, ev->name);
446 assert_se(streq(ev->name, "sub"));
447 } else
448 assert_not_reached("unexpected inotify event");
449
450 maybe_exit(s, c);
451 return 1;
452 }
453
454 static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
455 struct inotify_context *c = userdata;
456
457 if (ev->mask & IN_Q_OVERFLOW) {
458 log_info("delete-self-handler: overflow");
459 c->delete_self_handler_called = true;
460 } else if (ev->mask & IN_DELETE_SELF) {
461 log_info("delete-self-handler: delete-self");
462 c->delete_self_handler_called = true;
463 } else if (ev->mask & IN_IGNORED) {
464 log_info("delete-self-handler: ignore");
465 } else
466 assert_not_reached("unexpected inotify event (delete-self)");
467
468 maybe_exit(s, c);
469 return 1;
470 }
471
472 static void test_inotify(unsigned n_create_events) {
473 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
474 sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL;
475 struct inotify_context context = {
476 .n_create_events = n_create_events,
477 };
478 sd_event *e = NULL;
479 const char *q;
480 unsigned i;
481
482 assert_se(sd_event_default(&e) >= 0);
483
484 assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0);
485
486 assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE|IN_ONLYDIR, inotify_handler, &context) >= 0);
487 assert_se(sd_event_add_inotify(e, &b, p, IN_CREATE|IN_DELETE|IN_DONT_FOLLOW, inotify_handler, &context) >= 0);
488 assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_IDLE) >= 0);
489 assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_NORMAL) >= 0);
490 assert_se(sd_event_add_inotify(e, &c, p, IN_CREATE|IN_DELETE|IN_EXCL_UNLINK, inotify_handler, &context) >= 0);
491 assert_se(sd_event_source_set_priority(c, SD_EVENT_PRIORITY_IDLE) >= 0);
492
493 assert_se(sd_event_source_set_description(a, "0") >= 0);
494 assert_se(sd_event_source_set_description(b, "1") >= 0);
495 assert_se(sd_event_source_set_description(c, "2") >= 0);
496
497 q = strjoina(p, "/sub");
498 assert_se(touch(q) >= 0);
499 assert_se(sd_event_add_inotify(e, &d, q, IN_DELETE_SELF, delete_self_handler, &context) >= 0);
500
501 for (i = 0; i < n_create_events; i++) {
502 char buf[DECIMAL_STR_MAX(unsigned)+1];
503 _cleanup_free_ char *z;
504
505 xsprintf(buf, "%u", i);
506 assert_se(z = path_join(p, buf));
507
508 assert_se(touch(z) >= 0);
509 }
510
511 assert_se(unlink(q) >= 0);
512
513 assert_se(sd_event_loop(e) >= 0);
514
515 sd_event_source_unref(a);
516 sd_event_source_unref(b);
517 sd_event_source_unref(c);
518 sd_event_source_unref(d);
519
520 sd_event_unref(e);
521 }
522
523 static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
524 assert_se(s);
525 assert_se(si);
526
527 assert_se(si->si_uid == getuid());
528 assert_se(si->si_signo == SIGCHLD);
529 assert_se(si->si_code == CLD_EXITED);
530 assert_se(si->si_status == 66);
531
532 log_info("got pidfd on %c", PTR_TO_INT(userdata));
533
534 assert_se(userdata == INT_TO_PTR('p'));
535
536 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
537 sd_event_source_unref(s);
538
539 return 0;
540 }
541
542 static void test_pidfd(void) {
543 sd_event_source *s = NULL, *t = NULL;
544 sd_event *e = NULL;
545 int pidfd;
546 pid_t pid, pid2;
547
548 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
549
550 pid = fork();
551 if (pid == 0)
552 /* child */
553 _exit(66);
554
555 assert_se(pid > 1);
556
557 pidfd = pidfd_open(pid, 0);
558 if (pidfd < 0) {
559 /* No pidfd_open() supported or blocked? */
560 assert_se(ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno));
561 (void) wait_for_terminate(pid, NULL);
562 return;
563 }
564
565 pid2 = fork();
566 if (pid2 == 0)
567 freeze();
568
569 assert_se(pid > 2);
570
571 assert_se(sd_event_default(&e) >= 0);
572 assert_se(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED, pidfd_handler, INT_TO_PTR('p')) >= 0);
573 assert_se(sd_event_source_set_child_pidfd_own(s, true) >= 0);
574
575 /* This one should never trigger, since our second child lives forever */
576 assert_se(sd_event_add_child(e, &t, pid2, WEXITED, pidfd_handler, INT_TO_PTR('q')) >= 0);
577 assert_se(sd_event_source_set_child_process_own(t, true) >= 0);
578
579 assert_se(sd_event_loop(e) >= 0);
580
581 /* Child should still be alive */
582 assert_se(kill(pid2, 0) >= 0);
583
584 t = sd_event_source_unref(t);
585
586 /* Child should now be dead, since we dropped the ref */
587 assert_se(kill(pid2, 0) < 0 && errno == ESRCH);
588
589 sd_event_unref(e);
590 }
591
592 static int ratelimit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
593 unsigned *c = (unsigned*) userdata;
594 *c += 1;
595 return 0;
596 }
597
598 static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
599 int r;
600
601 r = sd_event_source_set_enabled(s, SD_EVENT_ON);
602 if (r < 0)
603 log_warning_errno(r, "Failed to turn on notify event source: %m");
604
605 r = sd_event_source_set_time(s, usec + 1000);
606 if (r < 0)
607 log_error_errno(r, "Failed to restart watchdog event source: %m");
608
609 unsigned *c = (unsigned*) userdata;
610 *c += 1;
611
612 return 0;
613 }
614
615 static void test_ratelimit(void) {
616 _cleanup_close_pair_ int p[2] = {-1, -1};
617 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
618 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
619 uint64_t interval;
620 unsigned count, burst;
621
622 assert_se(sd_event_default(&e) >= 0);
623 assert_se(pipe2(p, O_CLOEXEC|O_NONBLOCK) >= 0);
624
625 assert_se(sd_event_add_io(e, &s, p[0], EPOLLIN, ratelimit_io_handler, &count) >= 0);
626 assert_se(sd_event_source_set_description(s, "test-ratelimit-io") >= 0);
627 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
628 assert_se(sd_event_source_get_ratelimit(s, &interval, &burst) >= 0);
629 assert_se(interval == 1 * USEC_PER_SEC && burst == 5);
630
631 assert_se(write(p[1], "1", 1) == 1);
632
633 count = 0;
634 for (unsigned i = 0; i < 10; i++) {
635 log_debug("slow loop iteration %u", i);
636 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
637 assert_se(usleep(250 * USEC_PER_MSEC) >= 0);
638 }
639
640 assert_se(sd_event_source_is_ratelimited(s) == 0);
641 assert_se(count == 10);
642 log_info("ratelimit_io_handler: called %d times, event source not ratelimited", count);
643
644 assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
645 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
646
647 count = 0;
648 for (unsigned i = 0; i < 10; i++) {
649 log_debug("fast event loop iteration %u", i);
650 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
651 assert_se(usleep(10) >= 0);
652 }
653 log_info("ratelimit_io_handler: called %d times, event source got ratelimited", count);
654 assert_se(count < 10);
655
656 s = sd_event_source_unref(s);
657 safe_close_pair(p);
658
659 count = 0;
660 assert_se(sd_event_add_time_relative(e, &s, CLOCK_MONOTONIC, 1000, 1, ratelimit_time_handler, &count) >= 0);
661 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) == 0);
662
663 do {
664 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
665 } while (!sd_event_source_is_ratelimited(s));
666
667 log_info("ratelimit_time_handler: called %d times, event source got ratelimited", count);
668 assert_se(count == 10);
669
670 /* In order to get rid of active rate limit client needs to disable it explicitly */
671 assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
672 assert_se(!sd_event_source_is_ratelimited(s));
673
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 10 more times, event source got ratelimited");
681 assert_se(count == 20);
682 }
683
684 int main(int argc, char *argv[]) {
685 test_setup_logging(LOG_DEBUG);
686
687 test_basic(true); /* test with pidfd */
688 test_basic(false); /* test without pidfd */
689
690 test_sd_event_now();
691 test_rtqueue();
692
693 test_inotify(100); /* should work without overflow */
694 test_inotify(33000); /* should trigger a q overflow */
695
696 test_pidfd();
697
698 test_ratelimit();
699
700 return 0;
701 }