select.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifdef WIN32
  17. /* POSIX defines 1024 for the FD_SETSIZE */
  18. #define FD_SETSIZE 1024
  19. #endif
  20. #include "fspr.h"
  21. #include "fspr_poll.h"
  22. #include "fspr_time.h"
  23. #include "fspr_portable.h"
  24. #include "fspr_arch_networkio.h"
  25. #include "fspr_arch_file_io.h"
  26. #include "fspr_arch_poll_private.h"
  27. #ifdef POLL_USES_SELECT
  28. APR_DECLARE(fspr_status_t) fspr_poll(fspr_pollfd_t *aprset, int num,
  29. fspr_int32_t *nsds,
  30. fspr_interval_time_t timeout)
  31. {
  32. fd_set readset, writeset, exceptset;
  33. int rv, i;
  34. int maxfd = -1;
  35. struct timeval tv, *tvptr;
  36. #ifdef NETWARE
  37. fspr_datatype_e set_type = APR_NO_DESC;
  38. #endif
  39. if (timeout < 0) {
  40. tvptr = NULL;
  41. }
  42. else {
  43. tv.tv_sec = (long) fspr_time_sec(timeout);
  44. tv.tv_usec = (long) fspr_time_usec(timeout);
  45. tvptr = &tv;
  46. }
  47. FD_ZERO(&readset);
  48. FD_ZERO(&writeset);
  49. FD_ZERO(&exceptset);
  50. for (i = 0; i < num; i++) {
  51. fspr_os_sock_t fd;
  52. aprset[i].rtnevents = 0;
  53. if (aprset[i].desc_type == APR_POLL_SOCKET) {
  54. #ifdef NETWARE
  55. if (HAS_PIPES(set_type)) {
  56. return APR_EBADF;
  57. }
  58. else {
  59. set_type = APR_POLL_SOCKET;
  60. }
  61. #endif
  62. fd = aprset[i].desc.s->socketdes;
  63. }
  64. else if (aprset[i].desc_type == APR_POLL_FILE) {
  65. #if !APR_FILES_AS_SOCKETS
  66. return APR_EBADF;
  67. #else
  68. #ifdef NETWARE
  69. if (aprset[i].desc.f->is_pipe && !HAS_SOCKETS(set_type)) {
  70. set_type = APR_POLL_FILE;
  71. }
  72. else
  73. return APR_EBADF;
  74. #endif /* NETWARE */
  75. fd = aprset[i].desc.f->filedes;
  76. #endif /* APR_FILES_AS_SOCKETS */
  77. }
  78. else {
  79. break;
  80. }
  81. #if !defined(WIN32) && !defined(NETWARE) /* socket sets handled with array of handles */
  82. if (fd >= FD_SETSIZE) {
  83. /* XXX invent new error code so application has a clue */
  84. return APR_EBADF;
  85. }
  86. #endif
  87. if (aprset[i].reqevents & APR_POLLIN) {
  88. FD_SET(fd, &readset);
  89. }
  90. if (aprset[i].reqevents & APR_POLLOUT) {
  91. FD_SET(fd, &writeset);
  92. }
  93. if (aprset[i].reqevents &
  94. (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
  95. FD_SET(fd, &exceptset);
  96. }
  97. if ((int) fd > maxfd) {
  98. maxfd = (int) fd;
  99. }
  100. }
  101. #ifdef NETWARE
  102. if (HAS_PIPES(set_type)) {
  103. rv = pipe_select(maxfd + 1, &readset, &writeset, &exceptset, tvptr);
  104. }
  105. else {
  106. #endif
  107. rv = select(maxfd + 1, &readset, &writeset, &exceptset, tvptr);
  108. #ifdef NETWARE
  109. }
  110. #endif
  111. (*nsds) = rv;
  112. if ((*nsds) == 0) {
  113. return APR_TIMEUP;
  114. }
  115. if ((*nsds) < 0) {
  116. return fspr_get_netos_error();
  117. }
  118. (*nsds) = 0;
  119. for (i = 0; i < num; i++) {
  120. fspr_os_sock_t fd;
  121. if (aprset[i].desc_type == APR_POLL_SOCKET) {
  122. fd = aprset[i].desc.s->socketdes;
  123. }
  124. else if (aprset[i].desc_type == APR_POLL_FILE) {
  125. #if !APR_FILES_AS_SOCKETS
  126. return APR_EBADF;
  127. #else
  128. fd = aprset[i].desc.f->filedes;
  129. #endif
  130. }
  131. else {
  132. break;
  133. }
  134. if (FD_ISSET(fd, &readset)) {
  135. aprset[i].rtnevents |= APR_POLLIN;
  136. }
  137. if (FD_ISSET(fd, &writeset)) {
  138. aprset[i].rtnevents |= APR_POLLOUT;
  139. }
  140. if (FD_ISSET(fd, &exceptset)) {
  141. aprset[i].rtnevents |= APR_POLLERR;
  142. }
  143. if (aprset[i].rtnevents) {
  144. (*nsds)++;
  145. }
  146. }
  147. return APR_SUCCESS;
  148. }
  149. #endif /* POLL_USES_SELECT */
  150. #ifdef POLLSET_USES_SELECT
  151. struct fspr_pollset_t
  152. {
  153. fspr_pool_t *pool;
  154. fspr_uint32_t nelts;
  155. fspr_uint32_t nalloc;
  156. fd_set readset, writeset, exceptset;
  157. int maxfd;
  158. fspr_pollfd_t *query_set;
  159. fspr_pollfd_t *result_set;
  160. #ifdef NETWARE
  161. int set_type;
  162. #endif
  163. };
  164. APR_DECLARE(fspr_status_t) fspr_pollset_create(fspr_pollset_t **pollset,
  165. fspr_uint32_t size,
  166. fspr_pool_t *p,
  167. fspr_uint32_t flags)
  168. {
  169. if (flags & APR_POLLSET_THREADSAFE) {
  170. *pollset = NULL;
  171. return APR_ENOTIMPL;
  172. }
  173. #ifdef FD_SETSIZE
  174. if (size > FD_SETSIZE) {
  175. *pollset = NULL;
  176. return APR_EINVAL;
  177. }
  178. #endif
  179. *pollset = fspr_palloc(p, sizeof(**pollset));
  180. (*pollset)->nelts = 0;
  181. (*pollset)->nalloc = size;
  182. (*pollset)->pool = p;
  183. FD_ZERO(&((*pollset)->readset));
  184. FD_ZERO(&((*pollset)->writeset));
  185. FD_ZERO(&((*pollset)->exceptset));
  186. (*pollset)->maxfd = 0;
  187. #ifdef NETWARE
  188. (*pollset)->set_type = APR_NO_DESC;
  189. #endif
  190. (*pollset)->query_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  191. (*pollset)->result_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  192. return APR_SUCCESS;
  193. }
  194. APR_DECLARE(fspr_status_t) fspr_pollset_destroy(fspr_pollset_t * pollset)
  195. {
  196. return APR_SUCCESS;
  197. }
  198. APR_DECLARE(fspr_status_t) fspr_pollset_add(fspr_pollset_t *pollset,
  199. const fspr_pollfd_t *descriptor)
  200. {
  201. fspr_os_sock_t fd;
  202. if (pollset->nelts == pollset->nalloc) {
  203. return APR_ENOMEM;
  204. }
  205. pollset->query_set[pollset->nelts] = *descriptor;
  206. if (descriptor->desc_type == APR_POLL_SOCKET) {
  207. #ifdef NETWARE
  208. /* NetWare can't handle mixed descriptor types in select() */
  209. if (HAS_PIPES(pollset->set_type)) {
  210. return APR_EBADF;
  211. }
  212. else {
  213. pollset->set_type = APR_POLL_SOCKET;
  214. }
  215. #endif
  216. fd = descriptor->desc.s->socketdes;
  217. }
  218. else {
  219. #if !APR_FILES_AS_SOCKETS
  220. return APR_EBADF;
  221. #else
  222. #ifdef NETWARE
  223. /* NetWare can't handle mixed descriptor types in select() */
  224. if (descriptor->desc.f->is_pipe && !HAS_SOCKETS(pollset->set_type)) {
  225. pollset->set_type = APR_POLL_FILE;
  226. fd = descriptor->desc.f->filedes;
  227. }
  228. else {
  229. return APR_EBADF;
  230. }
  231. #else
  232. fd = descriptor->desc.f->filedes;
  233. #endif
  234. #endif
  235. }
  236. #if !defined(WIN32) && !defined(NETWARE) /* socket sets handled with array of handles */
  237. if (fd >= FD_SETSIZE) {
  238. /* XXX invent new error code so application has a clue */
  239. return APR_EBADF;
  240. }
  241. #endif
  242. if (descriptor->reqevents & APR_POLLIN) {
  243. FD_SET(fd, &(pollset->readset));
  244. }
  245. if (descriptor->reqevents & APR_POLLOUT) {
  246. FD_SET(fd, &(pollset->writeset));
  247. }
  248. if (descriptor->reqevents &
  249. (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
  250. FD_SET(fd, &(pollset->exceptset));
  251. }
  252. if ((int) fd > pollset->maxfd) {
  253. pollset->maxfd = (int) fd;
  254. }
  255. pollset->nelts++;
  256. return APR_SUCCESS;
  257. }
  258. APR_DECLARE(fspr_status_t) fspr_pollset_remove(fspr_pollset_t * pollset,
  259. const fspr_pollfd_t * descriptor)
  260. {
  261. fspr_uint32_t i;
  262. fspr_os_sock_t fd;
  263. if (descriptor->desc_type == APR_POLL_SOCKET) {
  264. fd = descriptor->desc.s->socketdes;
  265. }
  266. else {
  267. #if !APR_FILES_AS_SOCKETS
  268. return APR_EBADF;
  269. #else
  270. fd = descriptor->desc.f->filedes;
  271. #endif
  272. }
  273. for (i = 0; i < pollset->nelts; i++) {
  274. if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  275. /* Found an instance of the fd: remove this and any other copies */
  276. fspr_uint32_t dst = i;
  277. fspr_uint32_t old_nelts = pollset->nelts;
  278. pollset->nelts--;
  279. for (i++; i < old_nelts; i++) {
  280. if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  281. pollset->nelts--;
  282. }
  283. else {
  284. pollset->query_set[dst] = pollset->query_set[i];
  285. dst++;
  286. }
  287. }
  288. FD_CLR(fd, &(pollset->readset));
  289. FD_CLR(fd, &(pollset->writeset));
  290. FD_CLR(fd, &(pollset->exceptset));
  291. if (((int) fd == pollset->maxfd) && (pollset->maxfd > 0)) {
  292. pollset->maxfd--;
  293. }
  294. return APR_SUCCESS;
  295. }
  296. }
  297. return APR_NOTFOUND;
  298. }
  299. APR_DECLARE(fspr_status_t) fspr_pollset_poll(fspr_pollset_t *pollset,
  300. fspr_interval_time_t timeout,
  301. fspr_int32_t *num,
  302. const fspr_pollfd_t **descriptors)
  303. {
  304. int rv;
  305. fspr_uint32_t i, j;
  306. struct timeval tv, *tvptr;
  307. fd_set readset, writeset, exceptset;
  308. if (timeout < 0) {
  309. tvptr = NULL;
  310. }
  311. else {
  312. tv.tv_sec = (long) fspr_time_sec(timeout);
  313. tv.tv_usec = (long) fspr_time_usec(timeout);
  314. tvptr = &tv;
  315. }
  316. memcpy(&readset, &(pollset->readset), sizeof(fd_set));
  317. memcpy(&writeset, &(pollset->writeset), sizeof(fd_set));
  318. memcpy(&exceptset, &(pollset->exceptset), sizeof(fd_set));
  319. #ifdef NETWARE
  320. if (HAS_PIPES(pollset->set_type)) {
  321. rv = pipe_select(pollset->maxfd + 1, &readset, &writeset, &exceptset,
  322. tvptr);
  323. }
  324. else
  325. #endif
  326. rv = select(pollset->maxfd + 1, &readset, &writeset, &exceptset,
  327. tvptr);
  328. (*num) = rv;
  329. if (rv < 0) {
  330. return fspr_get_netos_error();
  331. }
  332. if (rv == 0) {
  333. return APR_TIMEUP;
  334. }
  335. j = 0;
  336. for (i = 0; i < pollset->nelts; i++) {
  337. fspr_os_sock_t fd;
  338. if (pollset->query_set[i].desc_type == APR_POLL_SOCKET) {
  339. fd = pollset->query_set[i].desc.s->socketdes;
  340. }
  341. else {
  342. #if !APR_FILES_AS_SOCKETS
  343. return APR_EBADF;
  344. #else
  345. fd = pollset->query_set[i].desc.f->filedes;
  346. #endif
  347. }
  348. if (FD_ISSET(fd, &readset) || FD_ISSET(fd, &writeset) ||
  349. FD_ISSET(fd, &exceptset)) {
  350. pollset->result_set[j] = pollset->query_set[i];
  351. pollset->result_set[j].rtnevents = 0;
  352. if (FD_ISSET(fd, &readset)) {
  353. pollset->result_set[j].rtnevents |= APR_POLLIN;
  354. }
  355. if (FD_ISSET(fd, &writeset)) {
  356. pollset->result_set[j].rtnevents |= APR_POLLOUT;
  357. }
  358. if (FD_ISSET(fd, &exceptset)) {
  359. pollset->result_set[j].rtnevents |= APR_POLLERR;
  360. }
  361. j++;
  362. }
  363. }
  364. (*num) = j;
  365. if (descriptors)
  366. *descriptors = pollset->result_set;
  367. return APR_SUCCESS;
  368. }
  369. #endif /* POLLSET_USES_SELECT */