… | |
… | |
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 | |
… | |
… | |
438 | } |
440 | } |
439 | |
441 | |
440 | static void |
442 | static void |
441 | siginit (void) |
443 | siginit (void) |
442 | { |
444 | { |
|
|
445 | #ifndef WIN32 |
443 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
444 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
445 | |
448 | |
446 | /* rather than sort out wether we really need nb, set it */ |
449 | /* rather than sort out wether we really need nb, set it */ |
447 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
448 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
452 | #endif |
449 | |
453 | |
450 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
451 | ev_io_start (&sigev); |
455 | ev_io_start (&sigev); |
452 | } |
456 | } |
453 | |
457 | |
… | |
… | |
465 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
466 | |
470 | |
467 | static struct ev_child *childs [PID_HASHSIZE]; |
471 | static struct ev_child *childs [PID_HASHSIZE]; |
468 | static struct ev_signal childev; |
472 | static struct ev_signal childev; |
469 | |
473 | |
|
|
474 | #ifndef WIN32 |
|
|
475 | |
470 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
471 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
472 | #endif |
478 | #endif |
473 | |
479 | |
474 | 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 |
475 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
476 | { |
498 | { |
477 | struct ev_child *w; |
|
|
478 | int pid, status; |
499 | int pid, status; |
479 | |
500 | |
|
|
501 | printf ("chld %x\n", revents);//D |
480 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
481 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
503 | { |
482 | if (w->pid == pid || !w->pid) |
504 | /* make sure we are called again until all childs have been reaped */ |
483 | { |
|
|
484 | w->status = status; |
|
|
485 | event ((W)w, EV_CHILD); |
505 | event ((W)sw, EV_SIGNAL); |
486 | } |
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 | } |
487 | } |
510 | } |
|
|
511 | |
|
|
512 | #endif |
488 | |
513 | |
489 | /*****************************************************************************/ |
514 | /*****************************************************************************/ |
490 | |
515 | |
491 | #if EV_USE_KQUEUE |
516 | #if EV_USE_KQUEUE |
492 | # include "ev_kqueue.c" |
517 | # include "ev_kqueue.c" |
… | |
… | |
562 | #endif |
587 | #endif |
563 | |
588 | |
564 | if (ev_method) |
589 | if (ev_method) |
565 | { |
590 | { |
566 | ev_watcher_init (&sigev, sigcb); |
591 | ev_watcher_init (&sigev, sigcb); |
|
|
592 | ev_set_priority (&sigev, EV_MAXPRI); |
567 | siginit (); |
593 | siginit (); |
568 | |
594 | |
|
|
595 | #ifndef WIN32 |
569 | ev_signal_init (&childev, childcb, SIGCHLD); |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
597 | ev_set_priority (&childev, EV_MAXPRI); |
570 | ev_signal_start (&childev); |
598 | ev_signal_start (&childev); |
|
|
599 | #endif |
571 | } |
600 | } |
572 | } |
601 | } |
573 | |
602 | |
574 | return ev_method; |
603 | return ev_method; |
575 | } |
604 | } |
… | |
… | |
997 | } |
1026 | } |
998 | |
1027 | |
999 | ev_stop ((W)w); |
1028 | ev_stop ((W)w); |
1000 | } |
1029 | } |
1001 | |
1030 | |
|
|
1031 | #ifndef SA_RESTART |
|
|
1032 | # define SA_RESTART 0 |
|
|
1033 | #endif |
|
|
1034 | |
1002 | void |
1035 | void |
1003 | ev_signal_start (struct ev_signal *w) |
1036 | ev_signal_start (struct ev_signal *w) |
1004 | { |
1037 | { |
1005 | if (ev_is_active (w)) |
1038 | if (ev_is_active (w)) |
1006 | return; |
1039 | return; |
… | |
… | |
1014 | if (!w->next) |
1047 | if (!w->next) |
1015 | { |
1048 | { |
1016 | struct sigaction sa; |
1049 | struct sigaction sa; |
1017 | sa.sa_handler = sighandler; |
1050 | sa.sa_handler = sighandler; |
1018 | sigfillset (&sa.sa_mask); |
1051 | sigfillset (&sa.sa_mask); |
1019 | sa.sa_flags = 0; |
1052 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1020 | sigaction (w->signum, &sa, 0); |
1053 | sigaction (w->signum, &sa, 0); |
1021 | } |
1054 | } |
1022 | } |
1055 | } |
1023 | |
1056 | |
1024 | void |
1057 | void |