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;
33 static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
35 log_info("got IO on %c", PTR_TO_INT(userdata));
37 if (userdata == INT_TO_PTR('a')) {
38 assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
41 } else if (userdata == INT_TO_PTR('b')) {
45 assert_not_reached("Yuck!");
50 static int child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
55 log_info("got child on %c", PTR_TO_INT(userdata));
57 assert(userdata == INT_TO_PTR('f'));
59 assert_se(sd_event_request_quit(sd_event_get(s)) >= 0);
60 sd_event_source_unref(s);
65 static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
73 log_info("got signal on %c", PTR_TO_INT(userdata));
75 assert(userdata == INT_TO_PTR('e'));
77 assert_se(sigemptyset(&ss) >= 0);
78 assert_se(sigaddset(&ss, SIGCHLD) >= 0);
79 assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
87 assert_se(sd_event_add_child(sd_event_get(s), pid, WEXITED, child_handler, INT_TO_PTR('f'), &p) >= 0);
88 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
90 sd_event_source_unref(s);
95 static int defer_handler(sd_event_source *s, void *userdata) {
101 log_info("got defer on %c", PTR_TO_INT(userdata));
103 assert(userdata == INT_TO_PTR('d'));
105 assert_se(sigemptyset(&ss) >= 0);
106 assert_se(sigaddset(&ss, SIGUSR1) >= 0);
107 assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
108 assert_se(sd_event_add_signal(sd_event_get(s), SIGUSR1, signal_handler, INT_TO_PTR('e'), &p) >= 0);
109 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
112 sd_event_source_unref(s);
117 static bool do_quit = false;
119 static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
120 log_info("got timer on %c", PTR_TO_INT(userdata));
122 if (userdata == INT_TO_PTR('c')) {
127 assert_se(sd_event_add_defer(sd_event_get(s), defer_handler, INT_TO_PTR('d'), &p) >= 0);
128 assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
134 assert_not_reached("Huh?");
139 static bool got_quit = false;
141 static int quit_handler(sd_event_source *s, void *userdata) {
142 log_info("got quit handler on %c", PTR_TO_INT(userdata));
149 int main(int argc, char *argv[]) {
151 sd_event_source *x = NULL, *y = NULL, *z = NULL, *q = NULL;
152 static const char ch = 'x';
153 int a[2] = { -1, -1 }, b[2] = { -1, -1};
155 assert_se(pipe(a) >= 0);
156 assert_se(pipe(b) >= 0);
158 assert_se(sd_event_new(&e) >= 0);
160 got_a = false, got_b = false, got_c = false;
162 assert_se(sd_event_add_io(e, a[0], EPOLLIN, io_handler, INT_TO_PTR('a'), &x) >= 0);
163 assert_se(sd_event_add_io(e, b[0], EPOLLIN, io_handler, INT_TO_PTR('b'), &y) >= 0);
164 assert_se(sd_event_add_monotonic(e, 0, 0, time_handler, INT_TO_PTR('c'), &z) >= 0);
165 assert_se(sd_event_add_quit(e, quit_handler, INT_TO_PTR('g'), &q) >= 0);
167 assert_se(sd_event_source_set_priority(x, 99) >= 0);
168 assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
169 assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
170 assert_se(sd_event_source_set_priority(z, 50) >= 0);
171 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
172 assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
174 assert_se(write(a[1], &ch, 1) >= 0);
175 assert_se(write(b[1], &ch, 1) >= 0);
177 assert_se(!got_a && !got_b && !got_c);
179 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
181 assert_se(!got_a && got_b && !got_c);
183 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
185 assert_se(!got_a && got_b && got_c);
187 assert_se(sd_event_run(e, (uint64_t) -1) >= 1);
189 assert_se(got_a && got_b && got_c);
191 sd_event_source_unref(x);
192 sd_event_source_unref(y);
195 assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0);
196 assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
198 assert_se(sd_event_loop(e) >= 0);
200 sd_event_source_unref(z);
201 sd_event_source_unref(q);