… | |
… | |
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; |
… | |
… | |
376 | { |
413 | { |
377 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
378 | |
415 | |
379 | if (!gotsig) |
416 | if (!gotsig) |
380 | { |
417 | { |
|
|
418 | int old_errno = errno; |
381 | gotsig = 1; |
419 | gotsig = 1; |
382 | write (sigpipe [1], &signum, 1); |
420 | write (sigpipe [1], &signum, 1); |
|
|
421 | errno = old_errno; |
383 | } |
422 | } |
384 | } |
423 | } |
385 | |
424 | |
386 | static void |
425 | static void |
387 | sigcb (struct ev_io *iow, int revents) |
426 | sigcb (struct ev_io *iow, int revents) |
… | |
… | |
403 | } |
442 | } |
404 | |
443 | |
405 | static void |
444 | static void |
406 | siginit (void) |
445 | siginit (void) |
407 | { |
446 | { |
|
|
447 | #ifndef WIN32 |
408 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
409 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
410 | |
450 | |
411 | /* rather than sort out wether we really need nb, set it */ |
451 | /* rather than sort out wether we really need nb, set it */ |
412 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
413 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
454 | #endif |
414 | |
455 | |
415 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
416 | ev_io_start (&sigev); |
457 | ev_io_start (&sigev); |
417 | } |
458 | } |
418 | |
459 | |
… | |
… | |
430 | /*****************************************************************************/ |
471 | /*****************************************************************************/ |
431 | |
472 | |
432 | static struct ev_child *childs [PID_HASHSIZE]; |
473 | static struct ev_child *childs [PID_HASHSIZE]; |
433 | static struct ev_signal childev; |
474 | static struct ev_signal childev; |
434 | |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
435 | #ifndef WCONTINUED |
478 | #ifndef WCONTINUED |
436 | # define WCONTINUED 0 |
479 | # define WCONTINUED 0 |
437 | #endif |
480 | #endif |
438 | |
481 | |
439 | static void |
482 | static void |
|
|
483 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
484 | { |
|
|
485 | struct ev_child *w; |
|
|
486 | |
|
|
487 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
488 | if (w->pid == pid || !w->pid) |
|
|
489 | { |
|
|
490 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
491 | w->rpid = pid; |
|
|
492 | w->rstatus = status; |
|
|
493 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
494 | event ((W)w, EV_CHILD); |
|
|
495 | } |
|
|
496 | } |
|
|
497 | |
|
|
498 | static void |
440 | childcb (struct ev_signal *sw, int revents) |
499 | childcb (struct ev_signal *sw, int revents) |
441 | { |
500 | { |
442 | struct ev_child *w; |
|
|
443 | int pid, status; |
501 | int pid, status; |
444 | |
502 | |
|
|
503 | printf ("chld %x\n", revents);//D |
445 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
446 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
505 | { |
447 | if (w->pid == pid || !w->pid) |
506 | /* make sure we are called again until all childs have been reaped */ |
448 | { |
|
|
449 | w->status = status; |
|
|
450 | event ((W)w, EV_CHILD); |
507 | event ((W)sw, EV_SIGNAL); |
451 | } |
508 | |
|
|
509 | child_reap (sw, pid, pid, status); |
|
|
510 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
511 | } |
452 | } |
512 | } |
|
|
513 | |
|
|
514 | #endif |
453 | |
515 | |
454 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
455 | |
517 | |
|
|
518 | #if EV_USE_KQUEUE |
|
|
519 | # include "ev_kqueue.c" |
|
|
520 | #endif |
456 | #if EV_USE_EPOLL |
521 | #if EV_USE_EPOLL |
457 | # include "ev_epoll.c" |
522 | # include "ev_epoll.c" |
458 | #endif |
523 | #endif |
|
|
524 | #if EV_USE_POLL |
|
|
525 | # include "ev_poll.c" |
|
|
526 | #endif |
459 | #if EV_USE_SELECT |
527 | #if EV_USE_SELECT |
460 | # include "ev_select.c" |
528 | # include "ev_select.c" |
461 | #endif |
529 | #endif |
462 | |
530 | |
463 | int |
531 | int |
… | |
… | |
470 | ev_version_minor (void) |
538 | ev_version_minor (void) |
471 | { |
539 | { |
472 | return EV_VERSION_MINOR; |
540 | return EV_VERSION_MINOR; |
473 | } |
541 | } |
474 | |
542 | |
|
|
543 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
544 | static int |
|
|
545 | enable_secure () |
|
|
546 | { |
|
|
547 | return getuid () != geteuid () |
|
|
548 | || getgid () != getegid (); |
|
|
549 | } |
|
|
550 | |
475 | int ev_init (int flags) |
551 | int ev_init (int methods) |
476 | { |
552 | { |
477 | if (!ev_method) |
553 | if (!ev_method) |
478 | { |
554 | { |
479 | #if EV_USE_MONOTONIC |
555 | #if EV_USE_MONOTONIC |
480 | { |
556 | { |
… | |
… | |
490 | diff = ev_now - now; |
566 | diff = ev_now - now; |
491 | |
567 | |
492 | if (pipe (sigpipe)) |
568 | if (pipe (sigpipe)) |
493 | return 0; |
569 | return 0; |
494 | |
570 | |
|
|
571 | if (methods == EVMETHOD_AUTO) |
|
|
572 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
573 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
574 | else |
495 | ev_method = EVMETHOD_NONE; |
575 | methods = EVMETHOD_ANY; |
|
|
576 | |
|
|
577 | ev_method = 0; |
|
|
578 | #if EV_USE_KQUEUE |
|
|
579 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
580 | #endif |
496 | #if EV_USE_EPOLL |
581 | #if EV_USE_EPOLL |
497 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
582 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
583 | #endif |
|
|
584 | #if EV_USE_POLL |
|
|
585 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
498 | #endif |
586 | #endif |
499 | #if EV_USE_SELECT |
587 | #if EV_USE_SELECT |
500 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
588 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
501 | #endif |
589 | #endif |
502 | |
590 | |
503 | if (ev_method) |
591 | if (ev_method) |
504 | { |
592 | { |
505 | ev_watcher_init (&sigev, sigcb); |
593 | ev_watcher_init (&sigev, sigcb); |
|
|
594 | ev_set_priority (&sigev, EV_MAXPRI); |
506 | siginit (); |
595 | siginit (); |
507 | |
596 | |
|
|
597 | #ifndef WIN32 |
508 | ev_signal_init (&childev, childcb, SIGCHLD); |
598 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
599 | ev_set_priority (&childev, EV_MAXPRI); |
509 | ev_signal_start (&childev); |
600 | ev_signal_start (&childev); |
|
|
601 | #endif |
510 | } |
602 | } |
511 | } |
603 | } |
512 | |
604 | |
513 | return ev_method; |
605 | return ev_method; |
514 | } |
606 | } |
… | |
… | |
545 | /*****************************************************************************/ |
637 | /*****************************************************************************/ |
546 | |
638 | |
547 | static void |
639 | static void |
548 | call_pending (void) |
640 | call_pending (void) |
549 | { |
641 | { |
|
|
642 | int pri; |
|
|
643 | |
|
|
644 | for (pri = NUMPRI; pri--; ) |
550 | while (pendingcnt) |
645 | while (pendingcnt [pri]) |
551 | { |
646 | { |
552 | ANPENDING *p = pendings + --pendingcnt; |
647 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
553 | |
648 | |
554 | if (p->w) |
649 | if (p->w) |
555 | { |
650 | { |
556 | p->w->pending = 0; |
651 | p->w->pending = 0; |
557 | p->w->cb (p->w, p->events); |
652 | p->w->cb (p->w, p->events); |
558 | } |
653 | } |
559 | } |
654 | } |
560 | } |
655 | } |
561 | |
656 | |
562 | static void |
657 | static void |
563 | timers_reify (void) |
658 | timers_reify (void) |
564 | { |
659 | { |
… | |
… | |
796 | static void |
891 | static void |
797 | ev_clear_pending (W w) |
892 | ev_clear_pending (W w) |
798 | { |
893 | { |
799 | if (w->pending) |
894 | if (w->pending) |
800 | { |
895 | { |
801 | pendings [w->pending - 1].w = 0; |
896 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
802 | w->pending = 0; |
897 | w->pending = 0; |
803 | } |
898 | } |
804 | } |
899 | } |
805 | |
900 | |
806 | static void |
901 | static void |
807 | ev_start (W w, int active) |
902 | ev_start (W w, int active) |
808 | { |
903 | { |
|
|
904 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
905 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
906 | |
809 | w->active = active; |
907 | w->active = active; |
810 | } |
908 | } |
811 | |
909 | |
812 | static void |
910 | static void |
813 | ev_stop (W w) |
911 | ev_stop (W w) |
… | |
… | |
930 | } |
1028 | } |
931 | |
1029 | |
932 | ev_stop ((W)w); |
1030 | ev_stop ((W)w); |
933 | } |
1031 | } |
934 | |
1032 | |
|
|
1033 | #ifndef SA_RESTART |
|
|
1034 | # define SA_RESTART 0 |
|
|
1035 | #endif |
|
|
1036 | |
935 | void |
1037 | void |
936 | ev_signal_start (struct ev_signal *w) |
1038 | ev_signal_start (struct ev_signal *w) |
937 | { |
1039 | { |
938 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
939 | return; |
1041 | return; |
… | |
… | |
947 | if (!w->next) |
1049 | if (!w->next) |
948 | { |
1050 | { |
949 | struct sigaction sa; |
1051 | struct sigaction sa; |
950 | sa.sa_handler = sighandler; |
1052 | sa.sa_handler = sighandler; |
951 | sigfillset (&sa.sa_mask); |
1053 | sigfillset (&sa.sa_mask); |
952 | sa.sa_flags = 0; |
1054 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
953 | sigaction (w->signum, &sa, 0); |
1055 | sigaction (w->signum, &sa, 0); |
954 | } |
1056 | } |
955 | } |
1057 | } |
956 | |
1058 | |
957 | void |
1059 | void |