select.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
  2. /*
  3. * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
  4. * Copyright 2007-2012 Niels Provos and Nick Mathewson
  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. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "event2/event-config.h"
  29. #include "evconfig-private.h"
  30. #ifdef EVENT__HAVE_SELECT
  31. #ifdef __APPLE__
  32. /* Apple wants us to define this if we might ever pass more than
  33. * FD_SETSIZE bits to select(). */
  34. #define _DARWIN_UNLIMITED_SELECT
  35. #endif
  36. #include <sys/types.h>
  37. #ifdef EVENT__HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #ifdef EVENT__HAVE_SYS_SELECT_H
  41. #include <sys/select.h>
  42. #endif
  43. #include <sys/queue.h>
  44. #include <signal.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include <errno.h>
  50. #include "event-internal.h"
  51. #include "evsignal-internal.h"
  52. #include "event2/thread.h"
  53. #include "evthread-internal.h"
  54. #include "log-internal.h"
  55. #include "evmap-internal.h"
  56. #ifndef EVENT__HAVE_FD_MASK
  57. /* This type is mandatory, but Android doesn't define it. */
  58. typedef unsigned long fd_mask;
  59. #endif
  60. #ifndef NFDBITS
  61. #define NFDBITS (sizeof(fd_mask)*8)
  62. #endif
  63. /* Divide positive x by y, rounding up. */
  64. #define DIV_ROUNDUP(x, y) (((x)+((y)-1))/(y))
  65. /* How many bytes to allocate for N fds? */
  66. #define SELECT_ALLOC_SIZE(n) \
  67. (DIV_ROUNDUP(n, NFDBITS) * sizeof(fd_mask))
  68. struct selectop {
  69. int event_fds; /* Highest fd in fd set */
  70. int event_fdsz;
  71. int resize_out_sets;
  72. fd_set *event_readset_in;
  73. fd_set *event_writeset_in;
  74. fd_set *event_readset_out;
  75. fd_set *event_writeset_out;
  76. };
  77. static void *select_init(struct event_base *);
  78. static int select_add(struct event_base *, int, short old, short events, void*);
  79. static int select_del(struct event_base *, int, short old, short events, void*);
  80. static int select_dispatch(struct event_base *, struct timeval *);
  81. static void select_dealloc(struct event_base *);
  82. const struct eventop selectops = {
  83. "select",
  84. select_init,
  85. select_add,
  86. select_del,
  87. select_dispatch,
  88. select_dealloc,
  89. 1, /* need_reinit. */
  90. EV_FEATURE_FDS,
  91. 0,
  92. };
  93. static int select_resize(struct selectop *sop, int fdsz);
  94. static void select_free_selectop(struct selectop *sop);
  95. static void *
  96. select_init(struct event_base *base)
  97. {
  98. struct selectop *sop;
  99. if (!(sop = mm_calloc(1, sizeof(struct selectop))))
  100. return (NULL);
  101. if (select_resize(sop, SELECT_ALLOC_SIZE(32 + 1))) {
  102. select_free_selectop(sop);
  103. return (NULL);
  104. }
  105. evsig_init_(base);
  106. evutil_weakrand_seed_(&base->weakrand_seed, 0);
  107. return (sop);
  108. }
  109. #ifdef CHECK_INVARIANTS
  110. static void
  111. check_selectop(struct selectop *sop)
  112. {
  113. /* nothing to be done here */
  114. }
  115. #else
  116. #define check_selectop(sop) do { (void) sop; } while (0)
  117. #endif
  118. static int
  119. select_dispatch(struct event_base *base, struct timeval *tv)
  120. {
  121. int res=0, i, j, nfds;
  122. struct selectop *sop = base->evbase;
  123. check_selectop(sop);
  124. if (sop->resize_out_sets) {
  125. fd_set *readset_out=NULL, *writeset_out=NULL;
  126. size_t sz = sop->event_fdsz;
  127. if (!(readset_out = mm_realloc(sop->event_readset_out, sz)))
  128. return (-1);
  129. sop->event_readset_out = readset_out;
  130. if (!(writeset_out = mm_realloc(sop->event_writeset_out, sz))) {
  131. /* We don't free readset_out here, since it was
  132. * already successfully reallocated. The next time
  133. * we call select_dispatch, the realloc will be a
  134. * no-op. */
  135. return (-1);
  136. }
  137. sop->event_writeset_out = writeset_out;
  138. sop->resize_out_sets = 0;
  139. }
  140. memcpy(sop->event_readset_out, sop->event_readset_in,
  141. sop->event_fdsz);
  142. memcpy(sop->event_writeset_out, sop->event_writeset_in,
  143. sop->event_fdsz);
  144. nfds = sop->event_fds+1;
  145. EVBASE_RELEASE_LOCK(base, th_base_lock);
  146. res = select(nfds, sop->event_readset_out,
  147. sop->event_writeset_out, NULL, tv);
  148. EVBASE_ACQUIRE_LOCK(base, th_base_lock);
  149. check_selectop(sop);
  150. if (res == -1) {
  151. if (errno != EINTR) {
  152. event_warn("select");
  153. return (-1);
  154. }
  155. return (0);
  156. }
  157. event_debug(("%s: select reports %d", __func__, res));
  158. check_selectop(sop);
  159. i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
  160. for (j = 0; j < nfds; ++j) {
  161. if (++i >= nfds)
  162. i = 0;
  163. res = 0;
  164. if (FD_ISSET(i, sop->event_readset_out))
  165. res |= EV_READ;
  166. if (FD_ISSET(i, sop->event_writeset_out))
  167. res |= EV_WRITE;
  168. if (res == 0)
  169. continue;
  170. evmap_io_active_(base, i, res);
  171. }
  172. check_selectop(sop);
  173. return (0);
  174. }
  175. static int
  176. select_resize(struct selectop *sop, int fdsz)
  177. {
  178. fd_set *readset_in = NULL;
  179. fd_set *writeset_in = NULL;
  180. if (sop->event_readset_in)
  181. check_selectop(sop);
  182. if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL)
  183. goto error;
  184. sop->event_readset_in = readset_in;
  185. if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) {
  186. /* Note that this will leave event_readset_in expanded.
  187. * That's okay; we wouldn't want to free it, since that would
  188. * change the semantics of select_resize from "expand the
  189. * readset_in and writeset_in, or return -1" to "expand the
  190. * *set_in members, or trash them and return -1."
  191. */
  192. goto error;
  193. }
  194. sop->event_writeset_in = writeset_in;
  195. sop->resize_out_sets = 1;
  196. memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
  197. fdsz - sop->event_fdsz);
  198. memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
  199. fdsz - sop->event_fdsz);
  200. sop->event_fdsz = fdsz;
  201. check_selectop(sop);
  202. return (0);
  203. error:
  204. event_warn("malloc");
  205. return (-1);
  206. }
  207. static int
  208. select_add(struct event_base *base, int fd, short old, short events, void *p)
  209. {
  210. struct selectop *sop = base->evbase;
  211. (void) p;
  212. EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
  213. check_selectop(sop);
  214. /*
  215. * Keep track of the highest fd, so that we can calculate the size
  216. * of the fd_sets for select(2)
  217. */
  218. if (sop->event_fds < fd) {
  219. int fdsz = sop->event_fdsz;
  220. if (fdsz < (int)sizeof(fd_mask))
  221. fdsz = (int)sizeof(fd_mask);
  222. /* In theory we should worry about overflow here. In
  223. * reality, though, the highest fd on a unixy system will
  224. * not overflow here. XXXX */
  225. while (fdsz < (int) SELECT_ALLOC_SIZE(fd + 1))
  226. fdsz *= 2;
  227. if (fdsz != sop->event_fdsz) {
  228. if (select_resize(sop, fdsz)) {
  229. check_selectop(sop);
  230. return (-1);
  231. }
  232. }
  233. sop->event_fds = fd;
  234. }
  235. if (events & EV_READ)
  236. FD_SET(fd, sop->event_readset_in);
  237. if (events & EV_WRITE)
  238. FD_SET(fd, sop->event_writeset_in);
  239. check_selectop(sop);
  240. return (0);
  241. }
  242. /*
  243. * Nothing to be done here.
  244. */
  245. static int
  246. select_del(struct event_base *base, int fd, short old, short events, void *p)
  247. {
  248. struct selectop *sop = base->evbase;
  249. (void)p;
  250. EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
  251. check_selectop(sop);
  252. if (sop->event_fds < fd) {
  253. check_selectop(sop);
  254. return (0);
  255. }
  256. if (events & EV_READ)
  257. FD_CLR(fd, sop->event_readset_in);
  258. if (events & EV_WRITE)
  259. FD_CLR(fd, sop->event_writeset_in);
  260. check_selectop(sop);
  261. return (0);
  262. }
  263. static void
  264. select_free_selectop(struct selectop *sop)
  265. {
  266. if (sop->event_readset_in)
  267. mm_free(sop->event_readset_in);
  268. if (sop->event_writeset_in)
  269. mm_free(sop->event_writeset_in);
  270. if (sop->event_readset_out)
  271. mm_free(sop->event_readset_out);
  272. if (sop->event_writeset_out)
  273. mm_free(sop->event_writeset_out);
  274. memset(sop, 0, sizeof(struct selectop));
  275. mm_free(sop);
  276. }
  277. static void
  278. select_dealloc(struct event_base *base)
  279. {
  280. evsig_dealloc_(base);
  281. select_free_selectop(base->evbase);
  282. }
  283. #endif /* EVENT__HAVE_SELECT */