… | |
… | |
42 | #include <stdio.h> |
42 | #include <stdio.h> |
43 | |
43 | |
44 | #include <assert.h> |
44 | #include <assert.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
47 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
48 | #include <sys/time.h> |
50 | #include <sys/time.h> |
49 | #include <time.h> |
51 | #include <time.h> |
50 | |
52 | |
51 | /**/ |
53 | /**/ |
52 | |
54 | |
… | |
… | |
56 | |
58 | |
57 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
58 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
59 | #endif |
61 | #endif |
60 | |
62 | |
|
|
63 | #ifndef EV_USE_POLL |
|
|
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
65 | #endif |
|
|
66 | |
61 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
62 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
|
|
69 | #endif |
|
|
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
63 | #endif |
73 | #endif |
64 | |
74 | |
65 | #ifndef EV_USE_REALTIME |
75 | #ifndef EV_USE_REALTIME |
66 | # define EV_USE_REALTIME 1 |
76 | # define EV_USE_REALTIME 1 |
67 | #endif |
77 | #endif |
… | |
… | |
95 | # define inline static |
105 | # define inline static |
96 | #endif |
106 | #endif |
97 | |
107 | |
98 | #define expect_false(expr) expect ((expr) != 0, 0) |
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
99 | #define expect_true(expr) expect ((expr) != 0, 1) |
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
100 | |
113 | |
101 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
102 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
103 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
104 | |
117 | |
… | |
… | |
189 | { |
202 | { |
190 | W w; |
203 | W w; |
191 | int events; |
204 | int events; |
192 | } ANPENDING; |
205 | } ANPENDING; |
193 | |
206 | |
194 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
195 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
196 | |
209 | |
197 | static void |
210 | static void |
198 | event (W w, int events) |
211 | event (W w, int events) |
199 | { |
212 | { |
200 | if (w->pending) |
213 | if (w->pending) |
201 | { |
214 | { |
202 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
203 | return; |
216 | return; |
204 | } |
217 | } |
205 | |
218 | |
206 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
207 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
208 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
209 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
210 | } |
223 | } |
211 | |
224 | |
212 | static void |
225 | static void |
213 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
214 | { |
227 | { |
… | |
… | |
277 | ++fdchangecnt; |
290 | ++fdchangecnt; |
278 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
279 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
280 | } |
293 | } |
281 | |
294 | |
|
|
295 | static void |
|
|
296 | fd_kill (int fd) |
|
|
297 | { |
|
|
298 | struct ev_io *w; |
|
|
299 | |
|
|
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
|
|
302 | { |
|
|
303 | ev_io_stop (w); |
|
|
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
282 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
283 | static void |
309 | static void |
284 | fd_recheck (void) |
310 | fd_ebadf (void) |
285 | { |
311 | { |
286 | int fd; |
312 | int fd; |
287 | |
313 | |
288 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
289 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
290 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
291 | while (anfds [fd].head) |
317 | fd_kill (fd); |
|
|
318 | } |
|
|
319 | |
|
|
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
321 | static void |
|
|
322 | fd_enomem (void) |
|
|
323 | { |
|
|
324 | int fd = anfdmax; |
|
|
325 | |
|
|
326 | while (fd--) |
|
|
327 | if (anfds [fd].events) |
292 | { |
328 | { |
293 | ev_io_stop (anfds [fd].head); |
329 | close (fd); |
294 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
330 | fd_kill (fd); |
|
|
331 | return; |
295 | } |
332 | } |
296 | } |
333 | } |
297 | |
334 | |
298 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
299 | |
336 | |
300 | static struct ev_timer **timers; |
337 | static struct ev_timer **timers; |
… | |
… | |
403 | } |
440 | } |
404 | |
441 | |
405 | static void |
442 | static void |
406 | siginit (void) |
443 | siginit (void) |
407 | { |
444 | { |
|
|
445 | #ifndef WIN32 |
408 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
409 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
410 | |
448 | |
411 | /* rather than sort out wether we really need nb, set it */ |
449 | /* rather than sort out wether we really need nb, set it */ |
412 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
413 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
452 | #endif |
414 | |
453 | |
415 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
416 | ev_io_start (&sigev); |
455 | ev_io_start (&sigev); |
417 | } |
456 | } |
418 | |
457 | |
… | |
… | |
430 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
431 | |
470 | |
432 | static struct ev_child *childs [PID_HASHSIZE]; |
471 | static struct ev_child *childs [PID_HASHSIZE]; |
433 | static struct ev_signal childev; |
472 | static struct ev_signal childev; |
434 | |
473 | |
|
|
474 | #ifndef WIN32 |
|
|
475 | |
435 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
436 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
437 | #endif |
478 | #endif |
438 | |
479 | |
439 | static void |
480 | static void |
|
|
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
482 | { |
|
|
483 | struct ev_child *w; |
|
|
484 | |
|
|
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
486 | if (w->pid == pid || !w->pid) |
|
|
487 | { |
|
|
488 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
489 | w->rpid = pid; |
|
|
490 | w->rstatus = status; |
|
|
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | static void |
440 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
441 | { |
498 | { |
442 | struct ev_child *w; |
|
|
443 | int pid, status; |
499 | int pid, status; |
444 | |
500 | |
|
|
501 | printf ("chld %x\n", revents);//D |
445 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
446 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
503 | { |
447 | if (w->pid == pid || !w->pid) |
504 | /* make sure we are called again until all childs have been reaped */ |
448 | { |
|
|
449 | w->status = status; |
|
|
450 | event ((W)w, EV_CHILD); |
505 | event ((W)sw, EV_SIGNAL); |
451 | } |
506 | |
|
|
507 | child_reap (sw, pid, pid, status); |
|
|
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
509 | } |
452 | } |
510 | } |
|
|
511 | |
|
|
512 | #endif |
453 | |
513 | |
454 | /*****************************************************************************/ |
514 | /*****************************************************************************/ |
455 | |
515 | |
|
|
516 | #if EV_USE_KQUEUE |
|
|
517 | # include "ev_kqueue.c" |
|
|
518 | #endif |
456 | #if EV_USE_EPOLL |
519 | #if EV_USE_EPOLL |
457 | # include "ev_epoll.c" |
520 | # include "ev_epoll.c" |
458 | #endif |
521 | #endif |
|
|
522 | #if EV_USE_POLL |
|
|
523 | # include "ev_poll.c" |
|
|
524 | #endif |
459 | #if EV_USE_SELECT |
525 | #if EV_USE_SELECT |
460 | # include "ev_select.c" |
526 | # include "ev_select.c" |
461 | #endif |
527 | #endif |
462 | |
528 | |
463 | int |
529 | int |
… | |
… | |
470 | ev_version_minor (void) |
536 | ev_version_minor (void) |
471 | { |
537 | { |
472 | return EV_VERSION_MINOR; |
538 | return EV_VERSION_MINOR; |
473 | } |
539 | } |
474 | |
540 | |
|
|
541 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
542 | static int |
|
|
543 | enable_secure () |
|
|
544 | { |
|
|
545 | return getuid () != geteuid () |
|
|
546 | || getgid () != getegid (); |
|
|
547 | } |
|
|
548 | |
475 | int ev_init (int flags) |
549 | int ev_init (int methods) |
476 | { |
550 | { |
477 | if (!ev_method) |
551 | if (!ev_method) |
478 | { |
552 | { |
479 | #if EV_USE_MONOTONIC |
553 | #if EV_USE_MONOTONIC |
480 | { |
554 | { |
… | |
… | |
490 | diff = ev_now - now; |
564 | diff = ev_now - now; |
491 | |
565 | |
492 | if (pipe (sigpipe)) |
566 | if (pipe (sigpipe)) |
493 | return 0; |
567 | return 0; |
494 | |
568 | |
|
|
569 | if (methods == EVMETHOD_AUTO) |
|
|
570 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
572 | else |
495 | ev_method = EVMETHOD_NONE; |
573 | methods = EVMETHOD_ANY; |
|
|
574 | |
|
|
575 | ev_method = 0; |
|
|
576 | #if EV_USE_KQUEUE |
|
|
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
578 | #endif |
496 | #if EV_USE_EPOLL |
579 | #if EV_USE_EPOLL |
497 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
581 | #endif |
|
|
582 | #if EV_USE_POLL |
|
|
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
498 | #endif |
584 | #endif |
499 | #if EV_USE_SELECT |
585 | #if EV_USE_SELECT |
500 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
586 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
501 | #endif |
587 | #endif |
502 | |
588 | |
503 | if (ev_method) |
589 | if (ev_method) |
504 | { |
590 | { |
505 | ev_watcher_init (&sigev, sigcb); |
591 | ev_watcher_init (&sigev, sigcb); |
|
|
592 | ev_set_priority (&sigev, EV_MAXPRI); |
506 | siginit (); |
593 | siginit (); |
507 | |
594 | |
|
|
595 | #ifndef WIN32 |
508 | ev_signal_init (&childev, childcb, SIGCHLD); |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
597 | ev_set_priority (&childev, EV_MAXPRI); |
509 | ev_signal_start (&childev); |
598 | ev_signal_start (&childev); |
|
|
599 | #endif |
510 | } |
600 | } |
511 | } |
601 | } |
512 | |
602 | |
513 | return ev_method; |
603 | return ev_method; |
514 | } |
604 | } |
… | |
… | |
545 | /*****************************************************************************/ |
635 | /*****************************************************************************/ |
546 | |
636 | |
547 | static void |
637 | static void |
548 | call_pending (void) |
638 | call_pending (void) |
549 | { |
639 | { |
|
|
640 | int pri; |
|
|
641 | |
|
|
642 | for (pri = NUMPRI; pri--; ) |
550 | while (pendingcnt) |
643 | while (pendingcnt [pri]) |
551 | { |
644 | { |
552 | ANPENDING *p = pendings + --pendingcnt; |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
553 | |
646 | |
554 | if (p->w) |
647 | if (p->w) |
555 | { |
648 | { |
556 | p->w->pending = 0; |
649 | p->w->pending = 0; |
557 | p->w->cb (p->w, p->events); |
650 | p->w->cb (p->w, p->events); |
558 | } |
651 | } |
559 | } |
652 | } |
560 | } |
653 | } |
561 | |
654 | |
562 | static void |
655 | static void |
563 | timers_reify (void) |
656 | timers_reify (void) |
564 | { |
657 | { |
… | |
… | |
796 | static void |
889 | static void |
797 | ev_clear_pending (W w) |
890 | ev_clear_pending (W w) |
798 | { |
891 | { |
799 | if (w->pending) |
892 | if (w->pending) |
800 | { |
893 | { |
801 | pendings [w->pending - 1].w = 0; |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
802 | w->pending = 0; |
895 | w->pending = 0; |
803 | } |
896 | } |
804 | } |
897 | } |
805 | |
898 | |
806 | static void |
899 | static void |
807 | ev_start (W w, int active) |
900 | ev_start (W w, int active) |
808 | { |
901 | { |
|
|
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
904 | |
809 | w->active = active; |
905 | w->active = active; |
810 | } |
906 | } |
811 | |
907 | |
812 | static void |
908 | static void |
813 | ev_stop (W w) |
909 | ev_stop (W w) |
… | |
… | |
930 | } |
1026 | } |
931 | |
1027 | |
932 | ev_stop ((W)w); |
1028 | ev_stop ((W)w); |
933 | } |
1029 | } |
934 | |
1030 | |
|
|
1031 | #ifndef SA_RESTART |
|
|
1032 | # define SA_RESTART 0 |
|
|
1033 | #endif |
|
|
1034 | |
935 | void |
1035 | void |
936 | ev_signal_start (struct ev_signal *w) |
1036 | ev_signal_start (struct ev_signal *w) |
937 | { |
1037 | { |
938 | if (ev_is_active (w)) |
1038 | if (ev_is_active (w)) |
939 | return; |
1039 | return; |
… | |
… | |
947 | if (!w->next) |
1047 | if (!w->next) |
948 | { |
1048 | { |
949 | struct sigaction sa; |
1049 | struct sigaction sa; |
950 | sa.sa_handler = sighandler; |
1050 | sa.sa_handler = sighandler; |
951 | sigfillset (&sa.sa_mask); |
1051 | sigfillset (&sa.sa_mask); |
952 | sa.sa_flags = 0; |
1052 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
953 | sigaction (w->signum, &sa, 0); |
1053 | sigaction (w->signum, &sa, 0); |
954 | } |
1054 | } |
955 | } |
1055 | } |
956 | |
1056 | |
957 | void |
1057 | void |