epoll.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright 2000-2003 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <stdint.h>
  31. #include <sys/types.h>
  32. #include <sys/resource.h>
  33. #ifdef HAVE_SYS_TIME_H
  34. #include <sys/time.h>
  35. #else
  36. #include <sys/_libevent_time.h>
  37. #endif
  38. #include <sys/queue.h>
  39. #include <sys/epoll.h>
  40. #include <signal.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <errno.h>
  46. #ifdef HAVE_FCNTL_H
  47. #include <fcntl.h>
  48. #endif
  49. #include "event.h"
  50. #include "event-internal.h"
  51. #include "evsignal.h"
  52. #include "log.h"
  53. /* due to limitations in the epoll interface, we need to keep track of
  54. * all file descriptors outself.
  55. */
  56. struct evepoll {
  57. struct event *evread;
  58. struct event *evwrite;
  59. };
  60. struct epollop {
  61. struct evepoll *fds;
  62. int nfds;
  63. struct epoll_event *events;
  64. int nevents;
  65. int epfd;
  66. };
  67. static void *epoll_init (struct event_base *);
  68. static int epoll_add (void *, struct event *);
  69. static int epoll_del (void *, struct event *);
  70. static int epoll_dispatch (struct event_base *, void *, struct timeval *);
  71. static void epoll_dealloc (struct event_base *, void *);
  72. const struct eventop epollops = {
  73. "epoll",
  74. epoll_init,
  75. epoll_add,
  76. epoll_del,
  77. epoll_dispatch,
  78. epoll_dealloc,
  79. 1 /* need reinit */
  80. };
  81. #ifdef HAVE_SETFD
  82. #define FD_CLOSEONEXEC(x) do { \
  83. if (fcntl(x, F_SETFD, 1) == -1) \
  84. event_warn("fcntl(%d, F_SETFD)", x); \
  85. } while (0)
  86. #else
  87. #define FD_CLOSEONEXEC(x)
  88. #endif
  89. /* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
  90. * values bigger than (LONG_MAX - 999ULL)/HZ. HZ in the wild can be
  91. * as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
  92. * largest number of msec we can support here is 2147482. Let's
  93. * round that down by 47 seconds.
  94. */
  95. #define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
  96. #define INITIAL_NFILES 32
  97. #define INITIAL_NEVENTS 32
  98. #define MAX_NEVENTS 4096
  99. static void *
  100. epoll_init(struct event_base *base)
  101. {
  102. int epfd;
  103. struct epollop *epollop;
  104. /* Disable epollueue when this environment variable is set */
  105. if (evutil_getenv("EVENT_NOEPOLL"))
  106. return (NULL);
  107. /* Initalize the kernel queue */
  108. if ((epfd = epoll_create(32000)) == -1) {
  109. if (errno != ENOSYS)
  110. event_warn("epoll_create");
  111. return (NULL);
  112. }
  113. FD_CLOSEONEXEC(epfd);
  114. if (!(epollop = calloc(1, sizeof(struct epollop))))
  115. return (NULL);
  116. epollop->epfd = epfd;
  117. /* Initalize fields */
  118. epollop->events = malloc(INITIAL_NEVENTS * sizeof(struct epoll_event));
  119. if (epollop->events == NULL) {
  120. free(epollop);
  121. return (NULL);
  122. }
  123. epollop->nevents = INITIAL_NEVENTS;
  124. epollop->fds = calloc(INITIAL_NFILES, sizeof(struct evepoll));
  125. if (epollop->fds == NULL) {
  126. free(epollop->events);
  127. free(epollop);
  128. return (NULL);
  129. }
  130. epollop->nfds = INITIAL_NFILES;
  131. evsignal_init(base);
  132. return (epollop);
  133. }
  134. static int
  135. epoll_recalc(struct event_base *base, void *arg, int max)
  136. {
  137. struct epollop *epollop = arg;
  138. if (max >= epollop->nfds) {
  139. struct evepoll *fds;
  140. int nfds;
  141. nfds = epollop->nfds;
  142. while (nfds <= max)
  143. nfds <<= 1;
  144. fds = realloc(epollop->fds, nfds * sizeof(struct evepoll));
  145. if (fds == NULL) {
  146. event_warn("realloc");
  147. return (-1);
  148. }
  149. epollop->fds = fds;
  150. memset(fds + epollop->nfds, 0,
  151. (nfds - epollop->nfds) * sizeof(struct evepoll));
  152. epollop->nfds = nfds;
  153. }
  154. return (0);
  155. }
  156. static int
  157. epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
  158. {
  159. struct epollop *epollop = arg;
  160. struct epoll_event *events = epollop->events;
  161. struct evepoll *evep;
  162. int i, res, timeout = -1;
  163. if (tv != NULL)
  164. timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
  165. if (timeout > MAX_EPOLL_TIMEOUT_MSEC) {
  166. /* Linux kernels can wait forever if the timeout is too big;
  167. * see comment on MAX_EPOLL_TIMEOUT_MSEC. */
  168. timeout = MAX_EPOLL_TIMEOUT_MSEC;
  169. }
  170. res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
  171. if (res == -1) {
  172. if (errno != EINTR) {
  173. event_warn("epoll_wait");
  174. return (-1);
  175. }
  176. evsignal_process(base);
  177. return (0);
  178. } else if (base->sig.evsignal_caught) {
  179. evsignal_process(base);
  180. }
  181. event_debug(("%s: epoll_wait reports %d", __func__, res));
  182. for (i = 0; i < res; i++) {
  183. int what = events[i].events;
  184. struct event *evread = NULL, *evwrite = NULL;
  185. int fd = events[i].data.fd;
  186. if (fd < 0 || fd >= epollop->nfds)
  187. continue;
  188. evep = &epollop->fds[fd];
  189. if (what & (EPOLLHUP|EPOLLERR)) {
  190. evread = evep->evread;
  191. evwrite = evep->evwrite;
  192. } else {
  193. if (what & EPOLLIN) {
  194. evread = evep->evread;
  195. }
  196. if (what & EPOLLOUT) {
  197. evwrite = evep->evwrite;
  198. }
  199. }
  200. if (!(evread||evwrite))
  201. continue;
  202. if (evread != NULL)
  203. event_active(evread, EV_READ, 1);
  204. if (evwrite != NULL)
  205. event_active(evwrite, EV_WRITE, 1);
  206. }
  207. if (res == epollop->nevents && epollop->nevents < MAX_NEVENTS) {
  208. /* We used all of the event space this time. We should
  209. be ready for more events next time. */
  210. int new_nevents = epollop->nevents * 2;
  211. struct epoll_event *new_events;
  212. new_events = realloc(epollop->events,
  213. new_nevents * sizeof(struct epoll_event));
  214. if (new_events) {
  215. epollop->events = new_events;
  216. epollop->nevents = new_nevents;
  217. }
  218. }
  219. return (0);
  220. }
  221. static int
  222. epoll_add(void *arg, struct event *ev)
  223. {
  224. struct epollop *epollop = arg;
  225. struct epoll_event epev = {0, {0}};
  226. struct evepoll *evep;
  227. int fd, op, events;
  228. if (ev->ev_events & EV_SIGNAL)
  229. return (evsignal_add(ev));
  230. fd = ev->ev_fd;
  231. if (fd >= epollop->nfds) {
  232. /* Extent the file descriptor array as necessary */
  233. if (epoll_recalc(ev->ev_base, epollop, fd) == -1)
  234. return (-1);
  235. }
  236. evep = &epollop->fds[fd];
  237. op = EPOLL_CTL_ADD;
  238. events = 0;
  239. if (evep->evread != NULL) {
  240. events |= EPOLLIN;
  241. op = EPOLL_CTL_MOD;
  242. }
  243. if (evep->evwrite != NULL) {
  244. events |= EPOLLOUT;
  245. op = EPOLL_CTL_MOD;
  246. }
  247. if (ev->ev_events & EV_READ)
  248. events |= EPOLLIN;
  249. if (ev->ev_events & EV_WRITE)
  250. events |= EPOLLOUT;
  251. epev.data.fd = fd;
  252. epev.events = events;
  253. if (epoll_ctl(epollop->epfd, op, ev->ev_fd, &epev) == -1)
  254. return (-1);
  255. /* Update events responsible */
  256. if (ev->ev_events & EV_READ)
  257. evep->evread = ev;
  258. if (ev->ev_events & EV_WRITE)
  259. evep->evwrite = ev;
  260. return (0);
  261. }
  262. static int
  263. epoll_del(void *arg, struct event *ev)
  264. {
  265. struct epollop *epollop = arg;
  266. struct epoll_event epev = {0, {0}};
  267. struct evepoll *evep;
  268. int fd, events, op;
  269. int needwritedelete = 1, needreaddelete = 1;
  270. if (ev->ev_events & EV_SIGNAL)
  271. return (evsignal_del(ev));
  272. fd = ev->ev_fd;
  273. if (fd >= epollop->nfds)
  274. return (0);
  275. evep = &epollop->fds[fd];
  276. op = EPOLL_CTL_DEL;
  277. events = 0;
  278. if (ev->ev_events & EV_READ)
  279. events |= EPOLLIN;
  280. if (ev->ev_events & EV_WRITE)
  281. events |= EPOLLOUT;
  282. if ((events & (EPOLLIN|EPOLLOUT)) != (EPOLLIN|EPOLLOUT)) {
  283. if ((events & EPOLLIN) && evep->evwrite != NULL) {
  284. needwritedelete = 0;
  285. events = EPOLLOUT;
  286. op = EPOLL_CTL_MOD;
  287. } else if ((events & EPOLLOUT) && evep->evread != NULL) {
  288. needreaddelete = 0;
  289. events = EPOLLIN;
  290. op = EPOLL_CTL_MOD;
  291. }
  292. }
  293. epev.events = events;
  294. epev.data.fd = fd;
  295. if (needreaddelete)
  296. evep->evread = NULL;
  297. if (needwritedelete)
  298. evep->evwrite = NULL;
  299. if (epoll_ctl(epollop->epfd, op, fd, &epev) == -1)
  300. return (-1);
  301. return (0);
  302. }
  303. static void
  304. epoll_dealloc(struct event_base *base, void *arg)
  305. {
  306. struct epollop *epollop = arg;
  307. evsignal_dealloc(base);
  308. if (epollop->fds)
  309. free(epollop->fds);
  310. if (epollop->events)
  311. free(epollop->events);
  312. if (epollop->epfd >= 0)
  313. close(epollop->epfd);
  314. memset(epollop, 0, sizeof(struct epollop));
  315. free(epollop);
  316. }