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

Comparing libev/ev.c (file contents):
Revision 1.44 by root, Fri Nov 2 20:59:14 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>
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
173 175
174/*****************************************************************************/ 176/*****************************************************************************/
175 177
176typedef struct 178typedef struct
177{ 179{
178 struct ev_io *head; 180 struct ev_watcher_list *head;
179 unsigned char events; 181 unsigned char events;
180 unsigned char reify; 182 unsigned char reify;
181} ANFD; 183} ANFD;
182 184
183static ANFD *anfds; 185static ANFD *anfds;
233fd_event (int fd, int events) 235fd_event (int fd, int events)
234{ 236{
235 ANFD *anfd = anfds + fd; 237 ANFD *anfd = anfds + fd;
236 struct ev_io *w; 238 struct ev_io *w;
237 239
238 for (w = anfd->head; w; w = w->next) 240 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
239 { 241 {
240 int ev = w->events & events; 242 int ev = w->events & events;
241 243
242 if (ev) 244 if (ev)
243 event ((W)w, ev); 245 event ((W)w, ev);
260 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
261 struct ev_io *w; 263 struct ev_io *w;
262 264
263 int events = 0; 265 int events = 0;
264 266
265 for (w = anfd->head; w; w = w->next) 267 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
266 events |= w->events; 268 events |= w->events;
267 269
268 anfd->reify = 0; 270 anfd->reify = 0;
269 271
270 if (anfd->events != events) 272 if (anfd->events != events)
293static void 295static void
294fd_kill (int fd) 296fd_kill (int fd)
295{ 297{
296 struct ev_io *w; 298 struct ev_io *w;
297 299
298 printf ("killing fd %d\n", fd);//D
299 while ((w = anfds [fd].head)) 300 while ((w = (struct ev_io *)anfds [fd].head))
300 { 301 {
301 ev_io_stop (w); 302 ev_io_stop (w);
302 event ((W)w, EV_ERROR | EV_READ | EV_WRITE); 303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
303 } 304 }
304} 305}
381 382
382/*****************************************************************************/ 383/*****************************************************************************/
383 384
384typedef struct 385typedef struct
385{ 386{
386 struct ev_signal *head; 387 struct ev_watcher_list *head;
387 sig_atomic_t volatile gotsig; 388 sig_atomic_t volatile gotsig;
388} ANSIG; 389} ANSIG;
389 390
390static ANSIG *signals; 391static ANSIG *signals;
391static int signalmax; 392static int signalmax;
411{ 412{
412 signals [signum - 1].gotsig = 1; 413 signals [signum - 1].gotsig = 1;
413 414
414 if (!gotsig) 415 if (!gotsig)
415 { 416 {
417 int old_errno = errno;
416 gotsig = 1; 418 gotsig = 1;
417 write (sigpipe [1], &signum, 1); 419 write (sigpipe [1], &signum, 1);
420 errno = old_errno;
418 } 421 }
419} 422}
420 423
421static void 424static void
422sigcb (struct ev_io *iow, int revents) 425sigcb (struct ev_io *iow, int revents)
423{ 426{
424 struct ev_signal *w; 427 struct ev_watcher_list *w;
425 int signum; 428 int signum;
426 429
427 read (sigpipe [0], &revents, 1); 430 read (sigpipe [0], &revents, 1);
428 gotsig = 0; 431 gotsig = 0;
429 432
438} 441}
439 442
440static void 443static void
441siginit (void) 444siginit (void)
442{ 445{
446#ifndef WIN32
443 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 447 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
444 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
445 449
446 /* rather than sort out wether we really need nb, set it */ 450 /* rather than sort out wether we really need nb, set it */
447 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 451 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
448 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
453#endif
449 454
450 ev_io_set (&sigev, sigpipe [0], EV_READ); 455 ev_io_set (&sigev, sigpipe [0], EV_READ);
451 ev_io_start (&sigev); 456 ev_io_start (&sigev);
452} 457}
453 458
465/*****************************************************************************/ 470/*****************************************************************************/
466 471
467static struct ev_child *childs [PID_HASHSIZE]; 472static struct ev_child *childs [PID_HASHSIZE];
468static struct ev_signal childev; 473static struct ev_signal childev;
469 474
475#ifndef WIN32
476
470#ifndef WCONTINUED 477#ifndef WCONTINUED
471# define WCONTINUED 0 478# define WCONTINUED 0
472#endif 479#endif
473 480
474static 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
475childcb (struct ev_signal *sw, int revents) 497childcb (struct ev_signal *sw, int revents)
476{ 498{
477 struct ev_child *w;
478 int pid, status; 499 int pid, status;
479 500
480 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
481 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 502 {
482 if (w->pid == pid || !w->pid) 503 /* make sure we are called again until all childs have been reaped */
483 {
484 w->status = status;
485 event ((W)w, EV_CHILD); 504 event ((W)sw, EV_SIGNAL);
486 } 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 }
487} 509}
510
511#endif
488 512
489/*****************************************************************************/ 513/*****************************************************************************/
490 514
491#if EV_USE_KQUEUE 515#if EV_USE_KQUEUE
492# include "ev_kqueue.c" 516# include "ev_kqueue.c"
511ev_version_minor (void) 535ev_version_minor (void)
512{ 536{
513 return EV_VERSION_MINOR; 537 return EV_VERSION_MINOR;
514} 538}
515 539
516/* 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 */
517static int 541static int
518enable_secure () 542enable_secure ()
519{ 543{
544#ifdef WIN32
545 return 0;
546#else
520 return getuid () != geteuid () 547 return getuid () != geteuid ()
521 || getgid () != getegid (); 548 || getgid () != getegid ();
549#endif
522} 550}
523 551
524int ev_init (int methods) 552int ev_init (int methods)
525{ 553{
526 if (!ev_method) 554 if (!ev_method)
540 568
541 if (pipe (sigpipe)) 569 if (pipe (sigpipe))
542 return 0; 570 return 0;
543 571
544 if (methods == EVMETHOD_AUTO) 572 if (methods == EVMETHOD_AUTO)
545 if (!enable_secure () && getenv ("LIBEV_METHODS")) 573 if (!enable_secure () && getenv ("LIBEV_METHODS"))
546 methods = atoi (getenv ("LIBEV_METHODS")); 574 methods = atoi (getenv ("LIBEV_METHODS"));
547 else 575 else
548 methods = EVMETHOD_ANY; 576 methods = EVMETHOD_ANY;
549 577
550 ev_method = 0; 578 ev_method = 0;
551#if EV_USE_KQUEUE 579#if EV_USE_KQUEUE
552 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); 580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
553#endif 581#endif
562#endif 590#endif
563 591
564 if (ev_method) 592 if (ev_method)
565 { 593 {
566 ev_watcher_init (&sigev, sigcb); 594 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI);
567 siginit (); 596 siginit ();
568 597
598#ifndef WIN32
569 ev_signal_init (&childev, childcb, SIGCHLD); 599 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI);
570 ev_signal_start (&childev); 601 ev_signal_start (&childev);
602#endif
571 } 603 }
572 } 604 }
573 605
574 return ev_method; 606 return ev_method;
575} 607}
997 } 1029 }
998 1030
999 ev_stop ((W)w); 1031 ev_stop ((W)w);
1000} 1032}
1001 1033
1034#ifndef SA_RESTART
1035# define SA_RESTART 0
1036#endif
1037
1002void 1038void
1003ev_signal_start (struct ev_signal *w) 1039ev_signal_start (struct ev_signal *w)
1004{ 1040{
1005 if (ev_is_active (w)) 1041 if (ev_is_active (w))
1006 return; 1042 return;
1014 if (!w->next) 1050 if (!w->next)
1015 { 1051 {
1016 struct sigaction sa; 1052 struct sigaction sa;
1017 sa.sa_handler = sighandler; 1053 sa.sa_handler = sighandler;
1018 sigfillset (&sa.sa_mask); 1054 sigfillset (&sa.sa_mask);
1019 sa.sa_flags = 0; 1055 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1020 sigaction (w->signum, &sa, 0); 1056 sigaction (w->signum, &sa, 0);
1021 } 1057 }
1022} 1058}
1023 1059
1024void 1060void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines