]>
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" |
3ffd4af2 | 8 | #include "fd-util.h" |
41e09d62 | 9 | #include "fs-util.h" |
fd38203a | 10 | #include "log.h" |
0c0cdb06 | 11 | #include "macro.h" |
3ecb3bdc | 12 | #include "missing_syscall.h" |
41e09d62 | 13 | #include "parse-util.h" |
657ee2d8 | 14 | #include "path-util.h" |
41e09d62 LP |
15 | #include "process-util.h" |
16 | #include "rm-rf.h" | |
24882e06 | 17 | #include "signal-util.h" |
41e09d62 LP |
18 | #include "stdio-util.h" |
19 | #include "string-util.h" | |
6d7c4033 | 20 | #include "tests.h" |
e4de7287 | 21 | #include "tmpfile-util.h" |
3ffd4af2 | 22 | #include "util.h" |
fd38203a LP |
23 | |
24 | static int prepare_handler(sd_event_source *s, void *userdata) { | |
25 | log_info("preparing %c", PTR_TO_INT(userdata)); | |
26 | return 1; | |
27 | } | |
28 | ||
12179984 | 29 | static bool got_a, got_b, got_c, got_unref; |
9ec9694c | 30 | static unsigned got_d; |
fd38203a | 31 | |
12179984 LP |
32 | static int unref_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
33 | sd_event_source_unref(s); | |
34 | got_unref = true; | |
35 | return 0; | |
36 | } | |
37 | ||
fd38203a LP |
38 | static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
39 | ||
40 | log_info("got IO on %c", PTR_TO_INT(userdata)); | |
41 | ||
42 | if (userdata == INT_TO_PTR('a')) { | |
baf76283 | 43 | assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0); |
fd38203a LP |
44 | assert_se(!got_a); |
45 | got_a = true; | |
46 | } else if (userdata == INT_TO_PTR('b')) { | |
47 | assert_se(!got_b); | |
48 | got_b = true; | |
9ec9694c DS |
49 | } else if (userdata == INT_TO_PTR('d')) { |
50 | got_d++; | |
51 | if (got_d < 2) | |
52 | assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0); | |
53 | else | |
54 | assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0); | |
fd38203a LP |
55 | } else |
56 | assert_not_reached("Yuck!"); | |
57 | ||
58 | return 1; | |
59 | } | |
60 | ||
61 | static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) { | |
62 | ||
0c0cdb06 RC |
63 | assert_se(s); |
64 | assert_se(si); | |
fd38203a | 65 | |
3ecb3bdc LP |
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); | |
70 | ||
fd38203a LP |
71 | log_info("got child on %c", PTR_TO_INT(userdata)); |
72 | ||
0c0cdb06 | 73 | assert_se(userdata == INT_TO_PTR('f')); |
fd38203a | 74 | |
6203e07a | 75 | assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0); |
fd38203a LP |
76 | sd_event_source_unref(s); |
77 | ||
78 | return 1; | |
79 | } | |
80 | ||
81 | static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
39883f62 | 82 | sd_event_source *p = NULL; |
fd38203a | 83 | pid_t pid; |
3ecb3bdc | 84 | siginfo_t plain_si; |
fd38203a | 85 | |
0c0cdb06 RC |
86 | assert_se(s); |
87 | assert_se(si); | |
fd38203a LP |
88 | |
89 | log_info("got signal on %c", PTR_TO_INT(userdata)); | |
90 | ||
0c0cdb06 | 91 | assert_se(userdata == INT_TO_PTR('e')); |
fd38203a | 92 | |
3ecb3bdc | 93 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGUSR2, -1) >= 0); |
fd38203a LP |
94 | |
95 | pid = fork(); | |
96 | assert_se(pid >= 0); | |
97 | ||
3ecb3bdc LP |
98 | if (pid == 0) { |
99 | sigset_t ss; | |
100 | ||
101 | assert_se(sigemptyset(&ss) >= 0); | |
102 | assert_se(sigaddset(&ss, SIGUSR2) >= 0); | |
103 | ||
104 | zero(plain_si); | |
105 | assert_se(sigwaitinfo(&ss, &plain_si) >= 0); | |
106 | ||
107 | assert_se(plain_si.si_signo == SIGUSR2); | |
108 | assert_se(plain_si.si_value.sival_int == 4711); | |
109 | ||
110 | _exit(78); | |
111 | } | |
fd38203a | 112 | |
151b9b96 | 113 | assert_se(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f')) >= 0); |
baf76283 | 114 | assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0); |
3ecb3bdc LP |
115 | assert_se(sd_event_source_set_child_process_own(p, true) >= 0); |
116 | ||
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 | |
119 | * to */ | |
120 | zero(plain_si); | |
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; | |
126 | ||
127 | assert_se(sd_event_source_send_child_signal(p, SIGUSR2, &plain_si, 0) >= 0); | |
fd38203a LP |
128 | |
129 | sd_event_source_unref(s); | |
130 | ||
131 | return 1; | |
132 | } | |
133 | ||
134 | static int defer_handler(sd_event_source *s, void *userdata) { | |
39883f62 | 135 | sd_event_source *p = NULL; |
fd38203a | 136 | |
0c0cdb06 | 137 | assert_se(s); |
fd38203a LP |
138 | |
139 | log_info("got defer on %c", PTR_TO_INT(userdata)); | |
140 | ||
0c0cdb06 | 141 | assert_se(userdata == INT_TO_PTR('d')); |
fd38203a | 142 | |
72c0a2c2 LP |
143 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1, -1) >= 0); |
144 | ||
151b9b96 | 145 | assert_se(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e')) >= 0); |
baf76283 | 146 | assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0); |
fd38203a LP |
147 | raise(SIGUSR1); |
148 | ||
149 | sd_event_source_unref(s); | |
150 | ||
151 | return 1; | |
152 | } | |
153 | ||
3ecb3bdc | 154 | static bool do_quit; |
fd38203a LP |
155 | |
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)); | |
158 | ||
159 | if (userdata == INT_TO_PTR('c')) { | |
160 | ||
161 | if (do_quit) { | |
162 | sd_event_source *p; | |
163 | ||
151b9b96 | 164 | assert_se(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d')) >= 0); |
baf76283 | 165 | assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0); |
fd38203a | 166 | } else { |
0c0cdb06 | 167 | assert_se(!got_c); |
fd38203a LP |
168 | got_c = true; |
169 | } | |
170 | } else | |
171 | assert_not_reached("Huh?"); | |
172 | ||
173 | return 2; | |
174 | } | |
175 | ||
6203e07a | 176 | static bool got_exit = false; |
da7e457c | 177 | |
6203e07a | 178 | static int exit_handler(sd_event_source *s, void *userdata) { |
da7e457c LP |
179 | log_info("got quit handler on %c", PTR_TO_INT(userdata)); |
180 | ||
6203e07a | 181 | got_exit = true; |
da7e457c LP |
182 | |
183 | return 3; | |
184 | } | |
185 | ||
509a07ad EV |
186 | static bool got_post = false; |
187 | ||
188 | static int post_handler(sd_event_source *s, void *userdata) { | |
189 | log_info("got post handler"); | |
190 | ||
191 | got_post = true; | |
192 | ||
193 | return 2; | |
194 | } | |
195 | ||
3ecb3bdc | 196 | static void test_basic(bool with_pidfd) { |
fd38203a | 197 | sd_event *e = NULL; |
12179984 | 198 | sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL; |
fd38203a | 199 | static const char ch = 'x'; |
12179984 | 200 | int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 }; |
591df2b5 | 201 | uint64_t event_now; |
6680b8d1 | 202 | int64_t priority; |
fd38203a | 203 | |
3ecb3bdc LP |
204 | assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0); |
205 | ||
fd38203a LP |
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 LP |
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); | |
222 | ||
9ec9694c DS |
223 | got_a = false, got_b = false, got_c = false, got_d = 0; |
224 | ||
3fe91079 | 225 | /* Add a oneshot handler, trigger it, reenable it, and trigger |
9ec9694c | 226 | * it again. */ |
151b9b96 | 227 | assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0); |
9ec9694c DS |
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); | |
fd38203a | 235 | |
151b9b96 LP |
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); | |
3ecb3bdc LP |
238 | |
239 | do_quit = false; | |
6a0f1f6d | 240 | assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0); |
151b9b96 | 241 | assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0); |
fd38203a LP |
242 | |
243 | assert_se(sd_event_source_set_priority(x, 99) >= 0); | |
6680b8d1 ME |
244 | assert_se(sd_event_source_get_priority(x, &priority) >= 0); |
245 | assert_se(priority == 99); | |
baf76283 | 246 | assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0); |
fd38203a LP |
247 | assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0); |
248 | assert_se(sd_event_source_set_priority(z, 50) >= 0); | |
baf76283 | 249 | assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); |
fd38203a | 250 | assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0); |
a71fe8b8 LP |
251 | |
252 | /* Test for floating event sources */ | |
72c0a2c2 | 253 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1, -1) >= 0); |
a71fe8b8 | 254 | assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0); |
fd38203a LP |
255 | |
256 | assert_se(write(a[1], &ch, 1) >= 0); | |
257 | assert_se(write(b[1], &ch, 1) >= 0); | |
258 | ||
259 | assert_se(!got_a && !got_b && !got_c); | |
260 | ||
261 | assert_se(sd_event_run(e, (uint64_t) -1) >= 1); | |
262 | ||
263 | assert_se(!got_a && got_b && !got_c); | |
264 | ||
265 | assert_se(sd_event_run(e, (uint64_t) -1) >= 1); | |
266 | ||
267 | assert_se(!got_a && got_b && got_c); | |
268 | ||
269 | assert_se(sd_event_run(e, (uint64_t) -1) >= 1); | |
270 | ||
271 | assert_se(got_a && got_b && got_c); | |
272 | ||
273 | sd_event_source_unref(x); | |
274 | sd_event_source_unref(y); | |
275 | ||
276 | do_quit = true; | |
509a07ad | 277 | assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0); |
591df2b5 EV |
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); | |
baf76283 | 280 | assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0); |
fd38203a LP |
281 | |
282 | assert_se(sd_event_loop(e) >= 0); | |
509a07ad | 283 | assert_se(got_post); |
5657c75f | 284 | assert_se(got_exit); |
fd38203a LP |
285 | |
286 | sd_event_source_unref(z); | |
da7e457c | 287 | sd_event_source_unref(q); |
fd38203a | 288 | |
d5e4ec5b LP |
289 | sd_event_source_unref(w); |
290 | ||
fd38203a LP |
291 | sd_event_unref(e); |
292 | ||
3d94f76c LP |
293 | safe_close_pair(a); |
294 | safe_close_pair(b); | |
295 | safe_close_pair(d); | |
296 | safe_close_pair(k); | |
3ecb3bdc LP |
297 | |
298 | assert_se(unsetenv("SYSTEMD_PIDFD") >= 0); | |
9da4cb2b LP |
299 | } |
300 | ||
2c86ba5a ZJS |
301 | static void test_sd_event_now(void) { |
302 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
303 | uint64_t event_now; | |
304 | ||
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); | |
3411372e LP |
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); | |
312 | } | |
2c86ba5a ZJS |
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); | |
315 | ||
316 | assert_se(sd_event_run(e, 0) == 0); | |
317 | ||
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); | |
3411372e LP |
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); | |
324 | } | |
2c86ba5a ZJS |
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); | |
327 | } | |
328 | ||
9da4cb2b LP |
329 | static int last_rtqueue_sigval = 0; |
330 | static int n_rtqueue = 0; | |
331 | ||
332 | static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
333 | last_rtqueue_sigval = si->ssi_int; | |
313cefa1 | 334 | n_rtqueue++; |
9da4cb2b LP |
335 | return 0; |
336 | } | |
337 | ||
338 | static void test_rtqueue(void) { | |
339 | sd_event_source *u = NULL, *v = NULL, *s = NULL; | |
340 | sd_event *e = NULL; | |
341 | ||
342 | assert_se(sd_event_default(&e) >= 0); | |
343 | ||
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); | |
348 | ||
349 | assert_se(sd_event_source_set_priority(v, -10) >= 0); | |
350 | ||
19500112 YW |
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); | |
9da4cb2b LP |
356 | |
357 | assert_se(n_rtqueue == 0); | |
358 | assert_se(last_rtqueue_sigval == 0); | |
359 | ||
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 */ | |
363 | ||
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 */ | |
367 | ||
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 */ | |
371 | ||
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 */ | |
375 | ||
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); | |
379 | ||
380 | sd_event_source_unref(u); | |
381 | sd_event_source_unref(v); | |
382 | sd_event_source_unref(s); | |
383 | ||
384 | sd_event_unref(e); | |
385 | } | |
386 | ||
41e09d62 LP |
387 | #define CREATE_EVENTS_MAX (70000U) |
388 | ||
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; | |
394 | }; | |
395 | ||
396 | static void maybe_exit(sd_event_source *s, struct inotify_context *c) { | |
397 | unsigned n; | |
398 | ||
399 | assert(s); | |
400 | assert(c); | |
401 | ||
402 | if (!c->delete_self_handler_called) | |
403 | return; | |
404 | ||
405 | for (n = 0; n < 3; n++) { | |
406 | unsigned i; | |
407 | ||
408 | if (c->create_overflow & (1U << n)) | |
409 | continue; | |
410 | ||
411 | for (i = 0; i < c->n_create_events; i++) | |
412 | if (!(c->create_called[i] & (1U << n))) | |
413 | return; | |
414 | } | |
415 | ||
416 | sd_event_exit(sd_event_source_get_event(s), 0); | |
417 | } | |
418 | ||
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; | |
422 | unsigned bit, n; | |
423 | ||
424 | assert_se(sd_event_source_get_description(s, &description) >= 0); | |
425 | assert_se(safe_atou(description, &n) >= 0); | |
426 | ||
427 | assert_se(n <= 3); | |
428 | bit = 1U << n; | |
429 | ||
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) { | |
434 | unsigned i; | |
435 | ||
8788a568 | 436 | log_debug("inotify-handler <%s>: create on %s", description, ev->name); |
41e09d62 LP |
437 | |
438 | if (!streq(ev->name, "sub")) { | |
439 | assert_se(safe_atou(ev->name, &i) >= 0); | |
440 | ||
441 | assert_se(i < c->n_create_events); | |
442 | c->create_called[i] |= bit; | |
443 | } | |
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")); | |
447 | } else | |
448 | assert_not_reached("unexpected inotify event"); | |
449 | ||
450 | maybe_exit(s, c); | |
451 | return 1; | |
452 | } | |
453 | ||
454 | static int delete_self_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) { | |
455 | struct inotify_context *c = userdata; | |
456 | ||
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"); | |
465 | } else | |
466 | assert_not_reached("unexpected inotify event (delete-self)"); | |
467 | ||
468 | maybe_exit(s, c); | |
469 | return 1; | |
470 | } | |
471 | ||
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, | |
477 | }; | |
478 | sd_event *e = NULL; | |
479 | const char *q; | |
480 | unsigned i; | |
481 | ||
482 | assert_se(sd_event_default(&e) >= 0); | |
483 | ||
484 | assert_se(mkdtemp_malloc("/tmp/test-inotify-XXXXXX", &p) >= 0); | |
485 | ||
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); | |
492 | ||
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); | |
496 | ||
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); | |
500 | ||
501 | for (i = 0; i < n_create_events; i++) { | |
502 | char buf[DECIMAL_STR_MAX(unsigned)+1]; | |
503 | _cleanup_free_ char *z; | |
504 | ||
505 | xsprintf(buf, "%u", i); | |
657ee2d8 | 506 | assert_se(z = path_join(p, buf)); |
41e09d62 LP |
507 | |
508 | assert_se(touch(z) >= 0); | |
509 | } | |
510 | ||
511 | assert_se(unlink(q) >= 0); | |
512 | ||
513 | assert_se(sd_event_loop(e) >= 0); | |
514 | ||
515 | sd_event_source_unref(a); | |
516 | sd_event_source_unref(b); | |
517 | sd_event_source_unref(c); | |
518 | sd_event_source_unref(d); | |
519 | ||
520 | sd_event_unref(e); | |
521 | } | |
522 | ||
3ecb3bdc LP |
523 | static int pidfd_handler(sd_event_source *s, const siginfo_t *si, void *userdata) { |
524 | assert_se(s); | |
525 | assert_se(si); | |
526 | ||
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); | |
531 | ||
532 | log_info("got pidfd on %c", PTR_TO_INT(userdata)); | |
533 | ||
534 | assert_se(userdata == INT_TO_PTR('p')); | |
535 | ||
536 | assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0); | |
537 | sd_event_source_unref(s); | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | static void test_pidfd(void) { | |
543 | sd_event_source *s = NULL, *t = NULL; | |
544 | sd_event *e = NULL; | |
545 | int pidfd; | |
546 | pid_t pid, pid2; | |
547 | ||
548 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0); | |
549 | ||
550 | pid = fork(); | |
d7a0f1f4 | 551 | if (pid == 0) |
3ecb3bdc LP |
552 | /* child */ |
553 | _exit(66); | |
3ecb3bdc LP |
554 | |
555 | assert_se(pid > 1); | |
556 | ||
557 | pidfd = pidfd_open(pid, 0); | |
558 | if (pidfd < 0) { | |
559 | /* No pidfd_open() supported or blocked? */ | |
560 | assert_se(ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)); | |
561 | (void) wait_for_terminate(pid, NULL); | |
562 | return; | |
563 | } | |
564 | ||
565 | pid2 = fork(); | |
566 | if (pid2 == 0) | |
567 | freeze(); | |
568 | ||
569 | assert_se(pid > 2); | |
570 | ||
571 | assert_se(sd_event_default(&e) >= 0); | |
572 | assert_se(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED, pidfd_handler, INT_TO_PTR('p')) >= 0); | |
573 | assert_se(sd_event_source_set_child_pidfd_own(s, true) >= 0); | |
574 | ||
575 | /* This one should never trigger, since our second child lives forever */ | |
576 | assert_se(sd_event_add_child(e, &t, pid2, WEXITED, pidfd_handler, INT_TO_PTR('q')) >= 0); | |
577 | assert_se(sd_event_source_set_child_process_own(t, true) >= 0); | |
578 | ||
579 | assert_se(sd_event_loop(e) >= 0); | |
580 | ||
581 | /* Child should still be alive */ | |
582 | assert_se(kill(pid2, 0) >= 0); | |
583 | ||
584 | t = sd_event_source_unref(t); | |
585 | ||
586 | /* Child should now be dead, since we dropped the ref */ | |
587 | assert_se(kill(pid2, 0) < 0 && errno == ESRCH); | |
588 | ||
589 | sd_event_unref(e); | |
590 | } | |
591 | ||
9da4cb2b | 592 | int main(int argc, char *argv[]) { |
8788a568 | 593 | test_setup_logging(LOG_INFO); |
2c86ba5a | 594 | |
3ecb3bdc LP |
595 | test_basic(true); /* test with pidfd */ |
596 | test_basic(false); /* test without pidfd */ | |
597 | ||
2c86ba5a | 598 | test_sd_event_now(); |
9da4cb2b | 599 | test_rtqueue(); |
fd38203a | 600 | |
41e09d62 LP |
601 | test_inotify(100); /* should work without overflow */ |
602 | test_inotify(33000); /* should trigger a q overflow */ | |
603 | ||
3ecb3bdc LP |
604 | test_pidfd(); |
605 | ||
fd38203a LP |
606 | return 0; |
607 | } |