… | |
… | |
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 | |
… | |
… | |
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
63 | #endif |
65 | #endif |
64 | |
66 | |
65 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
66 | # 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 |
67 | #endif |
73 | #endif |
68 | |
74 | |
69 | #ifndef EV_USE_REALTIME |
75 | #ifndef EV_USE_REALTIME |
70 | # define EV_USE_REALTIME 1 |
76 | # define EV_USE_REALTIME 1 |
71 | #endif |
77 | #endif |
… | |
… | |
407 | { |
413 | { |
408 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
409 | |
415 | |
410 | if (!gotsig) |
416 | if (!gotsig) |
411 | { |
417 | { |
|
|
418 | int old_errno = errno; |
412 | gotsig = 1; |
419 | gotsig = 1; |
413 | write (sigpipe [1], &signum, 1); |
420 | write (sigpipe [1], &signum, 1); |
|
|
421 | errno = old_errno; |
414 | } |
422 | } |
415 | } |
423 | } |
416 | |
424 | |
417 | static void |
425 | static void |
418 | sigcb (struct ev_io *iow, int revents) |
426 | sigcb (struct ev_io *iow, int revents) |
… | |
… | |
434 | } |
442 | } |
435 | |
443 | |
436 | static void |
444 | static void |
437 | siginit (void) |
445 | siginit (void) |
438 | { |
446 | { |
|
|
447 | #ifndef WIN32 |
439 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
440 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
441 | |
450 | |
442 | /* rather than sort out wether we really need nb, set it */ |
451 | /* rather than sort out wether we really need nb, set it */ |
443 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
444 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
454 | #endif |
445 | |
455 | |
446 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
447 | ev_io_start (&sigev); |
457 | ev_io_start (&sigev); |
448 | } |
458 | } |
449 | |
459 | |
… | |
… | |
461 | /*****************************************************************************/ |
471 | /*****************************************************************************/ |
462 | |
472 | |
463 | static struct ev_child *childs [PID_HASHSIZE]; |
473 | static struct ev_child *childs [PID_HASHSIZE]; |
464 | static struct ev_signal childev; |
474 | static struct ev_signal childev; |
465 | |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
466 | #ifndef WCONTINUED |
478 | #ifndef WCONTINUED |
467 | # define WCONTINUED 0 |
479 | # define WCONTINUED 0 |
468 | #endif |
480 | #endif |
469 | |
481 | |
470 | 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 |
471 | childcb (struct ev_signal *sw, int revents) |
499 | childcb (struct ev_signal *sw, int revents) |
472 | { |
500 | { |
473 | struct ev_child *w; |
|
|
474 | int pid, status; |
501 | int pid, status; |
475 | |
502 | |
|
|
503 | printf ("chld %x\n", revents);//D |
476 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
477 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
505 | { |
478 | if (w->pid == pid || !w->pid) |
506 | /* make sure we are called again until all childs have been reaped */ |
479 | { |
|
|
480 | w->status = status; |
|
|
481 | event ((W)w, EV_CHILD); |
507 | event ((W)sw, EV_SIGNAL); |
482 | } |
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 | } |
483 | } |
512 | } |
|
|
513 | |
|
|
514 | #endif |
484 | |
515 | |
485 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
486 | |
517 | |
|
|
518 | #if EV_USE_KQUEUE |
|
|
519 | # include "ev_kqueue.c" |
|
|
520 | #endif |
487 | #if EV_USE_EPOLL |
521 | #if EV_USE_EPOLL |
488 | # include "ev_epoll.c" |
522 | # include "ev_epoll.c" |
489 | #endif |
523 | #endif |
490 | #if EV_USE_POLL |
524 | #if EV_USE_POLL |
491 | # include "ev_poll.c" |
525 | # include "ev_poll.c" |
… | |
… | |
539 | methods = atoi (getenv ("LIBEV_METHODS")); |
573 | methods = atoi (getenv ("LIBEV_METHODS")); |
540 | else |
574 | else |
541 | methods = EVMETHOD_ANY; |
575 | methods = EVMETHOD_ANY; |
542 | |
576 | |
543 | ev_method = 0; |
577 | ev_method = 0; |
|
|
578 | #if EV_USE_KQUEUE |
|
|
579 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
580 | #endif |
544 | #if EV_USE_EPOLL |
581 | #if EV_USE_EPOLL |
545 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
582 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
546 | #endif |
583 | #endif |
547 | #if EV_USE_POLL |
584 | #if EV_USE_POLL |
548 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
585 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
… | |
… | |
552 | #endif |
589 | #endif |
553 | |
590 | |
554 | if (ev_method) |
591 | if (ev_method) |
555 | { |
592 | { |
556 | ev_watcher_init (&sigev, sigcb); |
593 | ev_watcher_init (&sigev, sigcb); |
|
|
594 | ev_set_priority (&sigev, EV_MAXPRI); |
557 | siginit (); |
595 | siginit (); |
558 | |
596 | |
|
|
597 | #ifndef WIN32 |
559 | ev_signal_init (&childev, childcb, SIGCHLD); |
598 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
599 | ev_set_priority (&childev, EV_MAXPRI); |
560 | ev_signal_start (&childev); |
600 | ev_signal_start (&childev); |
|
|
601 | #endif |
561 | } |
602 | } |
562 | } |
603 | } |
563 | |
604 | |
564 | return ev_method; |
605 | return ev_method; |
565 | } |
606 | } |
… | |
… | |
987 | } |
1028 | } |
988 | |
1029 | |
989 | ev_stop ((W)w); |
1030 | ev_stop ((W)w); |
990 | } |
1031 | } |
991 | |
1032 | |
|
|
1033 | #ifndef SA_RESTART |
|
|
1034 | # define SA_RESTART 0 |
|
|
1035 | #endif |
|
|
1036 | |
992 | void |
1037 | void |
993 | ev_signal_start (struct ev_signal *w) |
1038 | ev_signal_start (struct ev_signal *w) |
994 | { |
1039 | { |
995 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
996 | return; |
1041 | return; |
… | |
… | |
1004 | if (!w->next) |
1049 | if (!w->next) |
1005 | { |
1050 | { |
1006 | struct sigaction sa; |
1051 | struct sigaction sa; |
1007 | sa.sa_handler = sighandler; |
1052 | sa.sa_handler = sighandler; |
1008 | sigfillset (&sa.sa_mask); |
1053 | sigfillset (&sa.sa_mask); |
1009 | sa.sa_flags = 0; |
1054 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1010 | sigaction (w->signum, &sa, 0); |
1055 | sigaction (w->signum, &sa, 0); |
1011 | } |
1056 | } |
1012 | } |
1057 | } |
1013 | |
1058 | |
1014 | void |
1059 | void |