ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.42 by root, Fri Nov 2 20:05:05 2007 UTC vs.
Revision 1.49 by root, Sat Nov 3 16:16:58 2007 UTC

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
417static void 425static void
418sigcb (struct ev_io *iow, int revents) 426sigcb (struct ev_io *iow, int revents)
434} 442}
435 443
436static void 444static void
437siginit (void) 445siginit (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
463static struct ev_child *childs [PID_HASHSIZE]; 473static struct ev_child *childs [PID_HASHSIZE];
464static struct ev_signal childev; 474static 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
470static void 482static void
483child_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
498static void
471childcb (struct ev_signal *sw, int revents) 499childcb (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"
504ev_version_minor (void) 538ev_version_minor (void)
505{ 539{
506 return EV_VERSION_MINOR; 540 return EV_VERSION_MINOR;
507} 541}
508 542
509/* 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 */
510static int 544static int
511enable_secure () 545enable_secure ()
512{ 546{
547#ifdef WIN32
548 return 0;
549#else
513 return getuid () != geteuid () 550 return getuid () != geteuid ()
514 || getgid () != getegid (); 551 || getgid () != getegid ();
552#endif
515} 553}
516 554
517int ev_init (int methods) 555int ev_init (int methods)
518{ 556{
519 if (!ev_method) 557 if (!ev_method)
539 methods = atoi (getenv ("LIBEV_METHODS")); 577 methods = atoi (getenv ("LIBEV_METHODS"));
540 else 578 else
541 methods = EVMETHOD_ANY; 579 methods = EVMETHOD_ANY;
542 580
543 ev_method = 0; 581 ev_method = 0;
582#if EV_USE_KQUEUE
583 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
584#endif
544#if EV_USE_EPOLL 585#if EV_USE_EPOLL
545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 586 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
546#endif 587#endif
547#if EV_USE_POLL 588#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 589 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
552#endif 593#endif
553 594
554 if (ev_method) 595 if (ev_method)
555 { 596 {
556 ev_watcher_init (&sigev, sigcb); 597 ev_watcher_init (&sigev, sigcb);
598 ev_set_priority (&sigev, EV_MAXPRI);
557 siginit (); 599 siginit ();
558 600
601#ifndef WIN32
559 ev_signal_init (&childev, childcb, SIGCHLD); 602 ev_signal_init (&childev, childcb, SIGCHLD);
603 ev_set_priority (&childev, EV_MAXPRI);
560 ev_signal_start (&childev); 604 ev_signal_start (&childev);
605#endif
561 } 606 }
562 } 607 }
563 608
564 return ev_method; 609 return ev_method;
565} 610}
858} 903}
859 904
860static void 905static void
861ev_start (W w, int active) 906ev_start (W w, int active)
862{ 907{
908 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
909 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
910
863 w->active = active; 911 w->active = active;
864} 912}
865 913
866static void 914static void
867ev_stop (W w) 915ev_stop (W w)
984 } 1032 }
985 1033
986 ev_stop ((W)w); 1034 ev_stop ((W)w);
987} 1035}
988 1036
1037#ifndef SA_RESTART
1038# define SA_RESTART 0
1039#endif
1040
989void 1041void
990ev_signal_start (struct ev_signal *w) 1042ev_signal_start (struct ev_signal *w)
991{ 1043{
992 if (ev_is_active (w)) 1044 if (ev_is_active (w))
993 return; 1045 return;
1001 if (!w->next) 1053 if (!w->next)
1002 { 1054 {
1003 struct sigaction sa; 1055 struct sigaction sa;
1004 sa.sa_handler = sighandler; 1056 sa.sa_handler = sighandler;
1005 sigfillset (&sa.sa_mask); 1057 sigfillset (&sa.sa_mask);
1006 sa.sa_flags = 0; 1058 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1007 sigaction (w->signum, &sa, 0); 1059 sigaction (w->signum, &sa, 0);
1008 } 1060 }
1009} 1061}
1010 1062
1011void 1063void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines