• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/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 bool uloop_handle_sigchld = true;
 62 static int uloop_status = 0;
 63 static bool do_sigchld = false;
 64 
 65 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
 66 static int cur_fd, cur_nfds;
 67 static int uloop_run_depth = 0;
 68 
 69 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
 70 
 71 #ifdef USE_KQUEUE
 72 #include "uloop-kqueue.c"
 73 #endif
 74 
 75 #ifdef USE_EPOLL
 76 #include "uloop-epoll.c"
 77 #endif
 78 
 79 static void waker_consume(struct uloop_fd *fd, unsigned int events)
 80 {
 81         char buf[4];
 82 
 83         while (read(fd->fd, buf, 4) > 0)
 84                 ;
 85 }
 86 
 87 static int waker_pipe = -1;
 88 static struct uloop_fd waker_fd = {
 89         .fd = -1,
 90         .cb = waker_consume,
 91 };
 92 
 93 static void waker_init_fd(int fd)
 94 {
 95         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
 96         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
 97 }
 98 
 99 static int waker_init(void)
100 {
101         int fds[2];
102 
103         if (waker_pipe >= 0)
104                 return 0;
105 
106         if (pipe(fds) < 0)
107                 return -1;
108 
109         waker_init_fd(fds[0]);
110         waker_init_fd(fds[1]);
111         waker_pipe = fds[1];
112 
113         waker_fd.fd = fds[0];
114         waker_fd.cb = waker_consume;
115         uloop_fd_add(&waker_fd, ULOOP_READ);
116 
117         return 0;
118 }
119 
120 static void uloop_setup_signals(bool add);
121 
122 int uloop_init(void)
123 {
124         if (uloop_init_pollfd() < 0)
125                 return -1;
126 
127         if (waker_init() < 0) {
128                 uloop_done();
129                 return -1;
130         }
131 
132         uloop_setup_signals(true);
133 
134         return 0;
135 }
136 
137 static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
138 {
139         struct uloop_fd_stack *cur;
140 
141         /*
142          * Do not buffer events for level-triggered fds, they will keep firing.
143          * Caller needs to take care of recursion issues.
144          */
145         if (!(fd->flags & ULOOP_EDGE_TRIGGER))
146                 return false;
147 
148         for (cur = fd_stack; cur; cur = cur->next) {
149                 if (cur->fd != fd)
150                         continue;
151 
152                 if (events < 0)
153                         cur->fd = NULL;
154                 else
155                         cur->events |= events | ULOOP_EVENT_BUFFERED;
156 
157                 return true;
158         }
159 
160         return false;
161 }
162 
163 static void uloop_run_events(int timeout)
164 {
165         struct uloop_fd_event *cur;
166         struct uloop_fd *fd;
167 
168         if (!cur_nfds) {
169                 cur_fd = 0;
170                 cur_nfds = uloop_fetch_events(timeout);
171                 if (cur_nfds < 0)
172                         cur_nfds = 0;
173         }
174 
175         while (cur_nfds > 0) {
176                 struct uloop_fd_stack stack_cur;
177                 unsigned int events;
178 
179                 cur = &cur_fds[cur_fd++];
180                 cur_nfds--;
181 
182                 fd = cur->fd;
183                 events = cur->events;
184                 if (!fd)
185                         continue;
186 
187                 if (!fd->cb)
188                         continue;
189 
190                 if (uloop_fd_stack_event(fd, cur->events))
191                         continue;
192 
193                 stack_cur.next = fd_stack;
194                 stack_cur.fd = fd;
195                 fd_stack = &stack_cur;
196                 do {
197                         stack_cur.events = 0;
198                         fd->cb(fd, events);
199                         events = stack_cur.events & ULOOP_EVENT_MASK;
200                 } while (stack_cur.fd && events);
201                 fd_stack = stack_cur.next;
202 
203                 return;
204         }
205 }
206 
207 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
208 {
209         unsigned int fl;
210         int ret;
211 
212         if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
213                 return uloop_fd_delete(sock);
214 
215         if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
216                 fl = fcntl(sock->fd, F_GETFL, 0);
217                 fl |= O_NONBLOCK;
218                 fcntl(sock->fd, F_SETFL, fl);
219         }
220 
221         ret = register_poll(sock, flags);
222         if (ret < 0)
223                 goto out;
224 
225         sock->registered = true;
226         sock->eof = false;
227         sock->error = false;
228 
229 out:
230         return ret;
231 }
232 
233 int uloop_fd_delete(struct uloop_fd *fd)
234 {
235         int i;
236 
237         for (i = 0; i < cur_nfds; i++) {
238                 if (cur_fds[cur_fd + i].fd != fd)
239                         continue;
240 
241                 cur_fds[cur_fd + i].fd = NULL;
242         }
243 
244         if (!fd->registered)
245                 return 0;
246 
247         fd->registered = false;
248         uloop_fd_stack_event(fd, -1);
249         return __uloop_fd_delete(fd);
250 }
251 
252 static int tv_diff(struct timeval *t1, struct timeval *t2)
253 {
254         return
255                 (t1->tv_sec - t2->tv_sec) * 1000 +
256                 (t1->tv_usec - t2->tv_usec) / 1000;
257 }
258 
259 int uloop_timeout_add(struct uloop_timeout *timeout)
260 {
261         struct uloop_timeout *tmp;
262         struct list_head *h = &timeouts;
263 
264         if (timeout->pending)
265                 return -1;
266 
267         list_for_each_entry(tmp, &timeouts, list) {
268                 if (tv_diff(&tmp->time, &timeout->time) > 0) {
269                         h = &tmp->list;
270                         break;
271                 }
272         }
273 
274         list_add_tail(&timeout->list, h);
275         timeout->pending = true;
276 
277         return 0;
278 }
279 
280 static void uloop_gettime(struct timeval *tv)
281 {
282         struct timespec ts;
283 
284         clock_gettime(CLOCK_MONOTONIC, &ts);
285         tv->tv_sec = ts.tv_sec;
286         tv->tv_usec = ts.tv_nsec / 1000;
287 }
288 
289 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
290 {
291         struct timeval *time = &timeout->time;
292 
293         if (timeout->pending)
294                 uloop_timeout_cancel(timeout);
295 
296         uloop_gettime(time);
297 
298         time->tv_sec += msecs / 1000;
299         time->tv_usec += (msecs % 1000) * 1000;
300 
301         if (time->tv_usec > 1000000) {
302                 time->tv_sec++;
303                 time->tv_usec -= 1000000;
304         }
305 
306         return uloop_timeout_add(timeout);
307 }
308 
309 int uloop_timeout_cancel(struct uloop_timeout *timeout)
310 {
311         if (!timeout->pending)
312                 return -1;
313 
314         list_del(&timeout->list);
315         timeout->pending = false;
316 
317         return 0;
318 }
319 
320 int uloop_timeout_remaining(struct uloop_timeout *timeout)
321 {
322         struct timeval now;
323 
324         if (!timeout->pending)
325                 return -1;
326 
327         uloop_gettime(&now);
328 
329         return tv_diff(&timeout->time, &now);
330 }
331 
332 int uloop_process_add(struct uloop_process *p)
333 {
334         struct uloop_process *tmp;
335         struct list_head *h = &processes;
336 
337         if (p->pending)
338                 return -1;
339 
340         list_for_each_entry(tmp, &processes, list) {
341                 if (tmp->pid > p->pid) {
342                         h = &tmp->list;
343                         break;
344                 }
345         }
346 
347         list_add_tail(&p->list, h);
348         p->pending = true;
349 
350         return 0;
351 }
352 
353 int uloop_process_delete(struct uloop_process *p)
354 {
355         if (!p->pending)
356                 return -1;
357 
358         list_del(&p->list);
359         p->pending = false;
360 
361         return 0;
362 }
363 
364 static void uloop_handle_processes(void)
365 {
366         struct uloop_process *p, *tmp;
367         pid_t pid;
368         int ret;
369 
370         do_sigchld = false;
371 
372         while (1) {
373                 pid = waitpid(-1, &ret, WNOHANG);
374                 if (pid < 0 && errno == EINTR)
375                         continue;
376 
377                 if (pid <= 0)
378                         return;
379 
380                 list_for_each_entry_safe(p, tmp, &processes, list) {
381                         if (p->pid < pid)
382                                 continue;
383 
384                         if (p->pid > pid)
385                                 break;
386 
387                         uloop_process_delete(p);
388                         p->cb(p, ret);
389                 }
390         }
391 
392 }
393 
394 static void uloop_signal_wake(void)
395 {
396         do {
397                 if (write(waker_pipe, "w", 1) < 0) {
398                         if (errno == EINTR)
399                                 continue;
400                 }
401                 break;
402         } while (1);
403 }
404 
405 static void uloop_handle_sigint(int signo)
406 {
407         uloop_status = signo;
408         uloop_cancelled = true;
409         uloop_signal_wake();
410 }
411 
412 static void uloop_sigchld(int signo)
413 {
414         do_sigchld = true;
415         uloop_signal_wake();
416 }
417 
418 static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
419 {
420         struct sigaction s;
421         struct sigaction *act;
422 
423         act = NULL;
424         sigaction(signum, NULL, &s);
425 
426         if (add) {
427                 if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
428                         memcpy(old, &s, sizeof(struct sigaction));
429                         s.sa_handler = handler;
430                         s.sa_flags = 0;
431                         act = &s;
432                 }
433         }
434         else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
435                         act = old;
436         }
437 
438         if (act != NULL)
439                 sigaction(signum, act, NULL);
440 }
441 
442 static void uloop_ignore_signal(int signum, bool ignore)
443 {
444         struct sigaction s;
445         void *new_handler = NULL;
446 
447         sigaction(signum, NULL, &s);
448 
449         if (ignore) {
450                 if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
451                         new_handler = SIG_IGN;
452         } else {
453                 if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
454                         new_handler = SIG_DFL;
455         }
456 
457         if (new_handler) {
458                 s.sa_handler = new_handler;
459                 s.sa_flags = 0;
460                 sigaction(signum, &s, NULL);
461         }
462 }
463 
464 static void uloop_setup_signals(bool add)
465 {
466         static struct sigaction old_sigint, old_sigchld, old_sigterm;
467 
468         uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
469         uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
470 
471         if (uloop_handle_sigchld)
472                 uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
473 
474         uloop_ignore_signal(SIGPIPE, add);
475 }
476 
477 static int uloop_get_next_timeout(struct timeval *tv)
478 {
479         struct uloop_timeout *timeout;
480         int diff;
481 
482         if (list_empty(&timeouts))
483                 return -1;
484 
485         timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
486         diff = tv_diff(&timeout->time, tv);
487         if (diff < 0)
488                 return 0;
489 
490         return diff;
491 }
492 
493 static void uloop_process_timeouts(struct timeval *tv)
494 {
495         struct uloop_timeout *t;
496 
497         while (!list_empty(&timeouts)) {
498                 t = list_first_entry(&timeouts, struct uloop_timeout, list);
499 
500                 if (tv_diff(&t->time, tv) > 0)
501                         break;
502 
503                 uloop_timeout_cancel(t);
504                 if (t->cb)
505                         t->cb(t);
506         }
507 }
508 
509 static void uloop_clear_timeouts(void)
510 {
511         struct uloop_timeout *t, *tmp;
512 
513         list_for_each_entry_safe(t, tmp, &timeouts, list)
514                 uloop_timeout_cancel(t);
515 }
516 
517 static void uloop_clear_processes(void)
518 {
519         struct uloop_process *p, *tmp;
520 
521         list_for_each_entry_safe(p, tmp, &processes, list)
522                 uloop_process_delete(p);
523 }
524 
525 bool uloop_cancelling(void)
526 {
527         return uloop_run_depth > 0 && uloop_cancelled;
528 }
529 
530 int uloop_run_timeout(int timeout)
531 {
532         int next_time = 0;
533         struct timeval tv;
534 
535         uloop_run_depth++;
536 
537         uloop_status = 0;
538         uloop_cancelled = false;
539         while (!uloop_cancelled)
540         {
541                 uloop_gettime(&tv);
542                 uloop_process_timeouts(&tv);
543 
544                 if (do_sigchld)
545                         uloop_handle_processes();
546 
547                 if (uloop_cancelled)
548                         break;
549 
550                 uloop_gettime(&tv);
551 
552                 next_time = uloop_get_next_timeout(&tv);
553                 if (timeout >= 0 && timeout < next_time)
554                         next_time = timeout;
555                 uloop_run_events(next_time);
556         }
557 
558         --uloop_run_depth;
559 
560         return uloop_status;
561 }
562 
563 void uloop_done(void)
564 {
565         uloop_setup_signals(false);
566 
567         if (poll_fd >= 0) {
568                 close(poll_fd);
569                 poll_fd = -1;
570         }
571 
572         if (waker_pipe >= 0) {
573                 uloop_fd_delete(&waker_fd);
574                 close(waker_pipe);
575                 close(waker_fd.fd);
576                 waker_pipe = -1;
577         }
578 
579         uloop_clear_timeouts();
580         uloop_clear_processes();
581 }
582 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt