Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

409 lines
12 KiB

  1. #include "io_internal.h"
  2. #ifdef HAVE_EPOLL
  3. #define _XOPEN_SOURCE
  4. #endif
  5. #ifdef HAVE_SIGIO
  6. #define _GNU_SOURCE
  7. #include <signal.h>
  8. #endif
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #ifdef __MINGW32__
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #else
  15. #include <poll.h>
  16. #endif
  17. #include <errno.h>
  18. #ifdef HAVE_KQUEUE
  19. #include <sys/event.h>
  20. #endif
  21. #ifdef HAVE_EPOLL
  22. #include <inttypes.h>
  23. #include <sys/epoll.h>
  24. #endif
  25. #ifdef HAVE_DEVPOLL
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/devpoll.h>
  29. #endif
  30. #ifdef __dietlibc__
  31. #include <fmt.h>
  32. #include <write12.h>
  33. #endif
  34. #ifdef DEBUG
  35. #include <stdio.h>
  36. #endif
  37. #ifndef EPOLLRDNORM
  38. #define EPOLLRDNORM 0
  39. #endif
  40. #ifndef EPOLLRDBAND
  41. #define EPOLLRDNORM 0
  42. #endif
  43. #if 0
  44. static void handleevent(int fd,int readable,int writable,int error) {
  45. io_entry* e=array_get(&io_fds,sizeof(io_entry),fd);
  46. if (e) {
  47. int curevents=0,newevents;
  48. if (e->kernelwantread) curevents |= POLLIN;
  49. if (e->kernelwantwrite) curevents |= POLLOUT;
  50. #ifdef DEBUG
  51. if (readable && !e->kernelwantread)
  52. printf("got unexpected read event on fd #%d\n",fd);
  53. if (writable && !e->kernelwantwrite)
  54. printf("got unexpected write event on fd #%d\n",fd);
  55. #endif
  56. if (error) {
  57. /* signal whatever app is looking for */
  58. if (e->wantread) readable=1;
  59. if (e->wantwrite) writable=1;
  60. }
  61. if (readable && !e->canread) {
  62. e->canread=1;
  63. if (e->wantread) {
  64. e->next_read=first_readable;
  65. first_readable=y[i].data.fd;
  66. }
  67. }
  68. if (writable && !e->canwrite) {
  69. e->canwrite=1;
  70. if (e->wantwrite) {
  71. e->next_write=first_writeable;
  72. first_writeable=y[i].data.fd;
  73. }
  74. }
  75. /* TODO: wie kommuniziere ich nach außen, was der Caller tun soll?
  76. * Bitfeld-Integer? */
  77. newevents=0;
  78. if (!e->canread || e->wantread) {
  79. newevents|=EPOLLIN;
  80. e->kernelwantread=1;
  81. } else
  82. e->kernelwantread=0;
  83. if (!e->canwrite || e->wantwrite) {
  84. newevents|=EPOLLOUT;
  85. e->kernelwantwrite=1;
  86. } else
  87. e->kernelwantwrite=0;
  88. if (newevents != curevents) {
  89. #if 0
  90. printf("canread %d, wantread %d, kernelwantread %d, canwrite %d, wantwrite %d, kernelwantwrite %d\n",
  91. e->canread,e->wantread,e->kernelwantread,e->canwrite,e->wantwrite,e->kernelwantwrite);
  92. printf("newevents: read %d write %d\n",!!(newevents&EPOLLIN),!!(newevents&EPOLLOUT));
  93. #endif
  94. y[i].events=newevents;
  95. if (newevents) {
  96. epoll_ctl(io_master,EPOLL_CTL_MOD,y[i].data.fd,y+i);
  97. } else {
  98. epoll_ctl(io_master,EPOLL_CTL_DEL,y[i].data.fd,y+i);
  99. --io_wanted_fds;
  100. }
  101. }
  102. } else {
  103. epoll_ctl(io_master,EPOLL_CTL_DEL,y[i].data.fd,y+i);
  104. }
  105. }
  106. #endif
  107. int64 io_waituntil2(int64 milliseconds) {
  108. #ifndef __MINGW32__
  109. struct pollfd* p;
  110. #endif
  111. long i,j,r;
  112. if (!io_wanted_fds) return 0;
  113. #ifdef HAVE_EPOLL
  114. if (io_waitmode==EPOLL) {
  115. int n;
  116. struct epoll_event y[100];
  117. io_entry* e;
  118. if (alt_firstread>=0 && (e=iarray_get(&io_fds,alt_firstread)) && e->canread) return 1;
  119. if (alt_firstwrite>=0 && (e=iarray_get(&io_fds,alt_firstwrite)) && e->canwrite) return 1;
  120. if ((n=epoll_wait(io_master,y,100,milliseconds))==-1) return -1;
  121. for (i=0; i<n; ++i) {
  122. e=iarray_get(&io_fds,y[i].data.fd);
  123. if (e) {
  124. if (y[i].events&(POLLERR|POLLHUP)) {
  125. /* error; signal whatever app is looking for */
  126. if (e->wantread) y[i].events|=POLLIN;
  127. if (e->wantwrite) y[i].events|=POLLOUT;
  128. }
  129. if (y[i].events&POLLIN && !e->canread) {
  130. debug_printf(("io_waituntil2: enqueueing %ld in normal read queue before %ld\n",info.si_fd,first_readable));
  131. e->canread=1;
  132. e->next_read=first_readable;
  133. first_readable=y[i].data.fd;
  134. }
  135. if (y[i].events&POLLOUT && !e->canwrite) {
  136. debug_printf(("io_waituntil2: enqueueing %ld in normal write queue before %ld\n",info.si_fd,first_writeable));
  137. e->canwrite=1;
  138. e->next_write=first_writeable;
  139. first_writeable=y[i].data.fd;
  140. }
  141. } else {
  142. #ifdef __dietlibc__
  143. char buf[FMT_ULONG];
  144. buf[fmt_ulong(buf,y[i].data.fd)]=0;
  145. __write2("got epoll event on invalid fd ");
  146. __write2(buf);
  147. __write2("!\n");
  148. #endif
  149. epoll_ctl(io_master,EPOLL_CTL_DEL,y[i].data.fd,y+i);
  150. }
  151. }
  152. return n;
  153. }
  154. #endif
  155. #ifdef HAVE_KQUEUE
  156. if (io_waitmode==KQUEUE) {
  157. struct kevent y[100];
  158. int n;
  159. struct timespec ts;
  160. ts.tv_sec=milliseconds/1000; ts.tv_nsec=(milliseconds%1000)*1000000;
  161. if ((n=kevent(io_master,0,0,y,100,milliseconds!=-1?&ts:0))==-1) return -1;
  162. for (i=n-1; i>=0; --i) {
  163. io_entry* e=iarray_get(&io_fds,y[--n].ident);
  164. if (e) {
  165. if (y[n].flags&EV_ERROR) {
  166. /* error; signal whatever app is looking for */
  167. if (e->wantread) y[n].filter=EVFILT_READ; else
  168. if (e->wantwrite) y[n].filter=EVFILT_WRITE;
  169. }
  170. if (!e->canread && (y[n].filter==EVFILT_READ)) {
  171. e->canread=1;
  172. e->next_read=first_readable;
  173. first_readable=y[n].ident;
  174. }
  175. if (!e->canwrite && (y[n].filter==EVFILT_WRITE)) {
  176. e->canwrite=1;
  177. e->next_write=first_writeable;
  178. first_writeable=y[i].ident;
  179. }
  180. #ifdef DEBUG
  181. } else {
  182. fprintf(stderr,"got kevent on fd#%d, which is not in array!\n",y[n].ident);
  183. #endif
  184. }
  185. }
  186. return n;
  187. }
  188. #endif
  189. #ifdef HAVE_DEVPOLL
  190. if (io_waitmode==DEVPOLL) {
  191. dvpoll_t timeout;
  192. struct pollfd y[100];
  193. int n;
  194. timeout.dp_timeout=milliseconds;
  195. timeout.dp_nfds=100;
  196. timeout.dp_fds=y;
  197. if ((n=ioctl(io_master,DP_POLL,&timeout))==-1) return -1;
  198. for (i=n-1; i>=0; --i) {
  199. io_entry* e=iarray_get(&io_fds,y[--n].fd);
  200. if (e) {
  201. if (y[n].revents&(POLLERR|POLLHUP|POLLNVAL)) {
  202. /* error; signal whatever app is looking for */
  203. if (e->wantread) y[n].revents=POLLIN;
  204. if (e->wantwrite) y[n].revents=POLLOUT;
  205. }
  206. if (!e->canread && (y[n].revents&POLLIN)) {
  207. e->canread=1;
  208. if (e->next_read==-1) {
  209. e->next_read=first_readable;
  210. first_readable=y[n].fd;
  211. }
  212. }
  213. if (!e->canwrite && (y[n].revents&POLLOUT)) {
  214. e->canwrite=1;
  215. if (e->next_write==-1) {
  216. e->next_write=first_writeable;
  217. first_writeable=y[i].fd;
  218. }
  219. }
  220. #ifdef DEBUG
  221. } else {
  222. fprintf(stderr,"got kevent on fd#%d, which is not in array!\n",y[n].fd);
  223. #endif
  224. }
  225. }
  226. return n;
  227. }
  228. #endif
  229. #ifdef HAVE_SIGIO
  230. if (io_waitmode==_SIGIO) {
  231. siginfo_t info;
  232. struct timespec ts;
  233. int r;
  234. io_entry* e;
  235. if (alt_firstread>=0 && (e=iarray_get(&io_fds,alt_firstread)) && e->canread) return 1;
  236. if (alt_firstwrite>=0 && (e=iarray_get(&io_fds,alt_firstwrite)) && e->canwrite) return 1;
  237. if (milliseconds==-1)
  238. r=sigwaitinfo(&io_ss,&info);
  239. else {
  240. ts.tv_sec=milliseconds/1000; ts.tv_nsec=(milliseconds%1000)*1000000;
  241. r=sigtimedwait(&io_ss,&info,&ts);
  242. }
  243. switch (r) {
  244. case SIGIO:
  245. /* signal queue overflow */
  246. signal(io_signum,SIG_DFL);
  247. goto dopoll;
  248. default:
  249. if (r==io_signum) {
  250. io_entry* e=iarray_get(&io_fds,info.si_fd);
  251. if (e) {
  252. if (info.si_band&(POLLERR|POLLHUP)) {
  253. /* error; signal whatever app is looking for */
  254. if (e->wantread) info.si_band|=POLLIN;
  255. if (e->wantwrite) info.si_band|=POLLOUT;
  256. }
  257. if (info.si_band&POLLIN && !e->canread) {
  258. debug_printf(("io_waituntil2: enqueueing %ld in normal read queue before %ld\n",info.si_fd,first_readable));
  259. e->canread=1;
  260. e->next_read=first_readable;
  261. first_readable=info.si_fd;
  262. }
  263. if (info.si_band&POLLOUT && !e->canwrite) {
  264. debug_printf(("io_waituntil2: enqueueing %ld in normal write queue before %ld\n",info.si_fd,first_writeable));
  265. e->canwrite=1;
  266. e->next_write=first_writeable;
  267. first_writeable=info.si_fd;
  268. }
  269. #ifdef DEBUG
  270. } else {
  271. fprintf(stderr,"got kevent on fd#%d, which is not in array!\n",info.si_fd);
  272. #endif
  273. }
  274. }
  275. }
  276. return 1;
  277. }
  278. dopoll:
  279. #endif
  280. #ifdef __MINGW32__
  281. DWORD numberofbytes;
  282. ULONG_PTR x;
  283. LPOVERLAPPED o;
  284. if (first_readable!=-1 || first_writeable!=-1) {
  285. fprintf(stderr,"io_waituntil2() returning immediately because first_readable(%p) or first_writeable(%p) are set\n",first_readable,first_writeable);
  286. return;
  287. }
  288. fprintf(stderr,"Calling GetQueuedCompletionStatus %p...",io_comport);
  289. if (GetQueuedCompletionStatus(io_comport,&numberofbytes,&x,&o,milliseconds==-1?INFINITE:milliseconds)) {
  290. io_entry* e=iarray_get(&io_fds,x);
  291. fprintf(stderr," OK. Got %x, e=%p\n",x,e);
  292. if (!e) return 0;
  293. e->errorcode=0;
  294. fprintf(stderr,"o=%p, e->or=%p, e->ow=%p, e->os=%p\n",o,&e->or,&e->ow,&e->os);
  295. fprintf(stderr,"e->readqueued=%d, e->writequeued=%d, e->acceptqueued=%d, e->connectqueued=%d, e->sendfilequeued=%d\n",
  296. e->readqueued,e->writequeued,e->acceptqueued,e->connectqueued,e->sendfilequeued);
  297. if (o==&e->or && e->readqueued==1) {
  298. e->readqueued=2;
  299. e->canread=1;
  300. e->bytes_read=numberofbytes;
  301. e->next_read=first_readable;
  302. first_readable=x;
  303. // printf("read %lu bytes on fd %lu: %p\n",numberofbytes,x,e);
  304. } else if (o==&e->ow && e->writequeued==1) {
  305. e->writequeued=2;
  306. e->canwrite=1;
  307. e->bytes_written=numberofbytes;
  308. e->next_write=first_writeable;
  309. first_writeable=x;
  310. } else if (o==&e->or && e->acceptqueued==1) {
  311. e->acceptqueued=2;
  312. e->canread=1;
  313. e->next_read=first_readable;
  314. first_readable=x;
  315. } else if (o==&e->ow && e->connectqueued==1) {
  316. e->connectqueued=2;
  317. e->canwrite=1;
  318. e->next_write=first_writeable;
  319. first_writeable=x;
  320. } else if (o==&e->os && e->sendfilequeued==1) {
  321. e->sendfilequeued=2;
  322. e->canwrite=1;
  323. e->bytes_written=numberofbytes;
  324. e->next_write=first_writeable;
  325. first_writeable=x;
  326. }
  327. return 1;
  328. } else {
  329. /* either the overlapped I/O request failed or we timed out */
  330. DWORD err;
  331. io_entry* e;
  332. fprintf(stderr," failure, o=%p.\n",o);
  333. if (!o) return 0; /* timeout */
  334. /* we got a completion packet for a failed I/O operation */
  335. err=GetLastError();
  336. if (err==WAIT_TIMEOUT) return 0; /* or maybe not */
  337. e=iarray_get(&io_fds,x);
  338. if (!e) return 0; /* WTF?! */
  339. e->errorcode=err;
  340. if (o==&e->or && (e->readqueued || e->acceptqueued)) {
  341. if (e->readqueued) e->readqueued=2; else
  342. if (e->acceptqueued) e->acceptqueued=2;
  343. e->canread=1;
  344. e->bytes_read=-1;
  345. e->next_read=first_readable;
  346. first_readable=x;
  347. } else if ((o==&e->ow || o==&e->os) &&
  348. (e->writequeued || e->connectqueued || e->sendfilequeued)) {
  349. if (o==&e->ow) {
  350. if (e->writequeued) e->writequeued=2; else
  351. if (e->connectqueued) e->connectqueued=2;
  352. } else if (o==&e->os) e->sendfilequeued=2;
  353. e->canwrite=1;
  354. e->bytes_written=-1;
  355. e->next_write=first_writeable;
  356. first_writeable=x;
  357. }
  358. return 1;
  359. }
  360. #else
  361. for (i=r=0; (size_t)i<iarray_length(&io_fds); ++i) {
  362. io_entry* e=iarray_get(&io_fds,i);
  363. if (!e) return -1;
  364. e->canread=e->canwrite=0;
  365. if (e->wantread || e->wantwrite) {
  366. struct pollfd* p;
  367. if ((p=array_allocate(&io_pollfds,sizeof(struct pollfd),r))) {
  368. p->fd=i;
  369. p->events=(e->wantread?POLLIN:0) + (e->wantwrite?POLLOUT:0);
  370. ++r;
  371. } else
  372. return -1;
  373. }
  374. }
  375. p=array_start(&io_pollfds);
  376. if ((i=poll(array_start(&io_pollfds),r,milliseconds))<1) return -1;
  377. for (j=r-1; j>=0; --j) {
  378. io_entry* e=iarray_get(&io_fds,p->fd);
  379. if (p->revents&(POLLERR|POLLHUP|POLLNVAL)) {
  380. /* error; signal whatever app is looking for */
  381. if (e->wantread) p->revents|=POLLIN;
  382. if (e->wantwrite) p->revents|=POLLOUT;
  383. }
  384. if (!e->canread && (p->revents&POLLIN)) {
  385. e->canread=1;
  386. e->next_read=first_readable;
  387. first_readable=p->fd;
  388. }
  389. if (!e->canwrite && (p->revents&POLLOUT)) {
  390. e->canwrite=1;
  391. e->next_write=first_writeable;
  392. first_writeable=p->fd;
  393. }
  394. p++;
  395. }
  396. return i;
  397. #endif
  398. }