io_wait.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. LM_WARN("kqueue changes array full trying to flush...\n");
  225. tspec.tv_sec=0;
  226. tspec.tv_nsec=0;
  227. again:
  228. n=kevent(h->kq_fd, h->kq_changes, h->kq_nchanges, 0, 0, &tspec);
  229. if (unlikely(n == -1)){
  230. if (unlikely(errno == EINTR)) goto again;
  231. else {
  232. /* for a detailed explanation of what follows see
  233. io_wait_loop_kqueue EV_ERROR case */
  234. if (unlikely(!(errno == EBADF || errno == ENOENT)))
  235. BUG("kq_ev_change: kevent flush changes failed"
  236. " (unexpected error): %s [%d]\n",
  237. strerror(errno), errno);
  238. /* ignore error even if it's not a EBADF/ENOENT */
  239. /* one of the file descriptors is bad, probably already
  240. closed => try to apply changes one-by-one */
  241. for (r = 0; r < h->kq_nchanges; r++) {
  242. retry2:
  243. n = kevent(h->kq_fd, &h->kq_changes[r], 1, 0, 0, &tspec);
  244. if (n==-1) {
  245. if (unlikely(errno == EINTR))
  246. goto retry2;
  247. /* for a detailed explanation of what follows see
  248. io_wait_loop_kqueue EV_ERROR case */
  249. if (unlikely(!(errno == EBADF || errno == ENOENT)))
  250. BUG("kq_ev_change: kevent flush changes failed:"
  251. " (unexpected error) %s [%d] (%d/%lu)\n",
  252. strerror(errno), errno,
  253. r, (unsigned long)h->kq_nchanges);
  254. continue; /* skip over it */
  255. }
  256. }
  257. }
  258. }
  259. h->kq_nchanges=0; /* changes array is empty */
  260. }
  261. EV_SET(&h->kq_changes[h->kq_nchanges], fd, filter, flag, 0, 0,
  262. KEV_UDATA_CAST data);
  263. h->kq_nchanges++;
  264. return 0;
  265. }
  266. #endif
  267. /* generic io_watch_add function
  268. * Params:
  269. * h - pointer to initialized io_wait handle
  270. * fd - fd to watch
  271. * events - bitmap with the fd events for which the fd should be watched
  272. * (combination of POLLIN and POLLOUT)
  273. * type - fd type (non 0 value, returned in the call to handle_io)
  274. * data - pointer/private data returned in the handle_io call
  275. * returns 0 on success, -1 on error
  276. *
  277. * WARNING: handle_io() can be called immediately (from io_watch_add()) so
  278. * make sure that any dependent init. (e.g. data stuff) is made before
  279. * calling io_watch_add
  280. *
  281. * this version should be faster than pointers to poll_method specific
  282. * functions (it avoids functions calls, the overhead being only an extra
  283. * switch())*/
  284. inline static int io_watch_add( io_wait_h* h,
  285. int fd,
  286. short events,
  287. fd_type type,
  288. void* data)
  289. {
  290. /* helper macros */
  291. #define fd_array_setup(ev) \
  292. do{ \
  293. h->fd_array[h->fd_no].fd=fd; \
  294. h->fd_array[h->fd_no].events=(ev); /* useless for select */ \
  295. h->fd_array[h->fd_no].revents=0; /* useless for select */ \
  296. }while(0)
  297. #define set_fd_flags(f) \
  298. do{ \
  299. flags=fcntl(fd, F_GETFL); \
  300. if (flags==-1){ \
  301. LM_ERR("fnctl: GETFL failed: %s [%d]\n", \
  302. strerror(errno), errno); \
  303. goto error; \
  304. } \
  305. if (fcntl(fd, F_SETFL, flags|(f))==-1){ \
  306. LM_ERR("fnctl: SETFL failed: %s [%d]\n", \
  307. strerror(errno), errno); \
  308. goto error; \
  309. } \
  310. }while(0)
  311. struct fd_map* e;
  312. int flags;
  313. #ifdef HAVE_EPOLL
  314. struct epoll_event ep_event;
  315. #endif
  316. #ifdef HAVE_DEVPOLL
  317. struct pollfd pfd;
  318. #endif
  319. #if defined(HAVE_SIGIO_RT) || defined (HAVE_EPOLL)
  320. int n;
  321. #endif
  322. #if defined(HAVE_SIGIO_RT)
  323. int idx;
  324. int check_io;
  325. struct pollfd pf;
  326. check_io=0; /* set to 1 if we need to check for pre-existing queued
  327. io/data on the fd */
  328. idx=-1;
  329. #endif
  330. e=0;
  331. /* sanity checks */
  332. if (unlikely(fd==-1)){
  333. LM_CRIT("fd is -1!\n");
  334. goto error;
  335. }
  336. if (unlikely((events&(POLLIN|POLLOUT))==0)){
  337. LM_CRIT("invalid events: 0x%0x\n", events);
  338. goto error;
  339. }
  340. /* check if not too big */
  341. if (unlikely(h->fd_no>=h->max_fd_no)){
  342. LM_CRIT("maximum fd number exceeded: %d/%d\n", h->fd_no, h->max_fd_no);
  343. goto error;
  344. }
  345. DBG("DBG: io_watch_add(%p, %d, %d, %p), fd_no=%d\n",
  346. h, fd, type, data, h->fd_no);
  347. /* hash sanity check */
  348. e=get_fd_map(h, fd);
  349. if (unlikely(e && (e->type!=0 /*F_NONE*/))){
  350. LM_ERR("trying to overwrite entry %d"
  351. " watched for %x in the hash(%d, %d, %p) with (%d, %d, %p)\n",
  352. fd, events, e->fd, e->type, e->data, fd, type, data);
  353. e=0;
  354. goto error;
  355. }
  356. if (unlikely((e=hash_fd_map(h, fd, events, type, data))==0)){
  357. LM_ERR("failed to hash the fd %d\n", fd);
  358. goto error;
  359. }
  360. switch(h->poll_method){ /* faster then pointer to functions */
  361. case POLL_POLL:
  362. #ifdef POLLRDHUP
  363. /* listen to POLLRDHUP by default (if POLLIN) */
  364. events|=((int)!(events & POLLIN) - 1) & POLLRDHUP;
  365. #endif /* POLLRDHUP */
  366. fd_array_setup(events);
  367. set_fd_flags(O_NONBLOCK);
  368. break;
  369. #ifdef HAVE_SELECT
  370. case POLL_SELECT:
  371. fd_array_setup(events);
  372. if (likely(events & POLLIN))
  373. FD_SET(fd, &h->master_rset);
  374. if (unlikely(events & POLLOUT))
  375. FD_SET(fd, &h->master_wset);
  376. if (h->max_fd_select<fd) h->max_fd_select=fd;
  377. break;
  378. #endif
  379. #ifdef HAVE_SIGIO_RT
  380. case POLL_SIGIO_RT:
  381. fd_array_setup(events);
  382. /* re-set O_ASYNC might be needed, if not done from
  383. * io_watch_del (or if somebody wants to add a fd which has
  384. * already O_ASYNC/F_SETSIG set on a duplicate)
  385. */
  386. /* set async & signal */
  387. if (fcntl(fd, F_SETOWN, my_pid())==-1){
  388. LM_ERR("fnctl: SETOWN failed: %s [%d]\n",
  389. strerror(errno), errno);
  390. goto error;
  391. }
  392. if (fcntl(fd, F_SETSIG, h->signo)==-1){
  393. LM_ERR("fnctl: SETSIG failed: %s [%d]\n",
  394. strerror(errno), errno);
  395. goto error;
  396. }
  397. /* set both non-blocking and async */
  398. set_fd_flags(O_ASYNC| O_NONBLOCK);
  399. #ifdef EXTRA_DEBUG
  400. DBG("io_watch_add: sigio_rt on f %d, signal %d to pid %d\n",
  401. fd, h->signo, my_pid());
  402. #endif
  403. /* empty socket receive buffer, if buffer is already full
  404. * no more space to put packets
  405. * => no more signals are ever generated
  406. * also when moving fds, the freshly moved fd might have
  407. * already some bytes queued, we want to get them now
  408. * and not later -- andrei */
  409. idx=h->fd_no;
  410. check_io=1;
  411. break;
  412. #endif
  413. #ifdef HAVE_EPOLL
  414. case POLL_EPOLL_LT:
  415. ep_event.events=
  416. #ifdef POLLRDHUP
  417. /* listen for EPOLLRDHUP too */
  418. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  419. #else /* POLLRDHUP */
  420. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  421. #endif /* POLLRDHUP */
  422. (EPOLLOUT & ((int)!(events & POLLOUT)-1) );
  423. ep_event.data.ptr=e;
  424. again1:
  425. n=epoll_ctl(h->epfd, EPOLL_CTL_ADD, fd, &ep_event);
  426. if (unlikely(n==-1)){
  427. if (errno==EAGAIN) goto again1;
  428. LM_ERR("epoll_ctl failed: %s [%d]\n", strerror(errno), errno);
  429. goto error;
  430. }
  431. break;
  432. case POLL_EPOLL_ET:
  433. set_fd_flags(O_NONBLOCK);
  434. ep_event.events=
  435. #ifdef POLLRDHUP
  436. /* listen for EPOLLRDHUP too */
  437. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  438. #else /* POLLRDHUP */
  439. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  440. #endif /* POLLRDHUP */
  441. (EPOLLOUT & ((int)!(events & POLLOUT)-1) ) |
  442. EPOLLET;
  443. ep_event.data.ptr=e;
  444. again2:
  445. n=epoll_ctl(h->epfd, EPOLL_CTL_ADD, fd, &ep_event);
  446. if (unlikely(n==-1)){
  447. if (errno==EAGAIN) goto again2;
  448. LM_ERR("epoll_ctl failed: %s [%d]\n", strerror(errno), errno);
  449. goto error;
  450. }
  451. break;
  452. #endif
  453. #ifdef HAVE_KQUEUE
  454. case POLL_KQUEUE:
  455. if (likely( events & POLLIN)){
  456. if (unlikely(kq_ev_change(h, fd, EVFILT_READ, EV_ADD, e)==-1))
  457. goto error;
  458. }
  459. if (unlikely( events & POLLOUT)){
  460. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE, EV_ADD, e)==-1))
  461. {
  462. if (likely(events & POLLIN)){
  463. kq_ev_change(h, fd, EVFILT_READ, EV_DELETE, 0);
  464. }
  465. goto error;
  466. }
  467. }
  468. break;
  469. #endif
  470. #ifdef HAVE_DEVPOLL
  471. case POLL_DEVPOLL:
  472. pfd.fd=fd;
  473. pfd.events=events;
  474. pfd.revents=0;
  475. again_devpoll:
  476. if (write(h->dpoll_fd, &pfd, sizeof(pfd))==-1){
  477. if (errno==EAGAIN) goto again_devpoll;
  478. LM_ERR("/dev/poll write failed: %s [%d]\n",
  479. strerror(errno), errno);
  480. goto error;
  481. }
  482. break;
  483. #endif
  484. default:
  485. LM_CRIT("no support for poll method %s (%d)\n",
  486. poll_method_str[h->poll_method], h->poll_method);
  487. goto error;
  488. }
  489. h->fd_no++; /* "activate" changes, for epoll/kqueue/devpoll it
  490. has only informative value */
  491. #if defined(HAVE_SIGIO_RT)
  492. if (check_io){
  493. /* handle possible pre-existing events */
  494. pf.fd=fd;
  495. pf.events=events;
  496. check_io_again:
  497. n=0;
  498. while(e->type && ((n=poll(&pf, 1, 0))>0) &&
  499. (handle_io(e, pf.revents, idx)>0) &&
  500. (pf.revents & (e->events|POLLERR|POLLHUP)));
  501. if (unlikely(e->type && (n==-1))){
  502. if (errno==EINTR) goto check_io_again;
  503. LM_ERR("check_io poll: %s [%d]\n", strerror(errno), errno);
  504. }
  505. }
  506. #endif
  507. return 0;
  508. error:
  509. if (e) unhash_fd_map(e);
  510. return -1;
  511. #undef fd_array_setup
  512. #undef set_fd_flags
  513. }
  514. #define IO_FD_CLOSING 16
  515. /* parameters: h - handler
  516. * fd - file descriptor
  517. * index - index in the fd_array if known, -1 if not
  518. * (if index==-1 fd_array will be searched for the
  519. * corresponding fd* entry -- slower but unavoidable in
  520. * some cases). index is not used (no fd_array) for epoll,
  521. * /dev/poll and kqueue
  522. * flags - optimization flags, e.g. IO_FD_CLOSING, the fd was
  523. * or will shortly be closed, in some cases we can avoid
  524. * extra remove operations (e.g.: epoll, kqueue, sigio)
  525. * returns 0 if ok, -1 on error */
  526. inline static int io_watch_del(io_wait_h* h, int fd, int idx, int flags)
  527. {
  528. #define fix_fd_array \
  529. do{\
  530. if (unlikely(idx==-1)){ \
  531. /* fix idx if -1 and needed */ \
  532. for (idx=0; (idx<h->fd_no) && \
  533. (h->fd_array[idx].fd!=fd); idx++); \
  534. } \
  535. if (likely(idx<h->fd_no)){ \
  536. memmove(&h->fd_array[idx], &h->fd_array[idx+1], \
  537. (h->fd_no-(idx+1))*sizeof(*(h->fd_array))); \
  538. if ((idx<=h->crt_fd_array_idx) && (h->crt_fd_array_idx>=0)) \
  539. h->crt_fd_array_idx--; \
  540. } \
  541. }while(0)
  542. struct fd_map* e;
  543. int events;
  544. #ifdef HAVE_EPOLL
  545. int n;
  546. struct epoll_event ep_event;
  547. #endif
  548. #ifdef HAVE_DEVPOLL
  549. struct pollfd pfd;
  550. #endif
  551. #ifdef HAVE_SIGIO_RT
  552. int fd_flags;
  553. #endif
  554. if (unlikely((fd<0) || (fd>=h->max_fd_no))){
  555. LM_CRIT("invalid fd %d, not in [0, %d) \n", fd, h->fd_no);
  556. goto error;
  557. }
  558. DBG("DBG: io_watch_del (%p, %d, %d, 0x%x) fd_no=%d called\n",
  559. h, fd, idx, flags, h->fd_no);
  560. e=get_fd_map(h, fd);
  561. /* more sanity checks */
  562. if (unlikely(e==0)){
  563. LM_CRIT("no corresponding hash entry for %d\n", fd);
  564. goto error;
  565. }
  566. if (unlikely(e->type==0 /*F_NONE*/)){
  567. LM_ERR("trying to delete already erased"
  568. " entry %d in the hash(%d, %d, %p) flags %x)\n",
  569. fd, e->fd, e->type, e->data, flags);
  570. goto error;
  571. }
  572. events=e->events;
  573. switch(h->poll_method){
  574. case POLL_POLL:
  575. fix_fd_array;
  576. break;
  577. #ifdef HAVE_SELECT
  578. case POLL_SELECT:
  579. if (likely(events & POLLIN))
  580. FD_CLR(fd, &h->master_rset);
  581. if (unlikely(events & POLLOUT))
  582. FD_CLR(fd, &h->master_wset);
  583. if (unlikely(h->max_fd_select && (h->max_fd_select==fd)))
  584. /* we don't know the prev. max, so we just decrement it */
  585. h->max_fd_select--;
  586. fix_fd_array;
  587. break;
  588. #endif
  589. #ifdef HAVE_SIGIO_RT
  590. case POLL_SIGIO_RT:
  591. /* the O_ASYNC flag must be reset all the time, the fd
  592. * can be changed only if O_ASYNC is reset (if not and
  593. * the fd is a duplicate, you will get signals from the dup. fd
  594. * and not from the original, even if the dup. fd was closed
  595. * and the signals re-set on the original) -- andrei
  596. */
  597. /*if (!(flags & IO_FD_CLOSING)){*/
  598. /* reset ASYNC */
  599. fd_flags=fcntl(fd, F_GETFL);
  600. if (unlikely(fd_flags==-1)){
  601. LM_ERR("fnctl: GETFL failed: %s [%d]\n",
  602. strerror(errno), errno);
  603. goto error;
  604. }
  605. if (unlikely(fcntl(fd, F_SETFL, fd_flags&(~O_ASYNC))==-1)){
  606. LM_ERR("fnctl: SETFL failed: %s [%d]\n",
  607. strerror(errno), errno);
  608. goto error;
  609. }
  610. fix_fd_array; /* only on success */
  611. break;
  612. #endif
  613. #ifdef HAVE_EPOLL
  614. case POLL_EPOLL_LT:
  615. case POLL_EPOLL_ET:
  616. /* epoll doesn't seem to automatically remove sockets,
  617. * if the socket is a duplicate/moved and the original
  618. * is still open. The fd is removed from the epoll set
  619. * only when the original (and all the copies?) is/are
  620. * closed. This is probably a bug in epoll. --andrei */
  621. #ifdef EPOLL_NO_CLOSE_BUG
  622. if (!(flags & IO_FD_CLOSING)){
  623. #endif
  624. again_epoll:
  625. n=epoll_ctl(h->epfd, EPOLL_CTL_DEL, fd, &ep_event);
  626. if (unlikely(n==-1)){
  627. if (errno==EAGAIN) goto again_epoll;
  628. LM_ERR("removing fd from epoll list failed: %s [%d]\n",
  629. strerror(errno), errno);
  630. goto error;
  631. }
  632. #ifdef EPOLL_NO_CLOSE_BUG
  633. }
  634. #endif
  635. break;
  636. #endif
  637. #ifdef HAVE_KQUEUE
  638. case POLL_KQUEUE:
  639. if (!(flags & IO_FD_CLOSING)){
  640. if (likely(events & POLLIN)){
  641. if (unlikely(kq_ev_change(h, fd, EVFILT_READ,
  642. EV_DELETE, 0) ==-1)){
  643. /* try to delete the write filter anyway */
  644. if (events & POLLOUT){
  645. kq_ev_change(h, fd, EVFILT_WRITE, EV_DELETE, 0);
  646. }
  647. goto error;
  648. }
  649. }
  650. if (unlikely(events & POLLOUT)){
  651. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE,
  652. EV_DELETE, 0) ==-1))
  653. goto error;
  654. }
  655. }
  656. break;
  657. #endif
  658. #ifdef HAVE_DEVPOLL
  659. case POLL_DEVPOLL:
  660. /* for /dev/poll the closed fds _must_ be removed
  661. (they are not removed automatically on close()) */
  662. pfd.fd=fd;
  663. pfd.events=POLLREMOVE;
  664. pfd.revents=0;
  665. again_devpoll:
  666. if (write(h->dpoll_fd, &pfd, sizeof(pfd))==-1){
  667. if (errno==EINTR) goto again_devpoll;
  668. LM_ERR("removing fd from /dev/poll failed: %s [%d]\n",
  669. strerror(errno), errno);
  670. goto error;
  671. }
  672. break;
  673. #endif
  674. default:
  675. LM_CRIT("no support for poll method %s (%d)\n",
  676. poll_method_str[h->poll_method], h->poll_method);
  677. goto error;
  678. }
  679. unhash_fd_map(e); /* only on success */
  680. h->fd_no--;
  681. return 0;
  682. error:
  683. return -1;
  684. #undef fix_fd_array
  685. }
  686. /* parameters: h - handler
  687. * fd - file descriptor
  688. * events - new events to watch for
  689. * idx - index in the fd_array if known, -1 if not
  690. * (if index==-1 fd_array will be searched for the
  691. * corresponding fd* entry -- slower but unavoidable in
  692. * some cases). index is not used (no fd_array) for epoll,
  693. * /dev/poll and kqueue
  694. * returns 0 if ok, -1 on error */
  695. inline static int io_watch_chg(io_wait_h* h, int fd, short events, int idx )
  696. {
  697. #define fd_array_chg(ev) \
  698. do{\
  699. if (unlikely(idx==-1)){ \
  700. /* fix idx if -1 and needed */ \
  701. for (idx=0; (idx<h->fd_no) && \
  702. (h->fd_array[idx].fd!=fd); idx++); \
  703. } \
  704. if (likely(idx<h->fd_no)){ \
  705. h->fd_array[idx].events=(ev); \
  706. } \
  707. }while(0)
  708. struct fd_map* e;
  709. int add_events;
  710. int del_events;
  711. #ifdef HAVE_DEVPOLL
  712. struct pollfd pfd;
  713. #endif
  714. #ifdef HAVE_EPOLL
  715. int n;
  716. struct epoll_event ep_event;
  717. #endif
  718. if (unlikely((fd<0) || (fd>=h->max_fd_no))){
  719. LM_CRIT("invalid fd %d, not in [0, %d) \n", fd, h->fd_no);
  720. goto error;
  721. }
  722. if (unlikely((events&(POLLIN|POLLOUT))==0)){
  723. LM_CRIT("invalid events: 0x%0x\n", events);
  724. goto error;
  725. }
  726. DBG("DBG: io_watch_chg (%p, %d, 0x%x, 0x%x) fd_no=%d called\n",
  727. h, fd, events, idx, h->fd_no);
  728. e=get_fd_map(h, fd);
  729. /* more sanity checks */
  730. if (unlikely(e==0)){
  731. LM_CRIT("no corresponding hash entry for %d\n", fd);
  732. goto error;
  733. }
  734. if (unlikely(e->type==0 /*F_NONE*/)){
  735. LM_ERR("trying to change an already erased"
  736. " entry %d in the hash(%d, %d, %p) )\n",
  737. fd, e->fd, e->type, e->data);
  738. goto error;
  739. }
  740. add_events=events & ~e->events;
  741. del_events=e->events & ~events;
  742. switch(h->poll_method){
  743. case POLL_POLL:
  744. #ifdef POLLRDHUP
  745. fd_array_chg(events |
  746. /* listen to POLLRDHUP by default (if POLLIN) */
  747. (((int)!(events & POLLIN) - 1) & POLLRDHUP)
  748. );
  749. #else /* POLLRDHUP */
  750. fd_array_chg(events);
  751. #endif /* POLLRDHUP */
  752. break;
  753. #ifdef HAVE_SELECT
  754. case POLL_SELECT:
  755. fd_array_chg(events);
  756. if (unlikely(del_events & POLLIN))
  757. FD_CLR(fd, &h->master_rset);
  758. else if (unlikely(add_events & POLLIN))
  759. FD_SET(fd, &h->master_rset);
  760. if (likely(del_events & POLLOUT))
  761. FD_CLR(fd, &h->master_wset);
  762. else if (likely(add_events & POLLOUT))
  763. FD_SET(fd, &h->master_wset);
  764. break;
  765. #endif
  766. #ifdef HAVE_SIGIO_RT
  767. case POLL_SIGIO_RT:
  768. fd_array_chg(events);
  769. /* no need for check_io, since SIGIO_RT listens by default for all
  770. * the events */
  771. break;
  772. #endif
  773. #ifdef HAVE_EPOLL
  774. case POLL_EPOLL_LT:
  775. ep_event.events=
  776. #ifdef POLLRDHUP
  777. /* listen for EPOLLRDHUP too */
  778. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  779. #else /* POLLRDHUP */
  780. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  781. #endif /* POLLRDHUP */
  782. (EPOLLOUT & ((int)!(events & POLLOUT)-1) );
  783. ep_event.data.ptr=e;
  784. again_epoll_lt:
  785. n=epoll_ctl(h->epfd, EPOLL_CTL_MOD, fd, &ep_event);
  786. if (unlikely(n==-1)){
  787. if (errno==EAGAIN) goto again_epoll_lt;
  788. LM_ERR("modifying epoll events failed: %s [%d]\n",
  789. strerror(errno), errno);
  790. goto error;
  791. }
  792. break;
  793. case POLL_EPOLL_ET:
  794. ep_event.events=
  795. #ifdef POLLRDHUP
  796. /* listen for EPOLLRDHUP too */
  797. ((EPOLLIN|EPOLLRDHUP) & ((int)!(events & POLLIN)-1) ) |
  798. #else /* POLLRDHUP */
  799. (EPOLLIN & ((int)!(events & POLLIN)-1) ) |
  800. #endif /* POLLRDHUP */
  801. (EPOLLOUT & ((int)!(events & POLLOUT)-1) ) |
  802. EPOLLET;
  803. ep_event.data.ptr=e;
  804. again_epoll_et:
  805. n=epoll_ctl(h->epfd, EPOLL_CTL_MOD, fd, &ep_event);
  806. if (unlikely(n==-1)){
  807. if (errno==EAGAIN) goto again_epoll_et;
  808. LM_ERR("modifying epoll events failed: %s [%d]\n",
  809. strerror(errno), errno);
  810. goto error;
  811. }
  812. break;
  813. #endif
  814. #ifdef HAVE_KQUEUE
  815. case POLL_KQUEUE:
  816. if (unlikely(del_events & POLLIN)){
  817. if (unlikely(kq_ev_change(h, fd, EVFILT_READ,
  818. EV_DELETE, 0) ==-1))
  819. goto error;
  820. }else if (unlikely(add_events & POLLIN)){
  821. if (unlikely(kq_ev_change(h, fd, EVFILT_READ, EV_ADD, e) ==-1))
  822. goto error;
  823. }
  824. if (likely(del_events & POLLOUT)){
  825. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE,
  826. EV_DELETE, 0) ==-1))
  827. goto error;
  828. }else if (likely(add_events & POLLOUT)){
  829. if (unlikely(kq_ev_change(h, fd, EVFILT_WRITE, EV_ADD, e)==-1))
  830. goto error;
  831. }
  832. break;
  833. #endif
  834. #ifdef HAVE_DEVPOLL
  835. case POLL_DEVPOLL:
  836. /* for /dev/poll the closed fds _must_ be removed
  837. (they are not removed automatically on close()) */
  838. pfd.fd=fd;
  839. pfd.events=POLLREMOVE;
  840. pfd.revents=0;
  841. again_devpoll1:
  842. if (unlikely(write(h->dpoll_fd, &pfd, sizeof(pfd))==-1)){
  843. if (errno==EINTR) goto again_devpoll1;
  844. LM_ERR("removing fd from /dev/poll failed: %s [%d]\n",
  845. strerror(errno), errno);
  846. goto error;
  847. }
  848. again_devpoll2:
  849. pfd.events=events;
  850. pfd.revents=0;
  851. if (unlikely(write(h->dpoll_fd, &pfd, sizeof(pfd))==-1)){
  852. if (errno==EINTR) goto again_devpoll2;
  853. LM_ERR("re-adding fd to /dev/poll failed: %s [%d]\n",
  854. strerror(errno), errno);
  855. /* error re-adding the fd => mark it as removed/unhash */
  856. unhash_fd_map(e);
  857. goto error;
  858. }
  859. break;
  860. #endif
  861. default:
  862. LM_CRIT("no support for poll method %s (%d)\n",
  863. poll_method_str[h->poll_method], h->poll_method);
  864. goto error;
  865. }
  866. e->events=events; /* only on success */
  867. return 0;
  868. error:
  869. return -1;
  870. #undef fix_fd_array
  871. }
  872. /* io_wait_loop_x style function.
  873. * wait for io using poll()
  874. * params: h - io_wait handle
  875. * t - timeout in s
  876. * repeat - if !=0 handle_io will be called until it returns <=0
  877. * returns: number of IO events handled on success (can be 0), -1 on error
  878. */
  879. inline static int io_wait_loop_poll(io_wait_h* h, int t, int repeat)
  880. {
  881. int n, r;
  882. int ret;
  883. struct fd_map* fm;
  884. again:
  885. ret=n=poll(h->fd_array, h->fd_no, t*1000);
  886. if (n==-1){
  887. if (errno==EINTR) goto again; /* signal, ignore it */
  888. else{
  889. LM_ERR("poll: %s [%d]\n", strerror(errno), errno);
  890. goto error;
  891. }
  892. }
  893. for (r=0; (r<h->fd_no) && n; r++){
  894. fm=get_fd_map(h, h->fd_array[r].fd);
  895. if (h->fd_array[r].revents & (fm->events|POLLERR|POLLHUP)){
  896. n--;
  897. /* sanity checks */
  898. if (unlikely((h->fd_array[r].fd >= h->max_fd_no)||
  899. (h->fd_array[r].fd < 0))){
  900. LM_CRIT("bad fd %d (no in the 0 - %d range)\n",
  901. h->fd_array[r].fd, h->max_fd_no);
  902. /* try to continue anyway */
  903. h->fd_array[r].events=0; /* clear the events */
  904. continue;
  905. }
  906. h->crt_fd_array_idx=r;
  907. /* repeat handle_io if repeat, fd still watched (not deleted
  908. * inside handle_io), handle_io returns that there's still
  909. * IO and the fd is still watched for the triggering event */
  910. while(fm->type &&
  911. (handle_io(fm, h->fd_array[r].revents, r) > 0) &&
  912. repeat && ((fm->events|POLLERR|POLLHUP) &
  913. h->fd_array[r].revents));
  914. r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd)
  915. array shifting */
  916. }
  917. }
  918. error:
  919. return ret;
  920. }
  921. #ifdef HAVE_SELECT
  922. /* wait for io using select */
  923. inline static int io_wait_loop_select(io_wait_h* h, int t, int repeat)
  924. {
  925. fd_set sel_rset;
  926. fd_set sel_wset;
  927. int n, ret;
  928. struct timeval timeout;
  929. int r;
  930. struct fd_map* fm;
  931. int revents;
  932. again:
  933. sel_rset=h->master_rset;
  934. sel_wset=h->master_wset;
  935. timeout.tv_sec=t;
  936. timeout.tv_usec=0;
  937. ret=n=select(h->max_fd_select+1, &sel_rset, &sel_wset, 0, &timeout);
  938. if (n<0){
  939. if (errno==EINTR) goto again; /* just a signal */
  940. LM_ERR("select: %s [%d]\n", strerror(errno), errno);
  941. n=0;
  942. /* continue */
  943. }
  944. /* use poll fd array */
  945. for(r=0; (r<h->fd_no) && n; r++){
  946. revents=0;
  947. if (likely(FD_ISSET(h->fd_array[r].fd, &sel_rset)))
  948. revents|=POLLIN;
  949. if (unlikely(FD_ISSET(h->fd_array[r].fd, &sel_wset)))
  950. revents|=POLLOUT;
  951. if (unlikely(revents)){
  952. h->crt_fd_array_idx=r;
  953. fm=get_fd_map(h, h->fd_array[r].fd);
  954. while(fm->type && (fm->events & revents) &&
  955. (handle_io(fm, revents, r)>0) && repeat);
  956. r=h->crt_fd_array_idx; /* can change due to io_watch_del(fd)
  957. array shifting */
  958. n--;
  959. }
  960. };
  961. return ret;
  962. }
  963. #endif
  964. #ifdef HAVE_EPOLL
  965. inline static int io_wait_loop_epoll(io_wait_h* h, int t, int repeat)
  966. {
  967. int n, r;
  968. struct fd_map* fm;
  969. int revents;
  970. again:
  971. n=epoll_wait(h->epfd, h->ep_array, h->fd_no, t*1000);
  972. if (unlikely(n==-1)){
  973. if (errno==EINTR) goto again; /* signal, ignore it */
  974. else{
  975. LM_ERR("epoll_wait(%d, %p, %d, %d): %s [%d]\n",
  976. h->epfd, h->ep_array, h->fd_no, t*1000,
  977. strerror(errno), errno);
  978. goto error;
  979. }
  980. }
  981. #if 0
  982. if (n>1){
  983. for(r=0; r<n; r++){
  984. LM_ERR("ep_array[%d]= %x, %p\n",
  985. r, h->ep_array[r].events, h->ep_array[r].data.ptr);
  986. }
  987. }
  988. #endif
  989. for (r=0; r<n; r++){
  990. revents= (POLLIN & (!(h->ep_array[r].events & (EPOLLIN|EPOLLPRI))
  991. -1)) |
  992. (POLLOUT & (!(h->ep_array[r].events & EPOLLOUT)-1)) |
  993. (POLLERR & (!(h->ep_array[r].events & EPOLLERR)-1)) |
  994. (POLLHUP & (!(h->ep_array[r].events & EPOLLHUP)-1))
  995. #ifdef POLLRDHUP
  996. | (POLLRDHUP & (!(h->ep_array[r].events & EPOLLRDHUP)-1))
  997. #endif
  998. ;
  999. if (likely(revents)){
  1000. fm=(struct fd_map*)h->ep_array[r].data.ptr;
  1001. while(fm->type && ((fm->events|POLLERR|POLLHUP) & revents) &&
  1002. (handle_io(fm, revents, -1)>0) && repeat);
  1003. }else{
  1004. LM_ERR("unexpected event %x on %d/%d, data=%p\n",
  1005. h->ep_array[r].events, r+1, n, h->ep_array[r].data.ptr);
  1006. }
  1007. }
  1008. error:
  1009. return n;
  1010. }
  1011. #endif
  1012. #ifdef HAVE_KQUEUE
  1013. inline static int io_wait_loop_kqueue(io_wait_h* h, int t, int repeat)
  1014. {
  1015. int n, r;
  1016. struct timespec tspec;
  1017. struct fd_map* fm;
  1018. int orig_changes;
  1019. int apply_changes;
  1020. int revents;
  1021. tspec.tv_sec=t;
  1022. tspec.tv_nsec=0;
  1023. orig_changes=h->kq_nchanges;
  1024. apply_changes=orig_changes;
  1025. do {
  1026. again:
  1027. n=kevent(h->kq_fd, h->kq_changes, apply_changes, h->kq_array,
  1028. h->kq_array_size, &tspec);
  1029. if (unlikely(n==-1)){
  1030. if (unlikely(errno==EINTR)) goto again; /* signal, ignore it */
  1031. else {
  1032. /* for a detailed explanation of what follows see below
  1033. the EV_ERROR case */
  1034. if (unlikely(!(errno==EBADF || errno==ENOENT)))
  1035. BUG("io_wait_loop_kqueue: kevent: unexpected error"
  1036. " %s [%d]\n", strerror(errno), errno);
  1037. /* some of the FDs in kq_changes are bad (already closed)
  1038. and there is not enough space in kq_array to return all
  1039. of them back */
  1040. apply_changes = h->kq_array_size;
  1041. goto again;
  1042. }
  1043. }
  1044. /* remove applied changes */
  1045. h->kq_nchanges -= apply_changes;
  1046. if (unlikely(apply_changes < orig_changes)) {
  1047. orig_changes -= apply_changes;
  1048. memmove(&h->kq_changes[0], &h->kq_changes[apply_changes],
  1049. sizeof(h->kq_changes[0])*h->kq_nchanges);
  1050. apply_changes = (orig_changes < h->kq_array_size) ? orig_changes :
  1051. h->kq_array_size;
  1052. } else {
  1053. orig_changes = 0;
  1054. apply_changes = 0;
  1055. }
  1056. for (r=0; r<n; r++){
  1057. #ifdef EXTRA_DEBUG
  1058. DBG("DBG: kqueue: event %d/%d: fd=%d, udata=%lx, flags=0x%x\n",
  1059. r, n, h->kq_array[r].ident, (long)h->kq_array[r].udata,
  1060. h->kq_array[r].flags);
  1061. #endif
  1062. if (unlikely((h->kq_array[r].flags & EV_ERROR) ||
  1063. h->kq_array[r].udata == 0)){
  1064. /* error in changes: we ignore it if it has to do with a
  1065. bad fd or update==0. It can be caused by trying to remove an
  1066. already closed fd: race between adding something to the
  1067. changes array, close() and applying the changes (EBADF).
  1068. E.g. for ser tcp: tcp_main sends a fd to child for reading
  1069. => deletes it from the watched fds => the changes array
  1070. will contain an EV_DELETE for it. Before the changes
  1071. are applied (they are at the end of the main io_wait loop,
  1072. after all the fd events were processed), a CON_ERR sent
  1073. to tcp_main by a sender (send fail) is processed and causes
  1074. the fd to be closed. When the changes are applied =>
  1075. error for the EV_DELETE attempt of a closed fd.
  1076. Something similar can happen when a fd is scheduled
  1077. for removal, is close()'ed before being removed and
  1078. re-opened(a new sock. get the same fd). When the
  1079. watched fd changes will be applied the fd will be valid
  1080. (so no EBADF), but it's not already watch => ENOENT.
  1081. We report a BUG for the other errors (there's nothing
  1082. constructive we can do if we get an error we don't know
  1083. how to handle), but apart from that we ignore it in the
  1084. idea that it is better apply the rest of the changes,
  1085. rather then dropping all of them.
  1086. */
  1087. /*
  1088. example EV_ERROR for trying to delete a read watched fd,
  1089. that was already closed:
  1090. {
  1091. ident = 63, [fd]
  1092. filter = -1, [EVFILT_READ]
  1093. flags = 16384, [EV_ERROR]
  1094. fflags = 0,
  1095. data = 9, [errno = EBADF]
  1096. udata = 0x0
  1097. }
  1098. */
  1099. if (h->kq_array[r].data != EBADF &&
  1100. h->kq_array[r].data != ENOENT)
  1101. BUG("io_wait_loop_kqueue: kevent unexpected error on "
  1102. "fd %ld udata %lx: %s [%ld]\n",
  1103. (long)h->kq_array[r].ident,
  1104. (long)h->kq_array[r].udata,
  1105. strerror(h->kq_array[r].data),
  1106. (long)h->kq_array[r].data);
  1107. }else{
  1108. fm=(struct fd_map*)h->kq_array[r].udata;
  1109. if (likely(h->kq_array[r].filter==EVFILT_READ)){
  1110. revents=POLLIN |
  1111. (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP) |
  1112. (((int)!((h->kq_array[r].flags & EV_EOF) &&
  1113. h->kq_array[r].fflags != 0) - 1)&POLLERR);
  1114. while(fm->type && (fm->events & revents) &&
  1115. (handle_io(fm, revents, -1)>0) && repeat);
  1116. }else if (h->kq_array[r].filter==EVFILT_WRITE){
  1117. revents=POLLOUT |
  1118. (((int)!(h->kq_array[r].flags & EV_EOF)-1)&POLLHUP) |
  1119. (((int)!((h->kq_array[r].flags & EV_EOF) &&
  1120. h->kq_array[r].fflags != 0) - 1)&POLLERR);
  1121. while(fm->type && (fm->events & revents) &&
  1122. (handle_io(fm, revents, -1)>0) && repeat);
  1123. }else{
  1124. BUG("io_wait_loop_kqueue: unknown filter: kqueue: event "
  1125. "%d/%d: fd=%d, filter=%d, flags=0x%x, fflags=0x%x,"
  1126. " data=%lx, udata=%lx\n",
  1127. r, n, (int)h->kq_array[r].ident, (int)h->kq_array[r].filter,
  1128. h->kq_array[r].flags, h->kq_array[r].fflags,
  1129. (unsigned long)h->kq_array[r].data,
  1130. (unsigned long)h->kq_array[r].udata);
  1131. }
  1132. }
  1133. }
  1134. } while(unlikely(orig_changes));
  1135. return n;
  1136. }
  1137. #endif
  1138. #ifdef HAVE_SIGIO_RT
  1139. /* sigio rt version has no repeat (it doesn't make sense)*/
  1140. inline static int io_wait_loop_sigio_rt(io_wait_h* h, int t)
  1141. {
  1142. int n;
  1143. int ret;
  1144. struct timespec ts;
  1145. siginfo_t siginfo;
  1146. int sigio_band;
  1147. int sigio_fd;
  1148. struct fd_map* fm;
  1149. int revents;
  1150. #ifdef SIGINFO64_WORKARROUND
  1151. int* pi;
  1152. #endif
  1153. ret=1; /* 1 event per call normally */
  1154. ts.tv_sec=t;
  1155. ts.tv_nsec=0;
  1156. if (unlikely(!sigismember(&h->sset, h->signo) ||
  1157. !sigismember(&h->sset, SIGIO))) {
  1158. LM_CRIT("the signal mask is not properly set!\n");
  1159. goto error;
  1160. }
  1161. again:
  1162. n=sigtimedwait(&h->sset, &siginfo, &ts);
  1163. if (unlikely(n==-1)){
  1164. if (errno==EINTR) goto again; /* some other signal, ignore it */
  1165. else if (errno==EAGAIN){ /* timeout */
  1166. ret=0;
  1167. goto end;
  1168. }else{
  1169. LM_ERR("sigtimed_wait %s [%d]\n", strerror(errno), errno);
  1170. goto error;
  1171. }
  1172. }
  1173. if (likely(n!=SIGIO)){
  1174. #ifdef SIGINFO64_WORKARROUND
  1175. /* on linux siginfo.si_band is defined as long in userspace
  1176. * and as int in kernel (< 2.6.5) => on 64 bits things will break!
  1177. * (si_band will include si_fd, and si_fd will contain
  1178. * garbage).
  1179. * see /usr/src/linux/include/asm-generic/siginfo.h and
  1180. * /usr/include/bits/siginfo.h
  1181. * On newer kernels this is fixed (si_band is long in the kernel too).
  1182. * -- andrei */
  1183. if ((_os_ver<0x020605) && (sizeof(siginfo.si_band)>sizeof(int))){
  1184. pi=(int*)(void*)&siginfo.si_band; /* avoid type punning warnings */
  1185. sigio_band=*pi;
  1186. sigio_fd=*(pi+1);
  1187. }else
  1188. #endif
  1189. {
  1190. sigio_band=siginfo.si_band;
  1191. sigio_fd=siginfo.si_fd;
  1192. }
  1193. if (unlikely(siginfo.si_code==SI_SIGIO)){
  1194. /* old style, we don't know the event (linux 2.2.?) */
  1195. LM_WARN("old style sigio interface\n");
  1196. fm=get_fd_map(h, sigio_fd);
  1197. /* we can have queued signals generated by fds not watched
  1198. * any more, or by fds in transition, to a child => ignore them*/
  1199. if (fm->type)
  1200. handle_io(fm, POLLIN|POLLOUT, -1);
  1201. }else{
  1202. /* si_code contains the SIGPOLL reason: POLL_IN, POLL_OUT,
  1203. * POLL_MSG, POLL_ERR, POLL_PRI or POLL_HUP
  1204. * and si_band the translated poll event bitmap:
  1205. * POLLIN|POLLRDNORM (=POLL_IN),
  1206. * POLLOUT|POLLWRNORM|POLLWRBAND (=POLL_OUT),
  1207. * POLLIN|POLLRDNORM|POLLMSG (=POLL_MSG),
  1208. * POLLERR (=POLL_ERR),
  1209. * POLLPRI|POLLRDBAND (=POLL_PRI),
  1210. * POLLHUP|POLLERR (=POLL_HUP)
  1211. * [linux 2.6.22 fs/fcntl.c:447]
  1212. */
  1213. #ifdef EXTRA_DEBUG
  1214. DBG("io_wait_loop_sigio_rt: siginfo: signal=%d (%d),"
  1215. " si_code=%d, si_band=0x%x,"
  1216. " si_fd=%d\n",
  1217. siginfo.si_signo, n, siginfo.si_code,
  1218. (unsigned)sigio_band,
  1219. sigio_fd);
  1220. #endif
  1221. /* on some errors (e.g. when receving TCP RST), sigio_band will
  1222. * be set to 0x08 (POLLERR) or 0x18 (POLLERR|POLLHUP - on stream
  1223. * unix socket close) , so better catch all events --andrei */
  1224. if (likely(sigio_band)){
  1225. fm=get_fd_map(h, sigio_fd);
  1226. revents=sigio_band;
  1227. /* fix revents==POLLPRI case */
  1228. revents |= (!(revents & POLLPRI)-1) & POLLIN;
  1229. /* we can have queued signals generated by fds not watched
  1230. * any more, or by fds in transition, to a child
  1231. * => ignore them */
  1232. if (fm->type && ((fm->events|POLLERR|POLLHUP) & revents))
  1233. handle_io(fm, revents, -1);
  1234. else
  1235. DBG("WARNING: io_wait_loop_sigio_rt: ignoring event"
  1236. " %x on fd %d, watching for %x, si_code=%x "
  1237. "(fm->type=%d, fm->fd=%d, fm->data=%p)\n",
  1238. sigio_band, sigio_fd, fm->events, siginfo.si_code,
  1239. fm->type, fm->fd, fm->data);
  1240. }else{
  1241. LM_ERR("unexpected event on fd %d: %x\n", sigio_fd, sigio_band);
  1242. }
  1243. }
  1244. }else{
  1245. /* signal queue overflow
  1246. * TODO: increase signal queue size: 2.4x /proc/.., 2.6x -rlimits */
  1247. LM_WARN("signal queue overflowed - falling back to poll\n");
  1248. /* clear real-time signal queue
  1249. * both SIG_IGN and SIG_DFL are needed , it doesn't work
  1250. * only with SIG_DFL */
  1251. if (signal(h->signo, SIG_IGN)==SIG_ERR){
  1252. LM_CRIT("do_poll: couldn't reset signal to IGN\n");
  1253. }
  1254. if (signal(h->signo, SIG_DFL)==SIG_ERR){
  1255. LM_CRIT("do_poll: couldn't reset signal to DFL\n");
  1256. }
  1257. /* falling back to normal poll */
  1258. ret=io_wait_loop_poll(h, -1, 1);
  1259. }
  1260. end:
  1261. return ret;
  1262. error:
  1263. return -1;
  1264. }
  1265. #endif
  1266. #ifdef HAVE_DEVPOLL
  1267. inline static int io_wait_loop_devpoll(io_wait_h* h, int t, int repeat)
  1268. {
  1269. int n, r;
  1270. int ret;
  1271. struct dvpoll dpoll;
  1272. struct fd_map* fm;
  1273. dpoll.dp_timeout=t*1000;
  1274. dpoll.dp_nfds=h->fd_no;
  1275. dpoll.dp_fds=h->fd_array;
  1276. again:
  1277. ret=n=ioctl(h->dpoll_fd, DP_POLL, &dpoll);
  1278. if (unlikely(n==-1)){
  1279. if (errno==EINTR) goto again; /* signal, ignore it */
  1280. else{
  1281. LM_ERR("ioctl: %s [%d]\n", strerror(errno), errno);
  1282. goto error;
  1283. }
  1284. }
  1285. for (r=0; r< n; r++){
  1286. if (h->fd_array[r].revents & (POLLNVAL|POLLERR)){
  1287. LM_ERR("pollinval returned for fd %d, revents=%x\n",
  1288. h->fd_array[r].fd, h->fd_array[r].revents);
  1289. }
  1290. /* POLLIN|POLLHUP just go through */
  1291. fm=get_fd_map(h, h->fd_array[r].fd);
  1292. while(fm->type && (fm->events & h->fd_array[r].revents) &&
  1293. (handle_io(fm, h->fd_array[r].revents, r) > 0) && repeat);
  1294. }
  1295. error:
  1296. return ret;
  1297. }
  1298. #endif
  1299. /* init */
  1300. /* initializes the static vars/arrays
  1301. * params: h - pointer to the io_wait_h that will be initialized
  1302. * max_fd - maximum allowed fd number
  1303. * poll_m - poll method (0 for automatic best fit)
  1304. */
  1305. int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method);
  1306. /* destroys everything init_io_wait allocated */
  1307. void destroy_io_wait(io_wait_h* h);
  1308. #endif