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