… | |
… | |
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" |
… | |
… | |
511 | ev_version_minor (void) |
538 | ev_version_minor (void) |
512 | { |
539 | { |
513 | return EV_VERSION_MINOR; |
540 | return EV_VERSION_MINOR; |
514 | } |
541 | } |
515 | |
542 | |
516 | /* return true if we are running with elevated privileges and ignore env variables */ |
543 | /* return true if we are running with elevated privileges and should ignore env variables */ |
517 | static int |
544 | static int |
518 | enable_secure () |
545 | enable_secure () |
519 | { |
546 | { |
|
|
547 | #ifdef WIN32 |
|
|
548 | return 0; |
|
|
549 | #else |
520 | return getuid () != geteuid () |
550 | return getuid () != geteuid () |
521 | || getgid () != getegid (); |
551 | || getgid () != getegid (); |
|
|
552 | #endif |
522 | } |
553 | } |
523 | |
554 | |
524 | int ev_init (int methods) |
555 | int ev_init (int methods) |
525 | { |
556 | { |
526 | if (!ev_method) |
557 | if (!ev_method) |
… | |
… | |
562 | #endif |
593 | #endif |
563 | |
594 | |
564 | if (ev_method) |
595 | if (ev_method) |
565 | { |
596 | { |
566 | ev_watcher_init (&sigev, sigcb); |
597 | ev_watcher_init (&sigev, sigcb); |
|
|
598 | ev_set_priority (&sigev, EV_MAXPRI); |
567 | siginit (); |
599 | siginit (); |
568 | |
600 | |
|
|
601 | #ifndef WIN32 |
569 | ev_signal_init (&childev, childcb, SIGCHLD); |
602 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
603 | ev_set_priority (&childev, EV_MAXPRI); |
570 | ev_signal_start (&childev); |
604 | ev_signal_start (&childev); |
|
|
605 | #endif |
571 | } |
606 | } |
572 | } |
607 | } |
573 | |
608 | |
574 | return ev_method; |
609 | return ev_method; |
575 | } |
610 | } |
… | |
… | |
997 | } |
1032 | } |
998 | |
1033 | |
999 | ev_stop ((W)w); |
1034 | ev_stop ((W)w); |
1000 | } |
1035 | } |
1001 | |
1036 | |
|
|
1037 | #ifndef SA_RESTART |
|
|
1038 | # define SA_RESTART 0 |
|
|
1039 | #endif |
|
|
1040 | |
1002 | void |
1041 | void |
1003 | ev_signal_start (struct ev_signal *w) |
1042 | ev_signal_start (struct ev_signal *w) |
1004 | { |
1043 | { |
1005 | if (ev_is_active (w)) |
1044 | if (ev_is_active (w)) |
1006 | return; |
1045 | return; |
… | |
… | |
1014 | if (!w->next) |
1053 | if (!w->next) |
1015 | { |
1054 | { |
1016 | struct sigaction sa; |
1055 | struct sigaction sa; |
1017 | sa.sa_handler = sighandler; |
1056 | sa.sa_handler = sighandler; |
1018 | sigfillset (&sa.sa_mask); |
1057 | sigfillset (&sa.sa_mask); |
1019 | sa.sa_flags = 0; |
1058 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1020 | sigaction (w->signum, &sa, 0); |
1059 | sigaction (w->signum, &sa, 0); |
1021 | } |
1060 | } |
1022 | } |
1061 | } |
1023 | |
1062 | |
1024 | void |
1063 | void |