]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-event/test-event.c
tree-wide: fix a couple of typos
[thirdparty/systemd.git] / src / libsystemd / sd-event / test-event.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
fd38203a 2
fe993888 3#include <sys/wait.h>
2eeff0f4 4#include <unistd.h>
fe993888 5
fd38203a 6#include "sd-event.h"
3ffd4af2 7
41e09d62 8#include "alloc-util.h"
45a68ed3 9#include "exec-util.h"
3ffd4af2 10#include "fd-util.h"
41e09d62 11#include "fs-util.h"
fd38203a 12#include "log.h"
0c0cdb06 13#include "macro.h"
3ecb3bdc 14#include "missing_syscall.h"
41e09d62 15#include "parse-util.h"
657ee2d8 16#include "path-util.h"
41e09d62 17#include "process-util.h"
c14e57ba 18#include "random-util.h"
41e09d62 19#include "rm-rf.h"
24882e06 20#include "signal-util.h"
41e09d62
LP
21#include "stdio-util.h"
22#include "string-util.h"
6d7c4033 23#include "tests.h"
e4de7287 24#include "tmpfile-util.h"
fd38203a
LP
25
26static int prepare_handler(sd_event_source *s, void *userdata) {
27 log_info("preparing %c", PTR_TO_INT(userdata));
28 return 1;
29}
30
12179984 31static bool got_a, got_b, got_c, got_unref;
9ec9694c 32static unsigned got_d;
fd38203a 33
12179984
LP
34static 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
fd38203a
LP
40static 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')) {
baf76283 45 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
fd38203a
LP
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;
9ec9694c
DS
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);
fd38203a 57 } else
04499a70 58 assert_not_reached();
fd38203a
LP
59
60 return 1;
61}
62
63static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
64
0c0cdb06
RC
65 assert_se(s);
66 assert_se(si);
fd38203a 67
3ecb3bdc
LP
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
fd38203a
LP
73 log_info("got child on %c", PTR_TO_INT(userdata));
74
0c0cdb06 75 assert_se(userdata == INT_TO_PTR('f'));
fd38203a 76
6203e07a 77 assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
fd38203a
LP
78 sd_event_source_unref(s);
79
80 return 1;
81}
82
83static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
39883f62 84 sd_event_source *p = NULL;
fd38203a 85 pid_t pid;
3ecb3bdc 86 siginfo_t plain_si;
fd38203a 87
0c0cdb06
RC
88 assert_se(s);
89 assert_se(si);
fd38203a
LP
90
91 log_info("got signal on %c", PTR_TO_INT(userdata));
92
0c0cdb06 93 assert_se(userdata == INT_TO_PTR('e'));
fd38203a 94
3ecb3bdc 95 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGUSR2, -1) >= 0);
fd38203a
LP
96
97 pid = fork();
98 assert_se(pid >= 0);
99
3ecb3bdc
LP
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 }
fd38203a 114
151b9b96 115 assert_se(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f')) >= 0);
baf76283 116 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
3ecb3bdc
LP
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);
fd38203a
LP
130
131 sd_event_source_unref(s);
132
133 return 1;
134}
135
136static int defer_handler(sd_event_source *s, void *userdata) {
39883f62 137 sd_event_source *p = NULL;
fd38203a 138
0c0cdb06 139 assert_se(s);
fd38203a
LP
140
141 log_info("got defer on %c", PTR_TO_INT(userdata));
142
0c0cdb06 143 assert_se(userdata == INT_TO_PTR('d'));
fd38203a 144
72c0a2c2
LP
145 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1, -1) >= 0);
146
151b9b96 147 assert_se(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e')) >= 0);
baf76283 148 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
fd38203a
LP
149 raise(SIGUSR1);
150
151 sd_event_source_unref(s);
152
153 return 1;
154}
155
3ecb3bdc 156static bool do_quit;
fd38203a
LP
157
158static 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
151b9b96 166 assert_se(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d')) >= 0);
baf76283 167 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
fd38203a 168 } else {
0c0cdb06 169 assert_se(!got_c);
fd38203a
LP
170 got_c = true;
171 }
172 } else
04499a70 173 assert_not_reached();
fd38203a
LP
174
175 return 2;
176}
177
6203e07a 178static bool got_exit = false;
da7e457c 179
6203e07a 180static int exit_handler(sd_event_source *s, void *userdata) {
da7e457c
LP
181 log_info("got quit handler on %c", PTR_TO_INT(userdata));
182
6203e07a 183 got_exit = true;
da7e457c
LP
184
185 return 3;
186}
187
509a07ad
EV
188static bool got_post = false;
189
190static 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
68da8adf 198static void test_basic_one(bool with_pidfd) {
fd38203a 199 sd_event *e = NULL;
12179984 200 sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL;
fd38203a 201 static const char ch = 'x';
19ee48a6
YW
202 int a[2] = PIPE_EBADF, b[2] = PIPE_EBADF,
203 d[2] = PIPE_EBADF, k[2] = PIPE_EBADF;
591df2b5 204 uint64_t event_now;
6680b8d1 205 int64_t priority;
fd38203a 206
7fe11e84
YW
207 log_info("/* %s(pidfd=%s) */", __func__, yes_no(with_pidfd));
208
3ecb3bdc
LP
209 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0);
210
fd38203a
LP
211 assert_se(pipe(a) >= 0);
212 assert_se(pipe(b) >= 0);
9ec9694c 213 assert_se(pipe(d) >= 0);
12179984 214 assert_se(pipe(k) >= 0);
fd38203a 215
afc6adb5 216 assert_se(sd_event_default(&e) >= 0);
591df2b5 217 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
fd38203a 218
cde93897
LP
219 assert_se(sd_event_set_watchdog(e, true) >= 0);
220
12179984
LP
221 /* Test whether we cleanly can destroy an io event source from its own handler */
222 got_unref = false;
151b9b96 223 assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
12179984 224 assert_se(write(k[1], &ch, 1) == 1);
f5fbe71d 225 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
12179984
LP
226 assert_se(got_unref);
227
9ec9694c
DS
228 got_a = false, got_b = false, got_c = false, got_d = 0;
229
254d1313 230 /* Add a oneshot handler, trigger it, reenable it, and trigger it again. */
151b9b96 231 assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
9ec9694c
DS
232 assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
233 assert_se(write(d[1], &ch, 1) >= 0);
f5fbe71d 234 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9ec9694c
DS
235 assert_se(got_d == 1);
236 assert_se(write(d[1], &ch, 1) >= 0);
f5fbe71d 237 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9ec9694c 238 assert_se(got_d == 2);
fd38203a 239
151b9b96
LP
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);
3ecb3bdc
LP
242
243 do_quit = false;
6a0f1f6d 244 assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
151b9b96 245 assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);
fd38203a
LP
246
247 assert_se(sd_event_source_set_priority(x, 99) >= 0);
6680b8d1
ME
248 assert_se(sd_event_source_get_priority(x, &priority) >= 0);
249 assert_se(priority == 99);
baf76283 250 assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
fd38203a
LP
251 assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
252 assert_se(sd_event_source_set_priority(z, 50) >= 0);
baf76283 253 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
fd38203a 254 assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
a71fe8b8
LP
255
256 /* Test for floating event sources */
72c0a2c2 257 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1, -1) >= 0);
a71fe8b8 258 assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);
fd38203a
LP
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
f5fbe71d 265 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
fd38203a
LP
266
267 assert_se(!got_a && got_b && !got_c);
268
f5fbe71d 269 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
fd38203a
LP
270
271 assert_se(!got_a && got_b && got_c);
272
f5fbe71d 273 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
fd38203a
LP
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;
509a07ad 281 assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0);
591df2b5
EV
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);
baf76283 284 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
fd38203a
LP
285
286 assert_se(sd_event_loop(e) >= 0);
509a07ad 287 assert_se(got_post);
5657c75f 288 assert_se(got_exit);
fd38203a
LP
289
290 sd_event_source_unref(z);
da7e457c 291 sd_event_source_unref(q);
fd38203a 292
d5e4ec5b
LP
293 sd_event_source_unref(w);
294
fd38203a
LP
295 sd_event_unref(e);
296
3d94f76c
LP
297 safe_close_pair(a);
298 safe_close_pair(b);
299 safe_close_pair(d);
300 safe_close_pair(k);
3ecb3bdc
LP
301
302 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
9da4cb2b
LP
303}
304
68da8adf
JJ
305TEST(basic) {
306 test_basic_one(true); /* test with pidfd */
307 test_basic_one(false); /* test without pidfd */
308}
309
310TEST(sd_event_now) {
2c86ba5a
ZJS
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);
ba4e0427
LP
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);
2c86ba5a
ZJS
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);
ba4e0427
LP
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);
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
68da8adf 343TEST(rtqueue) {
9da4cb2b
LP
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
19500112
YW
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);
9da4cb2b
LP
361
362 assert_se(n_rtqueue == 0);
363 assert_se(last_rtqueue_sigval == 0);
364
f5fbe71d 365 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
366 assert_se(n_rtqueue == 1);
367 assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */
368
f5fbe71d 369 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
370 assert_se(n_rtqueue == 2);
371 assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */
372
f5fbe71d 373 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
374 assert_se(n_rtqueue == 3);
375 assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */
376
f5fbe71d 377 assert_se(sd_event_run(e, UINT64_MAX) >= 1);
9da4cb2b
LP
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
41e09d62
LP
392#define CREATE_EVENTS_MAX (70000U)
393
394struct 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
401static void maybe_exit(sd_event_source *s, struct inotify_context *c) {
402 unsigned n;
403
f21b863e
YW
404 assert_se(s);
405 assert_se(c);
41e09d62
LP
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
424static 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) {
834f3ba1
YW
439 if (streq(ev->name, "sub"))
440 log_debug("inotify-handler <%s>: create on %s", description, ev->name);
441 else {
442 unsigned i;
41e09d62 443
41e09d62 444 assert_se(safe_atou(ev->name, &i) >= 0);
41e09d62
LP
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
04499a70 452 assert_not_reached();
41e09d62
LP
453
454 maybe_exit(s, c);
455 return 1;
456}
457
458static 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
04499a70 470 assert_not_reached();
41e09d62
LP
471
472 maybe_exit(s, c);
473 return 1;
474}
475
68da8adf 476static void test_inotify_one(unsigned n_create_events) {
41e09d62
LP
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
7fe11e84
YW
486 log_info("/* %s(%u) */", __func__, n_create_events);
487
41e09d62
LP
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];
3d41b6b8 509 _cleanup_free_ char *z = NULL;
41e09d62
LP
510
511 xsprintf(buf, "%u", i);
657ee2d8 512 assert_se(z = path_join(p, buf));
41e09d62
LP
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
68da8adf
JJ
529TEST(inotify) {
530 test_inotify_one(100); /* should work without overflow */
531 test_inotify_one(33000); /* should trigger a q overflow */
532}
533
3ecb3bdc
LP
534static 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
68da8adf 553TEST(pidfd) {
3ecb3bdc
LP
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();
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
fd69f224
MS
626static int expired = -1;
627static int ratelimit_expired(sd_event_source *s, void *userdata) {
628 return ++expired;
629}
630
68da8adf 631TEST(ratelimit) {
19ee48a6 632 _cleanup_close_pair_ int p[2] = PIPE_EBADF;
68d89065
MS
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);
c0f86d66 658 log_info("ratelimit_io_handler: called %u times, event source not ratelimited", count);
68d89065
MS
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 }
c0f86d66 669 log_info("ratelimit_io_handler: called %u times, event source got ratelimited", count);
68d89065
MS
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
c0f86d66 683 log_info("ratelimit_time_handler: called %u times, event source got ratelimited", count);
68d89065
MS
684 assert_se(count == 10);
685
da115b93 686 /* In order to get rid of active rate limit client needs to disable it explicitly */
68d89065
MS
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
fd69f224
MS
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
68d89065
MS
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);
fd69f224
MS
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);
68d89065
MS
705}
706
68da8adf 707TEST(simple_timeout) {
c14e57ba
LP
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
035daf73
LP
726static 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
68da8adf 742TEST(inotify_self_destroy) {
035daf73
LP
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";
254d1313 746 _cleanup_close_ int fd = -EBADF;
035daf73
LP
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
c7b5a5a7
YW
760struct inotify_process_buffered_data_context {
761 const char *path[2];
762 unsigned i;
763};
764
765static 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
778TEST(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
2eeff0f4
LB
812TEST(fork) {
813 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
814 int r;
815
816 assert_se(sd_event_default(&e) >= 0);
817 assert_se(sd_event_prepare(e) == 0);
818
819 /* Check that after a fork the cleanup functions return NULL */
820 r = safe_fork("(bus-fork-test)", FORK_WAIT|FORK_LOG, NULL);
821 if (r == 0) {
822 assert_se(e);
823 assert_se(sd_event_ref(e) == NULL);
824 assert_se(sd_event_unref(e) == NULL);
825 _exit(EXIT_SUCCESS);
826 }
827
828 assert_se(r >= 0);
829}
830
2fdc274c
LP
831static int hup_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
832 unsigned *c = userdata;
833
834 assert_se(revents == EPOLLHUP);
835
836 (*c)++;
837 return 0;
838}
839
840TEST(leave_ratelimit) {
841 bool expect_ratelimit = false, manually_left_ratelimit = false;
842 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
843 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
5d2a48da 844 _cleanup_close_pair_ int pfd[2] = PIPE_EBADF;
2fdc274c
LP
845 unsigned c = 0;
846 int r;
847
848 assert_se(sd_event_default(&e) >= 0);
849
9a27ef09 850 /* Create an event source that will continuously fire by creating a pipe whose write side is closed,
2fdc274c
LP
851 * and which hence will only see EOF and constant EPOLLHUP */
852 assert_se(pipe2(pfd, O_CLOEXEC) >= 0);
853 assert_se(sd_event_add_io(e, &s, pfd[0], EPOLLIN, hup_callback, &c) >= 0);
854 assert_se(sd_event_source_set_io_fd_own(s, true) >= 0);
855 assert_se(sd_event_source_set_ratelimit(s, 5*USEC_PER_MINUTE, 5) >= 0);
856
857 pfd[0] = -EBADF;
9a27ef09 858 pfd[1] = safe_close(pfd[1]); /* Trigger continuous EOF */
2fdc274c
LP
859
860 for (;;) {
861 r = sd_event_prepare(e);
862 assert_se(r >= 0);
863
864 if (r == 0) {
865 r = sd_event_wait(e, UINT64_MAX);
866 assert_se(r > 0);
867 }
868
869 r = sd_event_dispatch(e);
870 assert_se(r > 0);
871
872 r = sd_event_source_is_ratelimited(s);
873 assert_se(r >= 0);
874
875 if (c < 5)
876 /* First four dispatches should just work */
877 assert_se(!r);
878 else if (c == 5) {
879 /* The fifth dispatch should still work, but we now expect the ratelimit to be hit subsequently */
880 if (!expect_ratelimit) {
881 assert_se(!r);
882 assert_se(sd_event_source_leave_ratelimit(s) == 0); /* this should be a NOP, and return 0 hence */
883 expect_ratelimit = true;
884 } else {
885 /* We expected the ratelimit, let's leave it manually, and verify it */
886 assert_se(r);
887 assert_se(sd_event_source_leave_ratelimit(s) > 0); /* we are ratelimited, hence should return > 0 */
888 assert_se(sd_event_source_is_ratelimited(s) == 0);
889
890 manually_left_ratelimit = true;
891 }
892
893 } else if (c == 6)
894 /* On the sixth iteration let's just exit */
895 break;
896 }
897
898 /* Verify we definitely hit the ratelimit and left it manually again */
899 assert_se(manually_left_ratelimit);
900}
901
68da8adf 902DEFINE_TEST_MAIN(LOG_DEBUG);