io.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. * Please read the file COPYING, README and AUTHORS for more information.
  7. *
  8. * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
  9. */
  10. #include "portab.h"
  11. /**
  12. * @file
  13. * I/O abstraction interface.
  14. */
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include "array.h"
  23. #include "io.h"
  24. #include "log.h"
  25. /* Enables extra debug messages in event add/delete/callback code. */
  26. /* #define DEBUG_IO */
  27. typedef struct {
  28. #ifdef PROTOTYPES
  29. void (*callback)(int, short);
  30. #else
  31. void (*callback)();
  32. #endif
  33. short what;
  34. } io_event;
  35. #define INIT_IOEVENT { NULL, -1, 0, NULL }
  36. #define IO_ERROR 4
  37. #define MAX_EVENTS 100
  38. #ifdef HAVE_EPOLL_CREATE
  39. # define IO_USE_EPOLL 1
  40. # ifdef HAVE_SELECT
  41. # define IO_USE_SELECT 1
  42. # endif
  43. #else
  44. # ifdef HAVE_KQUEUE
  45. # define IO_USE_KQUEUE 1
  46. # else
  47. # ifdef HAVE_SYS_DEVPOLL_H
  48. # define IO_USE_DEVPOLL 1
  49. # else
  50. # if defined(HAVE_POLL) && defined(HAVE_POLL_H)
  51. # define IO_USE_POLL 1
  52. # else
  53. # ifdef HAVE_SELECT
  54. # define IO_USE_SELECT 1
  55. # else
  56. # error "no IO API available!?"
  57. # endif /* HAVE_SELECT */
  58. # endif /* HAVE_POLL */
  59. # endif /* HAVE_SYS_DEVPOLL_H */
  60. # endif /* HAVE_KQUEUE */
  61. #endif /* HAVE_EPOLL_CREATE */
  62. static bool library_initialized = false;
  63. #ifdef IO_USE_EPOLL
  64. #include <sys/epoll.h>
  65. static int io_masterfd = -1;
  66. static bool io_event_change_epoll(int fd, short what, const int action);
  67. static int io_dispatch_epoll(struct timeval *tv);
  68. #endif
  69. #ifdef IO_USE_KQUEUE
  70. #include <sys/types.h>
  71. #include <sys/event.h>
  72. static array io_evcache;
  73. static int io_masterfd;
  74. static int io_dispatch_kqueue(struct timeval *tv);
  75. static bool io_event_change_kqueue(int, short, const int action);
  76. #endif
  77. #ifdef IO_USE_POLL
  78. #include <poll.h>
  79. static array pollfds;
  80. static int poll_maxfd;
  81. static bool io_event_change_poll PARAMS((int fd, short what));
  82. #endif
  83. #ifdef IO_USE_DEVPOLL
  84. #include <sys/devpoll.h>
  85. static int io_masterfd;
  86. static bool io_event_change_devpoll(int fd, short what);
  87. #endif
  88. #ifdef IO_USE_SELECT
  89. #include "defines.h" /* for conn.h */
  90. #include "proc.h" /* for PROC_STAT (needed by conf.h) */
  91. #include "conn.h" /* for CONN_ID (needed by conf.h) */
  92. #include "conf.h" /* for Conf_MaxConnections */
  93. static fd_set readers;
  94. static fd_set writers;
  95. /*
  96. * this is the first argument for select(), i.e.
  97. * the largest fd registered, plus one.
  98. */
  99. static int select_maxfd;
  100. static int io_dispatch_select PARAMS((struct timeval *tv));
  101. #ifndef IO_USE_EPOLL
  102. #define io_masterfd -1
  103. #endif
  104. #endif /* IO_USE_SELECT */
  105. static array io_events;
  106. static void io_docallback PARAMS((int fd, short what));
  107. #ifdef DEBUG_IO
  108. static void
  109. io_debug(const char *s, int fd, int what)
  110. {
  111. Log(LOG_DEBUG, "%s: %d, %d\n", s, fd, what);
  112. }
  113. #else
  114. static inline void
  115. io_debug(const char UNUSED *s,int UNUSED a, int UNUSED b)
  116. { /* NOTHING */ }
  117. #endif
  118. static io_event *
  119. io_event_get(int fd)
  120. {
  121. io_event *i;
  122. assert(fd >= 0);
  123. i = (io_event *) array_get(&io_events, sizeof(io_event), (size_t) fd);
  124. assert(i != NULL);
  125. return i;
  126. }
  127. #ifdef IO_USE_DEVPOLL
  128. static int
  129. io_dispatch_devpoll(struct timeval *tv)
  130. {
  131. struct dvpoll dvp;
  132. time_t sec = tv->tv_sec * 1000;
  133. int i, ret, timeout = tv->tv_usec + sec;
  134. short what;
  135. struct pollfd p[MAX_EVENTS];
  136. if (timeout < 0)
  137. timeout = 1000;
  138. dvp.dp_timeout = timeout;
  139. dvp.dp_nfds = MAX_EVENTS;
  140. dvp.dp_fds = p;
  141. ret = ioctl(io_masterfd, DP_POLL, &dvp);
  142. for (i=0; i < ret ; i++) {
  143. what = 0;
  144. if (p[i].revents & (POLLIN|POLLPRI))
  145. what = IO_WANTREAD;
  146. if (p[i].revents & POLLOUT)
  147. what |= IO_WANTWRITE;
  148. if (p[i].revents && !what) {
  149. /* other flag is set, probably POLLERR */
  150. what = IO_ERROR;
  151. }
  152. io_docallback(p[i].fd, what);
  153. }
  154. return ret;
  155. }
  156. static bool
  157. io_event_change_devpoll(int fd, short what)
  158. {
  159. struct pollfd p;
  160. p.events = 0;
  161. if (what & IO_WANTREAD)
  162. p.events = POLLIN | POLLPRI;
  163. if (what & IO_WANTWRITE)
  164. p.events |= POLLOUT;
  165. p.fd = fd;
  166. return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
  167. }
  168. static void
  169. io_close_devpoll(int fd)
  170. {
  171. struct pollfd p;
  172. p.events = POLLREMOVE;
  173. p.fd = fd;
  174. write(io_masterfd, &p, sizeof p);
  175. }
  176. static void
  177. io_library_init_devpoll(unsigned int eventsize)
  178. {
  179. io_masterfd = open("/dev/poll", O_RDWR);
  180. if (io_masterfd >= 0)
  181. library_initialized = true;
  182. Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
  183. eventsize, io_masterfd);
  184. }
  185. #else
  186. static inline void
  187. io_close_devpoll(int UNUSED x)
  188. { /* NOTHING */ }
  189. static inline void
  190. io_library_init_devpoll(unsigned int UNUSED ev)
  191. { /* NOTHING */ }
  192. #endif
  193. #ifdef IO_USE_POLL
  194. static int
  195. io_dispatch_poll(struct timeval *tv)
  196. {
  197. time_t sec = tv->tv_sec * 1000;
  198. int i, ret, timeout = tv->tv_usec + sec;
  199. int fds_ready;
  200. short what;
  201. struct pollfd *p = array_start(&pollfds);
  202. if (timeout < 0)
  203. timeout = 1000;
  204. ret = poll(p, poll_maxfd + 1, timeout);
  205. if (ret <= 0)
  206. return ret;
  207. fds_ready = ret;
  208. for (i=0; i <= poll_maxfd; i++) {
  209. what = 0;
  210. if (p[i].revents & (POLLIN|POLLPRI))
  211. what = IO_WANTREAD;
  212. if (p[i].revents & POLLOUT)
  213. what |= IO_WANTWRITE;
  214. if (p[i].revents && !what) {
  215. /* other flag is set, probably POLLERR */
  216. what = IO_ERROR;
  217. }
  218. if (what) {
  219. fds_ready--;
  220. io_docallback(i, what);
  221. }
  222. if (fds_ready <= 0)
  223. break;
  224. }
  225. return ret;
  226. }
  227. static bool
  228. io_event_change_poll(int fd, short what)
  229. {
  230. struct pollfd *p;
  231. short events = 0;
  232. if (what & IO_WANTREAD)
  233. events = POLLIN | POLLPRI;
  234. if (what & IO_WANTWRITE)
  235. events |= POLLOUT;
  236. p = array_alloc(&pollfds, sizeof *p, fd);
  237. if (p) {
  238. p->events = events;
  239. p->fd = fd;
  240. if (fd > poll_maxfd)
  241. poll_maxfd = fd;
  242. }
  243. return p != NULL;
  244. }
  245. static void
  246. io_close_poll(int fd)
  247. {
  248. struct pollfd *p;
  249. p = array_get(&pollfds, sizeof *p, fd);
  250. if (!p) return;
  251. p->fd = -1;
  252. if (fd == poll_maxfd) {
  253. while (poll_maxfd > 0) {
  254. --poll_maxfd;
  255. p = array_get(&pollfds, sizeof *p, poll_maxfd);
  256. if (p && p->fd >= 0)
  257. break;
  258. }
  259. }
  260. }
  261. static void
  262. io_library_init_poll(unsigned int eventsize)
  263. {
  264. struct pollfd *p;
  265. array_init(&pollfds);
  266. poll_maxfd = 0;
  267. Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
  268. eventsize);
  269. p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
  270. if (p) {
  271. unsigned i;
  272. p = array_start(&pollfds);
  273. for (i = 0; i < eventsize; i++)
  274. p[i].fd = -1;
  275. library_initialized = true;
  276. }
  277. }
  278. #else
  279. static inline void
  280. io_close_poll(int UNUSED x)
  281. { /* NOTHING */ }
  282. static inline void
  283. io_library_init_poll(unsigned int UNUSED ev)
  284. { /* NOTHING */ }
  285. #endif
  286. #ifdef IO_USE_SELECT
  287. static int
  288. io_dispatch_select(struct timeval *tv)
  289. {
  290. fd_set readers_tmp;
  291. fd_set writers_tmp;
  292. short what;
  293. int ret, i;
  294. int fds_ready;
  295. readers_tmp = readers;
  296. writers_tmp = writers;
  297. ret = select(select_maxfd + 1, &readers_tmp, &writers_tmp, NULL, tv);
  298. if (ret <= 0)
  299. return ret;
  300. fds_ready = ret;
  301. for (i = 0; i <= select_maxfd; i++) {
  302. what = 0;
  303. if (FD_ISSET(i, &readers_tmp)) {
  304. what = IO_WANTREAD;
  305. fds_ready--;
  306. }
  307. if (FD_ISSET(i, &writers_tmp)) {
  308. what |= IO_WANTWRITE;
  309. fds_ready--;
  310. }
  311. if (what)
  312. io_docallback(i, what);
  313. if (fds_ready <= 0)
  314. break;
  315. }
  316. return ret;
  317. }
  318. static void
  319. io_library_init_select(unsigned int eventsize)
  320. {
  321. if (library_initialized)
  322. return;
  323. Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
  324. eventsize);
  325. FD_ZERO(&readers);
  326. FD_ZERO(&writers);
  327. #ifdef FD_SETSIZE
  328. if (Conf_MaxConnections >= (int)FD_SETSIZE) {
  329. Log(LOG_WARNING,
  330. "MaxConnections (%d) exceeds limit (%u), changed MaxConnections to %u.",
  331. Conf_MaxConnections, FD_SETSIZE, FD_SETSIZE - 1);
  332. Conf_MaxConnections = FD_SETSIZE - 1;
  333. }
  334. #else
  335. Log(LOG_WARNING,
  336. "FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
  337. #endif /* FD_SETSIZE */
  338. library_initialized = true;
  339. }
  340. static void
  341. io_close_select(int fd)
  342. {
  343. io_event *i;
  344. if (io_masterfd >= 0) /* Are we using epoll()? */
  345. return;
  346. FD_CLR(fd, &writers);
  347. FD_CLR(fd, &readers);
  348. i = io_event_get(fd);
  349. if (!i) return;
  350. if (fd == select_maxfd) {
  351. while (select_maxfd>0) {
  352. --select_maxfd; /* find largest fd */
  353. i = io_event_get(select_maxfd);
  354. if (i && i->callback) break;
  355. }
  356. }
  357. }
  358. #else
  359. static inline void
  360. io_library_init_select(int UNUSED x)
  361. { /* NOTHING */ }
  362. static inline void
  363. io_close_select(int UNUSED x)
  364. { /* NOTHING */ }
  365. #endif /* SELECT */
  366. #ifdef IO_USE_EPOLL
  367. static bool
  368. io_event_change_epoll(int fd, short what, const int action)
  369. {
  370. struct epoll_event ev = { 0, {0} };
  371. ev.data.fd = fd;
  372. if (what & IO_WANTREAD)
  373. ev.events = EPOLLIN | EPOLLPRI;
  374. if (what & IO_WANTWRITE)
  375. ev.events |= EPOLLOUT;
  376. return epoll_ctl(io_masterfd, action, fd, &ev) == 0;
  377. }
  378. static int
  379. io_dispatch_epoll(struct timeval *tv)
  380. {
  381. time_t sec = tv->tv_sec * 1000;
  382. int i, ret, timeout = tv->tv_usec + sec;
  383. struct epoll_event epoll_ev[MAX_EVENTS];
  384. short type;
  385. if (timeout < 0)
  386. timeout = 1000;
  387. ret = epoll_wait(io_masterfd, epoll_ev, MAX_EVENTS, timeout);
  388. for (i = 0; i < ret; i++) {
  389. type = 0;
  390. if (epoll_ev[i].events & (EPOLLERR | EPOLLHUP))
  391. type = IO_ERROR;
  392. if (epoll_ev[i].events & (EPOLLIN | EPOLLPRI))
  393. type |= IO_WANTREAD;
  394. if (epoll_ev[i].events & EPOLLOUT)
  395. type |= IO_WANTWRITE;
  396. io_docallback(epoll_ev[i].data.fd, type);
  397. }
  398. return ret;
  399. }
  400. static void
  401. io_library_init_epoll(unsigned int eventsize)
  402. {
  403. int ecreate_hint = (int)eventsize;
  404. if (ecreate_hint <= 0)
  405. ecreate_hint = 128;
  406. io_masterfd = epoll_create(ecreate_hint);
  407. if (io_masterfd >= 0) {
  408. library_initialized = true;
  409. Log(LOG_INFO,
  410. "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
  411. ecreate_hint, eventsize, io_masterfd);
  412. return;
  413. }
  414. #ifdef IO_USE_SELECT
  415. Log(LOG_INFO, "Can't initialize epoll() IO interface, falling back to select() ...");
  416. #endif
  417. }
  418. #else
  419. static inline void
  420. io_library_init_epoll(unsigned int UNUSED ev)
  421. { /* NOTHING */ }
  422. #endif /* IO_USE_EPOLL */
  423. #ifdef IO_USE_KQUEUE
  424. static bool
  425. io_event_kqueue_commit_cache(void)
  426. {
  427. struct kevent *events;
  428. bool ret;
  429. int len = (int) array_length(&io_evcache, sizeof (struct kevent));
  430. if (!len) /* nothing to do */
  431. return true;
  432. assert(len>0);
  433. if (len < 0) {
  434. array_free(&io_evcache);
  435. return false;
  436. }
  437. events = array_start(&io_evcache);
  438. assert(events != NULL);
  439. ret = kevent(io_masterfd, events, len, NULL, 0, NULL) == 0;
  440. if (ret)
  441. array_trunc(&io_evcache);
  442. return ret;
  443. }
  444. static bool
  445. io_event_change_kqueue(int fd, short what, const int action)
  446. {
  447. struct kevent kev;
  448. bool ret = true;
  449. if (what & IO_WANTREAD) {
  450. EV_SET(&kev, fd, EVFILT_READ, action, 0, 0, 0);
  451. ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
  452. if (!ret)
  453. ret = kevent(io_masterfd, &kev,1, NULL, 0, NULL) == 0;
  454. }
  455. if (ret && (what & IO_WANTWRITE)) {
  456. EV_SET(&kev, fd, EVFILT_WRITE, action, 0, 0, 0);
  457. ret = array_catb(&io_evcache, (char*) &kev, sizeof (kev));
  458. if (!ret)
  459. ret = kevent(io_masterfd, &kev, 1, NULL, 0, NULL) == 0;
  460. }
  461. if (array_length(&io_evcache, sizeof kev) >= 100)
  462. io_event_kqueue_commit_cache();
  463. return ret;
  464. }
  465. static int
  466. io_dispatch_kqueue(struct timeval *tv)
  467. {
  468. int i, ret;
  469. struct kevent kev[MAX_EVENTS];
  470. struct kevent *newevents;
  471. struct timespec ts;
  472. int newevents_len;
  473. ts.tv_sec = tv->tv_sec;
  474. ts.tv_nsec = tv->tv_usec * 1000;
  475. newevents_len = (int) array_length(&io_evcache, sizeof (struct kevent));
  476. newevents = (newevents_len > 0) ? array_start(&io_evcache) : NULL;
  477. assert(newevents_len >= 0);
  478. ret = kevent(io_masterfd, newevents, newevents_len, kev, MAX_EVENTS, &ts);
  479. if (newevents && ret != -1)
  480. array_trunc(&io_evcache);
  481. for (i = 0; i < ret; i++) {
  482. io_debug("dispatch_kqueue: fd, kev.flags", (int)kev[i].ident, kev[i].flags);
  483. if (kev[i].flags & (EV_EOF|EV_ERROR)) {
  484. if (kev[i].flags & EV_ERROR)
  485. Log(LOG_ERR, "kevent fd %d: EV_ERROR (%s)",
  486. (int)kev[i].ident, strerror((int)kev[i].data));
  487. io_docallback((int)kev[i].ident, IO_ERROR);
  488. continue;
  489. }
  490. switch (kev[i].filter) {
  491. case EVFILT_READ:
  492. io_docallback((int)kev[i].ident, IO_WANTREAD);
  493. break;
  494. case EVFILT_WRITE:
  495. io_docallback((int)kev[i].ident, IO_WANTWRITE);
  496. break;
  497. default:
  498. LogDebug("Unknown kev.filter number %d for fd %d",
  499. kev[i].filter, kev[i].ident);
  500. /* Fall through */
  501. case EV_ERROR:
  502. io_docallback((int)kev[i].ident, IO_ERROR);
  503. break;
  504. }
  505. }
  506. return ret;
  507. }
  508. static void
  509. io_library_init_kqueue(unsigned int eventsize)
  510. {
  511. io_masterfd = kqueue();
  512. Log(LOG_INFO,
  513. "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
  514. eventsize, io_masterfd);
  515. if (io_masterfd >= 0)
  516. library_initialized = true;
  517. }
  518. #else
  519. static inline void
  520. io_library_init_kqueue(unsigned int UNUSED ev)
  521. { /* NOTHING */ }
  522. #endif
  523. bool
  524. io_library_init(unsigned int eventsize)
  525. {
  526. if (library_initialized)
  527. return true;
  528. if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
  529. eventsize = 0;
  530. io_library_init_epoll(eventsize);
  531. io_library_init_kqueue(eventsize);
  532. io_library_init_devpoll(eventsize);
  533. io_library_init_poll(eventsize);
  534. io_library_init_select(eventsize);
  535. return library_initialized;
  536. }
  537. void
  538. io_library_shutdown(void)
  539. {
  540. #ifdef IO_USE_SELECT
  541. FD_ZERO(&readers);
  542. FD_ZERO(&writers);
  543. #endif
  544. #if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE) || defined(IO_USE_DEVPOLL)
  545. if (io_masterfd >= 0)
  546. close(io_masterfd);
  547. io_masterfd = -1;
  548. #endif
  549. #ifdef IO_USE_KQUEUE
  550. array_free(&io_evcache);
  551. #endif
  552. library_initialized = false;
  553. }
  554. bool
  555. io_event_setcb(int fd, void (*cbfunc) (int, short))
  556. {
  557. io_event *i = io_event_get(fd);
  558. if (!i)
  559. return false;
  560. i->callback = cbfunc;
  561. return true;
  562. }
  563. static bool
  564. backend_create_ev(int fd, short what)
  565. {
  566. bool ret;
  567. #ifdef IO_USE_DEVPOLL
  568. ret = io_event_change_devpoll(fd, what);
  569. #endif
  570. #ifdef IO_USE_POLL
  571. ret = io_event_change_poll(fd, what);
  572. #endif
  573. #ifdef IO_USE_EPOLL
  574. ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
  575. #endif
  576. #ifdef IO_USE_KQUEUE
  577. ret = io_event_change_kqueue(fd, what, EV_ADD|EV_ENABLE);
  578. #endif
  579. #ifdef IO_USE_SELECT
  580. if (io_masterfd < 0)
  581. ret = io_event_add(fd, what);
  582. #endif
  583. return ret;
  584. }
  585. bool
  586. io_event_create(int fd, short what, void (*cbfunc) (int, short))
  587. {
  588. bool ret;
  589. io_event *i;
  590. assert(fd >= 0);
  591. #if defined(IO_USE_SELECT) && defined(FD_SETSIZE)
  592. if (io_masterfd < 0 && fd >= FD_SETSIZE) {
  593. Log(LOG_ERR,
  594. "fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
  595. fd, FD_SETSIZE);
  596. return false;
  597. }
  598. #endif
  599. i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
  600. if (!i) {
  601. Log(LOG_WARNING,
  602. "array_alloc failed: could not allocate space for %d io_event structures",
  603. fd);
  604. return false;
  605. }
  606. i->callback = cbfunc;
  607. i->what = 0;
  608. ret = backend_create_ev(fd, what);
  609. if (ret)
  610. i->what = what;
  611. return ret;
  612. }
  613. bool
  614. io_event_add(int fd, short what)
  615. {
  616. io_event *i = io_event_get(fd);
  617. if (!i) return false;
  618. if ((i->what & what) == what) /* event type is already registered */
  619. return true;
  620. io_debug("io_event_add: fd, what", fd, what);
  621. i->what |= what;
  622. #ifdef IO_USE_EPOLL
  623. if (io_masterfd >= 0)
  624. return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
  625. #endif
  626. #ifdef IO_USE_KQUEUE
  627. return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
  628. #endif
  629. #ifdef IO_USE_DEVPOLL
  630. return io_event_change_devpoll(fd, i->what);
  631. #endif
  632. #ifdef IO_USE_POLL
  633. return io_event_change_poll(fd, i->what);
  634. #endif
  635. #ifdef IO_USE_SELECT
  636. if (fd > select_maxfd)
  637. select_maxfd = fd;
  638. if (what & IO_WANTREAD)
  639. FD_SET(fd, &readers);
  640. if (what & IO_WANTWRITE)
  641. FD_SET(fd, &writers);
  642. return true;
  643. #endif
  644. return false;
  645. }
  646. bool
  647. io_setnonblock(int fd)
  648. {
  649. int flags = fcntl(fd, F_GETFL);
  650. if (flags == -1)
  651. return false;
  652. #ifndef O_NONBLOCK
  653. #define O_NONBLOCK O_NDELAY
  654. #endif
  655. flags |= O_NONBLOCK;
  656. return fcntl(fd, F_SETFL, flags) == 0;
  657. }
  658. bool
  659. io_setcloexec(int fd)
  660. {
  661. int flags = fcntl(fd, F_GETFD);
  662. if (flags == -1)
  663. return false;
  664. #ifdef FD_CLOEXEC
  665. flags |= FD_CLOEXEC;
  666. #endif
  667. return fcntl(fd, F_SETFD, flags) == 0;
  668. }
  669. bool
  670. io_close(int fd)
  671. {
  672. io_event *i;
  673. i = io_event_get(fd);
  674. #ifdef IO_USE_KQUEUE
  675. if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
  676. io_event_kqueue_commit_cache();
  677. /* both kqueue and epoll remove fd from all sets automatically on the last close
  678. * of the descriptor. since we don't know if this is the last close we'll have
  679. * to remove the set explicitly. */
  680. if (i) {
  681. io_event_change_kqueue(fd, i->what, EV_DELETE);
  682. io_event_kqueue_commit_cache();
  683. }
  684. #endif
  685. io_close_devpoll(fd);
  686. io_close_poll(fd);
  687. io_close_select(fd);
  688. #ifdef IO_USE_EPOLL
  689. io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
  690. #endif
  691. if (i) {
  692. i->callback = NULL;
  693. i->what = 0;
  694. }
  695. return close(fd) == 0;
  696. }
  697. bool
  698. io_event_del(int fd, short what)
  699. {
  700. io_event *i = io_event_get(fd);
  701. io_debug("io_event_del: trying to delete eventtype; fd, what", fd, what);
  702. if (!i) return false;
  703. if (!(i->what & what)) /* event is already disabled */
  704. return true;
  705. i->what &= ~what;
  706. #ifdef IO_USE_DEVPOLL
  707. return io_event_change_devpoll(fd, i->what);
  708. #endif
  709. #ifdef IO_USE_POLL
  710. return io_event_change_poll(fd, i->what);
  711. #endif
  712. #ifdef IO_USE_EPOLL
  713. if (io_masterfd >= 0)
  714. return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
  715. #endif
  716. #ifdef IO_USE_KQUEUE
  717. return io_event_change_kqueue(fd, what, EV_DISABLE);
  718. #endif
  719. #ifdef IO_USE_SELECT
  720. if (what & IO_WANTWRITE)
  721. FD_CLR(fd, &writers);
  722. if (what & IO_WANTREAD)
  723. FD_CLR(fd, &readers);
  724. return true;
  725. #endif
  726. return false;
  727. }
  728. int
  729. io_dispatch(struct timeval *tv)
  730. {
  731. #ifdef IO_USE_EPOLL
  732. if (io_masterfd >= 0)
  733. return io_dispatch_epoll(tv);
  734. #endif
  735. #ifdef IO_USE_SELECT
  736. return io_dispatch_select(tv);
  737. #endif
  738. #ifdef IO_USE_KQUEUE
  739. return io_dispatch_kqueue(tv);
  740. #endif
  741. #ifdef IO_USE_DEVPOLL
  742. return io_dispatch_devpoll(tv);
  743. #endif
  744. #ifdef IO_USE_POLL
  745. return io_dispatch_poll(tv);
  746. #endif
  747. return -1;
  748. }
  749. /* call the callback function inside the struct matching fd */
  750. static void
  751. io_docallback(int fd, short what)
  752. {
  753. io_event *i = io_event_get(fd);
  754. io_debug("io_docallback; fd, what", fd, what);
  755. if (i->callback) { /* callback might be NULL if a previous callback function
  756. called io_close on this fd */
  757. i->callback(fd, (what & IO_ERROR) ? i->what : what);
  758. }
  759. /* if error indicator is set, we return the event(s) that were registered */
  760. }