io_wait.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * tcp io wait common stuff used by tcp_main.c & tcp_read.c
  20. * All the functions are inline because of speed reasons and because they are
  21. * used only from 2 places.
  22. * You also have to define:
  23. * int handle_io(struct fd_map* fm, short events, int idx) (see below)
  24. * (this could be trivially replaced by a callback pointer entry attached
  25. * to the io_wait handler if more flexibility rather then performance
  26. * is needed)
  27. * fd_type - define to some enum of you choice and define also
  28. * FD_TYPE_DEFINED (if you don't do it fd_type will be defined
  29. * to int). 0 has a special not set/not init. meaning
  30. * (a lot of sanity checks and the sigio_rt code are based on
  31. * this assumption)
  32. * local_malloc (defaults to pkg_malloc)
  33. * local_free (defaults to pkg_free)
  34. *
  35. */
  36. /*
  37. * History:
  38. * --------
  39. * 2005-06-13 created by andrei
  40. * 2005-06-26 added kqueue (andrei)
  41. * 2005-07-01 added /dev/poll (andrei)
  42. * 2006-05-30 sigio 64 bit workarround enabled for kernels < 2.6.5 (andrei)
  43. * 2007-11-22 when handle_io() is called in a loop check & stop if the fd was
  44. * removed inside handle_io() (andrei)
  45. * 2007-11-29 support for write (POLLOUT); added io_watch_chg() (andrei)
  46. * 2008-02-04 POLLRDHUP & EPOLLRDHUP support (automatically enabled if POLLIN
  47. * is set) (andrei)
  48. * 2010-06-17 re-enabled & enhanced the EV_ERROR for kqueue (andrei)
  49. */
  50. #ifndef _io_wait_h
  51. #define _io_wait_h
  52. #include <errno.h>
  53. #include <string.h>
  54. #ifdef HAVE_SIGIO_RT
  55. #define __USE_GNU /* or else F_SETSIG won't be included */
  56. #include <sys/types.h> /* recv */
  57. #include <sys/socket.h> /* recv */
  58. #include <signal.h> /* sigprocmask, sigwait a.s.o */
  59. #endif
  60. #define _GNU_SOURCE /* for POLLRDHUP on linux */
  61. #include <poll.h>
  62. #include <fcntl.h>
  63. #ifdef HAVE_EPOLL
  64. #include <sys/epoll.h>
  65. #endif
  66. #ifdef HAVE_KQUEUE
  67. #include <sys/types.h> /* needed on freebsd */
  68. #include <sys/event.h>
  69. #include <sys/time.h>
  70. #endif
  71. #ifdef HAVE_DEVPOLL
  72. #include <sys/devpoll.h>
  73. #endif
  74. #ifdef HAVE_SELECT
  75. /* needed on openbsd for select*/
  76. #include <sys/time.h>
  77. #include <sys/types.h>
  78. #include <unistd.h>
  79. /* needed according to POSIX for select*/
  80. #include <sys/select.h>
  81. #endif
  82. #include "dprint.h"
  83. #include "poll_types.h" /* poll_types*/
  84. #ifdef HAVE_SIGIO_RT
  85. #include "pt.h" /* mypid() */
  86. #endif
  87. #include "compiler_opt.h"
  88. #ifdef HAVE_EPOLL
  89. /* fix defines for EPOLL */
  90. #if defined POLLRDHUP && ! defined EPOLLRDHUP
  91. #define EPOLLRDHUP POLLRDHUP /* should work on all linuxes */
  92. #endif /* POLLRDHUP && EPOLLRDHUP */
  93. #endif /* HAVE_EPOLL */
  94. extern int _os_ver; /* os version number, needed to select bugs workarrounds */
  95. #if 0
  96. enum fd_types; /* this should be defined from the including file,
  97. see tcp_main.c for an example,
  98. 0 has a special meaning: not used/empty*/
  99. #endif
  100. #ifndef FD_TYPE_DEFINED
  101. typedef int fd_type;
  102. #define FD_TYPE_DEFINED
  103. #endif
  104. /* maps a fd to some other structure; used in almost all cases
  105. * except epoll and maybe kqueue or /dev/poll */
  106. struct fd_map{
  107. int fd; /* fd no */
  108. fd_type type; /* "data" type */
  109. void* data; /* pointer to the corresponding structure */
  110. short events; /* events we are interested int */
  111. };
  112. #ifdef HAVE_KQUEUE
  113. #ifndef KQ_CHANGES_ARRAY_SIZE
  114. #define KQ_CHANGES_ARRAY_SIZE 256
  115. #ifdef __OS_netbsd
  116. #define KEV_UDATA_CAST (intptr_t)
  117. #else
  118. #define KEV_UDATA_CAST
  119. #endif
  120. #endif
  121. #endif
  122. /* handler structure */
  123. struct io_wait_handler{
  124. enum poll_types poll_method;
  125. int flags;
  126. struct fd_map* fd_hash;
  127. int fd_no; /* current index used in fd_array and the passed size for
  128. ep_array (for kq_array at least
  129. max(twice the size, kq_changes_size) should be
  130. be passed). */
  131. int max_fd_no; /* maximum fd no, is also the size of fd_array,
  132. fd_hash and ep_array*/
  133. /* common stuff for POLL, SIGIO_RT and SELECT
  134. * since poll support is always compiled => this will always be compiled */
  135. struct pollfd* fd_array; /* used also by devpoll as devpoll array */
  136. int crt_fd_array_idx; /* crt idx for which handle_io is called
  137. (updated also by del -> internal optimization) */
  138. /* end of common stuff */
  139. #ifdef HAVE_EPOLL
  140. int epfd; /* epoll ctrl fd */
  141. struct epoll_event* ep_array;
  142. #endif
  143. #ifdef HAVE_SIGIO_RT
  144. sigset_t sset; /* signal mask for sigio & sigrtmin */
  145. int signo; /* real time signal used */
  146. #endif
  147. #ifdef HAVE_KQUEUE
  148. int kq_fd;
  149. struct kevent* kq_array; /* used for the eventlist*/
  150. struct kevent* kq_changes; /* used for the changelist */
  151. size_t kq_nchanges;
  152. size_t kq_array_size; /* array size */
  153. size_t kq_changes_size; /* size of the changes array */
  154. #endif
  155. #ifdef HAVE_DEVPOLL
  156. int dpoll_fd;
  157. #endif
  158. #ifdef HAVE_SELECT
  159. fd_set master_rset; /* read set */
  160. fd_set master_wset; /* write set */
  161. int max_fd_select; /* maximum select used fd */
  162. #endif
  163. };
  164. typedef struct io_wait_handler io_wait_h;
  165. /* get the corresponding fd_map structure pointer */
  166. #define get_fd_map(h, fd) (&(h)->fd_hash[(fd)])
  167. /* remove a fd_map structure from the hash; the pointer must be returned
  168. * by get_fd_map or hash_fd_map*/
  169. #define unhash_fd_map(pfm) \
  170. do{ \
  171. (pfm)->type=0 /*F_NONE */; \
  172. (pfm)->fd=-1; \
  173. }while(0)
  174. /* add a fd_map structure to the fd hash */
  175. static inline struct fd_map* hash_fd_map( io_wait_h* h,
  176. int fd,
  177. short events,
  178. fd_type type,
  179. void* data)
  180. {
  181. h->fd_hash[fd].fd=fd;
  182. h->fd_hash[fd].events=events;
  183. h->fd_hash[fd].type=type;
  184. h->fd_hash[fd].data=data;
  185. return &h->fd_hash[fd];
  186. }
  187. #ifdef HANDLE_IO_INLINE
  188. /* generic handle io routine, this must be defined in the including file
  189. * (faster then registering a callback pointer)
  190. *
  191. * params: fm - pointer to a fd hash entry
  192. * events - combinations of POLLIN, POLLOUT, POLLERR & POLLHUP
  193. * idx - index in the fd_array (or -1 if not known)
  194. * return: -1 on error
  195. * 0 on EAGAIN or when by some other way it is known that no more
  196. * io events are queued on the fd (the receive buffer is empty).
  197. * Usefull to detect when there are no more io events queued for
  198. * sigio_rt, epoll_et, kqueue.
  199. * >0 on successfull read from the fd (when there might be more io
  200. * queued -- the receive buffer might still be non-empty)
  201. */
  202. inline static int handle_io(struct fd_map* fm, short events, int idx);
  203. #else
  204. int handle_io(struct fd_map* fm, short events, int idx);
  205. #endif
  206. #ifdef HAVE_KQUEUE
  207. /*
  208. * kqueue specific function: register a change
  209. * (adds a change to the kevent change array, and if full flushes it first)
  210. *
  211. * TODO: check if the event already exists in the change list or if it's
  212. * complementary to an event in the list (e.g. EVFILT_WRITE, EV_DELETE
  213. * and EVFILT_WRITE, EV_ADD for the same fd).
  214. * returns: -1 on error, 0 on success
  215. */
  216. static inline int kq_ev_change(io_wait_h* h, int fd, int filter, int flag,
  217. void* data)
  218. {
  219. int n;
  220. int r;
  221. struct timespec tspec;
  222. if (h->kq_nchanges>=h->kq_changes_size){
  223. /* changes array full ! */
  224. LOG(L_WARN, "WARNING: kq_ev_change: kqueue changes array full"
  225. " trying to flush...\n");
  226. tspec.tv_sec=0;
  227. tspec.tv_nsec=0;
  228. again:
  229. n=kevent(h->kq_fd, h->kq_changes, h->kq_nchanges, 0, 0, &tspec);
  230. if (unlikely(n == -1)){
  231. if (unlikely(errno == EINTR)) goto again;
  232. else {
  233. /* for a detailed explanation of what follows see
  234. io_wait_loop_kqueue EV_ERROR case */
  235. if (unlikely(!(errno == EBADF || errno == ENOENT)))
  236. BUG("kq_ev_change: kevent flush changes failed"
  237. " (unexpected error): %s [%d]\n",
  238. strerror(errno), errno);
  239. /* ignore error even if it's not a EBADF/ENOENT */
  240. /* one of the file descriptors is bad, probably already
  241. closed => try to apply changes one-by-one */
  242. for (r = 0; r < h->kq_nchanges; r++) {
  243. retry2:
  244. n = kevent(h->kq_fd, &h->kq_changes[r], 1, 0, 0, &tspec);
  245. if (n==-1) {
  246. if (unlikely(errno == EINTR))
  247. goto retry2;
  248. /* for a detailed explanation of what follows see
  249. io_wait_loop_kqueue EV_ERROR case */
  250. if (unlikely(!(errno == EBADF || errno == ENOENT)))
  251. BUG("kq_ev_change: kevent flush changes failed:"
  252. " (unexpected error) %s [%d] (%d/%lu)\n",
  253. strerror(errno), errno,
  254. r, (unsigned long)h->kq_nchanges);
  255. continue; /* skip over it */
  256. }
  257. }
  258. }
  259. }
  260. h->kq_nchanges=0; /* changes array is empty */
  261. }
  262. EV_SET(&h->kq_changes[h->kq_nchanges], fd, filter, flag, 0, 0,
  263. KEV_UDATA_CAST data);
  264. h->kq_nchanges++;
  265. return 0;
  266. }
  267. #endif
  268. /* generic io_watch_add function
  269. * Params:
  270. * h - pointer to initialized io_wait handle
  271. * fd - fd to watch
  272. * events - bitmap with the fd events for which the fd should be watched
  273. * (combination of POLLIN and POLLOUT)
  274. * type - fd type (non 0 value, returned in the call to handle_io)
  275. * data - pointer/private data returned in the handle_io call
  276. * returns 0 on success, -1 on error
  277. *
  278. * WARNING: handle_io() can be called immediately (from io_watch_add()) so
  279. * make sure that any dependent init. (e.g. data stuff) is made before
  280. * calling io_watch_add
  281. *
  282. * this version should be faster than pointers to poll_method specific
  283. * functions (it avoids functions calls, the overhead being only an extra
  284. * switch())*/
  285. inline static int io_watch_add( io_wait_h* h,
  286. int fd,
  287. short events,
  288. fd_type type,
  289. void* data)
  290. {
  291. /* helper macros */
  292. #define fd_array_setup(ev) \
  293. do{ \
  294. h->fd_array[h->fd_no].fd=fd; \
  295. h->fd_array[h->fd_no].events=(ev); /* useless for select */ \
  296. h->fd_array[h->fd_no].revents=0; /* useless for select */ \
  297. }while(0)
  298. #define set_fd_flags(f) \
  299. do{ \
  300. flags=fcntl(fd, F_GETFL); \
  301. if (flags==-1){ \
  302. LOG(L_ERR, "ERROR: io_watch_add: fnctl: GETFL failed:" \
  303. " %s [%d]\n", strerror(errno), errno); \
  304. goto error; \
  305. } \
  306. if (fcntl(fd, F_SETFL, flags|(f))==-1){ \
  307. LOG(L_ERR, "ERROR: io_watch_add: fnctl: SETFL" \
  308. " failed: %s [%d]\n", strerror(errno), errno); \
  309. goto error; \
  310. } \
  311. }while(0)
  312. struct fd_map* e;
  313. int flags;
  314. #ifdef HAVE_EPOLL
  315. struct epoll_event ep_event;
  316. #endif
  317. #ifdef HAVE_DEVPOLL
  318. struct pollfd pfd;
  319. #endif
  320. #if defined(HAVE_SIGIO_RT) || defined (HAVE_EPOLL)
  321. int n;
  322. #endif
  323. #if defined(HAVE_SIGIO_RT)
  324. int idx;
  325. int check_io;
  326. struct pollfd pf;
  327. check_io=0; /* set to 1 if we need to check for pre-existing queued
  328. io/data on the fd */
  329. idx=-1;
  330. #endif
  331. e=0;
  332. /* sanity checks */
  333. if (unlikely(fd==-1)){
  334. LOG(L_CRIT, "BUG: io_watch_add: fd is -1!\n");
  335. goto error;
  336. }
  337. if (unlikely((events&(POLLIN|POLLOUT))==0)){
  338. LOG(L_CRIT, "BUG: io_watch_add: invalid events: 0x%0x\n", events);
  339. goto error;
  340. }
  341. /* check if not too big */
  342. if (unlikely(h->fd_no>=h->max_fd_no)){
  343. LOG(L_CRIT, "ERROR: io_watch_add: maximum fd number exceeded:"
  344. " %d/%d\n", h->fd_no, h->max_fd_no);
  345. goto error;
  346. }
  347. DBG("DBG: io_watch_add(%p, %d, %d, %p), fd_no=%d\n",
  348. h, fd, type, data, h->fd_no);
  349. /* hash sanity check */
  350. e=get_fd_map(h, fd);
  351. if (unlikely(e && (e->type!=0 /*F_NONE*/))){
  352. LOG(L_ERR, "ERROR: io_watch_add: trying to overwrite entry %d"
  353. " watched for %x in the hash(%d, %d, %p) with (%d, %d, %p)\n",
  354. fd, events, e->fd, e->type, e->data, fd, type, data);
  355. e=0;
  356. goto error;
  357. }
  358. if (unlikely((e=hash_fd_map(h, fd, events, type, data))==0)){
  359. LOG(L_ERR, "ERROR: io_watch_add: failed to hash the fd %d\n", fd);
  360. goto error;
  361. }
  362. switch(h->poll_method){ /* faster then pointer to functions */
  363. case POLL_POLL:
  364. #ifdef POLLRDHUP
  365. /* listen to POLLRDHUP by default (if POLLIN) */
  366. events|=((int)!(events & POLLIN) - 1) & POLLRDHUP;
  367. #endif /* POLLRDHUP */
  368. fd_array_setup(events);
  369. set_fd_flags(O_NONBLOCK);
  370. break;
  371. #ifdef HAVE_SELECT
  372. case POLL_SELECT:
  373. fd_array_setup(events);
  374. if (likely(events & POLLIN))
  375. FD_SET(fd, &h->master_rset);
  376. if (unlikely(events & POLLOUT))
  377. FD_SET(fd, &h->master_wset);
  378. if (h->max_fd_select<fd) h->max_fd_select=fd;
  379. break;
  380. #endif
  381. #ifdef HAVE_SIGIO_RT
  382. case POLL_SIGIO_RT:
  383. fd_array_setup(events);
  384. /* re-set O_ASYNC might be needed, if not done from
  385. * io_watch_del (or if somebody wants to add a fd which has
  386. * already O_ASYNC/F_SETSIG set on a duplicate)
  387. */
  388. /* set async & signal */
  389. if (fcntl(fd, F_SETOWN, my_pid())==-1){
  390. LOG(L_ERR, "ERROR: io_watch_add: fnctl: SETOWN"
  391. " failed: %s [%d]\n", strerror(errno), errno);
  392. goto error;
  393. }
  394. if (fcntl(fd, F_SETSIG, h->signo)==-1){
  395. LOG(L_ERR, "ERROR: io_watch_add: fnctl: SETSIG"
  396. " failed: %s [%d]\n", strerror(errno), errno);
  397. goto error;
  398. }
  399. /* set both non-blocking and async */
  400. set_fd_flags(O_ASYNC| O_NONBLOCK);
  401. #ifdef EXTRA_DEBUG
  402. DBG("io_watch_add: sigio_rt on f %d, signal %d to pid %d\n",
  403. fd, h->signo, my_pid());
  404. #endif
  405. /* empty socket receive buffer, if buffer is already full
  406. * no more space to put packets
  407. * => no more signals are ever generated
  408. * also when moving fds, the freshly moved fd might have
  409. * already some bytes queued, we want to get them now
  410. * and not later -- andrei */
  411. idx=h->fd_no;
  412. check_io=1;
  413. break;
  414. #endif
  415. #ifdef HAVE_EPOLL
  416. case POLL_EPOLL_LT:
  417. ep_event.events=
  418. #ifdef POLLRDHUP
  419. /* listen for EPOLLRDHUP too */
  420. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  421. #else /* POLLRDHUP */
  422. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  423. #endif /* POLLRDHUP */
  424. (EPOLLOUT & ((int)!(events & POLLOUT)-1) );
  425. ep_event.data.ptr=e;
  426. again1:
  427. n=epoll_ctl(h->epfd, EPOLL_CTL_ADD, fd, &ep_event);
  428. if (unlikely(n==-1)){
  429. if (errno==EAGAIN) goto again1;
  430. LOG(L_ERR, "ERROR: io_watch_add: epoll_ctl failed: %s [%d]\n",
  431. strerror(errno), errno);
  432. goto error;
  433. }
  434. break;
  435. case POLL_EPOLL_ET:
  436. set_fd_flags(O_NONBLOCK);
  437. ep_event.events=
  438. #ifdef POLLRDHUP
  439. /* listen for EPOLLRDHUP too */
  440. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  441. #else /* POLLRDHUP */
  442. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  443. #endif /* POLLRDHUP */
  444. (EPOLLOUT & ((int)!(events & POLLOUT)-1) ) |
  445. EPOLLET;
  446. ep_event.data.ptr=e;
  447. again2:
  448. n=epoll_ctl(h->epfd, EPOLL_CTL_ADD, fd, &ep_event);
  449. if (unlikely(n==-1)){
  450. if (errno==EAGAIN) goto again2;
  451. LOG(L_ERR, "ERROR: io_watch_add: epoll_ctl failed: %s [%d]\n",
  452. strerror(errno), errno);
  453. goto error;
  454. }
  455. break;
  456. #endif
  457. #ifdef HAVE_KQUEUE
  458. case POLL_KQUEUE:
  459. if (likely( events & POLLIN)){
  460. if (unlikely(kq_ev_change(h, fd, EVFILT_READ, EV_ADD, e)==-1))
  461. goto error;
  462. }
  463. if (unlikely( events & POLLOUT)){
  464. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE, EV_ADD, e)==-1))
  465. {
  466. if (likely(events & POLLIN)){
  467. kq_ev_change(h, fd, EVFILT_READ, EV_DELETE, 0);
  468. }
  469. goto error;
  470. }
  471. }
  472. break;
  473. #endif
  474. #ifdef HAVE_DEVPOLL
  475. case POLL_DEVPOLL:
  476. pfd.fd=fd;
  477. pfd.events=events;
  478. pfd.revents=0;
  479. again_devpoll:
  480. if (write(h->dpoll_fd, &pfd, sizeof(pfd))==-1){
  481. if (errno==EAGAIN) goto again_devpoll;
  482. LOG(L_ERR, "ERROR: io_watch_add: /dev/poll write failed:"
  483. "%s [%d]\n", strerror(errno), errno);
  484. goto error;
  485. }
  486. break;
  487. #endif
  488. default:
  489. LOG(L_CRIT, "BUG: io_watch_add: no support for poll method "
  490. " %s (%d)\n", poll_method_str[h->poll_method],
  491. h->poll_method);
  492. goto error;
  493. }
  494. h->fd_no++; /* "activate" changes, for epoll/kqueue/devpoll it
  495. has only informative value */
  496. #if defined(HAVE_SIGIO_RT)
  497. if (check_io){
  498. /* handle possible pre-existing events */
  499. pf.fd=fd;
  500. pf.events=events;
  501. check_io_again:
  502. n=0;
  503. while(e->type && ((n=poll(&pf, 1, 0))>0) &&
  504. (handle_io(e, pf.revents, idx)>0) &&
  505. (pf.revents & (e->events|POLLERR|POLLHUP)));
  506. if (unlikely(e->type && (n==-1))){
  507. if (errno==EINTR) goto check_io_again;
  508. LOG(L_ERR, "ERROR: io_watch_add: check_io poll: %s [%d]\n",
  509. strerror(errno), errno);
  510. }
  511. }
  512. #endif
  513. return 0;
  514. error:
  515. if (e) unhash_fd_map(e);
  516. return -1;
  517. #undef fd_array_setup
  518. #undef set_fd_flags
  519. }
  520. #define IO_FD_CLOSING 16
  521. /* parameters: h - handler
  522. * fd - file descriptor
  523. * index - index in the fd_array if known, -1 if not
  524. * (if index==-1 fd_array will be searched for the
  525. * corresponding fd* entry -- slower but unavoidable in
  526. * some cases). index is not used (no fd_array) for epoll,
  527. * /dev/poll and kqueue
  528. * flags - optimization flags, e.g. IO_FD_CLOSING, the fd was
  529. * or will shortly be closed, in some cases we can avoid
  530. * extra remove operations (e.g.: epoll, kqueue, sigio)
  531. * returns 0 if ok, -1 on error */
  532. inline static int io_watch_del(io_wait_h* h, int fd, int idx, int flags)
  533. {
  534. #define fix_fd_array \
  535. do{\
  536. if (unlikely(idx==-1)){ \
  537. /* fix idx if -1 and needed */ \
  538. for (idx=0; (idx<h->fd_no) && \
  539. (h->fd_array[idx].fd!=fd); idx++); \
  540. } \
  541. if (likely(idx<h->fd_no)){ \
  542. memmove(&h->fd_array[idx], &h->fd_array[idx+1], \
  543. (h->fd_no-(idx+1))*sizeof(*(h->fd_array))); \
  544. if ((idx<=h->crt_fd_array_idx) && (h->crt_fd_array_idx>=0)) \
  545. h->crt_fd_array_idx--; \
  546. } \
  547. }while(0)
  548. struct fd_map* e;
  549. int events;
  550. #ifdef HAVE_EPOLL
  551. int n;
  552. struct epoll_event ep_event;
  553. #endif
  554. #ifdef HAVE_DEVPOLL
  555. struct pollfd pfd;
  556. #endif
  557. #ifdef HAVE_SIGIO_RT
  558. int fd_flags;
  559. #endif
  560. if (unlikely((fd<0) || (fd>=h->max_fd_no))){
  561. LOG(L_CRIT, "BUG: io_watch_del: invalid fd %d, not in [0, %d) \n",
  562. fd, h->fd_no);
  563. goto error;
  564. }
  565. DBG("DBG: io_watch_del (%p, %d, %d, 0x%x) fd_no=%d called\n",
  566. h, fd, idx, flags, h->fd_no);
  567. e=get_fd_map(h, fd);
  568. /* more sanity checks */
  569. if (unlikely(e==0)){
  570. LOG(L_CRIT, "BUG: io_watch_del: no corresponding hash entry for %d\n",
  571. fd);
  572. goto error;
  573. }
  574. if (unlikely(e->type==0 /*F_NONE*/)){
  575. LOG(L_ERR, "ERROR: io_watch_del: trying to delete already erased"
  576. " entry %d in the hash(%d, %d, %p) flags %x)\n",
  577. fd, e->fd, e->type, e->data, flags);
  578. goto error;
  579. }
  580. events=e->events;
  581. switch(h->poll_method){
  582. case POLL_POLL:
  583. fix_fd_array;
  584. break;
  585. #ifdef HAVE_SELECT
  586. case POLL_SELECT:
  587. if (likely(events & POLLIN))
  588. FD_CLR(fd, &h->master_rset);
  589. if (unlikely(events & POLLOUT))
  590. FD_CLR(fd, &h->master_wset);
  591. if (unlikely(h->max_fd_select && (h->max_fd_select==fd)))
  592. /* we don't know the prev. max, so we just decrement it */
  593. h->max_fd_select--;
  594. fix_fd_array;
  595. break;
  596. #endif
  597. #ifdef HAVE_SIGIO_RT
  598. case POLL_SIGIO_RT:
  599. /* the O_ASYNC flag must be reset all the time, the fd
  600. * can be changed only if O_ASYNC is reset (if not and
  601. * the fd is a duplicate, you will get signals from the dup. fd
  602. * and not from the original, even if the dup. fd was closed
  603. * and the signals re-set on the original) -- andrei
  604. */
  605. /*if (!(flags & IO_FD_CLOSING)){*/
  606. /* reset ASYNC */
  607. fd_flags=fcntl(fd, F_GETFL);
  608. if (unlikely(fd_flags==-1)){
  609. LOG(L_ERR, "ERROR: io_watch_del: fnctl: GETFL failed:"
  610. " %s [%d]\n", strerror(errno), errno);
  611. goto error;
  612. }
  613. if (unlikely(fcntl(fd, F_SETFL, fd_flags&(~O_ASYNC))==-1)){
  614. LOG(L_ERR, "ERROR: io_watch_del: fnctl: SETFL"
  615. " failed: %s [%d]\n", strerror(errno), errno);
  616. goto error;
  617. }
  618. fix_fd_array; /* only on success */
  619. break;
  620. #endif
  621. #ifdef HAVE_EPOLL
  622. case POLL_EPOLL_LT:
  623. case POLL_EPOLL_ET:
  624. /* epoll doesn't seem to automatically remove sockets,
  625. * if the socket is a duplicate/moved and the original
  626. * is still open. The fd is removed from the epoll set
  627. * only when the original (and all the copies?) is/are
  628. * closed. This is probably a bug in epoll. --andrei */
  629. #ifdef EPOLL_NO_CLOSE_BUG
  630. if (!(flags & IO_FD_CLOSING)){
  631. #endif
  632. again_epoll:
  633. n=epoll_ctl(h->epfd, EPOLL_CTL_DEL, fd, &ep_event);
  634. if (unlikely(n==-1)){
  635. if (errno==EAGAIN) goto again_epoll;
  636. LOG(L_ERR, "ERROR: io_watch_del: removing fd from epoll "
  637. "list failed: %s [%d]\n", strerror(errno), errno);
  638. goto error;
  639. }
  640. #ifdef EPOLL_NO_CLOSE_BUG
  641. }
  642. #endif
  643. break;
  644. #endif
  645. #ifdef HAVE_KQUEUE
  646. case POLL_KQUEUE:
  647. if (!(flags & IO_FD_CLOSING)){
  648. if (likely(events & POLLIN)){
  649. if (unlikely(kq_ev_change(h, fd, EVFILT_READ,
  650. EV_DELETE, 0) ==-1)){
  651. /* try to delete the write filter anyway */
  652. if (events & POLLOUT){
  653. kq_ev_change(h, fd, EVFILT_WRITE, EV_DELETE, 0);
  654. }
  655. goto error;
  656. }
  657. }
  658. if (unlikely(events & POLLOUT)){
  659. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE,
  660. EV_DELETE, 0) ==-1))
  661. goto error;
  662. }
  663. }
  664. break;
  665. #endif
  666. #ifdef HAVE_DEVPOLL
  667. case POLL_DEVPOLL:
  668. /* for /dev/poll the closed fds _must_ be removed
  669. (they are not removed automatically on close()) */
  670. pfd.fd=fd;
  671. pfd.events=POLLREMOVE;
  672. pfd.revents=0;
  673. again_devpoll:
  674. if (write(h->dpoll_fd, &pfd, sizeof(pfd))==-1){
  675. if (errno==EINTR) goto again_devpoll;
  676. LOG(L_ERR, "ERROR: io_watch_del: removing fd from "
  677. "/dev/poll failed: %s [%d]\n",
  678. strerror(errno), errno);
  679. goto error;
  680. }
  681. break;
  682. #endif
  683. default:
  684. LOG(L_CRIT, "BUG: io_watch_del: no support for poll method "
  685. " %s (%d)\n", poll_method_str[h->poll_method],
  686. h->poll_method);
  687. goto error;
  688. }
  689. unhash_fd_map(e); /* only on success */
  690. h->fd_no--;
  691. return 0;
  692. error:
  693. return -1;
  694. #undef fix_fd_array
  695. }
  696. /* parameters: h - handler
  697. * fd - file descriptor
  698. * events - new events to watch for
  699. * idx - index in the fd_array if known, -1 if not
  700. * (if index==-1 fd_array will be searched for the
  701. * corresponding fd* entry -- slower but unavoidable in
  702. * some cases). index is not used (no fd_array) for epoll,
  703. * /dev/poll and kqueue
  704. * returns 0 if ok, -1 on error */
  705. inline static int io_watch_chg(io_wait_h* h, int fd, short events, int idx )
  706. {
  707. #define fd_array_chg(ev) \
  708. do{\
  709. if (unlikely(idx==-1)){ \
  710. /* fix idx if -1 and needed */ \
  711. for (idx=0; (idx<h->fd_no) && \
  712. (h->fd_array[idx].fd!=fd); idx++); \
  713. } \
  714. if (likely(idx<h->fd_no)){ \
  715. h->fd_array[idx].events=(ev); \
  716. } \
  717. }while(0)
  718. struct fd_map* e;
  719. int add_events;
  720. int del_events;
  721. #ifdef HAVE_DEVPOLL
  722. struct pollfd pfd;
  723. #endif
  724. #ifdef HAVE_EPOLL
  725. int n;
  726. struct epoll_event ep_event;
  727. #endif
  728. if (unlikely((fd<0) || (fd>=h->max_fd_no))){
  729. LOG(L_CRIT, "BUG: io_watch_chg: invalid fd %d, not in [0, %d) \n",
  730. fd, h->fd_no);
  731. goto error;
  732. }
  733. if (unlikely((events&(POLLIN|POLLOUT))==0)){
  734. LOG(L_CRIT, "BUG: io_watch_chg: invalid events: 0x%0x\n", events);
  735. goto error;
  736. }
  737. DBG("DBG: io_watch_chg (%p, %d, 0x%x, 0x%x) fd_no=%d called\n",
  738. h, fd, events, idx, h->fd_no);
  739. e=get_fd_map(h, fd);
  740. /* more sanity checks */
  741. if (unlikely(e==0)){
  742. LOG(L_CRIT, "BUG: io_watch_chg: no corresponding hash entry for %d\n",
  743. fd);
  744. goto error;
  745. }
  746. if (unlikely(e->type==0 /*F_NONE*/)){
  747. LOG(L_ERR, "ERROR: io_watch_chg: trying to change an already erased"
  748. " entry %d in the hash(%d, %d, %p) )\n",
  749. fd, e->fd, e->type, e->data);
  750. goto error;
  751. }
  752. add_events=events & ~e->events;
  753. del_events=e->events & ~events;
  754. switch(h->poll_method){
  755. case POLL_POLL:
  756. #ifdef POLLRDHUP
  757. fd_array_chg(events |
  758. /* listen to POLLRDHUP by default (if POLLIN) */
  759. (((int)!(events & POLLIN) - 1) & POLLRDHUP)
  760. );
  761. #else /* POLLRDHUP */
  762. fd_array_chg(events);
  763. #endif /* POLLRDHUP */
  764. break;
  765. #ifdef HAVE_SELECT
  766. case POLL_SELECT:
  767. fd_array_chg(events);
  768. if (unlikely(del_events & POLLIN))
  769. FD_CLR(fd, &h->master_rset);
  770. else if (unlikely(add_events & POLLIN))
  771. FD_SET(fd, &h->master_rset);
  772. if (likely(del_events & POLLOUT))
  773. FD_CLR(fd, &h->master_wset);
  774. else if (likely(add_events & POLLOUT))
  775. FD_SET(fd, &h->master_wset);
  776. break;
  777. #endif
  778. #ifdef HAVE_SIGIO_RT
  779. case POLL_SIGIO_RT:
  780. fd_array_chg(events);
  781. /* no need for check_io, since SIGIO_RT listens by default for all
  782. * the events */
  783. break;
  784. #endif
  785. #ifdef HAVE_EPOLL
  786. case POLL_EPOLL_LT:
  787. ep_event.events=
  788. #ifdef POLLRDHUP
  789. /* listen for EPOLLRDHUP too */
  790. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  791. #else /* POLLRDHUP */
  792. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  793. #endif /* POLLRDHUP */
  794. (EPOLLOUT & ((int)!(events & POLLOUT)-1) );
  795. ep_event.data.ptr=e;
  796. again_epoll_lt:
  797. n=epoll_ctl(h->epfd, EPOLL_CTL_MOD, fd, &ep_event);
  798. if (unlikely(n==-1)){
  799. if (errno==EAGAIN) goto again_epoll_lt;
  800. LOG(L_ERR, "ERROR: io_watch_chg: modifying epoll events"
  801. " failed: %s [%d]\n", strerror(errno), errno);
  802. goto error;
  803. }
  804. break;
  805. case POLL_EPOLL_ET:
  806. ep_event.events=
  807. #ifdef POLLRDHUP
  808. /* listen for EPOLLRDHUP too */
  809. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  810. #else /* POLLRDHUP */
  811. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  812. #endif /* POLLRDHUP */
  813. (EPOLLOUT & ((int)!(events & POLLOUT)-1) ) |
  814. EPOLLET;
  815. ep_event.data.ptr=e;
  816. again_epoll_et:
  817. n=epoll_ctl(h->epfd, EPOLL_CTL_MOD, fd, &ep_event);
  818. if (unlikely(n==-1)){
  819. if (errno==EAGAIN) goto again_epoll_et;
  820. LOG(L_ERR, "ERROR: io_watch_chg: modifying epoll events"
  821. " failed: %s [%d]\n", strerror(errno), errno);
  822. goto error;
  823. }
  824. break;
  825. #endif
  826. #ifdef HAVE_KQUEUE
  827. case POLL_KQUEUE:
  828. if (unlikely(del_events & POLLIN)){
  829. if (unlikely(kq_ev_change(h, fd, EVFILT_READ,
  830. EV_DELETE, 0) ==-1))
  831. goto error;
  832. }else if (unlikely(add_events & POLLIN)){
  833. if (unlikely(kq_ev_change(h, fd, EVFILT_READ, EV_ADD, e) ==-1))
  834. goto error;
  835. }
  836. if (likely(del_events & POLLOUT)){
  837. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE,
  838. EV_DELETE, 0) ==-1))
  839. goto error;
  840. }else if (likely(add_events & POLLOUT)){
  841. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE, EV_ADD, e)==-1))
  842. goto error;
  843. }
  844. break;
  845. #endif
  846. #ifdef HAVE_DEVPOLL
  847. case POLL_DEVPOLL:
  848. /* for /dev/poll the closed fds _must_ be removed
  849. (they are not removed automatically on close()) */
  850. pfd.fd=fd;
  851. pfd.events=POLLREMOVE;
  852. pfd.revents=0;
  853. again_devpoll1:
  854. if (unlikely(write(h->dpoll_fd, &pfd, sizeof(pfd))==-1)){
  855. if (errno==EINTR) goto again_devpoll1;
  856. LOG(L_ERR, "ERROR: io_watch_chg: removing fd from "
  857. "/dev/poll failed: %s [%d]\n",
  858. strerror(errno), errno);
  859. goto error;
  860. }
  861. again_devpoll2:
  862. pfd.events=events;
  863. pfd.revents=0;
  864. if (unlikely(write(h->dpoll_fd, &pfd, sizeof(pfd))==-1)){
  865. if (errno==EINTR) goto again_devpoll2;
  866. LOG(L_ERR, "ERROR: io_watch_chg: re-adding fd to "
  867. "/dev/poll failed: %s [%d]\n",
  868. strerror(errno), errno);
  869. /* error re-adding the fd => mark it as removed/unhash */
  870. unhash_fd_map(e);
  871. goto error;
  872. }
  873. break;
  874. #endif
  875. default:
  876. LOG(L_CRIT, "BUG: io_watch_chg: no support for poll method "
  877. " %s (%d)\n", poll_method_str[h->poll_method],
  878. h->poll_method);
  879. goto error;
  880. }
  881. e->events=events; /* only on success */
  882. return 0;
  883. error:
  884. return -1;
  885. #undef fix_fd_array
  886. }
  887. /* io_wait_loop_x style function.
  888. * wait for io using poll()
  889. * params: h - io_wait handle
  890. * t - timeout in s
  891. * repeat - if !=0 handle_io will be called until it returns <=0
  892. * returns: number of IO events handled on success (can be 0), -1 on error
  893. */
  894. inline static int io_wait_loop_poll(io_wait_h* h, int t, int repeat)
  895. {
  896. int n, r;
  897. int ret;
  898. struct fd_map* fm;
  899. again:
  900. ret=n=poll(h->fd_array, h->fd_no, t*1000);
  901. if (n==-1){
  902. if (errno==EINTR) goto again; /* signal, ignore it */
  903. else{
  904. LOG(L_ERR, "ERROR:io_wait_loop_poll: poll: %s [%d]\n",
  905. strerror(errno), errno);
  906. goto error;
  907. }
  908. }
  909. for (r=0; (r<h->fd_no) && n; r++){
  910. fm=get_fd_map(h, h->fd_array[r].fd);
  911. if (h->fd_array[r].revents & (fm->events|POLLERR|POLLHUP)){
  912. n--;
  913. /* sanity checks */
  914. if (unlikely((h->fd_array[r].fd >= h->max_fd_no)||
  915. (h->fd_array[r].fd < 0))){
  916. LOG(L_CRIT, "BUG: io_wait_loop_poll: bad fd %d "
  917. "(no in the 0 - %d range)\n",
  918. h->fd_array[r].fd, h->max_fd_no);
  919. /* try to continue anyway */
  920. h->fd_array[r].events=0; /* clear the events */
  921. continue;
  922. }
  923. h->crt_fd_array_idx=r;
  924. /* repeat handle_io if repeat, fd still watched (not deleted
  925. * inside handle_io), handle_io returns that there's still
  926. * IO and the fd is still watched for the triggering event */
  927. while(fm->type &&
  928. (handle_io(fm, h->fd_array[r].revents, r) > 0) &&
  929. repeat && ((fm->events|POLLERR|POLLHUP) &
  930. h->fd_array[r].revents));
  931. r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd)
  932. array shifting */
  933. }
  934. }
  935. error:
  936. return ret;
  937. }
  938. #ifdef HAVE_SELECT
  939. /* wait for io using select */
  940. inline static int io_wait_loop_select(io_wait_h* h, int t, int repeat)
  941. {
  942. fd_set sel_rset;
  943. fd_set sel_wset;
  944. int n, ret;
  945. struct timeval timeout;
  946. int r;
  947. struct fd_map* fm;
  948. int revents;
  949. again:
  950. sel_rset=h->master_rset;
  951. sel_wset=h->master_wset;
  952. timeout.tv_sec=t;
  953. timeout.tv_usec=0;
  954. ret=n=select(h->max_fd_select+1, &sel_rset, &sel_wset, 0, &timeout);
  955. if (n<0){
  956. if (errno==EINTR) goto again; /* just a signal */
  957. LOG(L_ERR, "ERROR: io_wait_loop_select: select: %s [%d]\n",
  958. strerror(errno), errno);
  959. n=0;
  960. /* continue */
  961. }
  962. /* use poll fd array */
  963. for(r=0; (r<h->fd_no) && n; r++){
  964. revents=0;
  965. if (likely(FD_ISSET(h->fd_array[r].fd, &sel_rset)))
  966. revents|=POLLIN;
  967. if (unlikely(FD_ISSET(h->fd_array[r].fd, &sel_wset)))
  968. revents|=POLLOUT;
  969. if (unlikely(revents)){
  970. h->crt_fd_array_idx=r;
  971. fm=get_fd_map(h, h->fd_array[r].fd);
  972. while(fm->type && (fm->events & revents) &&
  973. (handle_io(fm, revents, r)>0) && repeat);
  974. r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd)
  975. array shifting */
  976. n--;
  977. }
  978. };
  979. return ret;
  980. }
  981. #endif
  982. #ifdef HAVE_EPOLL
  983. inline static int io_wait_loop_epoll(io_wait_h* h, int t, int repeat)
  984. {
  985. int n, r;
  986. struct fd_map* fm;
  987. int revents;
  988. again:
  989. n=epoll_wait(h->epfd, h->ep_array, h->fd_no, t*1000);
  990. if (unlikely(n==-1)){
  991. if (errno==EINTR) goto again; /* signal, ignore it */
  992. else{
  993. LOG(L_ERR, "ERROR:io_wait_loop_epoll: "
  994. "epoll_wait(%d, %p, %d, %d): %s [%d]\n",
  995. h->epfd, h->ep_array, h->fd_no, t*1000,
  996. strerror(errno), errno);
  997. goto error;
  998. }
  999. }
  1000. #if 0
  1001. if (n>1){
  1002. for(r=0; r<n; r++){
  1003. LOG(L_ERR, "WARNING: ep_array[%d]= %x, %p\n",
  1004. r, h->ep_array[r].events, h->ep_array[r].data.ptr);
  1005. }
  1006. }
  1007. #endif
  1008. for (r=0; r<n; r++){
  1009. revents= (POLLIN & (!(h->ep_array[r].events & (EPOLLIN|EPOLLPRI))
  1010. -1)) |
  1011. (POLLOUT & (!(h->ep_array[r].events & EPOLLOUT)-1)) |
  1012. (POLLERR & (!(h->ep_array[r].events & EPOLLERR)-1)) |
  1013. (POLLHUP & (!(h->ep_array[r].events & EPOLLHUP)-1))
  1014. #ifdef POLLRDHUP
  1015. | (POLLRDHUP & (!(h->ep_array[r].events & EPOLLRDHUP)-1))
  1016. #endif
  1017. ;
  1018. if (likely(revents)){
  1019. fm=(struct fd_map*)h->ep_array[r].data.ptr;
  1020. while(fm->type && ((fm->events|POLLERR|POLLHUP) & revents) &&
  1021. (handle_io(fm, revents, -1)>0) && repeat);
  1022. }else{
  1023. LOG(L_ERR, "ERROR:io_wait_loop_epoll: unexpected event %x"
  1024. " on %d/%d, data=%p\n", h->ep_array[r].events,
  1025. r+1, n, h->ep_array[r].data.ptr);
  1026. }
  1027. }
  1028. error:
  1029. return n;
  1030. }
  1031. #endif
  1032. #ifdef HAVE_KQUEUE
  1033. inline static int io_wait_loop_kqueue(io_wait_h* h, int t, int repeat)
  1034. {
  1035. int n, r;
  1036. struct timespec tspec;
  1037. struct fd_map* fm;
  1038. int orig_changes;
  1039. int apply_changes;
  1040. int revents;
  1041. tspec.tv_sec=t;
  1042. tspec.tv_nsec=0;
  1043. orig_changes=h->kq_nchanges;
  1044. apply_changes=orig_changes;
  1045. do {
  1046. again:
  1047. n=kevent(h->kq_fd, h->kq_changes, apply_changes, h->kq_array,
  1048. h->kq_array_size, &tspec);
  1049. if (unlikely(n==-1)){
  1050. if (unlikely(errno==EINTR)) goto again; /* signal, ignore it */
  1051. else {
  1052. /* for a detailed explanation of what follows see below
  1053. the EV_ERROR case */
  1054. if (unlikely(!(errno==EBADF || errno==ENOENT)))
  1055. BUG("io_wait_loop_kqueue: kevent: unexpected error"
  1056. " %s [%d]\n", strerror(errno), errno);
  1057. /* some of the FDs in kq_changes are bad (already closed)
  1058. and there is not enough space in kq_array to return all
  1059. of them back */
  1060. apply_changes = h->kq_array_size;
  1061. goto again;
  1062. }
  1063. }
  1064. /* remove applied changes */
  1065. h->kq_nchanges -= apply_changes;
  1066. if (unlikely(apply_changes < orig_changes)) {
  1067. orig_changes -= apply_changes;
  1068. memmove(&h->kq_changes[0], &h->kq_changes[apply_changes],
  1069. sizeof(h->kq_changes[0])*h->kq_nchanges);
  1070. apply_changes = (orig_changes < h->kq_array_size) ? orig_changes :
  1071. h->kq_array_size;
  1072. } else {
  1073. orig_changes = 0;
  1074. apply_changes = 0;
  1075. }
  1076. for (r=0; r<n; r++){
  1077. #ifdef EXTRA_DEBUG
  1078. DBG("DBG: kqueue: event %d/%d: fd=%d, udata=%lx, flags=0x%x\n",
  1079. r, n, h->kq_array[r].ident, (long)h->kq_array[r].udata,
  1080. h->kq_array[r].flags);
  1081. #endif
  1082. if (unlikely((h->kq_array[r].flags & EV_ERROR) ||
  1083. h->kq_array[r].udata == 0)){
  1084. /* error in changes: we ignore it if it has to do with a
  1085. bad fd or update==0. It can be caused by trying to remove an
  1086. already closed fd: race between adding something to the
  1087. changes array, close() and applying the changes (EBADF).
  1088. E.g. for ser tcp: tcp_main sends a fd to child for reading
  1089. => deletes it from the watched fds => the changes array
  1090. will contain an EV_DELETE for it. Before the changes
  1091. are applied (they are at the end of the main io_wait loop,
  1092. after all the fd events were processed), a CON_ERR sent
  1093. to tcp_main by a sender (send fail) is processed and causes
  1094. the fd to be closed. When the changes are applied =>
  1095. error for the EV_DELETE attempt of a closed fd.
  1096. Something similar can happen when a fd is scheduled
  1097. for removal, is close()'ed before being removed and
  1098. re-opened(a new sock. get the same fd). When the
  1099. watched fd changes will be applied the fd will be valid
  1100. (so no EBADF), but it's not already watch => ENOENT.
  1101. We report a BUG for the other errors (there's nothing
  1102. constructive we can do if we get an error we don't know
  1103. how to handle), but apart from that we ignore it in the
  1104. idea that it is better apply the rest of the changes,
  1105. rather then dropping all of them.
  1106. */
  1107. /*
  1108. example EV_ERROR for trying to delete a read watched fd,
  1109. that was already closed:
  1110. {
  1111. ident = 63, [fd]
  1112. filter = -1, [EVFILT_READ]
  1113. flags = 16384, [EV_ERROR]
  1114. fflags = 0,
  1115. data = 9, [errno = EBADF]
  1116. udata = 0x0
  1117. }
  1118. */
  1119. if (h->kq_array[r].data != EBADF &&
  1120. h->kq_array[r].data != ENOENT)
  1121. BUG("io_wait_loop_kqueue: kevent unexpected error on "
  1122. "fd %ld udata %lx: %s [%ld]\n",
  1123. (long)h->kq_array[r].ident,
  1124. (long)h->kq_array[r].udata,
  1125. strerror(h->kq_array[r].data),
  1126. (long)h->kq_array[r].data);
  1127. }else{
  1128. fm=(struct fd_map*)h->kq_array[r].udata;
  1129. if (likely(h->kq_array[r].filter==EVFILT_READ)){
  1130. revents=POLLIN |
  1131. (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP) |
  1132. (((int)!((h->kq_array[r].flags & EV_EOF) &&
  1133. h->kq_array[r].fflags != 0) - 1)&POLLERR);
  1134. while(fm->type && (fm->events & revents) &&
  1135. (handle_io(fm, revents, -1)>0) && repeat);
  1136. }else if (h->kq_array[r].filter==EVFILT_WRITE){
  1137. revents=POLLOUT |
  1138. (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP) |
  1139. (((int)!((h->kq_array[r].flags & EV_EOF) &&
  1140. h->kq_array[r].fflags != 0) - 1)&POLLERR);
  1141. while(fm->type && (fm->events & revents) &&
  1142. (handle_io(fm, revents, -1)>0) && repeat);
  1143. }else{
  1144. BUG("io_wait_loop_kqueue: unknown filter: kqueue: event "
  1145. "%d/%d: fd=%d, filter=%d, flags=0x%x, fflags=0x%x,"
  1146. " data=%lx, udata=%lx\n",
  1147. r, n, (int)h->kq_array[r].ident, (int)h->kq_array[r].filter,
  1148. h->kq_array[r].flags, h->kq_array[r].fflags,
  1149. (unsigned long)h->kq_array[r].data,
  1150. (unsigned long)h->kq_array[r].udata);
  1151. }
  1152. }
  1153. }
  1154. } while(unlikely(orig_changes));
  1155. return n;
  1156. }
  1157. #endif
  1158. #ifdef HAVE_SIGIO_RT
  1159. /* sigio rt version has no repeat (it doesn't make sense)*/
  1160. inline static int io_wait_loop_sigio_rt(io_wait_h* h, int t)
  1161. {
  1162. int n;
  1163. int ret;
  1164. struct timespec ts;
  1165. siginfo_t siginfo;
  1166. int sigio_band;
  1167. int sigio_fd;
  1168. struct fd_map* fm;
  1169. int revents;
  1170. #ifdef SIGINFO64_WORKARROUND
  1171. int* pi;
  1172. #endif
  1173. ret=1; /* 1 event per call normally */
  1174. ts.tv_sec=t;
  1175. ts.tv_nsec=0;
  1176. if (unlikely(!sigismember(&h->sset, h->signo) ||
  1177. !sigismember(&h->sset, SIGIO))) {
  1178. LOG(L_CRIT, "BUG: io_wait_loop_sigio_rt: the signal mask"
  1179. " is not properly set!\n");
  1180. goto error;
  1181. }
  1182. again:
  1183. n=sigtimedwait(&h->sset, &siginfo, &ts);
  1184. if (unlikely(n==-1)){
  1185. if (errno==EINTR) goto again; /* some other signal, ignore it */
  1186. else if (errno==EAGAIN){ /* timeout */
  1187. ret=0;
  1188. goto end;
  1189. }else{
  1190. LOG(L_ERR, "ERROR: io_wait_loop_sigio_rt: sigtimed_wait"
  1191. " %s [%d]\n", strerror(errno), errno);
  1192. goto error;
  1193. }
  1194. }
  1195. if (likely(n!=SIGIO)){
  1196. #ifdef SIGINFO64_WORKARROUND
  1197. /* on linux siginfo.si_band is defined as long in userspace
  1198. * and as int in kernel (< 2.6.5) => on 64 bits things will break!
  1199. * (si_band will include si_fd, and si_fd will contain
  1200. * garbage).
  1201. * see /usr/src/linux/include/asm-generic/siginfo.h and
  1202. * /usr/include/bits/siginfo.h
  1203. * On newer kernels this is fixed (si_band is long in the kernel too).
  1204. * -- andrei */
  1205. if ((_os_ver<0x020605) && (sizeof(siginfo.si_band)>sizeof(int))){
  1206. pi=(int*)(void*)&siginfo.si_band; /* avoid type punning warnings */
  1207. sigio_band=*pi;
  1208. sigio_fd=*(pi+1);
  1209. }else
  1210. #endif
  1211. {
  1212. sigio_band=siginfo.si_band;
  1213. sigio_fd=siginfo.si_fd;
  1214. }
  1215. if (unlikely(siginfo.si_code==SI_SIGIO)){
  1216. /* old style, we don't know the event (linux 2.2.?) */
  1217. LOG(L_WARN, "WARNING: io_wait_loop_sigio_rt: old style sigio"
  1218. " interface\n");
  1219. fm=get_fd_map(h, sigio_fd);
  1220. /* we can have queued signals generated by fds not watched
  1221. * any more, or by fds in transition, to a child => ignore them*/
  1222. if (fm->type)
  1223. handle_io(fm, POLLIN|POLLOUT, -1);
  1224. }else{
  1225. /* si_code contains the SIGPOLL reason: POLL_IN, POLL_OUT,
  1226. * POLL_MSG, POLL_ERR, POLL_PRI or POLL_HUP
  1227. * and si_band the translated poll event bitmap:
  1228. * POLLIN|POLLRDNORM (=POLL_IN),
  1229. * POLLOUT|POLLWRNORM|POLLWRBAND (=POLL_OUT),
  1230. * POLLIN|POLLRDNORM|POLLMSG (=POLL_MSG),
  1231. * POLLERR (=POLL_ERR),
  1232. * POLLPRI|POLLRDBAND (=POLL_PRI),
  1233. * POLLHUP|POLLERR (=POLL_HUP)
  1234. * [linux 2.6.22 fs/fcntl.c:447]
  1235. */
  1236. #ifdef EXTRA_DEBUG
  1237. DBG("io_wait_loop_sigio_rt: siginfo: signal=%d (%d),"
  1238. " si_code=%d, si_band=0x%x,"
  1239. " si_fd=%d\n",
  1240. siginfo.si_signo, n, siginfo.si_code,
  1241. (unsigned)sigio_band,
  1242. sigio_fd);
  1243. #endif
  1244. /* on some errors (e.g. when receving TCP RST), sigio_band will
  1245. * be set to 0x08 (POLLERR) or 0x18 (POLLERR|POLLHUP - on stream
  1246. * unix socket close) , so better catch all events --andrei */
  1247. if (likely(sigio_band)){
  1248. fm=get_fd_map(h, sigio_fd);
  1249. revents=sigio_band;
  1250. /* fix revents==POLLPRI case */
  1251. revents |= (!(revents & POLLPRI)-1) & POLLIN;
  1252. /* we can have queued signals generated by fds not watched
  1253. * any more, or by fds in transition, to a child
  1254. * => ignore them */
  1255. if (fm->type && ((fm->events|POLLERR|POLLHUP) & revents))
  1256. handle_io(fm, revents, -1);
  1257. else
  1258. DBG("WARNING: io_wait_loop_sigio_rt: ignoring event"
  1259. " %x on fd %d, watching for %x, si_code=%x "
  1260. "(fm->type=%d, fm->fd=%d, fm->data=%p)\n",
  1261. sigio_band, sigio_fd, fm->events, siginfo.si_code,
  1262. fm->type, fm->fd, fm->data);
  1263. }else{
  1264. LOG(L_ERR, "ERROR: io_wait_loop_sigio_rt: unexpected event"
  1265. " on fd %d: %x\n", sigio_fd, sigio_band);
  1266. }
  1267. }
  1268. }else{
  1269. /* signal queue overflow
  1270. * TODO: increase signal queue size: 2.4x /proc/.., 2.6x -rlimits */
  1271. LOG(L_WARN, "WARNING: io_wait_loop_sigio_rt: signal queue overflowed"
  1272. "- falling back to poll\n");
  1273. /* clear real-time signal queue
  1274. * both SIG_IGN and SIG_DFL are needed , it doesn't work
  1275. * only with SIG_DFL */
  1276. if (signal(h->signo, SIG_IGN)==SIG_ERR){
  1277. LOG(L_CRIT, "BUG: do_poll: couldn't reset signal to IGN\n");
  1278. }
  1279. if (signal(h->signo, SIG_DFL)==SIG_ERR){
  1280. LOG(L_CRIT, "BUG: do_poll: couldn't reset signal to DFL\n");
  1281. }
  1282. /* falling back to normal poll */
  1283. ret=io_wait_loop_poll(h, -1, 1);
  1284. }
  1285. end:
  1286. return ret;
  1287. error:
  1288. return -1;
  1289. }
  1290. #endif
  1291. #ifdef HAVE_DEVPOLL
  1292. inline static int io_wait_loop_devpoll(io_wait_h* h, int t, int repeat)
  1293. {
  1294. int n, r;
  1295. int ret;
  1296. struct dvpoll dpoll;
  1297. struct fd_map* fm;
  1298. dpoll.dp_timeout=t*1000;
  1299. dpoll.dp_nfds=h->fd_no;
  1300. dpoll.dp_fds=h->fd_array;
  1301. again:
  1302. ret=n=ioctl(h->dpoll_fd, DP_POLL, &dpoll);
  1303. if (unlikely(n==-1)){
  1304. if (errno==EINTR) goto again; /* signal, ignore it */
  1305. else{
  1306. LOG(L_ERR, "ERROR:io_wait_loop_devpoll: ioctl: %s [%d]\n",
  1307. strerror(errno), errno);
  1308. goto error;
  1309. }
  1310. }
  1311. for (r=0; r< n; r++){
  1312. if (h->fd_array[r].revents & (POLLNVAL|POLLERR)){
  1313. LOG(L_ERR, "ERROR: io_wait_loop_devpoll: pollinval returned"
  1314. " for fd %d, revents=%x\n",
  1315. h->fd_array[r].fd, h->fd_array[r].revents);
  1316. }
  1317. /* POLLIN|POLLHUP just go through */
  1318. fm=get_fd_map(h, h->fd_array[r].fd);
  1319. while(fm->type && (fm->events & h->fd_array[r].revents) &&
  1320. (handle_io(fm, h->fd_array[r].revents, r) > 0) && repeat);
  1321. }
  1322. error:
  1323. return ret;
  1324. }
  1325. #endif
  1326. /* init */
  1327. /* initializes the static vars/arrays
  1328. * params: h - pointer to the io_wait_h that will be initialized
  1329. * max_fd - maximum allowed fd number
  1330. * poll_m - poll method (0 for automatic best fit)
  1331. */
  1332. int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method);
  1333. /* destroys everything init_io_wait allocated */
  1334. void destroy_io_wait(io_wait_h* h);
  1335. #endif