… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
175 | |
175 | |
176 | /*****************************************************************************/ |
176 | /*****************************************************************************/ |
177 | |
177 | |
178 | typedef struct |
178 | typedef struct |
179 | { |
179 | { |
180 | struct ev_io *head; |
180 | struct ev_watcher_list *head; |
181 | unsigned char events; |
181 | unsigned char events; |
182 | unsigned char reify; |
182 | unsigned char reify; |
183 | } ANFD; |
183 | } ANFD; |
184 | |
184 | |
185 | static ANFD *anfds; |
185 | static ANFD *anfds; |
… | |
… | |
235 | fd_event (int fd, int events) |
235 | fd_event (int fd, int events) |
236 | { |
236 | { |
237 | ANFD *anfd = anfds + fd; |
237 | ANFD *anfd = anfds + fd; |
238 | struct ev_io *w; |
238 | struct ev_io *w; |
239 | |
239 | |
240 | for (w = anfd->head; w; w = w->next) |
240 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
241 | { |
241 | { |
242 | int ev = w->events & events; |
242 | int ev = w->events & events; |
243 | |
243 | |
244 | if (ev) |
244 | if (ev) |
245 | event ((W)w, ev); |
245 | event ((W)w, ev); |
… | |
… | |
262 | ANFD *anfd = anfds + fd; |
262 | ANFD *anfd = anfds + fd; |
263 | struct ev_io *w; |
263 | struct ev_io *w; |
264 | |
264 | |
265 | int events = 0; |
265 | int events = 0; |
266 | |
266 | |
267 | for (w = anfd->head; w; w = w->next) |
267 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
268 | events |= w->events; |
268 | events |= w->events; |
269 | |
269 | |
270 | anfd->reify = 0; |
270 | anfd->reify = 0; |
271 | |
271 | |
272 | if (anfd->events != events) |
272 | if (anfd->events != events) |
… | |
… | |
295 | static void |
295 | static void |
296 | fd_kill (int fd) |
296 | fd_kill (int fd) |
297 | { |
297 | { |
298 | struct ev_io *w; |
298 | struct ev_io *w; |
299 | |
299 | |
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
300 | while ((w = (struct ev_io *)anfds [fd].head)) |
302 | { |
301 | { |
303 | ev_io_stop (w); |
302 | ev_io_stop (w); |
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
303 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
305 | } |
304 | } |
306 | } |
305 | } |
… | |
… | |
383 | |
382 | |
384 | /*****************************************************************************/ |
383 | /*****************************************************************************/ |
385 | |
384 | |
386 | typedef struct |
385 | typedef struct |
387 | { |
386 | { |
388 | struct ev_signal *head; |
387 | struct ev_watcher_list *head; |
389 | sig_atomic_t volatile gotsig; |
388 | sig_atomic_t volatile gotsig; |
390 | } ANSIG; |
389 | } ANSIG; |
391 | |
390 | |
392 | static ANSIG *signals; |
391 | static ANSIG *signals; |
393 | static int signalmax; |
392 | static int signalmax; |
… | |
… | |
413 | { |
412 | { |
414 | signals [signum - 1].gotsig = 1; |
413 | signals [signum - 1].gotsig = 1; |
415 | |
414 | |
416 | if (!gotsig) |
415 | if (!gotsig) |
417 | { |
416 | { |
|
|
417 | int old_errno = errno; |
418 | gotsig = 1; |
418 | gotsig = 1; |
419 | write (sigpipe [1], &signum, 1); |
419 | write (sigpipe [1], &signum, 1); |
|
|
420 | errno = old_errno; |
420 | } |
421 | } |
421 | } |
422 | } |
422 | |
423 | |
423 | static void |
424 | static void |
424 | sigcb (struct ev_io *iow, int revents) |
425 | sigcb (struct ev_io *iow, int revents) |
425 | { |
426 | { |
426 | struct ev_signal *w; |
427 | struct ev_watcher_list *w; |
427 | int signum; |
428 | int signum; |
428 | |
429 | |
429 | read (sigpipe [0], &revents, 1); |
430 | read (sigpipe [0], &revents, 1); |
430 | gotsig = 0; |
431 | gotsig = 0; |
431 | |
432 | |
… | |
… | |
476 | #ifndef WCONTINUED |
477 | #ifndef WCONTINUED |
477 | # define WCONTINUED 0 |
478 | # define WCONTINUED 0 |
478 | #endif |
479 | #endif |
479 | |
480 | |
480 | static void |
481 | static void |
|
|
482 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
483 | { |
|
|
484 | struct ev_child *w; |
|
|
485 | |
|
|
486 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
|
|
487 | if (w->pid == pid || !w->pid) |
|
|
488 | { |
|
|
489 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
490 | w->rpid = pid; |
|
|
491 | w->rstatus = status; |
|
|
492 | event ((W)w, EV_CHILD); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | static void |
481 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
482 | { |
498 | { |
483 | struct ev_child *w; |
|
|
484 | int pid, status; |
499 | int pid, status; |
485 | |
500 | |
486 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
501 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
487 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
502 | { |
488 | if (w->pid == pid || !w->pid) |
503 | /* make sure we are called again until all childs have been reaped */ |
489 | { |
|
|
490 | w->status = status; |
|
|
491 | event ((W)w, EV_CHILD); |
504 | event ((W)sw, EV_SIGNAL); |
492 | } |
505 | |
|
|
506 | child_reap (sw, pid, pid, status); |
|
|
507 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
508 | } |
493 | } |
509 | } |
494 | |
510 | |
495 | #endif |
511 | #endif |
496 | |
512 | |
497 | /*****************************************************************************/ |
513 | /*****************************************************************************/ |
… | |
… | |
519 | ev_version_minor (void) |
535 | ev_version_minor (void) |
520 | { |
536 | { |
521 | return EV_VERSION_MINOR; |
537 | return EV_VERSION_MINOR; |
522 | } |
538 | } |
523 | |
539 | |
524 | /* return true if we are running with elevated privileges and ignore env variables */ |
540 | /* return true if we are running with elevated privileges and should ignore env variables */ |
525 | static int |
541 | static int |
526 | enable_secure () |
542 | enable_secure () |
527 | { |
543 | { |
|
|
544 | #ifdef WIN32 |
|
|
545 | return 0; |
|
|
546 | #else |
528 | return getuid () != geteuid () |
547 | return getuid () != geteuid () |
529 | || getgid () != getegid (); |
548 | || getgid () != getegid (); |
|
|
549 | #endif |
530 | } |
550 | } |
531 | |
551 | |
532 | int ev_init (int methods) |
552 | int ev_init (int methods) |
533 | { |
553 | { |
534 | if (!ev_method) |
554 | if (!ev_method) |
… | |
… | |
548 | |
568 | |
549 | if (pipe (sigpipe)) |
569 | if (pipe (sigpipe)) |
550 | return 0; |
570 | return 0; |
551 | |
571 | |
552 | if (methods == EVMETHOD_AUTO) |
572 | if (methods == EVMETHOD_AUTO) |
553 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
573 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
554 | methods = atoi (getenv ("LIBEV_METHODS")); |
574 | methods = atoi (getenv ("LIBEV_METHODS")); |
555 | else |
575 | else |
556 | methods = EVMETHOD_ANY; |
576 | methods = EVMETHOD_ANY; |
557 | |
577 | |
558 | ev_method = 0; |
578 | ev_method = 0; |
559 | #if EV_USE_KQUEUE |
579 | #if EV_USE_KQUEUE |
560 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
580 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
561 | #endif |
581 | #endif |
… | |
… | |
570 | #endif |
590 | #endif |
571 | |
591 | |
572 | if (ev_method) |
592 | if (ev_method) |
573 | { |
593 | { |
574 | ev_watcher_init (&sigev, sigcb); |
594 | ev_watcher_init (&sigev, sigcb); |
|
|
595 | ev_set_priority (&sigev, EV_MAXPRI); |
575 | siginit (); |
596 | siginit (); |
576 | |
597 | |
577 | #ifndef WIN32 |
598 | #ifndef WIN32 |
578 | ev_signal_init (&childev, childcb, SIGCHLD); |
599 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
600 | ev_set_priority (&childev, EV_MAXPRI); |
579 | ev_signal_start (&childev); |
601 | ev_signal_start (&childev); |
580 | #endif |
602 | #endif |
581 | } |
603 | } |
582 | } |
604 | } |
583 | |
605 | |
… | |
… | |
1007 | } |
1029 | } |
1008 | |
1030 | |
1009 | ev_stop ((W)w); |
1031 | ev_stop ((W)w); |
1010 | } |
1032 | } |
1011 | |
1033 | |
|
|
1034 | #ifndef SA_RESTART |
|
|
1035 | # define SA_RESTART 0 |
|
|
1036 | #endif |
|
|
1037 | |
1012 | void |
1038 | void |
1013 | ev_signal_start (struct ev_signal *w) |
1039 | ev_signal_start (struct ev_signal *w) |
1014 | { |
1040 | { |
1015 | if (ev_is_active (w)) |
1041 | if (ev_is_active (w)) |
1016 | return; |
1042 | return; |
… | |
… | |
1024 | if (!w->next) |
1050 | if (!w->next) |
1025 | { |
1051 | { |
1026 | struct sigaction sa; |
1052 | struct sigaction sa; |
1027 | sa.sa_handler = sighandler; |
1053 | sa.sa_handler = sighandler; |
1028 | sigfillset (&sa.sa_mask); |
1054 | sigfillset (&sa.sa_mask); |
1029 | sa.sa_flags = 0; |
1055 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1030 | sigaction (w->signum, &sa, 0); |
1056 | sigaction (w->signum, &sa, 0); |
1031 | } |
1057 | } |
1032 | } |
1058 | } |
1033 | |
1059 | |
1034 | void |
1060 | void |