uloop: try to use signalfd for signal handling if available
[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 signal_fd = -1;
60 static int poll_fd = -1;
61 bool uloop_cancelled = false;
62 static bool do_sigchld = false;
63
64 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
65 static int cur_fd, cur_nfds;
66
67 static void uloop_handle_signal(int signo);
68
69 #ifdef USE_KQUEUE
70 #include "uloop-kqueue.c"
71 #endif
72
73 #ifdef USE_EPOLL
74 #include "uloop-epoll.c"
75 #endif
76
77 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
78 {
79 struct uloop_fd_stack *cur;
80
81 /*
82 * Do not buffer events for level-triggered fds, they will keep firing.
83 * Caller needs to take care of recursion issues.
84 */
85 if (!(fd->flags & ULOOP_EDGE_TRIGGER))
86 return false;
87
88 for (cur = fd_stack; cur; cur = cur->next) {
89 if (cur->fd != fd)
90 continue;
91
92 if (events < 0)
93 cur->fd = NULL;
94 else
95 cur->events |= events | ULOOP_EVENT_BUFFERED;
96
97 return true;
98 }
99
100 return false;
101 }
102
103 static void uloop_run_events(int timeout)
104 {
105 struct uloop_fd_event *cur;
106 struct uloop_fd *fd;
107
108 if (!cur_nfds) {
109 cur_fd = 0;
110 cur_nfds = uloop_fetch_events(timeout);
111 if (cur_nfds < 0)
112 cur_nfds = 0;
113 }
114
115 while (cur_nfds > 0) {
116 struct uloop_fd_stack stack_cur;
117 unsigned int events;
118
119 cur = &cur_fds[cur_fd++];
120 cur_nfds--;
121
122 fd = cur->fd;
123 events = cur->events;
124 if (!fd)
125 continue;
126
127 if (!fd->cb)
128 continue;
129
130 if (uloop_fd_stack_event(fd, cur->events))
131 continue;
132
133 stack_cur.next = fd_stack;
134 stack_cur.fd = fd;
135 fd_stack = &stack_cur;
136 do {
137 stack_cur.events = 0;
138 fd->cb(fd, events);
139 events = stack_cur.events & ULOOP_EVENT_MASK;
140 } while (stack_cur.fd && events);
141 fd_stack = stack_cur.next;
142
143 return;
144 }
145 }
146
147 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
148 {
149 unsigned int fl;
150 int ret;
151
152 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
153 return uloop_fd_delete(sock);
154
155 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
156 fl = fcntl(sock->fd, F_GETFL, 0);
157 fl |= O_NONBLOCK;
158 fcntl(sock->fd, F_SETFL, fl);
159 }
160
161 ret = register_poll(sock, flags);
162 if (ret < 0)
163 goto out;
164
165 sock->registered = true;
166 sock->eof = false;
167 sock->error = false;
168
169 out:
170 return ret;
171 }
172
173 int uloop_fd_delete(struct uloop_fd *fd)
174 {
175 int i;
176
177 for (i = 0; i < cur_nfds; i++) {
178 if (cur_fds[cur_fd + i].fd != fd)
179 continue;
180
181 cur_fds[cur_fd + i].fd = NULL;
182 }
183
184 if (!fd->registered)
185 return 0;
186
187 fd->registered = false;
188 uloop_fd_stack_event(fd, -1);
189 return __uloop_fd_delete(fd);
190 }
191
192 static int tv_diff(struct timeval *t1, struct timeval *t2)
193 {
194 return
195 (t1->tv_sec - t2->tv_sec) * 1000 +
196 (t1->tv_usec - t2->tv_usec) / 1000;
197 }
198
199 int uloop_timeout_add(struct uloop_timeout *timeout)
200 {
201 struct uloop_timeout *tmp;
202 struct list_head *h = &timeouts;
203
204 if (timeout->pending)
205 return -1;
206
207 list_for_each_entry(tmp, &timeouts, list) {
208 if (tv_diff(&tmp->time, &timeout->time) > 0) {
209 h = &tmp->list;
210 break;
211 }
212 }
213
214 list_add_tail(&timeout->list, h);
215 timeout->pending = true;
216
217 return 0;
218 }
219
220 static void uloop_gettime(struct timeval *tv)
221 {
222 struct timespec ts;
223
224 clock_gettime(CLOCK_MONOTONIC, &ts);
225 tv->tv_sec = ts.tv_sec;
226 tv->tv_usec = ts.tv_nsec / 1000;
227 }
228
229 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
230 {
231 struct timeval *time = &timeout->time;
232
233 if (timeout->pending)
234 uloop_timeout_cancel(timeout);
235
236 uloop_gettime(time);
237
238 time->tv_sec += msecs / 1000;
239 time->tv_usec += (msecs % 1000) * 1000;
240
241 if (time->tv_usec > 1000000) {
242 time->tv_sec++;
243 time->tv_usec -= 1000000;
244 }
245
246 return uloop_timeout_add(timeout);
247 }
248
249 int uloop_timeout_cancel(struct uloop_timeout *timeout)
250 {
251 if (!timeout->pending)
252 return -1;
253
254 list_del(&timeout->list);
255 timeout->pending = false;
256
257 return 0;
258 }
259
260 int uloop_timeout_remaining(struct uloop_timeout *timeout)
261 {
262 struct timeval now;
263
264 if (!timeout->pending)
265 return -1;
266
267 uloop_gettime(&now);
268
269 return tv_diff(&timeout->time, &now);
270 }
271
272 int uloop_process_add(struct uloop_process *p)
273 {
274 struct uloop_process *tmp;
275 struct list_head *h = &processes;
276
277 if (p->pending)
278 return -1;
279
280 list_for_each_entry(tmp, &processes, list) {
281 if (tmp->pid > p->pid) {
282 h = &tmp->list;
283 break;
284 }
285 }
286
287 list_add_tail(&p->list, h);
288 p->pending = true;
289
290 return 0;
291 }
292
293 int uloop_process_delete(struct uloop_process *p)
294 {
295 if (!p->pending)
296 return -1;
297
298 list_del(&p->list);
299 p->pending = false;
300
301 return 0;
302 }
303
304 static void uloop_handle_processes(void)
305 {
306 struct uloop_process *p, *tmp;
307 pid_t pid;
308 int ret;
309
310 do_sigchld = false;
311
312 while (1) {
313 pid = waitpid(-1, &ret, WNOHANG);
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_signal(int signo)
332 {
333 switch (signo) {
334 case SIGINT:
335 case SIGQUIT:
336 case SIGTERM:
337 uloop_cancelled = true;
338 break;
339 case SIGCHLD:
340 do_sigchld = true;
341 }
342 }
343
344 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
345 {
346 struct sigaction s;
347 struct sigaction *act;
348
349 act = NULL;
350 sigaction(signum, NULL, &s);
351
352 if (add) {
353 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
354 memcpy(old, &s, sizeof(struct sigaction));
355 s.sa_handler = handler;
356 s.sa_flags = 0;
357 act = &s;
358 }
359 }
360 else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
361 act = old;
362 }
363
364 if (act != NULL)
365 sigaction(signum, act, NULL);
366 }
367
368 static void uloop_ignore_signal(int signum, bool ignore)
369 {
370 struct sigaction s;
371 void *new_handler = NULL;
372
373 sigaction(signum, NULL, &s);
374
375 if (ignore) {
376 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
377 new_handler = SIG_IGN;
378 } else {
379 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
380 new_handler = SIG_DFL;
381 }
382
383 if (new_handler) {
384 s.sa_handler = new_handler;
385 s.sa_flags = 0;
386 sigaction(signum, &s, NULL);
387 }
388 }
389
390 static void uloop_setup_signals(bool add)
391 {
392 static struct sigaction old_sigint, old_sigchld, old_sigterm;
393
394 if (uloop_setup_signalfd(add))
395 return;
396
397 uloop_install_handler(SIGINT, uloop_handle_signal, &old_sigint, add);
398 uloop_install_handler(SIGTERM, uloop_handle_signal, &old_sigterm, add);
399 uloop_install_handler(SIGQUIT, uloop_handle_signal, &old_sigterm, add);
400 uloop_install_handler(SIGCHLD, uloop_handle_signal, &old_sigchld, add);
401
402 uloop_ignore_signal(SIGPIPE, add);
403 }
404
405 static int uloop_get_next_timeout(struct timeval *tv)
406 {
407 struct uloop_timeout *timeout;
408 int diff;
409
410 if (list_empty(&timeouts))
411 return -1;
412
413 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
414 diff = tv_diff(&timeout->time, tv);
415 if (diff < 0)
416 return 0;
417
418 return diff;
419 }
420
421 static void uloop_process_timeouts(struct timeval *tv)
422 {
423 struct uloop_timeout *t;
424
425 while (!list_empty(&timeouts)) {
426 t = list_first_entry(&timeouts, struct uloop_timeout, list);
427
428 if (tv_diff(&t->time, tv) > 0)
429 break;
430
431 uloop_timeout_cancel(t);
432 if (t->cb)
433 t->cb(t);
434 }
435 }
436
437 static void uloop_clear_timeouts(void)
438 {
439 struct uloop_timeout *t, *tmp;
440
441 list_for_each_entry_safe(t, tmp, &timeouts, list)
442 uloop_timeout_cancel(t);
443 }
444
445 static void uloop_clear_processes(void)
446 {
447 struct uloop_process *p, *tmp;
448
449 list_for_each_entry_safe(p, tmp, &processes, list)
450 uloop_process_delete(p);
451 }
452
453 void uloop_run(void)
454 {
455 static int recursive_calls = 0;
456 struct timeval tv;
457
458 /*
459 * Handlers are only updated for the first call to uloop_run() (and restored
460 * when this call is done).
461 */
462 if (!recursive_calls++)
463 uloop_setup_signals(true);
464
465 uloop_cancelled = false;
466 while(!uloop_cancelled)
467 {
468 uloop_gettime(&tv);
469 uloop_process_timeouts(&tv);
470
471 if (do_sigchld)
472 uloop_handle_processes();
473
474 if (uloop_cancelled)
475 break;
476
477 uloop_gettime(&tv);
478 uloop_run_events(uloop_get_next_timeout(&tv));
479 }
480
481 if (!--recursive_calls)
482 uloop_setup_signals(false);
483 }
484
485 void uloop_done(void)
486 {
487 if (signal_fd >= 0) {
488 close(signal_fd);
489 signal_fd = -1;
490 }
491
492 if (poll_fd < 0)
493 return;
494
495 close(poll_fd);
496 poll_fd = -1;
497
498 uloop_clear_timeouts();
499 uloop_clear_processes();
500 }