… | |
… | |
56 | |
56 | |
57 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
58 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
59 | #endif |
59 | #endif |
60 | |
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
61 | #ifndef EV_USE_EPOLL |
65 | #ifndef EV_USE_EPOLL |
62 | # define EV_USE_EPOLL 0 |
66 | # define EV_USE_EPOLL 0 |
63 | #endif |
67 | #endif |
64 | |
68 | |
65 | #ifndef EV_USE_REALTIME |
69 | #ifndef EV_USE_REALTIME |
… | |
… | |
277 | ++fdchangecnt; |
281 | ++fdchangecnt; |
278 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
282 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
279 | fdchanges [fdchangecnt - 1] = fd; |
283 | fdchanges [fdchangecnt - 1] = fd; |
280 | } |
284 | } |
281 | |
285 | |
|
|
286 | static void |
|
|
287 | fd_kill (int fd) |
|
|
288 | { |
|
|
289 | struct ev_io *w; |
|
|
290 | |
|
|
291 | printf ("killing fd %d\n", fd);//D |
|
|
292 | while ((w = anfds [fd].head)) |
|
|
293 | { |
|
|
294 | ev_io_stop (w); |
|
|
295 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
296 | } |
|
|
297 | } |
|
|
298 | |
282 | /* called on EBADF to verify fds */ |
299 | /* called on EBADF to verify fds */ |
283 | static void |
300 | static void |
284 | fd_recheck (void) |
301 | fd_ebadf (void) |
285 | { |
302 | { |
286 | int fd; |
303 | int fd; |
287 | |
304 | |
288 | for (fd = 0; fd < anfdmax; ++fd) |
305 | for (fd = 0; fd < anfdmax; ++fd) |
289 | if (anfds [fd].events) |
306 | if (anfds [fd].events) |
290 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
307 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
291 | while (anfds [fd].head) |
308 | fd_kill (fd); |
|
|
309 | } |
|
|
310 | |
|
|
311 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
312 | static void |
|
|
313 | fd_enomem (void) |
|
|
314 | { |
|
|
315 | int fd = anfdmax; |
|
|
316 | |
|
|
317 | while (fd--) |
|
|
318 | if (anfds [fd].events) |
292 | { |
319 | { |
293 | ev_io_stop (anfds [fd].head); |
320 | close (fd); |
294 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
321 | fd_kill (fd); |
|
|
322 | return; |
295 | } |
323 | } |
296 | } |
324 | } |
297 | |
325 | |
298 | /*****************************************************************************/ |
326 | /*****************************************************************************/ |
299 | |
327 | |
300 | static struct ev_timer **timers; |
328 | static struct ev_timer **timers; |
… | |
… | |
454 | /*****************************************************************************/ |
482 | /*****************************************************************************/ |
455 | |
483 | |
456 | #if EV_USE_EPOLL |
484 | #if EV_USE_EPOLL |
457 | # include "ev_epoll.c" |
485 | # include "ev_epoll.c" |
458 | #endif |
486 | #endif |
|
|
487 | #if EV_USE_POLL |
|
|
488 | # include "ev_poll.c" |
|
|
489 | #endif |
459 | #if EV_USE_SELECT |
490 | #if EV_USE_SELECT |
460 | # include "ev_select.c" |
491 | # include "ev_select.c" |
461 | #endif |
492 | #endif |
462 | |
493 | |
463 | int |
494 | int |
… | |
… | |
470 | ev_version_minor (void) |
501 | ev_version_minor (void) |
471 | { |
502 | { |
472 | return EV_VERSION_MINOR; |
503 | return EV_VERSION_MINOR; |
473 | } |
504 | } |
474 | |
505 | |
|
|
506 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
507 | static int |
|
|
508 | enable_secure () |
|
|
509 | { |
|
|
510 | return getuid () != geteuid () |
|
|
511 | || getgid () != getegid (); |
|
|
512 | } |
|
|
513 | |
475 | int ev_init (int flags) |
514 | int ev_init (int methods) |
476 | { |
515 | { |
477 | if (!ev_method) |
516 | if (!ev_method) |
478 | { |
517 | { |
479 | #if EV_USE_MONOTONIC |
518 | #if EV_USE_MONOTONIC |
480 | { |
519 | { |
… | |
… | |
490 | diff = ev_now - now; |
529 | diff = ev_now - now; |
491 | |
530 | |
492 | if (pipe (sigpipe)) |
531 | if (pipe (sigpipe)) |
493 | return 0; |
532 | return 0; |
494 | |
533 | |
|
|
534 | if (methods == EVMETHOD_AUTO) |
|
|
535 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
537 | else |
495 | ev_method = EVMETHOD_NONE; |
538 | methods = EVMETHOD_ANY; |
|
|
539 | |
|
|
540 | ev_method = 0; |
496 | #if EV_USE_EPOLL |
541 | #if EV_USE_EPOLL |
497 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
543 | #endif |
|
|
544 | #if EV_USE_POLL |
|
|
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
498 | #endif |
546 | #endif |
499 | #if EV_USE_SELECT |
547 | #if EV_USE_SELECT |
500 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
548 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
501 | #endif |
549 | #endif |
502 | |
550 | |
503 | if (ev_method) |
551 | if (ev_method) |
504 | { |
552 | { |
505 | ev_watcher_init (&sigev, sigcb); |
553 | ev_watcher_init (&sigev, sigcb); |