1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
12 #include "missing_syscall.h"
13 #include "parse-util.h"
14 #include "path-util.h"
15 #include "process-util.h"
17 #include "signal-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
21 #include "tmpfile-util.h"
24 static int prepare_handler(sd_event_source
*s
, void *userdata
) {
25 log_info("preparing %c", PTR_TO_INT(userdata
));
29 static bool got_a
, got_b
, got_c
, got_unref
;
30 static unsigned got_d
;
32 static int unref_handler(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
33 sd_event_source_unref(s
);
38 static int io_handler(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
40 log_info("got IO on %c", PTR_TO_INT(userdata
));
42 if (userdata
== INT_TO_PTR('a')) {
43 assert_se(sd_event_source_set_enabled(s
, SD_EVENT_OFF
) >= 0);
46 } else if (userdata
== INT_TO_PTR('b')) {
49 } else if (userdata
== INT_TO_PTR('d')) {
52 assert_se(sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
) >= 0);
54 assert_se(sd_event_source_set_enabled(s
, SD_EVENT_OFF
) >= 0);
56 assert_not_reached("Yuck!");
61 static int child_handler(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
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);
71 log_info("got child on %c", PTR_TO_INT(userdata
));
73 assert_se(userdata
== INT_TO_PTR('f'));
75 assert_se(sd_event_exit(sd_event_source_get_event(s
), 0) >= 0);
76 sd_event_source_unref(s
);
81 static int signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
82 sd_event_source
*p
= NULL
;
89 log_info("got signal on %c", PTR_TO_INT(userdata
));
91 assert_se(userdata
== INT_TO_PTR('e'));
93 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGUSR2
, -1) >= 0);
101 assert_se(sigemptyset(&ss
) >= 0);
102 assert_se(sigaddset(&ss
, SIGUSR2
) >= 0);
105 assert_se(sigwaitinfo(&ss
, &plain_si
) >= 0);
107 assert_se(plain_si
.si_signo
== SIGUSR2
);
108 assert_se(plain_si
.si_value
.sival_int
== 4711);
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);
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
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;
127 assert_se(sd_event_source_send_child_signal(p
, SIGUSR2
, &plain_si
, 0) >= 0);
129 sd_event_source_unref(s
);
134 static int defer_handler(sd_event_source
*s
, void *userdata
) {
135 sd_event_source
*p
= NULL
;
139 log_info("got defer on %c", PTR_TO_INT(userdata
));
141 assert_se(userdata
== INT_TO_PTR('d'));
143 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGUSR1
, -1) >= 0);
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);
149 sd_event_source_unref(s
);
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
));
159 if (userdata
== INT_TO_PTR('c')) {
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);
171 assert_not_reached("Huh?");
176 static bool got_exit
= false;
178 static int exit_handler(sd_event_source
*s
, void *userdata
) {
179 log_info("got quit handler on %c", PTR_TO_INT(userdata
));
186 static bool got_post
= false;
188 static int post_handler(sd_event_source
*s
, void *userdata
) {
189 log_info("got post handler");
196 static void test_basic(bool with_pidfd
) {
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 };
204 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd
), 1) >= 0);
206 assert_se(pipe(a
) >= 0);
207 assert_se(pipe(b
) >= 0);
208 assert_se(pipe(d
) >= 0);
209 assert_se(pipe(k
) >= 0);
211 assert_se(sd_event_default(&e
) >= 0);
212 assert_se(sd_event_now(e
, CLOCK_MONOTONIC
, &event_now
) > 0);
214 assert_se(sd_event_set_watchdog(e
, true) >= 0);
216 /* Test whether we cleanly can destroy an io event source from its own handler */
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
);
223 got_a
= false, got_b
= false, got_c
= false, got_d
= 0;
225 /* Add a oneshot handler, trigger it, reenable it, and trigger
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);
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);
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);
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);
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);
256 assert_se(write(a
[1], &ch
, 1) >= 0);
257 assert_se(write(b
[1], &ch
, 1) >= 0);
259 assert_se(!got_a
&& !got_b
&& !got_c
);
261 assert_se(sd_event_run(e
, (uint64_t) -1) >= 1);
263 assert_se(!got_a
&& got_b
&& !got_c
);
265 assert_se(sd_event_run(e
, (uint64_t) -1) >= 1);
267 assert_se(!got_a
&& got_b
&& got_c
);
269 assert_se(sd_event_run(e
, (uint64_t) -1) >= 1);
271 assert_se(got_a
&& got_b
&& got_c
);
273 sd_event_source_unref(x
);
274 sd_event_source_unref(y
);
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);
282 assert_se(sd_event_loop(e
) >= 0);
286 sd_event_source_unref(z
);
287 sd_event_source_unref(q
);
289 sd_event_source_unref(w
);
298 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
301 static void test_sd_event_now(void) {
302 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
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);
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
);
316 assert_se(sd_event_run(e
, 0) == 0);
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);
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
);
329 static int last_rtqueue_sigval
= 0;
330 static int n_rtqueue
= 0;
332 static int rtqueue_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
333 last_rtqueue_sigval
= si
->ssi_int
;
338 static void test_rtqueue(void) {
339 sd_event_source
*u
= NULL
, *v
= NULL
, *s
= NULL
;
342 assert_se(sd_event_default(&e
) >= 0);
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);
349 assert_se(sd_event_source_set_priority(v
, -10) >= 0);
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);
357 assert_se(n_rtqueue
== 0);
358 assert_se(last_rtqueue_sigval
== 0);
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 */
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 */
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 */
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 */
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);
380 sd_event_source_unref(u
);
381 sd_event_source_unref(v
);
382 sd_event_source_unref(s
);
387 #define CREATE_EVENTS_MAX (70000U)
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
;
396 static void maybe_exit(sd_event_source
*s
, struct inotify_context
*c
) {
402 if (!c
->delete_self_handler_called
)
405 for (n
= 0; n
< 3; n
++) {
408 if (c
->create_overflow
& (1U << n
))
411 for (i
= 0; i
< c
->n_create_events
; i
++)
412 if (!(c
->create_called
[i
] & (1U << n
)))
416 sd_event_exit(sd_event_source_get_event(s
), 0);
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
;
424 assert_se(sd_event_source_get_description(s
, &description
) >= 0);
425 assert_se(safe_atou(description
, &n
) >= 0);
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
) {
436 log_debug("inotify-handler <%s>: create on %s", description
, ev
->name
);
438 if (!streq(ev
->name
, "sub")) {
439 assert_se(safe_atou(ev
->name
, &i
) >= 0);
441 assert_se(i
< c
->n_create_events
);
442 c
->create_called
[i
] |= bit
;
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"));
448 assert_not_reached("unexpected inotify event");
454 static int delete_self_handler(sd_event_source
*s
, const struct inotify_event
*ev
, void *userdata
) {
455 struct inotify_context
*c
= userdata
;
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");
466 assert_not_reached("unexpected inotify event (delete-self)");
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
,
482 assert_se(sd_event_default(&e
) >= 0);
484 assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p
) >= 0);
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);
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);
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);
501 for (i
= 0; i
< n_create_events
; i
++) {
502 char buf
[DECIMAL_STR_MAX(unsigned)+1];
503 _cleanup_free_
char *z
;
505 xsprintf(buf
, "%u", i
);
506 assert_se(z
= path_join(p
, buf
));
508 assert_se(touch(z
) >= 0);
511 assert_se(unlink(q
) >= 0);
513 assert_se(sd_event_loop(e
) >= 0);
515 sd_event_source_unref(a
);
516 sd_event_source_unref(b
);
517 sd_event_source_unref(c
);
518 sd_event_source_unref(d
);
523 static int pidfd_handler(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
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);
532 log_info("got pidfd on %c", PTR_TO_INT(userdata
));
534 assert_se(userdata
== INT_TO_PTR('p'));
536 assert_se(sd_event_exit(sd_event_source_get_event(s
), 0) >= 0);
537 sd_event_source_unref(s
);
542 static void test_pidfd(void) {
543 sd_event_source
*s
= NULL
, *t
= NULL
;
548 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
558 pidfd
= pidfd_open(pid
, 0);
560 /* No pidfd_open() supported or blocked? */
561 assert_se(ERRNO_IS_NOT_SUPPORTED(errno
) || ERRNO_IS_PRIVILEGE(errno
));
562 (void) wait_for_terminate(pid
, NULL
);
572 assert_se(sd_event_default(&e
) >= 0);
573 assert_se(sd_event_add_child_pidfd(e
, &s
, pidfd
, WEXITED
, pidfd_handler
, INT_TO_PTR('p')) >= 0);
574 assert_se(sd_event_source_set_child_pidfd_own(s
, true) >= 0);
576 /* This one should never trigger, since our second child lives forever */
577 assert_se(sd_event_add_child(e
, &t
, pid2
, WEXITED
, pidfd_handler
, INT_TO_PTR('q')) >= 0);
578 assert_se(sd_event_source_set_child_process_own(t
, true) >= 0);
580 assert_se(sd_event_loop(e
) >= 0);
582 /* Child should still be alive */
583 assert_se(kill(pid2
, 0) >= 0);
585 t
= sd_event_source_unref(t
);
587 /* Child should now be dead, since we dropped the ref */
588 assert_se(kill(pid2
, 0) < 0 && errno
== ESRCH
);
593 int main(int argc
, char *argv
[]) {
594 test_setup_logging(LOG_INFO
);
596 test_basic(true); /* test with pidfd */
597 test_basic(false); /* test without pidfd */
602 test_inotify(100); /* should work without overflow */
603 test_inotify(33000); /* should trigger a q overflow */