]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-event/test-event.c
Merge pull request #23849 from mbiebl/more-https
[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_one(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 TEST(basic) {
306 test_basic_one(true); /* test with pidfd */
307 test_basic_one(false); /* test without pidfd */
308 }
309
310 TEST(sd_event_now) {
311 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
312 uint64_t event_now;
313
314 assert_se(sd_event_new(&e) >= 0);
315 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
316 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
317 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
318 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
319 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
320 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
321 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
322
323 assert_se(sd_event_run(e, 0) == 0);
324
325 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
326 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
327 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
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 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
331 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
332 }
333
334 static int last_rtqueue_sigval = 0;
335 static int n_rtqueue = 0;
336
337 static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
338 last_rtqueue_sigval = si->ssi_int;
339 n_rtqueue++;
340 return 0;
341 }
342
343 TEST(rtqueue) {
344 sd_event_source *u = NULL, *v = NULL, *s = NULL;
345 sd_event *e = NULL;
346
347 assert_se(sd_event_default(&e) >= 0);
348
349 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2, -1) >= 0);
350 assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0);
351 assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0);
352 assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0);
353
354 assert_se(sd_event_source_set_priority(v, -10) >= 0);
355
356 assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
357 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
358 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
359 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
360 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
361
362 assert_se(n_rtqueue == 0);
363 assert_se(last_rtqueue_sigval == 0);
364
365 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
366 assert_se(n_rtqueue == 1);
367 assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */
368
369 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
370 assert_se(n_rtqueue == 2);
371 assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */
372
373 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
374 assert_se(n_rtqueue == 3);
375 assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */
376
377 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
378 assert_se(n_rtqueue == 4);
379 assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */
380
381 assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */
382 assert_se(n_rtqueue == 4);
383 assert_se(last_rtqueue_sigval == 1);
384
385 sd_event_source_unref(u);
386 sd_event_source_unref(v);
387 sd_event_source_unref(s);
388
389 sd_event_unref(e);
390 }
391
392 #define CREATE_EVENTS_MAX (70000U)
393
394 struct inotify_context {
395 bool delete_self_handler_called;
396 unsigned create_called[CREATE_EVENTS_MAX];
397 unsigned create_overflow;
398 unsigned n_create_events;
399 };
400
401 static void maybe_exit(sd_event_source *s, struct inotify_context *c) {
402 unsigned n;
403
404 assert_se(s);
405 assert_se(c);
406
407 if (!c->delete_self_handler_called)
408 return;
409
410 for (n = 0; n < 3; n++) {
411 unsigned i;
412
413 if (c->create_overflow & (1U << n))
414 continue;
415
416 for (i = 0; i < c->n_create_events; i++)
417 if (!(c->create_called[i] & (1U << n)))
418 return;
419 }
420
421 sd_event_exit(sd_event_source_get_event(s), 0);
422 }
423
424 static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
425 struct inotify_context *c = userdata;
426 const char *description;
427 unsigned bit, n;
428
429 assert_se(sd_event_source_get_description(s, &description) >= 0);
430 assert_se(safe_atou(description, &n) >= 0);
431
432 assert_se(n <= 3);
433 bit = 1U << n;
434
435 if (ev->mask & IN_Q_OVERFLOW) {
436 log_info("inotify-handler <%s>: overflow", description);
437 c->create_overflow |= bit;
438 } else if (ev->mask & IN_CREATE) {
439 if (streq(ev->name, "sub"))
440 log_debug("inotify-handler <%s>: create on %s", description, ev->name);
441 else {
442 unsigned i;
443
444 assert_se(safe_atou(ev->name, &i) >= 0);
445 assert_se(i < c->n_create_events);
446 c->create_called[i] |= bit;
447 }
448 } else if (ev->mask & IN_DELETE) {
449 log_info("inotify-handler <%s>: delete of %s", description, ev->name);
450 assert_se(streq(ev->name, "sub"));
451 } else
452 assert_not_reached();
453
454 maybe_exit(s, c);
455 return 1;
456 }
457
458 static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
459 struct inotify_context *c = userdata;
460
461 if (ev->mask & IN_Q_OVERFLOW) {
462 log_info("delete-self-handler: overflow");
463 c->delete_self_handler_called = true;
464 } else if (ev->mask & IN_DELETE_SELF) {
465 log_info("delete-self-handler: delete-self");
466 c->delete_self_handler_called = true;
467 } else if (ev->mask & IN_IGNORED) {
468 log_info("delete-self-handler: ignore");
469 } else
470 assert_not_reached();
471
472 maybe_exit(s, c);
473 return 1;
474 }
475
476 static void test_inotify_one(unsigned n_create_events) {
477 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
478 sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL;
479 struct inotify_context context = {
480 .n_create_events = n_create_events,
481 };
482 sd_event *e = NULL;
483 const char *q;
484 unsigned i;
485
486 log_info("/* %s(%u) */", __func__, n_create_events);
487
488 assert_se(sd_event_default(&e) >= 0);
489
490 assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0);
491
492 assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE|IN_ONLYDIR, inotify_handler, &context) >= 0);
493 assert_se(sd_event_add_inotify(e, &b, p, IN_CREATE|IN_DELETE|IN_DONT_FOLLOW, inotify_handler, &context) >= 0);
494 assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_IDLE) >= 0);
495 assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_NORMAL) >= 0);
496 assert_se(sd_event_add_inotify(e, &c, p, IN_CREATE|IN_DELETE|IN_EXCL_UNLINK, inotify_handler, &context) >= 0);
497 assert_se(sd_event_source_set_priority(c, SD_EVENT_PRIORITY_IDLE) >= 0);
498
499 assert_se(sd_event_source_set_description(a, "0") >= 0);
500 assert_se(sd_event_source_set_description(b, "1") >= 0);
501 assert_se(sd_event_source_set_description(c, "2") >= 0);
502
503 q = strjoina(p, "/sub");
504 assert_se(touch(q) >= 0);
505 assert_se(sd_event_add_inotify(e, &d, q, IN_DELETE_SELF, delete_self_handler, &context) >= 0);
506
507 for (i = 0; i < n_create_events; i++) {
508 char buf[DECIMAL_STR_MAX(unsigned)+1];
509 _cleanup_free_ char *z;
510
511 xsprintf(buf, "%u", i);
512 assert_se(z = path_join(p, buf));
513
514 assert_se(touch(z) >= 0);
515 }
516
517 assert_se(unlink(q) >= 0);
518
519 assert_se(sd_event_loop(e) >= 0);
520
521 sd_event_source_unref(a);
522 sd_event_source_unref(b);
523 sd_event_source_unref(c);
524 sd_event_source_unref(d);
525
526 sd_event_unref(e);
527 }
528
529 TEST(inotify) {
530 test_inotify_one(100); /* should work without overflow */
531 test_inotify_one(33000); /* should trigger a q overflow */
532 }
533
534 static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
535 assert_se(s);
536 assert_se(si);
537
538 assert_se(si->si_uid == getuid());
539 assert_se(si->si_signo == SIGCHLD);
540 assert_se(si->si_code == CLD_EXITED);
541 assert_se(si->si_status == 66);
542
543 log_info("got pidfd on %c", PTR_TO_INT(userdata));
544
545 assert_se(userdata == INT_TO_PTR('p'));
546
547 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
548 sd_event_source_unref(s);
549
550 return 0;
551 }
552
553 TEST(pidfd) {
554 sd_event_source *s = NULL, *t = NULL;
555 sd_event *e = NULL;
556 int pidfd;
557 pid_t pid, pid2;
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 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] = {-1, -1};
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_se(sd_event_default(&e) >= 0);
639 assert_se(pipe2(p, O_CLOEXEC|O_NONBLOCK) >= 0);
640
641 assert_se(sd_event_add_io(e, &s, p[0], EPOLLIN, ratelimit_io_handler, &count) >= 0);
642 assert_se(sd_event_source_set_description(s, "test-ratelimit-io") >= 0);
643 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
644 assert_se(sd_event_source_get_ratelimit(s, &interval, &burst) >= 0);
645 assert_se(interval == 1 * USEC_PER_SEC && burst == 5);
646
647 assert_se(write(p[1], "1", 1) == 1);
648
649 count = 0;
650 for (unsigned i = 0; i < 10; i++) {
651 log_debug("slow loop iteration %u", i);
652 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
653 assert_se(usleep(250 * USEC_PER_MSEC) >= 0);
654 }
655
656 assert_se(sd_event_source_is_ratelimited(s) == 0);
657 assert_se(count == 10);
658 log_info("ratelimit_io_handler: called %d times, event source not ratelimited", count);
659
660 assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
661 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0);
662
663 count = 0;
664 for (unsigned i = 0; i < 10; i++) {
665 log_debug("fast event loop iteration %u", i);
666 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
667 assert_se(usleep(10) >= 0);
668 }
669 log_info("ratelimit_io_handler: called %d times, event source got ratelimited", count);
670 assert_se(count < 10);
671
672 s = sd_event_source_unref(s);
673 safe_close_pair(p);
674
675 count = 0;
676 assert_se(sd_event_add_time_relative(e, &s, CLOCK_MONOTONIC, 1000, 1, ratelimit_time_handler, &count) >= 0);
677 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) == 0);
678
679 do {
680 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
681 } while (!sd_event_source_is_ratelimited(s));
682
683 log_info("ratelimit_time_handler: called %d times, event source got ratelimited", count);
684 assert_se(count == 10);
685
686 /* In order to get rid of active rate limit client needs to disable it explicitly */
687 assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0);
688 assert_se(!sd_event_source_is_ratelimited(s));
689
690 assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) >= 0);
691
692 /* Set callback that will be invoked when we leave rate limited state. */
693 assert_se(sd_event_source_set_ratelimit_expire_callback(s, ratelimit_expired) >= 0);
694
695 do {
696 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
697 } while (!sd_event_source_is_ratelimited(s));
698
699 log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited");
700 assert_se(count == 20);
701
702 /* Dispatch the event loop once more and check that ratelimit expiration callback got called */
703 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
704 assert_se(expired == 0);
705 }
706
707 TEST(simple_timeout) {
708 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
709 usec_t f, t, some_time;
710
711 some_time = random_u64_range(2 * USEC_PER_SEC);
712
713 assert_se(sd_event_default(&e) >= 0);
714
715 assert_se(sd_event_prepare(e) == 0);
716
717 f = now(CLOCK_MONOTONIC);
718 assert_se(sd_event_wait(e, some_time) >= 0);
719 t = now(CLOCK_MONOTONIC);
720
721 /* The event loop may sleep longer than the specified time (timer accuracy, scheduling latencies, …),
722 * but never shorter. Let's check that. */
723 assert_se(t >= usec_add(f, some_time));
724 }
725
726 static int inotify_self_destroy_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
727 sd_event_source **p = userdata;
728
729 assert_se(ev);
730 assert_se(p);
731 assert_se(*p == s);
732
733 assert_se(FLAGS_SET(ev->mask, IN_ATTRIB));
734
735 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
736
737 *p = sd_event_source_unref(*p); /* here's what we actually intend to test: we destroy the event
738 * source from inside the event source handler */
739 return 1;
740 }
741
742 TEST(inotify_self_destroy) {
743 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
744 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
745 char path[] = "/tmp/inotifyXXXXXX";
746 _cleanup_close_ int fd = -1;
747
748 /* Tests that destroying an inotify event source from its own handler is safe */
749
750 assert_se(sd_event_default(&e) >= 0);
751
752 fd = mkostemp_safe(path);
753 assert_se(fd >= 0);
754 assert_se(sd_event_add_inotify_fd(e, &s, fd, IN_ATTRIB, inotify_self_destroy_handler, &s) >= 0);
755 fd = safe_close(fd);
756 assert_se(unlink(path) >= 0); /* This will trigger IN_ATTRIB because link count goes to zero */
757 assert_se(sd_event_loop(e) >= 0);
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_se(sd_event_source_get_description(s, &description) >= 0);
770
771 assert_se(c->i < 2);
772 assert_se(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_se(sd_event_default(&e) >= 0);
787
788 assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0);
789 assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &q) >= 0);
790
791 struct inotify_process_buffered_data_context context = {
792 .path = { p, q },
793 };
794
795 assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE, inotify_process_buffered_data_handler, &context) >= 0);
796 assert_se(sd_event_add_inotify(e, &b, q, IN_CREATE, inotify_process_buffered_data_handler, &context) >= 0);
797
798 assert_se(z = path_join(p, "aaa"));
799 assert_se(touch(z) >= 0);
800 z = mfree(z);
801 assert_se(z = path_join(q, "bbb"));
802 assert_se(touch(z) >= 0);
803 z = mfree(z);
804
805 assert_se(sd_event_run(e, 10 * USEC_PER_SEC) > 0);
806 assert_se(sd_event_prepare(e) > 0); /* issue #23826: this was 0. */
807 assert_se(sd_event_dispatch(e) > 0);
808 assert_se(sd_event_prepare(e) == 0);
809 assert_se(sd_event_wait(e, 0) == 0);
810 }
811
812 DEFINE_TEST_MAIN(LOG_DEBUG);