]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-event/test-event.c
systemctl: re-align colon in status output
[thirdparty/systemd.git] / src / libsystemd / sd-event / test-event.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
fd38203a 2
fe993888
MS
3#include <sys/wait.h>
4
fd38203a 5#include "sd-event.h"
3ffd4af2 6
41e09d62 7#include "alloc-util.h"
3ffd4af2 8#include "fd-util.h"
41e09d62 9#include "fs-util.h"
fd38203a 10#include "log.h"
0c0cdb06 11#include "macro.h"
3ecb3bdc 12#include "missing_syscall.h"
41e09d62 13#include "parse-util.h"
657ee2d8 14#include "path-util.h"
41e09d62 15#include "process-util.h"
c14e57ba 16#include "random-util.h"
41e09d62 17#include "rm-rf.h"
24882e06 18#include "signal-util.h"
41e09d62
LP
19#include "stdio-util.h"
20#include "string-util.h"
6d7c4033 21#include "tests.h"
e4de7287 22#include "tmpfile-util.h"
3ffd4af2 23#include "util.h"
fd38203a
LP
24
25static int prepare_handler(sd_event_source *s, void *userdata) {
26 log_info("preparing %c", PTR_TO_INT(userdata));
27 return 1;
28}
29
12179984 30static bool got_a, got_b, got_c, got_unref;
9ec9694c 31static unsigned got_d;
fd38203a 32
12179984
LP
33static int unref_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
34 sd_event_source_unref(s);
35 got_unref = true;
36 return 0;
37}
38
fd38203a
LP
39static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
40
41 log_info("got IO on %c", PTR_TO_INT(userdata));
42
43 if (userdata == INT_TO_PTR('a')) {
baf76283 44 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
fd38203a
LP
45 assert_se(!got_a);
46 got_a = true;
47 } else if (userdata == INT_TO_PTR('b')) {
48 assert_se(!got_b);
49 got_b = true;
9ec9694c
DS
50 } else if (userdata == INT_TO_PTR('d')) {
51 got_d++;
52 if (got_d < 2)
53 assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
54 else
55 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
fd38203a
LP
56 } else
57 assert_not_reached("Yuck!");
58
59 return 1;
60}
61
62static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
63
0c0cdb06
RC
64 assert_se(s);
65 assert_se(si);
fd38203a 66
3ecb3bdc
LP
67 assert_se(si->si_uid == getuid());
68 assert_se(si->si_signo == SIGCHLD);
69 assert_se(si->si_code == CLD_EXITED);
70 assert_se(si->si_status == 78);
71
fd38203a
LP
72 log_info("got child on %c", PTR_TO_INT(userdata));
73
0c0cdb06 74 assert_se(userdata == INT_TO_PTR('f'));
fd38203a 75
6203e07a 76 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
fd38203a
LP
77 sd_event_source_unref(s);
78
79 return 1;
80}
81
82static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
39883f62 83 sd_event_source *p = NULL;
fd38203a 84 pid_t pid;
3ecb3bdc 85 siginfo_t plain_si;
fd38203a 86
0c0cdb06
RC
87 assert_se(s);
88 assert_se(si);
fd38203a
LP
89
90 log_info("got signal on %c", PTR_TO_INT(userdata));
91
0c0cdb06 92 assert_se(userdata == INT_TO_PTR('e'));
fd38203a 93
3ecb3bdc 94 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGUSR2, -1) >= 0);
fd38203a
LP
95
96 pid = fork();
97 assert_se(pid >= 0);
98
3ecb3bdc
LP
99 if (pid == 0) {
100 sigset_t ss;
101
102 assert_se(sigemptyset(&ss) >= 0);
103 assert_se(sigaddset(&ss, SIGUSR2) >= 0);
104
105 zero(plain_si);
106 assert_se(sigwaitinfo(&ss, &plain_si) >= 0);
107
108 assert_se(plain_si.si_signo == SIGUSR2);
109 assert_se(plain_si.si_value.sival_int == 4711);
110
111 _exit(78);
112 }
fd38203a 113
151b9b96 114 assert_se(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f')) >= 0);
baf76283 115 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
3ecb3bdc
LP
116 assert_se(sd_event_source_set_child_process_own(p, true) >= 0);
117
118 /* We can't use structured initialization here, since the structure contains various unions and these
119 * fields lie in overlapping (carefully aligned) unions that LLVM is allergic to allow assignments
120 * to */
121 zero(plain_si);
122 plain_si.si_signo = SIGUSR2;
123 plain_si.si_code = SI_QUEUE;
124 plain_si.si_pid = getpid();
125 plain_si.si_uid = getuid();
126 plain_si.si_value.sival_int = 4711;
127
128 assert_se(sd_event_source_send_child_signal(p, SIGUSR2, &plain_si, 0) >= 0);
fd38203a
LP
129
130 sd_event_source_unref(s);
131
132 return 1;
133}
134
135static int defer_handler(sd_event_source *s, void *userdata) {
39883f62 136 sd_event_source *p = NULL;
fd38203a 137
0c0cdb06 138 assert_se(s);
fd38203a
LP
139
140 log_info("got defer on %c", PTR_TO_INT(userdata));
141
0c0cdb06 142 assert_se(userdata == INT_TO_PTR('d'));
fd38203a 143
72c0a2c2
LP
144 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1, -1) >= 0);
145
151b9b96 146 assert_se(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e')) >= 0);
baf76283 147 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
fd38203a
LP
148 raise(SIGUSR1);
149
150 sd_event_source_unref(s);
151
152 return 1;
153}
154
3ecb3bdc 155static bool do_quit;
fd38203a
LP
156
157static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
158 log_info("got timer on %c", PTR_TO_INT(userdata));
159
160 if (userdata == INT_TO_PTR('c')) {
161
162 if (do_quit) {
163 sd_event_source *p;
164
151b9b96 165 assert_se(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d')) >= 0);
baf76283 166 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
fd38203a 167 } else {
0c0cdb06 168 assert_se(!got_c);
fd38203a
LP
169 got_c = true;
170 }
171 } else
172 assert_not_reached("Huh?");
173
174 return 2;
175}
176
6203e07a 177static bool got_exit = false;
da7e457c 178
6203e07a 179static int exit_handler(sd_event_source *s, void *userdata) {
da7e457c
LP
180 log_info("got quit handler on %c", PTR_TO_INT(userdata));
181
6203e07a 182 got_exit = true;
da7e457c
LP
183
184 return 3;
185}
186
509a07ad
EV
187static bool got_post = false;
188
189static int post_handler(sd_event_source *s, void *userdata) {
190 log_info("got post handler");
191
192 got_post = true;
193
194 return 2;
195}
196
3ecb3bdc 197static void test_basic(bool with_pidfd) {
fd38203a 198 sd_event *e = NULL;
12179984 199 sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL;
fd38203a 200 static const char ch = 'x';
12179984 201 int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 };
591df2b5 202 uint64_t event_now;
6680b8d1 203 int64_t priority;
fd38203a 204
7fe11e84
YW
205 log_info("/* %s(pidfd=%s) */", __func__, yes_no(with_pidfd));
206
3ecb3bdc
LP
207 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0);
208
fd38203a
LP
209 assert_se(pipe(a) >= 0);
210 assert_se(pipe(b) >= 0);
9ec9694c 211 assert_se(pipe(d) >= 0);
12179984 212 assert_se(pipe(k) >= 0);
fd38203a 213
afc6adb5 214 assert_se(sd_event_default(&e) >= 0);
591df2b5 215 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
fd38203a 216
cde93897
LP
217 assert_se(sd_event_set_watchdog(e, true) >= 0);
218
12179984
LP
219 /* Test whether we cleanly can destroy an io event source from its own handler */
220 got_unref = false;
151b9b96 221 assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
12179984 222 assert_se(write(k[1], &ch, 1) == 1);
f5fbe71d 223 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
12179984
LP
224 assert_se(got_unref);
225
9ec9694c
DS
226 got_a = false, got_b = false, got_c = false, got_d = 0;
227
3fe91079 228 /* Add a oneshot handler, trigger it, reenable it, and trigger
9ec9694c 229 * it again. */
151b9b96 230 assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
9ec9694c
DS
231 assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
232 assert_se(write(d[1], &ch, 1) >= 0);
f5fbe71d 233 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9ec9694c
DS
234 assert_se(got_d == 1);
235 assert_se(write(d[1], &ch, 1) >= 0);
f5fbe71d 236 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9ec9694c 237 assert_se(got_d == 2);
fd38203a 238
151b9b96
LP
239 assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0);
240 assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0);
3ecb3bdc
LP
241
242 do_quit = false;
6a0f1f6d 243 assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
151b9b96 244 assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);
fd38203a
LP
245
246 assert_se(sd_event_source_set_priority(x, 99) >= 0);
6680b8d1
ME
247 assert_se(sd_event_source_get_priority(x, &priority) >= 0);
248 assert_se(priority == 99);
baf76283 249 assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
fd38203a
LP
250 assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
251 assert_se(sd_event_source_set_priority(z, 50) >= 0);
baf76283 252 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
fd38203a 253 assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
a71fe8b8
LP
254
255 /* Test for floating event sources */
72c0a2c2 256 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1, -1) >= 0);
a71fe8b8 257 assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);
fd38203a
LP
258
259 assert_se(write(a[1], &ch, 1) >= 0);
260 assert_se(write(b[1], &ch, 1) >= 0);
261
262 assert_se(!got_a && !got_b && !got_c);
263
f5fbe71d 264 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
fd38203a
LP
265
266 assert_se(!got_a && got_b && !got_c);
267
f5fbe71d 268 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
fd38203a
LP
269
270 assert_se(!got_a && got_b && got_c);
271
f5fbe71d 272 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
fd38203a
LP
273
274 assert_se(got_a && got_b && got_c);
275
276 sd_event_source_unref(x);
277 sd_event_source_unref(y);
278
279 do_quit = true;
509a07ad 280 assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0);
591df2b5
EV
281 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
282 assert_se(sd_event_source_set_time(z, event_now + 200 * USEC_PER_MSEC) >= 0);
baf76283 283 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
fd38203a
LP
284
285 assert_se(sd_event_loop(e) >= 0);
509a07ad 286 assert_se(got_post);
5657c75f 287 assert_se(got_exit);
fd38203a
LP
288
289 sd_event_source_unref(z);
da7e457c 290 sd_event_source_unref(q);
fd38203a 291
d5e4ec5b
LP
292 sd_event_source_unref(w);
293
fd38203a
LP
294 sd_event_unref(e);
295
3d94f76c
LP
296 safe_close_pair(a);
297 safe_close_pair(b);
298 safe_close_pair(d);
299 safe_close_pair(k);
3ecb3bdc
LP
300
301 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
9da4cb2b
LP
302}
303
2c86ba5a
ZJS
304static void test_sd_event_now(void) {
305 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
306 uint64_t event_now;
307
7fe11e84
YW
308 log_info("/* %s */", __func__);
309
2c86ba5a
ZJS
310 assert_se(sd_event_new(&e) >= 0);
311 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
312 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
313 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
3411372e
LP
314 if (clock_boottime_supported()) {
315 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
316 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
317 }
2c86ba5a
ZJS
318 assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
319 assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
320
321 assert_se(sd_event_run(e, 0) == 0);
322
323 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
324 assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
325 assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
3411372e
LP
326 if (clock_boottime_supported()) {
327 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0);
328 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0);
329 }
2c86ba5a
ZJS
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
9da4cb2b
LP
334static int last_rtqueue_sigval = 0;
335static int n_rtqueue = 0;
336
337static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
338 last_rtqueue_sigval = si->ssi_int;
313cefa1 339 n_rtqueue++;
9da4cb2b
LP
340 return 0;
341}
342
343static void test_rtqueue(void) {
344 sd_event_source *u = NULL, *v = NULL, *s = NULL;
345 sd_event *e = NULL;
346
7fe11e84
YW
347 log_info("/* %s */", __func__);
348
9da4cb2b
LP
349 assert_se(sd_event_default(&e) >= 0);
350
351 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2, -1) >= 0);
352 assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0);
353 assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0);
354 assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0);
355
356 assert_se(sd_event_source_set_priority(v, -10) >= 0);
357
19500112
YW
358 assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
359 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
360 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
361 assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
362 assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
9da4cb2b
LP
363
364 assert_se(n_rtqueue == 0);
365 assert_se(last_rtqueue_sigval == 0);
366
f5fbe71d 367 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
368 assert_se(n_rtqueue == 1);
369 assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */
370
f5fbe71d 371 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
372 assert_se(n_rtqueue == 2);
373 assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */
374
f5fbe71d 375 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
376 assert_se(n_rtqueue == 3);
377 assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */
378
f5fbe71d 379 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
380 assert_se(n_rtqueue == 4);
381 assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */
382
383 assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */
384 assert_se(n_rtqueue == 4);
385 assert_se(last_rtqueue_sigval == 1);
386
387 sd_event_source_unref(u);
388 sd_event_source_unref(v);
389 sd_event_source_unref(s);
390
391 sd_event_unref(e);
392}
393
41e09d62
LP
394#define CREATE_EVENTS_MAX (70000U)
395
396struct inotify_context {
397 bool delete_self_handler_called;
398 unsigned create_called[CREATE_EVENTS_MAX];
399 unsigned create_overflow;
400 unsigned n_create_events;
401};
402
403static void maybe_exit(sd_event_source *s, struct inotify_context *c) {
404 unsigned n;
405
406 assert(s);
407 assert(c);
408
409 if (!c->delete_self_handler_called)
410 return;
411
412 for (n = 0; n < 3; n++) {
413 unsigned i;
414
415 if (c->create_overflow & (1U << n))
416 continue;
417
418 for (i = 0; i < c->n_create_events; i++)
419 if (!(c->create_called[i] & (1U << n)))
420 return;
421 }
422
423 sd_event_exit(sd_event_source_get_event(s), 0);
424}
425
426static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
427 struct inotify_context *c = userdata;
428 const char *description;
429 unsigned bit, n;
430
431 assert_se(sd_event_source_get_description(s, &description) >= 0);
432 assert_se(safe_atou(description, &n) >= 0);
433
434 assert_se(n <= 3);
435 bit = 1U << n;
436
437 if (ev->mask & IN_Q_OVERFLOW) {
438 log_info("inotify-handler <%s>: overflow", description);
439 c->create_overflow |= bit;
440 } else if (ev->mask & IN_CREATE) {
441 unsigned i;
442
8788a568 443 log_debug("inotify-handler <%s>: create on %s", description, ev->name);
41e09d62
LP
444
445 if (!streq(ev->name, "sub")) {
446 assert_se(safe_atou(ev->name, &i) >= 0);
447
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("unexpected inotify event");
456
457 maybe_exit(s, c);
458 return 1;
459}
460
461static 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("unexpected inotify event (delete-self)");
474
475 maybe_exit(s, c);
476 return 1;
477}
478
479static 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
7fe11e84
YW
489 log_info("/* %s(%u) */", __func__, n_create_events);
490
41e09d62
LP
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);
657ee2d8 515 assert_se(z = path_join(p, buf));
41e09d62
LP
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
3ecb3bdc
LP
532static 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
551static 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
7fe11e84
YW
557 log_info("/* %s */", __func__);
558
3ecb3bdc
LP
559 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
560
561 pid = fork();
d7a0f1f4 562 if (pid == 0)
3ecb3bdc
LP
563 /* child */
564 _exit(66);
3ecb3bdc
LP
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
68d89065
MS
603static 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
609static 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
626static 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
7fe11e84
YW
633 log_info("/* %s */", __func__);
634
68d89065
MS
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
da115b93 683 /* In order to get rid of active rate limit client needs to disable it explicitly */
68d89065
MS
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
c14e57ba
LP
697static 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
9da4cb2b 716int main(int argc, char *argv[]) {
68d89065 717 test_setup_logging(LOG_DEBUG);
2c86ba5a 718
c14e57ba
LP
719 test_simple_timeout();
720
3ecb3bdc
LP
721 test_basic(true); /* test with pidfd */
722 test_basic(false); /* test without pidfd */
723
2c86ba5a 724 test_sd_event_now();
9da4cb2b 725 test_rtqueue();
fd38203a 726
41e09d62
LP
727 test_inotify(100); /* should work without overflow */
728 test_inotify(33000); /* should trigger a q overflow */
729
3ecb3bdc
LP
730 test_pidfd();
731
68d89065
MS
732 test_ratelimit();
733
fd38203a
LP
734 return 0;
735}