Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.3 KiB

14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de>
  3. * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <sys/queue.h>
  32. #include <sys/event.h>
  33. #include <string.h>
  34. #include <errno.h>
  35. static void
  36. kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
  37. {
  38. struct kevent *ke;
  39. ++kqueue_changecnt;
  40. array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2);
  41. EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0);
  42. }
  43. #ifndef NOTE_EOF
  44. # define NOTE_EOF 0
  45. #endif
  46. static void
  47. kqueue_modify (EV_P_ int fd, int oev, int nev)
  48. {
  49. if (oev != nev)
  50. {
  51. if (oev & EV_READ)
  52. kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0);
  53. if (oev & EV_WRITE)
  54. kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0);
  55. }
  56. /* to detect close/reopen reliably, we have to re-add */
  57. /* event requests even when oev == nev */
  58. if (nev & EV_READ)
  59. kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD, NOTE_EOF);
  60. if (nev & EV_WRITE)
  61. kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
  62. }
  63. static void
  64. kqueue_poll (EV_P_ ev_tstamp timeout)
  65. {
  66. int res, i;
  67. struct timespec ts;
  68. /* need to resize so there is enough space for errors */
  69. if (kqueue_changecnt > kqueue_eventmax)
  70. {
  71. ev_free (kqueue_events);
  72. kqueue_eventmax = array_roundsize (struct kevent, kqueue_changecnt);
  73. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  74. }
  75. ts.tv_sec = (time_t)timeout;
  76. ts.tv_nsec = (long)((timeout - (ev_tstamp)ts.tv_sec) * 1e9);
  77. res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
  78. kqueue_changecnt = 0;
  79. if (res < 0)
  80. {
  81. if (errno != EINTR)
  82. syserr ("(libev) kevent");
  83. return;
  84. }
  85. for (i = 0; i < res; ++i)
  86. {
  87. int fd = kqueue_events [i].ident;
  88. if (expect_false (kqueue_events [i].flags & EV_ERROR))
  89. {
  90. int err = kqueue_events [i].data;
  91. /*
  92. * errors that may happen
  93. * EBADF happens when the file discriptor has been
  94. * closed,
  95. * ENOENT when the file descriptor was closed and
  96. * then reopened.
  97. * EINVAL for some reasons not understood; EINVAL
  98. * should not be returned ever; but FreeBSD does :-\
  99. */
  100. /* we are only interested in errors for fds that we are interested in :) */
  101. if (anfds [fd].events)
  102. {
  103. if (err == ENOENT) /* resubmit changes on ENOENT */
  104. kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
  105. else if (err == EBADF) /* on EBADF, we re-check the fd */
  106. {
  107. if (fd_valid (fd))
  108. kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
  109. else
  110. fd_kill (EV_A_ fd);
  111. }
  112. else /* on all other errors, we error out on the fd */
  113. fd_kill (EV_A_ fd);
  114. }
  115. }
  116. else
  117. fd_event (
  118. EV_A_
  119. fd,
  120. kqueue_events [i].filter == EVFILT_READ ? EV_READ
  121. : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
  122. : 0
  123. );
  124. }
  125. if (expect_false (res == kqueue_eventmax))
  126. {
  127. ev_free (kqueue_events);
  128. kqueue_eventmax = array_roundsize (struct kevent, kqueue_eventmax << 1);
  129. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  130. }
  131. }
  132. static int
  133. kqueue_init (EV_P_ int flags)
  134. {
  135. struct kevent ch, ev;
  136. /* Initalize the kernel queue */
  137. if ((backend_fd = kqueue ()) < 0)
  138. return 0;
  139. fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
  140. /* Check for Mac OS X kqueue bug. */
  141. ch.ident = -1;
  142. ch.filter = EVFILT_READ;
  143. ch.flags = EV_ADD;
  144. /*
  145. * If kqueue works, then kevent will succeed, and it will
  146. * stick an error in ev. If kqueue is broken, then
  147. * kevent will fail.
  148. */
  149. if (kevent (backend_fd, &ch, 1, &ev, 1, 0) != 1
  150. || ev.ident != -1
  151. || ev.flags != EV_ERROR)
  152. {
  153. /* detected broken kqueue */
  154. close (backend_fd);
  155. return 0;
  156. }
  157. backend_fudge = 1e-3; /* needed to compensate for kevent returning early */
  158. backend_modify = kqueue_modify;
  159. backend_poll = kqueue_poll;
  160. kqueue_eventmax = 64; /* intiial number of events receivable per poll */
  161. kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
  162. kqueue_changes = 0;
  163. kqueue_changemax = 0;
  164. kqueue_changecnt = 0;
  165. return EVBACKEND_KQUEUE;
  166. }
  167. static void
  168. kqueue_destroy (EV_P)
  169. {
  170. close (backend_fd);
  171. ev_free (kqueue_events);
  172. ev_free (kqueue_changes);
  173. }
  174. static void
  175. kqueue_fork (EV_P)
  176. {
  177. close (backend_fd);
  178. while ((backend_fd = kqueue ()) < 0)
  179. syserr ("(libev) kqueue");
  180. fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
  181. /* re-register interest in fds */
  182. fd_rearm_all (EV_A);
  183. }