A clone of btpd with my configuration changes.
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.

377 lines
9.1 KiB

  1. /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
  2. /*
  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. * 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. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include <sys/types.h>
  32. #ifdef HAVE_SYS_TIME_H
  33. #include <sys/time.h>
  34. #else
  35. #include <sys/_time.h>
  36. #endif
  37. #include <sys/queue.h>
  38. #include <sys/tree.h>
  39. #include <signal.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <errno.h>
  45. #ifdef CHECK_INVARIANTS
  46. #include <assert.h>
  47. #endif
  48. #include "event.h"
  49. #include "event-internal.h"
  50. #include "evsignal.h"
  51. #include "log.h"
  52. #ifndef howmany
  53. #define howmany(x, y) (((x)+((y)-1))/(y))
  54. #endif
  55. extern volatile sig_atomic_t evsignal_caught;
  56. struct selectop {
  57. int event_fds; /* Highest fd in fd set */
  58. int event_fdsz;
  59. fd_set *event_readset_in;
  60. fd_set *event_writeset_in;
  61. fd_set *event_readset_out;
  62. fd_set *event_writeset_out;
  63. struct event **event_r_by_fd;
  64. struct event **event_w_by_fd;
  65. sigset_t evsigmask;
  66. };
  67. void *select_init (void);
  68. int select_add (void *, struct event *);
  69. int select_del (void *, struct event *);
  70. int select_recalc (struct event_base *, void *, int);
  71. int select_dispatch (struct event_base *, void *, struct timeval *);
  72. void select_dealloc (void *);
  73. const struct eventop selectops = {
  74. "select",
  75. select_init,
  76. select_add,
  77. select_del,
  78. select_recalc,
  79. select_dispatch,
  80. select_dealloc
  81. };
  82. static int select_resize(struct selectop *sop, int fdsz);
  83. void *
  84. select_init(void)
  85. {
  86. struct selectop *sop;
  87. /* Disable select when this environment variable is set */
  88. if (getenv("EVENT_NOSELECT"))
  89. return (NULL);
  90. if (!(sop = calloc(1, sizeof(struct selectop))))
  91. return (NULL);
  92. select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
  93. evsignal_init(&sop->evsigmask);
  94. return (sop);
  95. }
  96. #ifdef CHECK_INVARIANTS
  97. static void
  98. check_selectop(struct selectop *sop)
  99. {
  100. int i;
  101. for (i=0;i<=sop->event_fds;++i) {
  102. if (FD_ISSET(i, sop->event_readset_in)) {
  103. assert(sop->event_r_by_fd[i]);
  104. assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
  105. assert(sop->event_r_by_fd[i]->ev_fd == i);
  106. } else {
  107. assert(! sop->event_r_by_fd[i]);
  108. }
  109. if (FD_ISSET(i, sop->event_writeset_in)) {
  110. assert(sop->event_w_by_fd[i]);
  111. assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
  112. assert(sop->event_w_by_fd[i]->ev_fd == i);
  113. } else {
  114. assert(! sop->event_w_by_fd[i]);
  115. }
  116. }
  117. }
  118. #else
  119. #define check_selectop(sop) do {;} while (0)
  120. #endif
  121. /*
  122. * Called with the highest fd that we know about. If it is 0, completely
  123. * recalculate everything.
  124. */
  125. int
  126. select_recalc(struct event_base *base, void *arg, int max)
  127. {
  128. struct selectop *sop = arg;
  129. check_selectop(sop);
  130. return (evsignal_recalc(&sop->evsigmask));
  131. }
  132. int
  133. select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
  134. {
  135. int res, i;
  136. struct selectop *sop = arg;
  137. check_selectop(sop);
  138. memcpy(sop->event_readset_out, sop->event_readset_in,
  139. sop->event_fdsz);
  140. memcpy(sop->event_writeset_out, sop->event_writeset_in,
  141. sop->event_fdsz);
  142. if (evsignal_deliver(&sop->evsigmask) == -1)
  143. return (-1);
  144. res = select(sop->event_fds + 1, sop->event_readset_out,
  145. sop->event_writeset_out, NULL, tv);
  146. check_selectop(sop);
  147. if (evsignal_recalc(&sop->evsigmask) == -1)
  148. return (-1);
  149. if (res == -1) {
  150. if (errno != EINTR) {
  151. event_warn("select");
  152. return (-1);
  153. }
  154. evsignal_process();
  155. return (0);
  156. } else if (evsignal_caught)
  157. evsignal_process();
  158. event_debug(("%s: select reports %d", __func__, res));
  159. check_selectop(sop);
  160. for (i = 0; i <= sop->event_fds; ++i) {
  161. struct event *r_ev = NULL, *w_ev = NULL;
  162. res = 0;
  163. if (FD_ISSET(i, sop->event_readset_out)) {
  164. r_ev = sop->event_r_by_fd[i];
  165. res |= EV_READ;
  166. }
  167. if (FD_ISSET(i, sop->event_writeset_out)) {
  168. w_ev = sop->event_w_by_fd[i];
  169. res |= EV_WRITE;
  170. }
  171. if (r_ev && (res & r_ev->ev_events)) {
  172. if (!(r_ev->ev_events & EV_PERSIST))
  173. event_del(r_ev);
  174. event_active(r_ev, res & r_ev->ev_events, 1);
  175. }
  176. if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
  177. if (!(w_ev->ev_events & EV_PERSIST))
  178. event_del(w_ev);
  179. event_active(w_ev, res & w_ev->ev_events, 1);
  180. }
  181. }
  182. check_selectop(sop);
  183. return (0);
  184. }
  185. static int
  186. select_resize(struct selectop *sop, int fdsz)
  187. {
  188. int n_events, n_events_old;
  189. fd_set *readset_in = NULL;
  190. fd_set *writeset_in = NULL;
  191. fd_set *readset_out = NULL;
  192. fd_set *writeset_out = NULL;
  193. struct event **r_by_fd = NULL;
  194. struct event **w_by_fd = NULL;
  195. n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
  196. n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
  197. if (sop->event_readset_in)
  198. check_selectop(sop);
  199. if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
  200. goto error;
  201. sop->event_readset_in = readset_in;
  202. if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
  203. goto error;
  204. sop->event_readset_out = readset_out;
  205. if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
  206. goto error;
  207. sop->event_writeset_in = writeset_in;
  208. if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
  209. goto error;
  210. sop->event_writeset_out = writeset_out;
  211. if ((r_by_fd = realloc(sop->event_r_by_fd,
  212. n_events*sizeof(struct event*))) == NULL)
  213. goto error;
  214. sop->event_r_by_fd = r_by_fd;
  215. if ((w_by_fd = realloc(sop->event_w_by_fd,
  216. n_events * sizeof(struct event*))) == NULL)
  217. goto error;
  218. sop->event_w_by_fd = w_by_fd;
  219. memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
  220. fdsz - sop->event_fdsz);
  221. memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
  222. fdsz - sop->event_fdsz);
  223. memset(sop->event_r_by_fd + n_events_old, 0,
  224. (n_events-n_events_old) * sizeof(struct event*));
  225. memset(sop->event_w_by_fd + n_events_old, 0,
  226. (n_events-n_events_old) * sizeof(struct event*));
  227. sop->event_fdsz = fdsz;
  228. check_selectop(sop);
  229. return (0);
  230. error:
  231. event_warn("malloc");
  232. return (-1);
  233. }
  234. int
  235. select_add(void *arg, struct event *ev)
  236. {
  237. struct selectop *sop = arg;
  238. if (ev->ev_events & EV_SIGNAL)
  239. return (evsignal_add(&sop->evsigmask, ev));
  240. check_selectop(sop);
  241. /*
  242. * Keep track of the highest fd, so that we can calculate the size
  243. * of the fd_sets for select(2)
  244. */
  245. if (sop->event_fds < ev->ev_fd) {
  246. int fdsz = sop->event_fdsz;
  247. if (fdsz < sizeof(fd_mask))
  248. fdsz = sizeof(fd_mask);
  249. while (fdsz <
  250. (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
  251. fdsz *= 2;
  252. if (fdsz != sop->event_fdsz) {
  253. if (select_resize(sop, fdsz)) {
  254. check_selectop(sop);
  255. return (-1);
  256. }
  257. }
  258. sop->event_fds = ev->ev_fd;
  259. }
  260. if (ev->ev_events & EV_READ) {
  261. FD_SET(ev->ev_fd, sop->event_readset_in);
  262. sop->event_r_by_fd[ev->ev_fd] = ev;
  263. }
  264. if (ev->ev_events & EV_WRITE) {
  265. FD_SET(ev->ev_fd, sop->event_writeset_in);
  266. sop->event_w_by_fd[ev->ev_fd] = ev;
  267. }
  268. check_selectop(sop);
  269. return (0);
  270. }
  271. /*
  272. * Nothing to be done here.
  273. */
  274. int
  275. select_del(void *arg, struct event *ev)
  276. {
  277. struct selectop *sop = arg;
  278. check_selectop(sop);
  279. if (ev->ev_events & EV_SIGNAL)
  280. return (evsignal_del(&sop->evsigmask, ev));
  281. if (sop->event_fds < ev->ev_fd) {
  282. check_selectop(sop);
  283. return (0);
  284. }
  285. if (ev->ev_events & EV_READ) {
  286. FD_CLR(ev->ev_fd, sop->event_readset_in);
  287. sop->event_r_by_fd[ev->ev_fd] = NULL;
  288. }
  289. if (ev->ev_events & EV_WRITE) {
  290. FD_CLR(ev->ev_fd, sop->event_writeset_in);
  291. sop->event_w_by_fd[ev->ev_fd] = NULL;
  292. }
  293. check_selectop(sop);
  294. return (0);
  295. }
  296. void
  297. select_dealloc(void *arg)
  298. {
  299. struct selectop *sop = arg;
  300. if (sop->event_readset_in)
  301. free(sop->event_readset_in);
  302. if (sop->event_writeset_in)
  303. free(sop->event_writeset_in);
  304. if (sop->event_readset_out)
  305. free(sop->event_readset_out);
  306. if (sop->event_writeset_out)
  307. free(sop->event_writeset_out);
  308. if (sop->event_r_by_fd)
  309. free(sop->event_r_by_fd);
  310. if (sop->event_w_by_fd)
  311. free(sop->event_w_by_fd);
  312. memset(sop, 0, sizeof(struct selectop));
  313. free(sop);
  314. }