1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 static int prepare_handler(sd_event_source *s, void *userdata) {
27 log_info("preparing %c", PTR_TO_INT(userdata));
31 static bool got_a, got_b, got_c;
32 static unsigned got_d;
34 static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
36 log_info("got IO on %c", PTR_TO_INT(userdata));
38 if (userdata == INT_TO_PTR('a')) {
39 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
42 } else if (userdata == INT_TO_PTR('b')) {
45 } else if (userdata == INT_TO_PTR('d')) {
48 assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
50 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
52 assert_not_reached("Yuck!");
57 static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
62 log_info("got child on %c", PTR_TO_INT(userdata));
64 assert(userdata == INT_TO_PTR('f'));
66 assert_se(sd_event_request_quit(sd_event_get(s)) >= 0);
67 sd_event_source_unref(s);
72 static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
80 log_info("got signal on %c", PTR_TO_INT(userdata));
82 assert(userdata == INT_TO_PTR('e'));
84 assert_se(sigemptyset(&ss) >= 0);
85 assert_se(sigaddset(&ss, SIGCHLD) >= 0);
86 assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
94 assert_se(sd_event_add_child(sd_event_get(s), pid, WEXITED, child_handler, INT_TO_PTR('f'), &p) >= 0);
95 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
97 sd_event_source_unref(s);
102 static int defer_handler(sd_event_source *s, void *userdata) {
108 log_info("got defer on %c", PTR_TO_INT(userdata));
110 assert(userdata == INT_TO_PTR('d'));
112 assert_se(sigemptyset(&ss) >= 0);
113 assert_se(sigaddset(&ss, SIGUSR1) >= 0);
114 assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
115 assert_se(sd_event_add_signal(sd_event_get(s), SIGUSR1, signal_handler, INT_TO_PTR('e'), &p) >= 0);
116 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
119 sd_event_source_unref(s);
124 static bool do_quit = false;
126 static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
127 log_info("got timer on %c", PTR_TO_INT(userdata));
129 if (userdata == INT_TO_PTR('c')) {
134 assert_se(sd_event_add_defer(sd_event_get(s), defer_handler, INT_TO_PTR('d'), &p) >= 0);
135 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
141 assert_not_reached("Huh?");
146 static bool got_quit = false;
148 static int quit_handler(sd_event_source *s, void *userdata) {
149 log_info("got quit handler on %c", PTR_TO_INT(userdata));
156 int main(int argc, char *argv[]) {
158 sd_event_source *w = NULL, *x = NULL, *y = NULL, *z = NULL, *q = NULL;
159 static const char ch = 'x';
160 int a[2] = { -1, -1 }, b[2] = { -1, -1}, d[2] = { -1, -1};
162 assert_se(pipe(a) >= 0);
163 assert_se(pipe(b) >= 0);
164 assert_se(pipe(d) >= 0);
166 assert_se(sd_event_new(&e) >= 0);
168 got_a = false, got_b = false, got_c = false, got_d = 0;
170 /* Add a oneshot handler, trigger it, re-enable it, and trigger
172 assert_se(sd_event_add_io(e, d[0], EPOLLIN, io_handler, INT_TO_PTR('d'), &w) >= 0);
173 assert_se(sd_event_source_set_enabled(w, SD_EVENT_ONESHOT) >= 0);
174 assert_se(write(d[1], &ch, 1) >= 0);
175 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
176 assert_se(got_d == 1);
177 assert_se(write(d[1], &ch, 1) >= 0);
178 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
179 assert_se(got_d == 2);
181 assert_se(sd_event_add_io(e, a[0], EPOLLIN, io_handler, INT_TO_PTR('a'), &x) >= 0);
182 assert_se(sd_event_add_io(e, b[0], EPOLLIN, io_handler, INT_TO_PTR('b'), &y) >= 0);
183 assert_se(sd_event_add_monotonic(e, 0, 0, time_handler, INT_TO_PTR('c'), &z) >= 0);
184 assert_se(sd_event_add_quit(e, quit_handler, INT_TO_PTR('g'), &q) >= 0);
186 assert_se(sd_event_source_set_priority(x, 99) >= 0);
187 assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
188 assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
189 assert_se(sd_event_source_set_priority(z, 50) >= 0);
190 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
191 assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
193 assert_se(write(a[1], &ch, 1) >= 0);
194 assert_se(write(b[1], &ch, 1) >= 0);
196 assert_se(!got_a && !got_b && !got_c);
198 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
200 assert_se(!got_a && got_b && !got_c);
202 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
204 assert_se(!got_a && got_b && got_c);
206 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
208 assert_se(got_a && got_b && got_c);
210 sd_event_source_unref(x);
211 sd_event_source_unref(y);
214 assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0);
215 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
217 assert_se(sd_event_loop(e) >= 0);
219 sd_event_source_unref(z);
220 sd_event_source_unref(q);