]>
Commit | Line | Data |
---|---|---|
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" |
45a68ed3 | 8 | #include "exec-util.h" |
3ffd4af2 | 9 | #include "fd-util.h" |
41e09d62 | 10 | #include "fs-util.h" |
fd38203a | 11 | #include "log.h" |
0c0cdb06 | 12 | #include "macro.h" |
3ecb3bdc | 13 | #include "missing_syscall.h" |
41e09d62 | 14 | #include "parse-util.h" |
657ee2d8 | 15 | #include "path-util.h" |
41e09d62 | 16 | #include "process-util.h" |
c14e57ba | 17 | #include "random-util.h" |
41e09d62 | 18 | #include "rm-rf.h" |
24882e06 | 19 | #include "signal-util.h" |
41e09d62 LP |
20 | #include "stdio-util.h" |
21 | #include "string-util.h" | |
6d7c4033 | 22 | #include "tests.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 | |
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 | ||
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 | |
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 | 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 | ||
68da8adf | 197 | static void test_basic_one(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'; |
34014779 ZJS |
201 | int a[2] = { -EBADF, -EBADF }, b[2] = { -EBADF, -EBADF }, |
202 | d[2] = { -EBADF, -EBADF }, k[2] = { -EBADF, -EBADF }; | |
591df2b5 | 203 | uint64_t event_now; |
6680b8d1 | 204 | int64_t priority; |
fd38203a | 205 | |
7fe11e84 YW |
206 | log_info("/* %s(pidfd=%s) */", __func__, yes_no(with_pidfd)); |
207 | ||
3ecb3bdc LP |
208 | assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0); |
209 | ||
fd38203a LP |
210 | assert_se(pipe(a) >= 0); |
211 | assert_se(pipe(b) >= 0); | |
9ec9694c | 212 | assert_se(pipe(d) >= 0); |
12179984 | 213 | assert_se(pipe(k) >= 0); |
fd38203a | 214 | |
afc6adb5 | 215 | assert_se(sd_event_default(&e) >= 0); |
591df2b5 | 216 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0); |
fd38203a | 217 | |
cde93897 LP |
218 | assert_se(sd_event_set_watchdog(e, true) >= 0); |
219 | ||
12179984 LP |
220 | /* Test whether we cleanly can destroy an io event source from its own handler */ |
221 | got_unref = false; | |
151b9b96 | 222 | assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0); |
12179984 | 223 | assert_se(write(k[1], &ch, 1) == 1); |
f5fbe71d | 224 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
12179984 LP |
225 | assert_se(got_unref); |
226 | ||
9ec9694c DS |
227 | got_a = false, got_b = false, got_c = false, got_d = 0; |
228 | ||
254d1313 | 229 | /* Add a oneshot handler, trigger it, reenable it, and trigger 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 | ||
68da8adf JJ |
304 | TEST(basic) { |
305 | test_basic_one(true); /* test with pidfd */ | |
306 | test_basic_one(false); /* test without pidfd */ | |
307 | } | |
308 | ||
309 | TEST(sd_event_now) { | |
2c86ba5a ZJS |
310 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
311 | uint64_t event_now; | |
312 | ||
313 | assert_se(sd_event_new(&e) >= 0); | |
314 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0); | |
315 | assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0); | |
316 | assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0); | |
ba4e0427 LP |
317 | assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0); |
318 | assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0); | |
2c86ba5a ZJS |
319 | assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP); |
320 | assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP); | |
321 | ||
322 | assert_se(sd_event_run(e, 0) == 0); | |
323 | ||
324 | assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0); | |
325 | assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0); | |
326 | assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0); | |
ba4e0427 LP |
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); | |
2c86ba5a ZJS |
329 | assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP); |
330 | assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP); | |
331 | } | |
332 | ||
9da4cb2b LP |
333 | static int last_rtqueue_sigval = 0; |
334 | static int n_rtqueue = 0; | |
335 | ||
336 | static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
337 | last_rtqueue_sigval = si->ssi_int; | |
313cefa1 | 338 | n_rtqueue++; |
9da4cb2b LP |
339 | return 0; |
340 | } | |
341 | ||
68da8adf | 342 | TEST(rtqueue) { |
9da4cb2b LP |
343 | sd_event_source *u = NULL, *v = NULL, *s = NULL; |
344 | sd_event *e = NULL; | |
345 | ||
346 | assert_se(sd_event_default(&e) >= 0); | |
347 | ||
348 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2, -1) >= 0); | |
349 | assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0); | |
350 | assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0); | |
351 | assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0); | |
352 | ||
353 | assert_se(sd_event_source_set_priority(v, -10) >= 0); | |
354 | ||
19500112 YW |
355 | assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0); |
356 | assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0); | |
357 | assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0); | |
358 | assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0); | |
359 | assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0); | |
9da4cb2b LP |
360 | |
361 | assert_se(n_rtqueue == 0); | |
362 | assert_se(last_rtqueue_sigval == 0); | |
363 | ||
f5fbe71d | 364 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
365 | assert_se(n_rtqueue == 1); |
366 | assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */ | |
367 | ||
f5fbe71d | 368 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
369 | assert_se(n_rtqueue == 2); |
370 | assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */ | |
371 | ||
f5fbe71d | 372 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
373 | assert_se(n_rtqueue == 3); |
374 | assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */ | |
375 | ||
f5fbe71d | 376 | assert_se(sd_event_run(e, UINT64_MAX) >= 1); |
9da4cb2b LP |
377 | assert_se(n_rtqueue == 4); |
378 | assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */ | |
379 | ||
380 | assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */ | |
381 | assert_se(n_rtqueue == 4); | |
382 | assert_se(last_rtqueue_sigval == 1); | |
383 | ||
384 | sd_event_source_unref(u); | |
385 | sd_event_source_unref(v); | |
386 | sd_event_source_unref(s); | |
387 | ||
388 | sd_event_unref(e); | |
389 | } | |
390 | ||
41e09d62 LP |
391 | #define CREATE_EVENTS_MAX (70000U) |
392 | ||
393 | struct inotify_context { | |
394 | bool delete_self_handler_called; | |
395 | unsigned create_called[CREATE_EVENTS_MAX]; | |
396 | unsigned create_overflow; | |
397 | unsigned n_create_events; | |
398 | }; | |
399 | ||
400 | static void maybe_exit(sd_event_source *s, struct inotify_context *c) { | |
401 | unsigned n; | |
402 | ||
f21b863e YW |
403 | assert_se(s); |
404 | assert_se(c); | |
41e09d62 LP |
405 | |
406 | if (!c->delete_self_handler_called) | |
407 | return; | |
408 | ||
409 | for (n = 0; n < 3; n++) { | |
410 | unsigned i; | |
411 | ||
412 | if (c->create_overflow & (1U << n)) | |
413 | continue; | |
414 | ||
415 | for (i = 0; i < c->n_create_events; i++) | |
416 | if (!(c->create_called[i] & (1U << n))) | |
417 | return; | |
418 | } | |
419 | ||
420 | sd_event_exit(sd_event_source_get_event(s), 0); | |
421 | } | |
422 | ||
423 | static int inotify_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
424 | struct inotify_context *c = userdata; | |
425 | const char *description; | |
426 | unsigned bit, n; | |
427 | ||
428 | assert_se(sd_event_source_get_description(s, &description) >= 0); | |
429 | assert_se(safe_atou(description, &n) >= 0); | |
430 | ||
431 | assert_se(n <= 3); | |
432 | bit = 1U << n; | |
433 | ||
434 | if (ev->mask & IN_Q_OVERFLOW) { | |
435 | log_info("inotify-handler <%s>: overflow", description); | |
436 | c->create_overflow |= bit; | |
437 | } else if (ev->mask & IN_CREATE) { | |
834f3ba1 YW |
438 | if (streq(ev->name, "sub")) |
439 | log_debug("inotify-handler <%s>: create on %s", description, ev->name); | |
440 | else { | |
441 | unsigned i; | |
41e09d62 | 442 | |
41e09d62 | 443 | assert_se(safe_atou(ev->name, &i) >= 0); |
41e09d62 LP |
444 | assert_se(i < c->n_create_events); |
445 | c->create_called[i] |= bit; | |
446 | } | |
447 | } else if (ev->mask & IN_DELETE) { | |
448 | log_info("inotify-handler <%s>: delete of %s", description, ev->name); | |
449 | assert_se(streq(ev->name, "sub")); | |
450 | } else | |
04499a70 | 451 | assert_not_reached(); |
41e09d62 LP |
452 | |
453 | maybe_exit(s, c); | |
454 | return 1; | |
455 | } | |
456 | ||
457 | static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
458 | struct inotify_context *c = userdata; | |
459 | ||
460 | if (ev->mask & IN_Q_OVERFLOW) { | |
461 | log_info("delete-self-handler: overflow"); | |
462 | c->delete_self_handler_called = true; | |
463 | } else if (ev->mask & IN_DELETE_SELF) { | |
464 | log_info("delete-self-handler: delete-self"); | |
465 | c->delete_self_handler_called = true; | |
466 | } else if (ev->mask & IN_IGNORED) { | |
467 | log_info("delete-self-handler: ignore"); | |
468 | } else | |
04499a70 | 469 | assert_not_reached(); |
41e09d62 LP |
470 | |
471 | maybe_exit(s, c); | |
472 | return 1; | |
473 | } | |
474 | ||
68da8adf | 475 | static void test_inotify_one(unsigned n_create_events) { |
41e09d62 LP |
476 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; |
477 | sd_event_source *a = NULL, *b = NULL, *c = NULL, *d = NULL; | |
478 | struct inotify_context context = { | |
479 | .n_create_events = n_create_events, | |
480 | }; | |
481 | sd_event *e = NULL; | |
482 | const char *q; | |
483 | unsigned i; | |
484 | ||
7fe11e84 YW |
485 | log_info("/* %s(%u) */", __func__, n_create_events); |
486 | ||
41e09d62 LP |
487 | assert_se(sd_event_default(&e) >= 0); |
488 | ||
489 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0); | |
490 | ||
491 | assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE|IN_ONLYDIR, inotify_handler, &context) >= 0); | |
492 | assert_se(sd_event_add_inotify(e, &b, p, IN_CREATE|IN_DELETE|IN_DONT_FOLLOW, inotify_handler, &context) >= 0); | |
493 | assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_IDLE) >= 0); | |
494 | assert_se(sd_event_source_set_priority(b, SD_EVENT_PRIORITY_NORMAL) >= 0); | |
495 | assert_se(sd_event_add_inotify(e, &c, p, IN_CREATE|IN_DELETE|IN_EXCL_UNLINK, inotify_handler, &context) >= 0); | |
496 | assert_se(sd_event_source_set_priority(c, SD_EVENT_PRIORITY_IDLE) >= 0); | |
497 | ||
498 | assert_se(sd_event_source_set_description(a, "0") >= 0); | |
499 | assert_se(sd_event_source_set_description(b, "1") >= 0); | |
500 | assert_se(sd_event_source_set_description(c, "2") >= 0); | |
501 | ||
502 | q = strjoina(p, "/sub"); | |
503 | assert_se(touch(q) >= 0); | |
504 | assert_se(sd_event_add_inotify(e, &d, q, IN_DELETE_SELF, delete_self_handler, &context) >= 0); | |
505 | ||
506 | for (i = 0; i < n_create_events; i++) { | |
507 | char buf[DECIMAL_STR_MAX(unsigned)+1]; | |
508 | _cleanup_free_ char *z; | |
509 | ||
510 | xsprintf(buf, "%u", i); | |
657ee2d8 | 511 | assert_se(z = path_join(p, buf)); |
41e09d62 LP |
512 | |
513 | assert_se(touch(z) >= 0); | |
514 | } | |
515 | ||
516 | assert_se(unlink(q) >= 0); | |
517 | ||
518 | assert_se(sd_event_loop(e) >= 0); | |
519 | ||
520 | sd_event_source_unref(a); | |
521 | sd_event_source_unref(b); | |
522 | sd_event_source_unref(c); | |
523 | sd_event_source_unref(d); | |
524 | ||
525 | sd_event_unref(e); | |
526 | } | |
527 | ||
68da8adf JJ |
528 | TEST(inotify) { |
529 | test_inotify_one(100); /* should work without overflow */ | |
530 | test_inotify_one(33000); /* should trigger a q overflow */ | |
531 | } | |
532 | ||
3ecb3bdc LP |
533 | static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) { |
534 | assert_se(s); | |
535 | assert_se(si); | |
536 | ||
537 | assert_se(si->si_uid == getuid()); | |
538 | assert_se(si->si_signo == SIGCHLD); | |
539 | assert_se(si->si_code == CLD_EXITED); | |
540 | assert_se(si->si_status == 66); | |
541 | ||
542 | log_info("got pidfd on %c", PTR_TO_INT(userdata)); | |
543 | ||
544 | assert_se(userdata == INT_TO_PTR('p')); | |
545 | ||
546 | assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0); | |
547 | sd_event_source_unref(s); | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
68da8adf | 552 | TEST(pidfd) { |
3ecb3bdc LP |
553 | sd_event_source *s = NULL, *t = NULL; |
554 | sd_event *e = NULL; | |
555 | int pidfd; | |
556 | pid_t pid, pid2; | |
557 | ||
558 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0); | |
559 | ||
560 | pid = fork(); | |
d7a0f1f4 | 561 | if (pid == 0) |
3ecb3bdc LP |
562 | /* child */ |
563 | _exit(66); | |
3ecb3bdc LP |
564 | |
565 | assert_se(pid > 1); | |
566 | ||
567 | pidfd = pidfd_open(pid, 0); | |
568 | if (pidfd < 0) { | |
569 | /* No pidfd_open() supported or blocked? */ | |
570 | assert_se(ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)); | |
571 | (void) wait_for_terminate(pid, NULL); | |
572 | return; | |
573 | } | |
574 | ||
575 | pid2 = fork(); | |
576 | if (pid2 == 0) | |
577 | freeze(); | |
578 | ||
579 | assert_se(pid > 2); | |
580 | ||
581 | assert_se(sd_event_default(&e) >= 0); | |
582 | assert_se(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED, pidfd_handler, INT_TO_PTR('p')) >= 0); | |
583 | assert_se(sd_event_source_set_child_pidfd_own(s, true) >= 0); | |
584 | ||
585 | /* This one should never trigger, since our second child lives forever */ | |
586 | assert_se(sd_event_add_child(e, &t, pid2, WEXITED, pidfd_handler, INT_TO_PTR('q')) >= 0); | |
587 | assert_se(sd_event_source_set_child_process_own(t, true) >= 0); | |
588 | ||
589 | assert_se(sd_event_loop(e) >= 0); | |
590 | ||
591 | /* Child should still be alive */ | |
592 | assert_se(kill(pid2, 0) >= 0); | |
593 | ||
594 | t = sd_event_source_unref(t); | |
595 | ||
596 | /* Child should now be dead, since we dropped the ref */ | |
597 | assert_se(kill(pid2, 0) < 0 && errno == ESRCH); | |
598 | ||
599 | sd_event_unref(e); | |
600 | } | |
601 | ||
68d89065 MS |
602 | static int ratelimit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
603 | unsigned *c = (unsigned*) userdata; | |
604 | *c += 1; | |
605 | return 0; | |
606 | } | |
607 | ||
608 | static int ratelimit_time_handler(sd_event_source *s, uint64_t usec, void *userdata) { | |
609 | int r; | |
610 | ||
611 | r = sd_event_source_set_enabled(s, SD_EVENT_ON); | |
612 | if (r < 0) | |
613 | log_warning_errno(r, "Failed to turn on notify event source: %m"); | |
614 | ||
615 | r = sd_event_source_set_time(s, usec + 1000); | |
616 | if (r < 0) | |
617 | log_error_errno(r, "Failed to restart watchdog event source: %m"); | |
618 | ||
619 | unsigned *c = (unsigned*) userdata; | |
620 | *c += 1; | |
621 | ||
622 | return 0; | |
623 | } | |
624 | ||
fd69f224 MS |
625 | static int expired = -1; |
626 | static int ratelimit_expired(sd_event_source *s, void *userdata) { | |
627 | return ++expired; | |
628 | } | |
629 | ||
68da8adf | 630 | TEST(ratelimit) { |
68d89065 MS |
631 | _cleanup_close_pair_ int p[2] = {-1, -1}; |
632 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
633 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; | |
634 | uint64_t interval; | |
635 | unsigned count, burst; | |
636 | ||
637 | assert_se(sd_event_default(&e) >= 0); | |
638 | assert_se(pipe2(p, O_CLOEXEC|O_NONBLOCK) >= 0); | |
639 | ||
640 | assert_se(sd_event_add_io(e, &s, p[0], EPOLLIN, ratelimit_io_handler, &count) >= 0); | |
641 | assert_se(sd_event_source_set_description(s, "test-ratelimit-io") >= 0); | |
642 | assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0); | |
643 | assert_se(sd_event_source_get_ratelimit(s, &interval, &burst) >= 0); | |
644 | assert_se(interval == 1 * USEC_PER_SEC && burst == 5); | |
645 | ||
646 | assert_se(write(p[1], "1", 1) == 1); | |
647 | ||
648 | count = 0; | |
649 | for (unsigned i = 0; i < 10; i++) { | |
650 | log_debug("slow loop iteration %u", i); | |
651 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
652 | assert_se(usleep(250 * USEC_PER_MSEC) >= 0); | |
653 | } | |
654 | ||
655 | assert_se(sd_event_source_is_ratelimited(s) == 0); | |
656 | assert_se(count == 10); | |
c0f86d66 | 657 | log_info("ratelimit_io_handler: called %u times, event source not ratelimited", count); |
68d89065 MS |
658 | |
659 | assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0); | |
660 | assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 5) >= 0); | |
661 | ||
662 | count = 0; | |
663 | for (unsigned i = 0; i < 10; i++) { | |
664 | log_debug("fast event loop iteration %u", i); | |
665 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
666 | assert_se(usleep(10) >= 0); | |
667 | } | |
c0f86d66 | 668 | log_info("ratelimit_io_handler: called %u times, event source got ratelimited", count); |
68d89065 MS |
669 | assert_se(count < 10); |
670 | ||
671 | s = sd_event_source_unref(s); | |
672 | safe_close_pair(p); | |
673 | ||
674 | count = 0; | |
675 | assert_se(sd_event_add_time_relative(e, &s, CLOCK_MONOTONIC, 1000, 1, ratelimit_time_handler, &count) >= 0); | |
676 | assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) == 0); | |
677 | ||
678 | do { | |
679 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
680 | } while (!sd_event_source_is_ratelimited(s)); | |
681 | ||
c0f86d66 | 682 | log_info("ratelimit_time_handler: called %u times, event source got ratelimited", count); |
68d89065 MS |
683 | assert_se(count == 10); |
684 | ||
da115b93 | 685 | /* In order to get rid of active rate limit client needs to disable it explicitly */ |
68d89065 MS |
686 | assert_se(sd_event_source_set_ratelimit(s, 0, 0) >= 0); |
687 | assert_se(!sd_event_source_is_ratelimited(s)); | |
688 | ||
689 | assert_se(sd_event_source_set_ratelimit(s, 1 * USEC_PER_SEC, 10) >= 0); | |
690 | ||
fd69f224 MS |
691 | /* Set callback that will be invoked when we leave rate limited state. */ |
692 | assert_se(sd_event_source_set_ratelimit_expire_callback(s, ratelimit_expired) >= 0); | |
693 | ||
68d89065 MS |
694 | do { |
695 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
696 | } while (!sd_event_source_is_ratelimited(s)); | |
697 | ||
698 | log_info("ratelimit_time_handler: called 10 more times, event source got ratelimited"); | |
699 | assert_se(count == 20); | |
fd69f224 MS |
700 | |
701 | /* Dispatch the event loop once more and check that ratelimit expiration callback got called */ | |
702 | assert_se(sd_event_run(e, UINT64_MAX) >= 0); | |
703 | assert_se(expired == 0); | |
68d89065 MS |
704 | } |
705 | ||
68da8adf | 706 | TEST(simple_timeout) { |
c14e57ba LP |
707 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
708 | usec_t f, t, some_time; | |
709 | ||
710 | some_time = random_u64_range(2 * USEC_PER_SEC); | |
711 | ||
712 | assert_se(sd_event_default(&e) >= 0); | |
713 | ||
714 | assert_se(sd_event_prepare(e) == 0); | |
715 | ||
716 | f = now(CLOCK_MONOTONIC); | |
717 | assert_se(sd_event_wait(e, some_time) >= 0); | |
718 | t = now(CLOCK_MONOTONIC); | |
719 | ||
720 | /* The event loop may sleep longer than the specified time (timer accuracy, scheduling latencies, …), | |
721 | * but never shorter. Let's check that. */ | |
722 | assert_se(t >= usec_add(f, some_time)); | |
723 | } | |
724 | ||
035daf73 LP |
725 | static int inotify_self_destroy_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { |
726 | sd_event_source **p = userdata; | |
727 | ||
728 | assert_se(ev); | |
729 | assert_se(p); | |
730 | assert_se(*p == s); | |
731 | ||
732 | assert_se(FLAGS_SET(ev->mask, IN_ATTRIB)); | |
733 | ||
734 | assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0); | |
735 | ||
736 | *p = sd_event_source_unref(*p); /* here's what we actually intend to test: we destroy the event | |
737 | * source from inside the event source handler */ | |
738 | return 1; | |
739 | } | |
740 | ||
68da8adf | 741 | TEST(inotify_self_destroy) { |
035daf73 LP |
742 | _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
743 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
744 | char path[] = "/tmp/inotifyXXXXXX"; | |
254d1313 | 745 | _cleanup_close_ int fd = -EBADF; |
035daf73 LP |
746 | |
747 | /* Tests that destroying an inotify event source from its own handler is safe */ | |
748 | ||
749 | assert_se(sd_event_default(&e) >= 0); | |
750 | ||
751 | fd = mkostemp_safe(path); | |
752 | assert_se(fd >= 0); | |
753 | assert_se(sd_event_add_inotify_fd(e, &s, fd, IN_ATTRIB, inotify_self_destroy_handler, &s) >= 0); | |
754 | fd = safe_close(fd); | |
755 | assert_se(unlink(path) >= 0); /* This will trigger IN_ATTRIB because link count goes to zero */ | |
756 | assert_se(sd_event_loop(e) >= 0); | |
757 | } | |
758 | ||
c7b5a5a7 YW |
759 | struct inotify_process_buffered_data_context { |
760 | const char *path[2]; | |
761 | unsigned i; | |
762 | }; | |
763 | ||
764 | static int inotify_process_buffered_data_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
765 | struct inotify_process_buffered_data_context *c = ASSERT_PTR(userdata); | |
766 | const char *description; | |
767 | ||
768 | assert_se(sd_event_source_get_description(s, &description) >= 0); | |
769 | ||
770 | assert_se(c->i < 2); | |
771 | assert_se(streq(c->path[c->i], description)); | |
772 | c->i++; | |
773 | ||
774 | return 1; | |
775 | } | |
776 | ||
777 | TEST(inotify_process_buffered_data) { | |
778 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL, *q = NULL; | |
779 | _cleanup_(sd_event_source_unrefp) sd_event_source *a = NULL, *b = NULL; | |
780 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
781 | _cleanup_free_ char *z = NULL; | |
782 | ||
783 | /* For issue #23826 */ | |
784 | ||
785 | assert_se(sd_event_default(&e) >= 0); | |
786 | ||
787 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0); | |
788 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &q) >= 0); | |
789 | ||
790 | struct inotify_process_buffered_data_context context = { | |
791 | .path = { p, q }, | |
792 | }; | |
793 | ||
794 | assert_se(sd_event_add_inotify(e, &a, p, IN_CREATE, inotify_process_buffered_data_handler, &context) >= 0); | |
795 | assert_se(sd_event_add_inotify(e, &b, q, IN_CREATE, inotify_process_buffered_data_handler, &context) >= 0); | |
796 | ||
797 | assert_se(z = path_join(p, "aaa")); | |
798 | assert_se(touch(z) >= 0); | |
799 | z = mfree(z); | |
800 | assert_se(z = path_join(q, "bbb")); | |
801 | assert_se(touch(z) >= 0); | |
802 | z = mfree(z); | |
803 | ||
804 | assert_se(sd_event_run(e, 10 * USEC_PER_SEC) > 0); | |
805 | assert_se(sd_event_prepare(e) > 0); /* issue #23826: this was 0. */ | |
806 | assert_se(sd_event_dispatch(e) > 0); | |
807 | assert_se(sd_event_prepare(e) == 0); | |
808 | assert_se(sd_event_wait(e, 0) == 0); | |
809 | } | |
810 | ||
68da8adf | 811 | DEFINE_TEST_MAIN(LOG_DEBUG); |