io.c 18 KB

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