A clone of btpd with my configuration changes.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

370 satır
8.3 KiB

  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/_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 "evsignal.h"
  51. #include "log.h"
  52. extern volatile sig_atomic_t evsignal_caught;
  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. sigset_t evsigmask;
  67. };
  68. void *epoll_init (void);
  69. int epoll_add (void *, struct event *);
  70. int epoll_del (void *, struct event *);
  71. int epoll_recalc (struct event_base *, void *, int);
  72. int epoll_dispatch (struct event_base *, void *, struct timeval *);
  73. void epoll_dealloc (void *);
  74. struct eventop epollops = {
  75. "epoll",
  76. epoll_init,
  77. epoll_add,
  78. epoll_del,
  79. epoll_recalc,
  80. epoll_dispatch,
  81. epoll_dealloc
  82. };
  83. #ifdef HAVE_SETFD
  84. #define FD_CLOSEONEXEC(x) do { \
  85. if (fcntl(x, F_SETFD, 1) == -1) \
  86. event_warn("fcntl(%d, F_SETFD)", x); \
  87. } while (0)
  88. #else
  89. #define FD_CLOSEONEXEC(x)
  90. #endif
  91. #define NEVENT 32000
  92. void *
  93. epoll_init(void)
  94. {
  95. int epfd, nfiles = NEVENT;
  96. struct rlimit rl;
  97. struct epollop *epollop;
  98. /* Disable epollueue when this environment variable is set */
  99. if (getenv("EVENT_NOEPOLL"))
  100. return (NULL);
  101. if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
  102. rl.rlim_cur != RLIM_INFINITY) {
  103. /*
  104. * Solaris is somewhat retarded - it's important to drop
  105. * backwards compatibility when making changes. So, don't
  106. * dare to put rl.rlim_cur here.
  107. */
  108. nfiles = rl.rlim_cur - 1;
  109. }
  110. /* Initalize the kernel queue */
  111. if ((epfd = epoll_create(nfiles)) == -1) {
  112. event_warn("epoll_create");
  113. return (NULL);
  114. }
  115. FD_CLOSEONEXEC(epfd);
  116. if (!(epollop = calloc(1, sizeof(struct epollop))))
  117. return (NULL);
  118. epollop->epfd = epfd;
  119. /* Initalize fields */
  120. epollop->events = malloc(nfiles * sizeof(struct epoll_event));
  121. if (epollop->events == NULL) {
  122. free(epollop);
  123. return (NULL);
  124. }
  125. epollop->nevents = nfiles;
  126. epollop->fds = calloc(nfiles, sizeof(struct evepoll));
  127. if (epollop->fds == NULL) {
  128. free(epollop->events);
  129. free(epollop);
  130. return (NULL);
  131. }
  132. epollop->nfds = nfiles;
  133. evsignal_init(&epollop->evsigmask);
  134. return (epollop);
  135. }
  136. int
  137. epoll_recalc(struct event_base *base, void *arg, int max)
  138. {
  139. struct epollop *epollop = arg;
  140. if (max > epollop->nfds) {
  141. struct evepoll *fds;
  142. int nfds;
  143. nfds = epollop->nfds;
  144. while (nfds < max)
  145. nfds <<= 1;
  146. fds = realloc(epollop->fds, nfds * sizeof(struct evepoll));
  147. if (fds == NULL) {
  148. event_warn("realloc");
  149. return (-1);
  150. }
  151. epollop->fds = fds;
  152. memset(fds + epollop->nfds, 0,
  153. (nfds - epollop->nfds) * sizeof(struct evepoll));
  154. epollop->nfds = nfds;
  155. }
  156. return (evsignal_recalc(&epollop->evsigmask));
  157. }
  158. int
  159. epoll_dispatch(struct event_base *base, void *arg, struct timeval *tv)
  160. {
  161. struct epollop *epollop = arg;
  162. struct epoll_event *events = epollop->events;
  163. struct evepoll *evep;
  164. int i, res, timeout;
  165. if (evsignal_deliver(&epollop->evsigmask) == -1)
  166. return (-1);
  167. timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
  168. res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
  169. if (evsignal_recalc(&epollop->evsigmask) == -1)
  170. return (-1);
  171. if (res == -1) {
  172. if (errno != EINTR) {
  173. event_warn("epoll_wait");
  174. return (-1);
  175. }
  176. evsignal_process();
  177. return (0);
  178. } else if (evsignal_caught)
  179. evsignal_process();
  180. event_debug(("%s: epoll_wait reports %d", __func__, res));
  181. for (i = 0; i < res; i++) {
  182. int which = 0;
  183. int what = events[i].events;
  184. struct event *evread = NULL, *evwrite = NULL;
  185. evep = (struct evepoll *)events[i].data.ptr;
  186. if (what & EPOLLHUP)
  187. what |= EPOLLIN | EPOLLOUT;
  188. else if (what & EPOLLERR)
  189. what |= EPOLLIN | EPOLLOUT;
  190. if (what & EPOLLIN) {
  191. evread = evep->evread;
  192. which |= EV_READ;
  193. }
  194. if (what & EPOLLOUT) {
  195. evwrite = evep->evwrite;
  196. which |= EV_WRITE;
  197. }
  198. if (!which)
  199. continue;
  200. if (evread != NULL && !(evread->ev_events & EV_PERSIST))
  201. event_del(evread);
  202. if (evwrite != NULL && evwrite != evread &&
  203. !(evwrite->ev_events & EV_PERSIST))
  204. event_del(evwrite);
  205. if (evread != NULL)
  206. event_active(evread, EV_READ, 1);
  207. if (evwrite != NULL)
  208. event_active(evwrite, EV_WRITE, 1);
  209. }
  210. return (0);
  211. }
  212. int
  213. epoll_add(void *arg, struct event *ev)
  214. {
  215. struct epollop *epollop = arg;
  216. struct epoll_event epev = {0, {0}};
  217. struct evepoll *evep;
  218. int fd, op, events;
  219. if (ev->ev_events & EV_SIGNAL)
  220. return (evsignal_add(&epollop->evsigmask, ev));
  221. fd = ev->ev_fd;
  222. if (fd >= epollop->nfds) {
  223. /* Extent the file descriptor array as necessary */
  224. if (epoll_recalc(ev->ev_base, epollop, fd) == -1)
  225. return (-1);
  226. }
  227. evep = &epollop->fds[fd];
  228. op = EPOLL_CTL_ADD;
  229. events = 0;
  230. if (evep->evread != NULL) {
  231. events |= EPOLLIN;
  232. op = EPOLL_CTL_MOD;
  233. }
  234. if (evep->evwrite != NULL) {
  235. events |= EPOLLOUT;
  236. op = EPOLL_CTL_MOD;
  237. }
  238. if (ev->ev_events & EV_READ)
  239. events |= EPOLLIN;
  240. if (ev->ev_events & EV_WRITE)
  241. events |= EPOLLOUT;
  242. epev.data.ptr = evep;
  243. epev.events = events;
  244. if (epoll_ctl(epollop->epfd, op, ev->ev_fd, &epev) == -1)
  245. return (-1);
  246. /* Update events responsible */
  247. if (ev->ev_events & EV_READ)
  248. evep->evread = ev;
  249. if (ev->ev_events & EV_WRITE)
  250. evep->evwrite = ev;
  251. return (0);
  252. }
  253. int
  254. epoll_del(void *arg, struct event *ev)
  255. {
  256. struct epollop *epollop = arg;
  257. struct epoll_event epev = {0, {0}};
  258. struct evepoll *evep;
  259. int fd, events, op;
  260. int needwritedelete = 1, needreaddelete = 1;
  261. if (ev->ev_events & EV_SIGNAL)
  262. return (evsignal_del(&epollop->evsigmask, ev));
  263. fd = ev->ev_fd;
  264. if (fd >= epollop->nfds)
  265. return (0);
  266. evep = &epollop->fds[fd];
  267. op = EPOLL_CTL_DEL;
  268. events = 0;
  269. if (ev->ev_events & EV_READ)
  270. events |= EPOLLIN;
  271. if (ev->ev_events & EV_WRITE)
  272. events |= EPOLLOUT;
  273. if ((events & (EPOLLIN|EPOLLOUT)) != (EPOLLIN|EPOLLOUT)) {
  274. if ((events & EPOLLIN) && evep->evwrite != NULL) {
  275. needwritedelete = 0;
  276. events = EPOLLOUT;
  277. op = EPOLL_CTL_MOD;
  278. } else if ((events & EPOLLOUT) && evep->evread != NULL) {
  279. needreaddelete = 0;
  280. events = EPOLLIN;
  281. op = EPOLL_CTL_MOD;
  282. }
  283. }
  284. epev.events = events;
  285. epev.data.ptr = evep;
  286. if (needreaddelete)
  287. evep->evread = NULL;
  288. if (needwritedelete)
  289. evep->evwrite = NULL;
  290. if (epoll_ctl(epollop->epfd, op, fd, &epev) == -1)
  291. return (-1);
  292. return (0);
  293. }
  294. void
  295. epoll_dealloc(void *arg)
  296. {
  297. struct epollop *epollop = arg;
  298. if (epollop->fds)
  299. free(epollop->fds);
  300. if (epollop->events)
  301. free(epollop->events);
  302. if (epollop->epfd >= 0)
  303. close(epollop->epfd);
  304. memset(epollop, 0, sizeof(struct epollop));
  305. free(epollop);
  306. }