chiark / gitweb /
tree-wide: drop license boilerplate
[elogind.git] / src / libelogind / sd-event / test-event.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <sys/wait.h>
9
10 #include "sd-event.h"
11
12 #include "fd-util.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "signal-util.h"
16 #include "util.h"
17 /// Additional includes needed by elogind
18 #include "process-util.h"
19
20 static int prepare_handler(sd_event_source *s, void *userdata) {
21         log_info("preparing %c", PTR_TO_INT(userdata));
22         return 1;
23 }
24
25 static bool got_a, got_b, got_c, got_unref;
26 static unsigned got_d;
27
28 static int unref_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
29         sd_event_source_unref(s);
30         got_unref = true;
31         return 0;
32 }
33
34 static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
35
36         log_info("got IO on %c", PTR_TO_INT(userdata));
37
38         if (userdata == INT_TO_PTR('a')) {
39                 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
40                 assert_se(!got_a);
41                 got_a = true;
42         } else if (userdata == INT_TO_PTR('b')) {
43                 assert_se(!got_b);
44                 got_b = true;
45         } else if (userdata == INT_TO_PTR('d')) {
46                 got_d++;
47                 if (got_d < 2)
48                         assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
49                 else
50                         assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
51         } else
52                 assert_not_reached("Yuck!");
53
54         return 1;
55 }
56
57 static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
58
59         assert_se(s);
60         assert_se(si);
61
62         log_info("got child on %c", PTR_TO_INT(userdata));
63
64         assert_se(userdata == INT_TO_PTR('f'));
65
66         assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
67         sd_event_source_unref(s);
68
69         return 1;
70 }
71
72 static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
73         sd_event_source *p = NULL;
74         pid_t pid;
75
76         assert_se(s);
77         assert_se(si);
78
79         log_info("got signal on %c", PTR_TO_INT(userdata));
80
81         assert_se(userdata == INT_TO_PTR('e'));
82
83         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
84
85         pid = fork();
86         assert_se(pid >= 0);
87
88         if (pid == 0)
89                 _exit(EXIT_SUCCESS);
90
91         assert_se(sd_event_add_child(sd_event_source_get_event(s), &p, pid, WEXITED, child_handler, INT_TO_PTR('f')) >= 0);
92         assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
93
94         sd_event_source_unref(s);
95
96         return 1;
97 }
98
99 static int defer_handler(sd_event_source *s, void *userdata) {
100         sd_event_source *p = NULL;
101
102         assert_se(s);
103
104         log_info("got defer on %c", PTR_TO_INT(userdata));
105
106         assert_se(userdata == INT_TO_PTR('d'));
107
108         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGUSR1, -1) >= 0);
109
110         assert_se(sd_event_add_signal(sd_event_source_get_event(s), &p, SIGUSR1, signal_handler, INT_TO_PTR('e')) >= 0);
111         assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
112         raise(SIGUSR1);
113
114         sd_event_source_unref(s);
115
116         return 1;
117 }
118
119 static bool do_quit = false;
120
121 static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
122         log_info("got timer on %c", PTR_TO_INT(userdata));
123
124         if (userdata == INT_TO_PTR('c')) {
125
126                 if (do_quit) {
127                         sd_event_source *p;
128
129                         assert_se(sd_event_add_defer(sd_event_source_get_event(s), &p, defer_handler, INT_TO_PTR('d')) >= 0);
130                         assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
131                 } else {
132                         assert_se(!got_c);
133                         got_c = true;
134                 }
135         } else
136                 assert_not_reached("Huh?");
137
138         return 2;
139 }
140
141 static bool got_exit = false;
142
143 static int exit_handler(sd_event_source *s, void *userdata) {
144         log_info("got quit handler on %c", PTR_TO_INT(userdata));
145
146         got_exit = true;
147
148         return 3;
149 }
150
151 static bool got_post = false;
152
153 static int post_handler(sd_event_source *s, void *userdata) {
154         log_info("got post handler");
155
156         got_post = true;
157
158         return 2;
159 }
160
161 static void test_basic(void) {
162         sd_event *e = NULL;
163         sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL, *t = NULL;
164         static const char ch = 'x';
165         int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1}, k[2] = { -1, -1 };
166         uint64_t event_now;
167         int64_t priority;
168
169         assert_se(pipe(a) >= 0);
170         assert_se(pipe(b) >= 0);
171         assert_se(pipe(d) >= 0);
172         assert_se(pipe(k) >= 0);
173
174         assert_se(sd_event_default(&e) >= 0);
175         assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
176
177         assert_se(sd_event_set_watchdog(e, true) >= 0);
178
179         /* Test whether we cleanly can destroy an io event source from its own handler */
180         got_unref = false;
181         assert_se(sd_event_add_io(e, &t, k[0], EPOLLIN, unref_handler, NULL) >= 0);
182         assert_se(write(k[1], &ch, 1) == 1);
183         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
184         assert_se(got_unref);
185
186         got_a = false, got_b = false, got_c = false, got_d = 0;
187
188         /* Add a oneshot handler, trigger it, re-enable it, and trigger
189          * it again. */
190         assert_se(sd_event_add_io(e, &w, d[0], EPOLLIN, io_handler, INT_TO_PTR('d')) >= 0);
191         assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
192         assert_se(write(d[1], &ch, 1) >= 0);
193         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
194         assert_se(got_d == 1);
195         assert_se(write(d[1], &ch, 1) >= 0);
196         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
197         assert_se(got_d == 2);
198
199         assert_se(sd_event_add_io(e, &x, a[0], EPOLLIN, io_handler, INT_TO_PTR('a')) >= 0);
200         assert_se(sd_event_add_io(e, &y, b[0], EPOLLIN, io_handler, INT_TO_PTR('b')) >= 0);
201         assert_se(sd_event_add_time(e, &z, CLOCK_MONOTONIC, 0, 0, time_handler, INT_TO_PTR('c')) >= 0);
202         assert_se(sd_event_add_exit(e, &q, exit_handler, INT_TO_PTR('g')) >= 0);
203
204         assert_se(sd_event_source_set_priority(x, 99) >= 0);
205         assert_se(sd_event_source_get_priority(x, &priority) >= 0);
206         assert_se(priority == 99);
207         assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
208         assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
209         assert_se(sd_event_source_set_priority(z, 50) >= 0);
210         assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
211         assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
212
213         /* Test for floating event sources */
214         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+1, -1) >= 0);
215         assert_se(sd_event_add_signal(e, NULL, SIGRTMIN+1, NULL, NULL) >= 0);
216
217         assert_se(write(a[1], &ch, 1) >= 0);
218         assert_se(write(b[1], &ch, 1) >= 0);
219
220         assert_se(!got_a && !got_b && !got_c);
221
222         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
223
224         assert_se(!got_a && got_b && !got_c);
225
226         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
227
228         assert_se(!got_a && got_b && got_c);
229
230         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
231
232         assert_se(got_a && got_b && got_c);
233
234         sd_event_source_unref(x);
235         sd_event_source_unref(y);
236
237         do_quit = true;
238         assert_se(sd_event_add_post(e, NULL, post_handler, NULL) >= 0);
239         assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
240         assert_se(sd_event_source_set_time(z, event_now + 200 * USEC_PER_MSEC) >= 0);
241         assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
242
243         assert_se(sd_event_loop(e) >= 0);
244         assert_se(got_post);
245         assert_se(got_exit);
246
247         sd_event_source_unref(z);
248         sd_event_source_unref(q);
249
250         sd_event_source_unref(w);
251
252         sd_event_unref(e);
253
254         safe_close_pair(a);
255         safe_close_pair(b);
256         safe_close_pair(d);
257         safe_close_pair(k);
258 }
259
260 static void test_sd_event_now(void) {
261         _cleanup_(sd_event_unrefp) sd_event *e = NULL;
262         uint64_t event_now;
263
264         assert_se(sd_event_new(&e) >= 0);
265         assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
266         assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
267         assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
268         if (clock_boottime_supported()) {
269                 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
270                 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
271         }
272         assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
273         assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
274
275         assert_se(sd_event_run(e, 0) == 0);
276
277         assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
278         assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
279         assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
280         if (clock_boottime_supported()) {
281                 assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0);
282                 assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0);
283         }
284         assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
285         assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
286 }
287
288 static int last_rtqueue_sigval = 0;
289 static int n_rtqueue = 0;
290
291 static int rtqueue_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
292         last_rtqueue_sigval = si->ssi_int;
293         n_rtqueue++;
294         return 0;
295 }
296
297 static void test_rtqueue(void) {
298         sd_event_source *u = NULL, *v = NULL, *s = NULL;
299         sd_event *e = NULL;
300
301         assert_se(sd_event_default(&e) >= 0);
302
303         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN+2, SIGRTMIN+3, SIGUSR2, -1) >= 0);
304         assert_se(sd_event_add_signal(e, &u, SIGRTMIN+2, rtqueue_handler, NULL) >= 0);
305         assert_se(sd_event_add_signal(e, &v, SIGRTMIN+3, rtqueue_handler, NULL) >= 0);
306         assert_se(sd_event_add_signal(e, &s, SIGUSR2, rtqueue_handler, NULL) >= 0);
307
308         assert_se(sd_event_source_set_priority(v, -10) >= 0);
309
310         assert_se(sigqueue(getpid_cached(), SIGRTMIN+2, (union sigval) { .sival_int = 1 }) >= 0);
311         assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 2 }) >= 0);
312         assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 3 }) >= 0);
313         assert_se(sigqueue(getpid_cached(), SIGRTMIN+3, (union sigval) { .sival_int = 4 }) >= 0);
314         assert_se(sigqueue(getpid_cached(), SIGUSR2, (union sigval) { .sival_int = 5 }) >= 0);
315
316         assert_se(n_rtqueue == 0);
317         assert_se(last_rtqueue_sigval == 0);
318
319         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
320         assert_se(n_rtqueue == 1);
321         assert_se(last_rtqueue_sigval == 2); /* first SIGRTMIN+3 */
322
323         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
324         assert_se(n_rtqueue == 2);
325         assert_se(last_rtqueue_sigval == 4); /* second SIGRTMIN+3 */
326
327         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
328         assert_se(n_rtqueue == 3);
329         assert_se(last_rtqueue_sigval == 3); /* first SIGUSR2 */
330
331         assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
332         assert_se(n_rtqueue == 4);
333         assert_se(last_rtqueue_sigval == 1); /* SIGRTMIN+2 */
334
335         assert_se(sd_event_run(e, 0) == 0); /* the other SIGUSR2 is dropped, because the first one was still queued */
336         assert_se(n_rtqueue == 4);
337         assert_se(last_rtqueue_sigval == 1);
338
339         sd_event_source_unref(u);
340         sd_event_source_unref(v);
341         sd_event_source_unref(s);
342
343         sd_event_unref(e);
344 }
345
346 int main(int argc, char *argv[]) {
347
348         log_set_max_level(LOG_DEBUG);
349         log_parse_environment();
350
351         test_basic();
352         test_sd_event_now();
353         test_rtqueue();
354
355         return 0;
356 }