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

Comparing libev/ev.c (file contents):
Revision 1.46 by root, Sat Nov 3 09:20:12 2007 UTC vs.
Revision 1.50 by root, Sat Nov 3 19:41:55 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#if EV_USE_CONFIG_H 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33#endif 33#endif
34 34
35#include <math.h> 35#include <math.h>
36#include <stdlib.h> 36#include <stdlib.h>
175 175
176/*****************************************************************************/ 176/*****************************************************************************/
177 177
178typedef struct 178typedef struct
179{ 179{
180 struct ev_io *head; 180 struct ev_watcher_list *head;
181 unsigned char events; 181 unsigned char events;
182 unsigned char reify; 182 unsigned char reify;
183} ANFD; 183} ANFD;
184 184
185static ANFD *anfds; 185static ANFD *anfds;
235fd_event (int fd, int events) 235fd_event (int fd, int events)
236{ 236{
237 ANFD *anfd = anfds + fd; 237 ANFD *anfd = anfds + fd;
238 struct ev_io *w; 238 struct ev_io *w;
239 239
240 for (w = anfd->head; w; w = w->next) 240 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
241 { 241 {
242 int ev = w->events & events; 242 int ev = w->events & events;
243 243
244 if (ev) 244 if (ev)
245 event ((W)w, ev); 245 event ((W)w, ev);
262 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
263 struct ev_io *w; 263 struct ev_io *w;
264 264
265 int events = 0; 265 int events = 0;
266 266
267 for (w = anfd->head; w; w = w->next) 267 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
268 events |= w->events; 268 events |= w->events;
269 269
270 anfd->reify = 0; 270 anfd->reify = 0;
271 271
272 if (anfd->events != events) 272 if (anfd->events != events)
295static void 295static void
296fd_kill (int fd) 296fd_kill (int fd)
297{ 297{
298 struct ev_io *w; 298 struct ev_io *w;
299 299
300 printf ("killing fd %d\n", fd);//D
301 while ((w = anfds [fd].head)) 300 while ((w = (struct ev_io *)anfds [fd].head))
302 { 301 {
303 ev_io_stop (w); 302 ev_io_stop (w);
304 event ((W)w, EV_ERROR | EV_READ | EV_WRITE); 303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
305 } 304 }
306} 305}
383 382
384/*****************************************************************************/ 383/*****************************************************************************/
385 384
386typedef struct 385typedef struct
387{ 386{
388 struct ev_signal *head; 387 struct ev_watcher_list *head;
389 sig_atomic_t volatile gotsig; 388 sig_atomic_t volatile gotsig;
390} ANSIG; 389} ANSIG;
391 390
392static ANSIG *signals; 391static ANSIG *signals;
393static int signalmax; 392static int signalmax;
413{ 412{
414 signals [signum - 1].gotsig = 1; 413 signals [signum - 1].gotsig = 1;
415 414
416 if (!gotsig) 415 if (!gotsig)
417 { 416 {
417 int old_errno = errno;
418 gotsig = 1; 418 gotsig = 1;
419 write (sigpipe [1], &signum, 1); 419 write (sigpipe [1], &signum, 1);
420 errno = old_errno;
420 } 421 }
421} 422}
422 423
423static void 424static void
424sigcb (struct ev_io *iow, int revents) 425sigcb (struct ev_io *iow, int revents)
425{ 426{
426 struct ev_signal *w; 427 struct ev_watcher_list *w;
427 int signum; 428 int signum;
428 429
429 read (sigpipe [0], &revents, 1); 430 read (sigpipe [0], &revents, 1);
430 gotsig = 0; 431 gotsig = 0;
431 432
476#ifndef WCONTINUED 477#ifndef WCONTINUED
477# define WCONTINUED 0 478# define WCONTINUED 0
478#endif 479#endif
479 480
480static void 481static void
482child_reap (struct ev_signal *sw, int chain, int pid, int status)
483{
484 struct ev_child *w;
485
486 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
487 if (w->pid == pid || !w->pid)
488 {
489 w->priority = sw->priority; /* need to do it *now* */
490 w->rpid = pid;
491 w->rstatus = status;
492 event ((W)w, EV_CHILD);
493 }
494}
495
496static void
481childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
482{ 498{
483 struct ev_child *w;
484 int pid, status; 499 int pid, status;
485 500
486 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
487 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 502 {
488 if (w->pid == pid || !w->pid) 503 /* make sure we are called again until all childs have been reaped */
489 {
490 w->rpid = pid;
491 w->rstatus = status;
492 event ((W)w, EV_CHILD); 504 event ((W)sw, EV_SIGNAL);
493 } 505
506 child_reap (sw, pid, pid, status);
507 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
508 }
494} 509}
495 510
496#endif 511#endif
497 512
498/*****************************************************************************/ 513/*****************************************************************************/
520ev_version_minor (void) 535ev_version_minor (void)
521{ 536{
522 return EV_VERSION_MINOR; 537 return EV_VERSION_MINOR;
523} 538}
524 539
525/* return true if we are running with elevated privileges and ignore env variables */ 540/* return true if we are running with elevated privileges and should ignore env variables */
526static int 541static int
527enable_secure () 542enable_secure ()
528{ 543{
544#ifdef WIN32
545 return 0;
546#else
529 return getuid () != geteuid () 547 return getuid () != geteuid ()
530 || getgid () != getegid (); 548 || getgid () != getegid ();
549#endif
531} 550}
532 551
533int ev_init (int methods) 552int ev_init (int methods)
534{ 553{
535 if (!ev_method) 554 if (!ev_method)
549 568
550 if (pipe (sigpipe)) 569 if (pipe (sigpipe))
551 return 0; 570 return 0;
552 571
553 if (methods == EVMETHOD_AUTO) 572 if (methods == EVMETHOD_AUTO)
554 if (!enable_secure () && getenv ("LIBEV_METHODS")) 573 if (!enable_secure () && getenv ("LIBEV_METHODS"))
555 methods = atoi (getenv ("LIBEV_METHODS")); 574 methods = atoi (getenv ("LIBEV_METHODS"));
556 else 575 else
557 methods = EVMETHOD_ANY; 576 methods = EVMETHOD_ANY;
558 577
559 ev_method = 0; 578 ev_method = 0;
560#if EV_USE_KQUEUE 579#if EV_USE_KQUEUE
561 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); 580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
562#endif 581#endif
571#endif 590#endif
572 591
573 if (ev_method) 592 if (ev_method)
574 { 593 {
575 ev_watcher_init (&sigev, sigcb); 594 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI);
576 siginit (); 596 siginit ();
577 597
578#ifndef WIN32 598#ifndef WIN32
579 ev_signal_init (&childev, childcb, SIGCHLD); 599 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI);
580 ev_signal_start (&childev); 601 ev_signal_start (&childev);
581#endif 602#endif
582 } 603 }
583 } 604 }
584 605
1008 } 1029 }
1009 1030
1010 ev_stop ((W)w); 1031 ev_stop ((W)w);
1011} 1032}
1012 1033
1034#ifndef SA_RESTART
1035# define SA_RESTART 0
1036#endif
1037
1013void 1038void
1014ev_signal_start (struct ev_signal *w) 1039ev_signal_start (struct ev_signal *w)
1015{ 1040{
1016 if (ev_is_active (w)) 1041 if (ev_is_active (w))
1017 return; 1042 return;
1025 if (!w->next) 1050 if (!w->next)
1026 { 1051 {
1027 struct sigaction sa; 1052 struct sigaction sa;
1028 sa.sa_handler = sighandler; 1053 sa.sa_handler = sighandler;
1029 sigfillset (&sa.sa_mask); 1054 sigfillset (&sa.sa_mask);
1030 sa.sa_flags = 0; 1055 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1031 sigaction (w->signum, &sa, 0); 1056 sigaction (w->signum, &sa, 0);
1032 } 1057 }
1033} 1058}
1034 1059
1035void 1060void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines