]>
Commit | Line | Data |
---|---|---|
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" |
3ffd4af2 | 9 | #include "fd-util.h" |
41e09d62 | 10 | #include "fs-util.h" |
fd38203a | 11 | #include "log.h" |
41e09d62 | 12 | #include "parse-util.h" |
657ee2d8 | 13 | #include "path-util.h" |
5cdf13c7 | 14 | #include "pidfd-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" |
5cdf13c7 | 22 | #include "time-util.h" |
e4de7287 | 23 | #include "tmpfile-util.h" |
fd38203a LP |
24 | |
25 | static int prepare_handler(sd_event_source *s, void *userdata) { | |
26 | log_info("preparing %c", PTR_TO_INT(userdata)); | |
27 | return 1; | |
28 | } | |
29 | ||
12179984 | 30 | static bool got_a, got_b, got_c, got_unref; |
9ec9694c | 31 | static unsigned got_d; |
fd38203a | 32 | |
12179984 LP |
33 | static 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 |
39 | static 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 | 56 | } else |
04499a70 | 57 | assert_not_reached(); |
fd38203a LP |
58 | |
59 | return 1; | |
60 | } | |
61 | ||
62 | static 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 | ||
82 | static 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 | |
db7136ec | 94 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGUSR2) >= 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; | |
19b761a0 | 124 | plain_si.si_pid = getpid_cached(); |
3ecb3bdc LP |
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 | ||
135 | static 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 | |
db7136ec | 144 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1) >= 0); |
72c0a2c2 | 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 | 155 | static bool do_quit; |
fd38203a LP |
156 | |
157 | static 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 | |
04499a70 | 172 | assert_not_reached(); |
fd38203a LP |
173 | |
174 | return 2; | |
175 | } | |
176 | ||
6203e07a | 177 | static bool got_exit = false; |
da7e457c | 178 | |
6203e07a | 179 | static 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 |
187 | static bool got_post = false; |
188 | ||
189 | static 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 | ||
6e14c46b | 197 | TEST(basic) { |
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'; |
71136404 LP |
201 | int a[2] = EBADF_PAIR, b[2] = EBADF_PAIR, |
202 | d[2] = EBADF_PAIR, k[2] = EBADF_PAIR; | |
591df2b5 | 203 | uint64_t event_now; |
6680b8d1 | 204 | int64_t priority; |
fd38203a LP |
205 | |
206 | assert_se(pipe(a) >= 0); | |
207 | assert_se(pipe(b) >= 0); | |
9ec9694c | 208 | assert_se(pipe(d) >= 0); |
12179984 | 209 | assert_se(pipe(k) >= 0); |
fd38203a | 210 | |
afc6adb5 | 211 | assert_se(sd_event_default(&e) >= 0); |
591df2b5 | 212 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0); |
fd38203a | 213 | |
cde93897 LP |
214 | assert_se(sd_event_set_watchdog(e, true) >= 0); |
215 | ||
12179984 LP |
216 | /* Test whether we cleanly can destroy an io event source from its own handler */ |
217 | got_unref = false; | |
151b9b96 | 218 | assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0); |
12179984 | 219 | assert_se(write(k[1], &ch, 1) == 1); |
f5fbe71d | 220 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
12179984 LP |
221 | assert_se(got_unref); |
222 | ||
9ec9694c DS |
223 | got_a = false, got_b = false, got_c = false, got_d = 0; |
224 | ||
254d1313 | 225 | /* Add a oneshot handler, trigger it, reenable it, and trigger it again. */ |
151b9b96 | 226 | assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0); |
9ec9694c DS |
227 | assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0); |
228 | assert_se(write(d[1], &ch, 1) >= 0); | |
f5fbe71d | 229 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9ec9694c DS |
230 | assert_se(got_d == 1); |
231 | assert_se(write(d[1], &ch, 1) >= 0); | |
f5fbe71d | 232 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9ec9694c | 233 | assert_se(got_d == 2); |
fd38203a | 234 | |
151b9b96 LP |
235 | assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0); |
236 | assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0); | |
3ecb3bdc LP |
237 | |
238 | do_quit = false; | |
6a0f1f6d | 239 | assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0); |
151b9b96 | 240 | assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0); |
fd38203a LP |
241 | |
242 | assert_se(sd_event_source_set_priority(x, 99) >= 0); | |
6680b8d1 ME |
243 | assert_se(sd_event_source_get_priority(x, &priority) >= 0); |
244 | assert_se(priority == 99); | |
baf76283 | 245 | assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0); |
fd38203a LP |
246 | assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0); |
247 | assert_se(sd_event_source_set_priority(z, 50) >= 0); | |
baf76283 | 248 | assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); |
fd38203a | 249 | assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0); |
a71fe8b8 LP |
250 | |
251 | /* Test for floating event sources */ | |
db7136ec | 252 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1) >= 0); |
a71fe8b8 | 253 | assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0); |
fd38203a LP |
254 | |
255 | assert_se(write(a[1], &ch, 1) >= 0); | |
256 | assert_se(write(b[1], &ch, 1) >= 0); | |
257 | ||
258 | assert_se(!got_a && !got_b && !got_c); | |
259 | ||
f5fbe71d | 260 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
fd38203a LP |
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 | ||
272 | sd_event_source_unref(x); | |
273 | sd_event_source_unref(y); | |
274 | ||
275 | do_quit = true; | |
509a07ad | 276 | assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0); |
591df2b5 EV |
277 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0); |
278 | assert_se(sd_event_source_set_time(z, event_now + 200 * USEC_PER_MSEC) >= 0); | |
baf76283 | 279 | assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); |
fd38203a LP |
280 | |
281 | assert_se(sd_event_loop(e) >= 0); | |
509a07ad | 282 | assert_se(got_post); |
5657c75f | 283 | assert_se(got_exit); |
fd38203a LP |
284 | |
285 | sd_event_source_unref(z); | |
da7e457c | 286 | sd_event_source_unref(q); |
fd38203a | 287 | |
d5e4ec5b LP |
288 | sd_event_source_unref(w); |
289 | ||
fd38203a LP |
290 | sd_event_unref(e); |
291 | ||
3d94f76c LP |
292 | safe_close_pair(a); |
293 | safe_close_pair(b); | |
294 | safe_close_pair(d); | |
295 | safe_close_pair(k); | |
68da8adf JJ |
296 | } |
297 | ||
298 | TEST(sd_event_now) { | |
2c86ba5a ZJS |
299 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
300 | uint64_t event_now; | |
301 | ||
302 | assert_se(sd_event_new(&e) >= 0); | |
303 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0); | |
304 | assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0); | |
305 | assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0); | |
ba4e0427 LP |
306 | assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0); |
307 | assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0); | |
2c86ba5a ZJS |
308 | assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP); |
309 | assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP); | |
310 | ||
311 | assert_se(sd_event_run(e, 0) == 0); | |
312 | ||
313 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0); | |
314 | assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0); | |
315 | assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0); | |
ba4e0427 LP |
316 | assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0); |
317 | assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0); | |
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 | ||
9da4cb2b LP |
322 | static int last_rtqueue_sigval = 0; |
323 | static int n_rtqueue = 0; | |
324 | ||
325 | static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
326 | last_rtqueue_sigval = si->ssi_int; | |
313cefa1 | 327 | n_rtqueue++; |
9da4cb2b LP |
328 | return 0; |
329 | } | |
330 | ||
68da8adf | 331 | TEST(rtqueue) { |
9da4cb2b LP |
332 | sd_event_source *u = NULL, *v = NULL, *s = NULL; |
333 | sd_event *e = NULL; | |
334 | ||
335 | assert_se(sd_event_default(&e) >= 0); | |
336 | ||
db7136ec | 337 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2) >= 0); |
9da4cb2b LP |
338 | assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0); |
339 | assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0); | |
340 | assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0); | |
341 | ||
342 | assert_se(sd_event_source_set_priority(v, -10) >= 0); | |
343 | ||
19500112 YW |
344 | assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0); |
345 | assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0); | |
346 | assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0); | |
347 | assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0); | |
348 | assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0); | |
9da4cb2b LP |
349 | |
350 | assert_se(n_rtqueue == 0); | |
351 | assert_se(last_rtqueue_sigval == 0); | |
352 | ||
f5fbe71d | 353 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
354 | assert_se(n_rtqueue == 1); |
355 | assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */ | |
356 | ||
f5fbe71d | 357 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
358 | assert_se(n_rtqueue == 2); |
359 | assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */ | |
360 | ||
f5fbe71d | 361 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
362 | assert_se(n_rtqueue == 3); |
363 | assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */ | |
364 | ||
f5fbe71d | 365 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
366 | assert_se(n_rtqueue == 4); |
367 | assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */ | |
368 | ||
369 | assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */ | |
370 | assert_se(n_rtqueue == 4); | |
371 | assert_se(last_rtqueue_sigval == 1); | |
372 | ||
373 | sd_event_source_unref(u); | |
374 | sd_event_source_unref(v); | |
375 | sd_event_source_unref(s); | |
376 | ||
377 | sd_event_unref(e); | |
378 | } | |
379 | ||
41e09d62 LP |
380 | #define CREATE_EVENTS_MAX (70000U) |
381 | ||
382 | struct inotify_context { | |
383 | bool delete_self_handler_called; | |
384 | unsigned create_called[CREATE_EVENTS_MAX]; | |
385 | unsigned create_overflow; | |
386 | unsigned n_create_events; | |
4a7cd0ca | 387 | const char *path; |
41e09d62 LP |
388 | }; |
389 | ||
390 | static void maybe_exit(sd_event_source *s, struct inotify_context *c) { | |
391 | unsigned n; | |
392 | ||
f21b863e YW |
393 | assert_se(s); |
394 | assert_se(c); | |
41e09d62 LP |
395 | |
396 | if (!c->delete_self_handler_called) | |
397 | return; | |
398 | ||
399 | for (n = 0; n < 3; n++) { | |
400 | unsigned i; | |
401 | ||
402 | if (c->create_overflow & (1U << n)) | |
403 | continue; | |
404 | ||
405 | for (i = 0; i < c->n_create_events; i++) | |
406 | if (!(c->create_called[i] & (1U << n))) | |
407 | return; | |
408 | } | |
409 | ||
410 | sd_event_exit(sd_event_source_get_event(s), 0); | |
411 | } | |
412 | ||
413 | static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
4a7cd0ca YW |
414 | struct inotify_context *c = ASSERT_PTR(userdata); |
415 | const char *path, *description; | |
41e09d62 LP |
416 | unsigned bit, n; |
417 | ||
4a7cd0ca YW |
418 | assert_se(sd_event_source_get_inotify_path(s, &path) >= 0); |
419 | ||
41e09d62 LP |
420 | assert_se(sd_event_source_get_description(s, &description) >= 0); |
421 | assert_se(safe_atou(description, &n) >= 0); | |
422 | ||
423 | assert_se(n <= 3); | |
424 | bit = 1U << n; | |
425 | ||
426 | if (ev->mask & IN_Q_OVERFLOW) { | |
4a7cd0ca | 427 | log_info("inotify-handler for %s <%s>: overflow", path, description); |
41e09d62 LP |
428 | c->create_overflow |= bit; |
429 | } else if (ev->mask & IN_CREATE) { | |
4a7cd0ca | 430 | assert_se(path_equal_or_inode_same(path, c->path, 0)); |
834f3ba1 | 431 | if (streq(ev->name, "sub")) |
4a7cd0ca | 432 | log_debug("inotify-handler for %s <%s>: create on %s", path, description, ev->name); |
834f3ba1 YW |
433 | else { |
434 | unsigned i; | |
41e09d62 | 435 | |
41e09d62 | 436 | assert_se(safe_atou(ev->name, &i) >= 0); |
41e09d62 LP |
437 | assert_se(i < c->n_create_events); |
438 | c->create_called[i] |= bit; | |
439 | } | |
440 | } else if (ev->mask & IN_DELETE) { | |
4a7cd0ca | 441 | log_info("inotify-handler for %s <%s>: delete of %s", path, description, ev->name); |
41e09d62 LP |
442 | assert_se(streq(ev->name, "sub")); |
443 | } else | |
04499a70 | 444 | assert_not_reached(); |
41e09d62 LP |
445 | |
446 | maybe_exit(s, c); | |
447 | return 1; | |
448 | } | |
449 | ||
450 | static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
4a7cd0ca YW |
451 | struct inotify_context *c = ASSERT_PTR(userdata); |
452 | const char *path; | |
453 | ||
454 | assert_se(sd_event_source_get_inotify_path(s, &path) >= 0); | |
41e09d62 LP |
455 | |
456 | if (ev->mask & IN_Q_OVERFLOW) { | |
4a7cd0ca | 457 | log_info("delete-self-handler for %s: overflow", path); |
41e09d62 LP |
458 | c->delete_self_handler_called = true; |
459 | } else if (ev->mask & IN_DELETE_SELF) { | |
4a7cd0ca | 460 | log_info("delete-self-handler for %s: delete-self", path); |
41e09d62 LP |
461 | c->delete_self_handler_called = true; |
462 | } else if (ev->mask & IN_IGNORED) { | |
4a7cd0ca | 463 | log_info("delete-self-handler for %s: ignore", path); |
41e09d62 | 464 | } else |
04499a70 | 465 | assert_not_reached(); |
41e09d62 LP |
466 | |
467 | maybe_exit(s, c); | |
468 | return 1; | |
469 | } | |
470 | ||
68da8adf | 471 | static void test_inotify_one(unsigned n_create_events) { |
41e09d62 LP |
472 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
473 | sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL; | |
474 | struct inotify_context context = { | |
475 | .n_create_events = n_create_events, | |
476 | }; | |
477 | sd_event *e = NULL; | |
4a7cd0ca | 478 | const char *q, *pp; |
41e09d62 LP |
479 | unsigned i; |
480 | ||
7fe11e84 YW |
481 | log_info("/* %s(%u) */", __func__, n_create_events); |
482 | ||
41e09d62 LP |
483 | assert_se(sd_event_default(&e) >= 0); |
484 | ||
485 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0); | |
4a7cd0ca | 486 | context.path = p; |
41e09d62 LP |
487 | |
488 | assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE|IN_ONLYDIR, inotify_handler, &context) >= 0); | |
489 | assert_se(sd_event_add_inotify(e, &b, p, IN_CREATE|IN_DELETE|IN_DONT_FOLLOW, inotify_handler, &context) >= 0); | |
490 | assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_IDLE) >= 0); | |
491 | assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_NORMAL) >= 0); | |
492 | assert_se(sd_event_add_inotify(e, &c, p, IN_CREATE|IN_DELETE|IN_EXCL_UNLINK, inotify_handler, &context) >= 0); | |
493 | assert_se(sd_event_source_set_priority(c, SD_EVENT_PRIORITY_IDLE) >= 0); | |
494 | ||
495 | assert_se(sd_event_source_set_description(a, "0") >= 0); | |
496 | assert_se(sd_event_source_set_description(b, "1") >= 0); | |
497 | assert_se(sd_event_source_set_description(c, "2") >= 0); | |
498 | ||
74c4231c YW |
499 | assert_se(sd_event_source_get_inotify_path(a, &pp) >= 0); |
500 | assert_se(path_equal_or_inode_same(pp, p, 0)); | |
74c4231c YW |
501 | assert_se(sd_event_source_get_inotify_path(b, &pp) >= 0); |
502 | assert_se(path_equal_or_inode_same(pp, p, 0)); | |
74c4231c YW |
503 | assert_se(sd_event_source_get_inotify_path(b, &pp) >= 0); |
504 | assert_se(path_equal_or_inode_same(pp, p, 0)); | |
74c4231c | 505 | |
41e09d62 LP |
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]; | |
3d41b6b8 | 512 | _cleanup_free_ char *z = NULL; |
41e09d62 LP |
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 | ||
68da8adf JJ |
532 | TEST(inotify) { |
533 | test_inotify_one(100); /* should work without overflow */ | |
534 | test_inotify_one(33000); /* should trigger a q overflow */ | |
535 | } | |
536 | ||
3ecb3bdc LP |
537 | static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) { |
538 | assert_se(s); | |
539 | assert_se(si); | |
540 | ||
541 | assert_se(si->si_uid == getuid()); | |
542 | assert_se(si->si_signo == SIGCHLD); | |
543 | assert_se(si->si_code == CLD_EXITED); | |
544 | assert_se(si->si_status == 66); | |
545 | ||
546 | log_info("got pidfd on %c", PTR_TO_INT(userdata)); | |
547 | ||
548 | assert_se(userdata == INT_TO_PTR('p')); | |
549 | ||
550 | assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0); | |
551 | sd_event_source_unref(s); | |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
68da8adf | 556 | TEST(pidfd) { |
3ecb3bdc LP |
557 | sd_event_source *s = NULL, *t = NULL; |
558 | sd_event *e = NULL; | |
559 | int pidfd; | |
560 | pid_t pid, pid2; | |
561 | ||
db7136ec | 562 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD) >= 0); |
3ecb3bdc LP |
563 | |
564 | pid = fork(); | |
d7a0f1f4 | 565 | if (pid == 0) |
3ecb3bdc LP |
566 | /* child */ |
567 | _exit(66); | |
3ecb3bdc LP |
568 | |
569 | assert_se(pid > 1); | |
570 | ||
d9000d70 | 571 | ASSERT_OK(pidfd = pidfd_open(pid, 0)); |
3ecb3bdc LP |
572 | |
573 | pid2 = fork(); | |
574 | if (pid2 == 0) | |
575 | freeze(); | |
576 | ||
577 | assert_se(pid > 2); | |
578 | ||
579 | assert_se(sd_event_default(&e) >= 0); | |
580 | assert_se(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED, pidfd_handler, INT_TO_PTR('p')) >= 0); | |
581 | assert_se(sd_event_source_set_child_pidfd_own(s, true) >= 0); | |
582 | ||
583 | /* This one should never trigger, since our second child lives forever */ | |
584 | assert_se(sd_event_add_child(e, &t, pid2, WEXITED, pidfd_handler, INT_TO_PTR('q')) >= 0); | |
585 | assert_se(sd_event_source_set_child_process_own(t, true) >= 0); | |
586 | ||
587 | assert_se(sd_event_loop(e) >= 0); | |
588 | ||
589 | /* Child should still be alive */ | |
590 | assert_se(kill(pid2, 0) >= 0); | |
591 | ||
592 | t = sd_event_source_unref(t); | |
593 | ||
594 | /* Child should now be dead, since we dropped the ref */ | |
595 | assert_se(kill(pid2, 0) < 0 && errno == ESRCH); | |
596 | ||
597 | sd_event_unref(e); | |
598 | } | |
599 | ||
68d89065 MS |
600 | static int ratelimit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
601 | unsigned *c = (unsigned*) userdata; | |
602 | *c += 1; | |
603 | return 0; | |
604 | } | |
605 | ||
606 | static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userdata) { | |
607 | int r; | |
608 | ||
609 | r = sd_event_source_set_enabled(s, SD_EVENT_ON); | |
610 | if (r < 0) | |
611 | log_warning_errno(r, "Failed to turn on notify event source: %m"); | |
612 | ||
613 | r = sd_event_source_set_time(s, usec + 1000); | |
614 | if (r < 0) | |
615 | log_error_errno(r, "Failed to restart watchdog event source: %m"); | |
616 | ||
617 | unsigned *c = (unsigned*) userdata; | |
618 | *c += 1; | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
fd69f224 MS |
623 | static int expired = -1; |
624 | static int ratelimit_expired(sd_event_source *s, void *userdata) { | |
625 | return ++expired; | |
626 | } | |
627 | ||
68da8adf | 628 | TEST(ratelimit) { |
71136404 | 629 | _cleanup_close_pair_ int p[2] = EBADF_PAIR; |
68d89065 MS |
630 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
631 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; | |
632 | uint64_t interval; | |
633 | unsigned count, burst; | |
634 | ||
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); | |
4251512e | 650 | assert_se(usleep_safe(250 * USEC_PER_MSEC) >= 0); |
68d89065 MS |
651 | } |
652 | ||
653 | assert_se(sd_event_source_is_ratelimited(s) == 0); | |
654 | assert_se(count == 10); | |
c0f86d66 | 655 | log_info("ratelimit_io_handler: called %u times, event source not ratelimited", count); |
68d89065 MS |
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); | |
4251512e | 664 | assert_se(usleep_safe(10) >= 0); |
68d89065 | 665 | } |
c0f86d66 | 666 | log_info("ratelimit_io_handler: called %u times, event source got ratelimited", count); |
68d89065 MS |
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 | ||
c0f86d66 | 680 | log_info("ratelimit_time_handler: called %u times, event source got ratelimited", count); |
68d89065 MS |
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 | ||
fd69f224 MS |
689 | /* Set callback that will be invoked when we leave rate limited state. */ |
690 | assert_se(sd_event_source_set_ratelimit_expire_callback(s, ratelimit_expired) >= 0); | |
691 | ||
68d89065 MS |
692 | do { |
693 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
694 | } while (!sd_event_source_is_ratelimited(s)); | |
695 | ||
696 | log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited"); | |
697 | assert_se(count == 20); | |
fd69f224 MS |
698 | |
699 | /* Dispatch the event loop once more and check that ratelimit expiration callback got called */ | |
700 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
701 | assert_se(expired == 0); | |
68d89065 MS |
702 | } |
703 | ||
68da8adf | 704 | TEST(simple_timeout) { |
c14e57ba LP |
705 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
706 | usec_t f, t, some_time; | |
707 | ||
708 | some_time = random_u64_range(2 * USEC_PER_SEC); | |
709 | ||
710 | assert_se(sd_event_default(&e) >= 0); | |
711 | ||
712 | assert_se(sd_event_prepare(e) == 0); | |
713 | ||
714 | f = now(CLOCK_MONOTONIC); | |
715 | assert_se(sd_event_wait(e, some_time) >= 0); | |
716 | t = now(CLOCK_MONOTONIC); | |
717 | ||
718 | /* The event loop may sleep longer than the specified time (timer accuracy, scheduling latencies, …), | |
719 | * but never shorter. Let's check that. */ | |
720 | assert_se(t >= usec_add(f, some_time)); | |
721 | } | |
722 | ||
035daf73 LP |
723 | static int inotify_self_destroy_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { |
724 | sd_event_source **p = userdata; | |
725 | ||
726 | assert_se(ev); | |
727 | assert_se(p); | |
728 | assert_se(*p == s); | |
729 | ||
730 | assert_se(FLAGS_SET(ev->mask, IN_ATTRIB)); | |
731 | ||
732 | assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0); | |
733 | ||
734 | *p = sd_event_source_unref(*p); /* here's what we actually intend to test: we destroy the event | |
735 | * source from inside the event source handler */ | |
736 | return 1; | |
737 | } | |
738 | ||
68da8adf | 739 | TEST(inotify_self_destroy) { |
035daf73 LP |
740 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
741 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
742 | char path[] = "/tmp/inotifyXXXXXX"; | |
254d1313 | 743 | _cleanup_close_ int fd = -EBADF; |
035daf73 LP |
744 | |
745 | /* Tests that destroying an inotify event source from its own handler is safe */ | |
746 | ||
747 | assert_se(sd_event_default(&e) >= 0); | |
748 | ||
749 | fd = mkostemp_safe(path); | |
750 | assert_se(fd >= 0); | |
751 | assert_se(sd_event_add_inotify_fd(e, &s, fd, IN_ATTRIB, inotify_self_destroy_handler, &s) >= 0); | |
752 | fd = safe_close(fd); | |
753 | assert_se(unlink(path) >= 0); /* This will trigger IN_ATTRIB because link count goes to zero */ | |
754 | assert_se(sd_event_loop(e) >= 0); | |
755 | } | |
756 | ||
c7b5a5a7 YW |
757 | struct inotify_process_buffered_data_context { |
758 | const char *path[2]; | |
759 | unsigned i; | |
760 | }; | |
761 | ||
762 | static int inotify_process_buffered_data_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
763 | struct inotify_process_buffered_data_context *c = ASSERT_PTR(userdata); | |
764 | const char *description; | |
765 | ||
766 | assert_se(sd_event_source_get_description(s, &description) >= 0); | |
767 | ||
768 | assert_se(c->i < 2); | |
769 | assert_se(streq(c->path[c->i], description)); | |
770 | c->i++; | |
771 | ||
772 | return 1; | |
773 | } | |
774 | ||
775 | TEST(inotify_process_buffered_data) { | |
776 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL, *q = NULL; | |
777 | _cleanup_(sd_event_source_unrefp) sd_event_source *a = NULL, *b = NULL; | |
778 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
779 | _cleanup_free_ char *z = NULL; | |
780 | ||
781 | /* For issue #23826 */ | |
782 | ||
783 | assert_se(sd_event_default(&e) >= 0); | |
784 | ||
785 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0); | |
786 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &q) >= 0); | |
787 | ||
788 | struct inotify_process_buffered_data_context context = { | |
789 | .path = { p, q }, | |
790 | }; | |
791 | ||
792 | assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE, inotify_process_buffered_data_handler, &context) >= 0); | |
793 | assert_se(sd_event_add_inotify(e, &b, q, IN_CREATE, inotify_process_buffered_data_handler, &context) >= 0); | |
794 | ||
795 | assert_se(z = path_join(p, "aaa")); | |
796 | assert_se(touch(z) >= 0); | |
797 | z = mfree(z); | |
798 | assert_se(z = path_join(q, "bbb")); | |
799 | assert_se(touch(z) >= 0); | |
800 | z = mfree(z); | |
801 | ||
802 | assert_se(sd_event_run(e, 10 * USEC_PER_SEC) > 0); | |
803 | assert_se(sd_event_prepare(e) > 0); /* issue #23826: this was 0. */ | |
804 | assert_se(sd_event_dispatch(e) > 0); | |
805 | assert_se(sd_event_prepare(e) == 0); | |
806 | assert_se(sd_event_wait(e, 0) == 0); | |
807 | } | |
808 | ||
2eeff0f4 LB |
809 | TEST(fork) { |
810 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
811 | int r; | |
812 | ||
813 | assert_se(sd_event_default(&e) >= 0); | |
814 | assert_se(sd_event_prepare(e) == 0); | |
815 | ||
816 | /* Check that after a fork the cleanup functions return NULL */ | |
817 | r = safe_fork("(bus-fork-test)", FORK_WAIT|FORK_LOG, NULL); | |
818 | if (r == 0) { | |
819 | assert_se(e); | |
820 | assert_se(sd_event_ref(e) == NULL); | |
821 | assert_se(sd_event_unref(e) == NULL); | |
822 | _exit(EXIT_SUCCESS); | |
823 | } | |
824 | ||
825 | assert_se(r >= 0); | |
826 | } | |
827 | ||
2fa48059 YW |
828 | TEST(sd_event_source_set_io_fd) { |
829 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; | |
830 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
831 | _cleanup_close_pair_ int pfd_a[2] = EBADF_PAIR, pfd_b[2] = EBADF_PAIR; | |
832 | ||
833 | assert_se(sd_event_default(&e) >= 0); | |
834 | ||
835 | assert_se(pipe2(pfd_a, O_CLOEXEC) >= 0); | |
836 | assert_se(pipe2(pfd_b, O_CLOEXEC) >= 0); | |
837 | ||
838 | assert_se(sd_event_add_io(e, &s, pfd_a[0], EPOLLIN, NULL, INT_TO_PTR(-ENOANO)) >= 0); | |
839 | assert_se(sd_event_source_set_io_fd_own(s, true) >= 0); | |
840 | TAKE_FD(pfd_a[0]); | |
841 | ||
842 | assert_se(sd_event_source_set_io_fd(s, pfd_b[0]) >= 0); | |
843 | TAKE_FD(pfd_b[0]); | |
844 | } | |
845 | ||
2fdc274c LP |
846 | static int hup_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
847 | unsigned *c = userdata; | |
848 | ||
849 | assert_se(revents == EPOLLHUP); | |
850 | ||
851 | (*c)++; | |
852 | return 0; | |
853 | } | |
854 | ||
855 | TEST(leave_ratelimit) { | |
856 | bool expect_ratelimit = false, manually_left_ratelimit = false; | |
857 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; | |
858 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
71136404 | 859 | _cleanup_close_pair_ int pfd[2] = EBADF_PAIR; |
2fdc274c LP |
860 | unsigned c = 0; |
861 | int r; | |
862 | ||
863 | assert_se(sd_event_default(&e) >= 0); | |
864 | ||
9a27ef09 | 865 | /* Create an event source that will continuously fire by creating a pipe whose write side is closed, |
2fdc274c LP |
866 | * and which hence will only see EOF and constant EPOLLHUP */ |
867 | assert_se(pipe2(pfd, O_CLOEXEC) >= 0); | |
868 | assert_se(sd_event_add_io(e, &s, pfd[0], EPOLLIN, hup_callback, &c) >= 0); | |
869 | assert_se(sd_event_source_set_io_fd_own(s, true) >= 0); | |
870 | assert_se(sd_event_source_set_ratelimit(s, 5*USEC_PER_MINUTE, 5) >= 0); | |
871 | ||
872 | pfd[0] = -EBADF; | |
9a27ef09 | 873 | pfd[1] = safe_close(pfd[1]); /* Trigger continuous EOF */ |
2fdc274c LP |
874 | |
875 | for (;;) { | |
876 | r = sd_event_prepare(e); | |
877 | assert_se(r >= 0); | |
878 | ||
879 | if (r == 0) { | |
880 | r = sd_event_wait(e, UINT64_MAX); | |
881 | assert_se(r > 0); | |
882 | } | |
883 | ||
884 | r = sd_event_dispatch(e); | |
885 | assert_se(r > 0); | |
886 | ||
887 | r = sd_event_source_is_ratelimited(s); | |
888 | assert_se(r >= 0); | |
889 | ||
890 | if (c < 5) | |
891 | /* First four dispatches should just work */ | |
892 | assert_se(!r); | |
893 | else if (c == 5) { | |
894 | /* The fifth dispatch should still work, but we now expect the ratelimit to be hit subsequently */ | |
895 | if (!expect_ratelimit) { | |
896 | assert_se(!r); | |
897 | assert_se(sd_event_source_leave_ratelimit(s) == 0); /* this should be a NOP, and return 0 hence */ | |
898 | expect_ratelimit = true; | |
899 | } else { | |
900 | /* We expected the ratelimit, let's leave it manually, and verify it */ | |
901 | assert_se(r); | |
902 | assert_se(sd_event_source_leave_ratelimit(s) > 0); /* we are ratelimited, hence should return > 0 */ | |
903 | assert_se(sd_event_source_is_ratelimited(s) == 0); | |
904 | ||
905 | manually_left_ratelimit = true; | |
906 | } | |
907 | ||
908 | } else if (c == 6) | |
909 | /* On the sixth iteration let's just exit */ | |
910 | break; | |
911 | } | |
912 | ||
913 | /* Verify we definitely hit the ratelimit and left it manually again */ | |
914 | assert_se(manually_left_ratelimit); | |
915 | } | |
916 | ||
68da8adf | 917 | DEFINE_TEST_MAIN(LOG_DEBUG); |