uloop: revert signalfd support for now
[project/libubox.git] / uloop.c
1 /*
2 * uloop - event loop implementation
3 *
4 * Copyright (C) 2010-2016 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/time.h>
19 #include <sys/types.h>
20
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29
30 #include "uloop.h"
31 #include "utils.h"
32
33 #ifdef USE_KQUEUE
34 #include <sys/event.h>
35 #endif
36 #ifdef USE_EPOLL
37 #include <sys/epoll.h>
38 #endif
39 #include <sys/wait.h>
40
41 struct uloop_fd_event {
42 struct uloop_fd *fd;
43 unsigned int events;
44 };
45
46 struct uloop_fd_stack {
47 struct uloop_fd_stack *next;
48 struct uloop_fd *fd;
49 unsigned int events;
50 };
51
52 static struct uloop_fd_stack *fd_stack = NULL;
53
54 #define ULOOP_MAX_EVENTS 10
55
56 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
57 static struct list_head processes = LIST_HEAD_INIT(processes);
58
59 static int poll_fd = -1;
60 bool uloop_cancelled = false;
61 static bool do_sigchld = false;
62
63 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
64 static int cur_fd, cur_nfds;
65
66 #ifdef USE_KQUEUE
67 #include "uloop-kqueue.c"
68 #endif
69
70 #ifdef USE_EPOLL
71 #include "uloop-epoll.c"
72 #endif
73
74 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
75 {
76 struct uloop_fd_stack *cur;
77
78 /*
79 * Do not buffer events for level-triggered fds, they will keep firing.
80 * Caller needs to take care of recursion issues.
81 */
82 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
83 return false;
84
85 for (cur = fd_stack; cur; cur = cur->next) {
86 if (cur->fd != fd)
87 continue;
88
89 if (events < 0)
90 cur->fd = NULL;
91 else
92 cur->events |= events | ULOOP_EVENT_BUFFERED;
93
94 return true;
95 }
96
97 return false;
98 }
99
100 static void uloop_run_events(int timeout)
101 {
102 struct uloop_fd_event *cur;
103 struct uloop_fd *fd;
104
105 if (!cur_nfds) {
106 cur_fd = 0;
107 cur_nfds = uloop_fetch_events(timeout);
108 if (cur_nfds < 0)
109 cur_nfds = 0;
110 }
111
112 while (cur_nfds > 0) {
113 struct uloop_fd_stack stack_cur;
114 unsigned int events;
115
116 cur = &cur_fds[cur_fd++];
117 cur_nfds--;
118
119 fd = cur->fd;
120 events = cur->events;
121 if (!fd)
122 continue;
123
124 if (!fd->cb)
125 continue;
126
127 if (uloop_fd_stack_event(fd, cur->events))
128 continue;
129
130 stack_cur.next = fd_stack;
131 stack_cur.fd = fd;
132 fd_stack = &stack_cur;
133 do {
134 stack_cur.events = 0;
135 fd->cb(fd, events);
136 events = stack_cur.events & ULOOP_EVENT_MASK;
137 } while (stack_cur.fd && events);
138 fd_stack = stack_cur.next;
139
140 return;
141 }
142 }
143
144 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
145 {
146 unsigned int fl;
147 int ret;
148
149 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
150 return uloop_fd_delete(sock);
151
152 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
153 fl = fcntl(sock->fd, F_GETFL, 0);
154 fl |= O_NONBLOCK;
155 fcntl(sock->fd, F_SETFL, fl);
156 }
157
158 ret = register_poll(sock, flags);
159 if (ret < 0)
160 goto out;
161
162 sock->registered = true;
163 sock->eof = false;
164 sock->error = false;
165
166 out:
167 return ret;
168 }
169
170 int uloop_fd_delete(struct uloop_fd *fd)
171 {
172 int i;
173
174 for (i = 0; i < cur_nfds; i++) {
175 if (cur_fds[cur_fd + i].fd != fd)
176 continue;
177
178 cur_fds[cur_fd + i].fd = NULL;
179 }
180
181 if (!fd->registered)
182 return 0;
183
184 fd->registered = false;
185 uloop_fd_stack_event(fd, -1);
186 return __uloop_fd_delete(fd);
187 }
188
189 static int tv_diff(struct timeval *t1, struct timeval *t2)
190 {
191 return
192 (t1->tv_sec - t2->tv_sec) * 1000 +
193 (t1->tv_usec - t2->tv_usec) / 1000;
194 }
195
196 int uloop_timeout_add(struct uloop_timeout *timeout)
197 {
198 struct uloop_timeout *tmp;
199 struct list_head *h = &timeouts;
200
201 if (timeout->pending)
202 return -1;
203
204 list_for_each_entry(tmp, &timeouts, list) {
205 if (tv_diff(&tmp->time, &timeout->time) > 0) {
206 h = &tmp->list;
207 break;
208 }
209 }
210
211 list_add_tail(&timeout->list, h);
212 timeout->pending = true;
213
214 return 0;
215 }
216
217 static void uloop_gettime(struct timeval *tv)
218 {
219 struct timespec ts;
220
221 clock_gettime(CLOCK_MONOTONIC, &ts);
222 tv->tv_sec = ts.tv_sec;
223 tv->tv_usec = ts.tv_nsec / 1000;
224 }
225
226 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
227 {
228 struct timeval *time = &timeout->time;
229
230 if (timeout->pending)
231 uloop_timeout_cancel(timeout);
232
233 uloop_gettime(time);
234
235 time->tv_sec += msecs / 1000;
236 time->tv_usec += (msecs % 1000) * 1000;
237
238 if (time->tv_usec > 1000000) {
239 time->tv_sec++;
240 time->tv_usec -= 1000000;
241 }
242
243 return uloop_timeout_add(timeout);
244 }
245
246 int uloop_timeout_cancel(struct uloop_timeout *timeout)
247 {
248 if (!timeout->pending)
249 return -1;
250
251 list_del(&timeout->list);
252 timeout->pending = false;
253
254 return 0;
255 }
256
257 int uloop_timeout_remaining(struct uloop_timeout *timeout)
258 {
259 struct timeval now;
260
261 if (!timeout->pending)
262 return -1;
263
264 uloop_gettime(&now);
265
266 return tv_diff(&timeout->time, &now);
267 }
268
269 int uloop_process_add(struct uloop_process *p)
270 {
271 struct uloop_process *tmp;
272 struct list_head *h = &processes;
273
274 if (p->pending)
275 return -1;
276
277 list_for_each_entry(tmp, &processes, list) {
278 if (tmp->pid > p->pid) {
279 h = &tmp->list;
280 break;
281 }
282 }
283
284 list_add_tail(&p->list, h);
285 p->pending = true;
286
287 return 0;
288 }
289
290 int uloop_process_delete(struct uloop_process *p)
291 {
292 if (!p->pending)
293 return -1;
294
295 list_del(&p->list);
296 p->pending = false;
297
298 return 0;
299 }
300
301 static void uloop_handle_processes(void)
302 {
303 struct uloop_process *p, *tmp;
304 pid_t pid;
305 int ret;
306
307 do_sigchld = false;
308
309 while (1) {
310 pid = waitpid(-1, &ret, WNOHANG);
311 if (pid < 0 && errno == EINTR)
312 continue;
313
314 if (pid <= 0)
315 return;
316
317 list_for_each_entry_safe(p, tmp, &processes, list) {
318 if (p->pid < pid)
319 continue;
320
321 if (p->pid > pid)
322 break;
323
324 uloop_process_delete(p);
325 p->cb(p, ret);
326 }
327 }
328
329 }
330
331 static void uloop_handle_sigint(int signo)
332 {
333 uloop_cancelled = true;
334 }
335
336 static void uloop_sigchld(int signo)
337 {
338 do_sigchld = true;
339 }
340
341 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
342 {
343 struct sigaction s;
344 struct sigaction *act;
345
346 act = NULL;
347 sigaction(signum, NULL, &s);
348
349 if (add) {
350 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
351 memcpy(old, &s, sizeof(struct sigaction));
352 s.sa_handler = handler;
353 s.sa_flags = 0;
354 act = &s;
355 }
356 }
357 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
358 act = old;
359 }
360
361 if (act != NULL)
362 sigaction(signum, act, NULL);
363 }
364
365 static void uloop_ignore_signal(int signum, bool ignore)
366 {
367 struct sigaction s;
368 void *new_handler = NULL;
369
370 sigaction(signum, NULL, &s);
371
372 if (ignore) {
373 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
374 new_handler = SIG_IGN;
375 } else {
376 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
377 new_handler = SIG_DFL;
378 }
379
380 if (new_handler) {
381 s.sa_handler = new_handler;
382 s.sa_flags = 0;
383 sigaction(signum, &s, NULL);
384 }
385 }
386
387 static void uloop_setup_signals(bool add)
388 {
389 static struct sigaction old_sigint, old_sigchld, old_sigterm;
390
391 uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
392 uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
393 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
394
395 uloop_ignore_signal(SIGPIPE, add);
396 }
397
398 static int uloop_get_next_timeout(struct timeval *tv)
399 {
400 struct uloop_timeout *timeout;
401 int diff;
402
403 if (list_empty(&timeouts))
404 return -1;
405
406 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
407 diff = tv_diff(&timeout->time, tv);
408 if (diff < 0)
409 return 0;
410
411 return diff;
412 }
413
414 static void uloop_process_timeouts(struct timeval *tv)
415 {
416 struct uloop_timeout *t;
417
418 while (!list_empty(&timeouts)) {
419 t = list_first_entry(&timeouts, struct uloop_timeout, list);
420
421 if (tv_diff(&t->time, tv) > 0)
422 break;
423
424 uloop_timeout_cancel(t);
425 if (t->cb)
426 t->cb(t);
427 }
428 }
429
430 static void uloop_clear_timeouts(void)
431 {
432 struct uloop_timeout *t, *tmp;
433
434 list_for_each_entry_safe(t, tmp, &timeouts, list)
435 uloop_timeout_cancel(t);
436 }
437
438 static void uloop_clear_processes(void)
439 {
440 struct uloop_process *p, *tmp;
441
442 list_for_each_entry_safe(p, tmp, &processes, list)
443 uloop_process_delete(p);
444 }
445
446 void uloop_run(void)
447 {
448 static int recursive_calls = 0;
449 struct timeval tv;
450
451 /*
452 * Handlers are only updated for the first call to uloop_run() (and restored
453 * when this call is done).
454 */
455 if (!recursive_calls++)
456 uloop_setup_signals(true);
457
458 uloop_cancelled = false;
459 while(!uloop_cancelled)
460 {
461 uloop_gettime(&tv);
462 uloop_process_timeouts(&tv);
463
464 if (do_sigchld)
465 uloop_handle_processes();
466
467 if (uloop_cancelled)
468 break;
469
470 uloop_gettime(&tv);
471 uloop_run_events(uloop_get_next_timeout(&tv));
472 }
473
474 if (!--recursive_calls)
475 uloop_setup_signals(false);
476 }
477
478 void uloop_done(void)
479 {
480 if (poll_fd < 0)
481 return;
482
483 close(poll_fd);
484 poll_fd = -1;
485
486 uloop_clear_timeouts();
487 uloop_clear_processes();
488 }