poll.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "fspr_arch_poll_private.h"
  17. #if defined(POLL_USES_POLL) || defined(POLLSET_USES_POLL)
  18. static fspr_int16_t get_event(fspr_int16_t event)
  19. {
  20. fspr_int16_t rv = 0;
  21. if (event & APR_POLLIN)
  22. rv |= POLLIN;
  23. if (event & APR_POLLPRI)
  24. rv |= POLLPRI;
  25. if (event & APR_POLLOUT)
  26. rv |= POLLOUT;
  27. if (event & APR_POLLERR)
  28. rv |= POLLERR;
  29. if (event & APR_POLLHUP)
  30. rv |= POLLHUP;
  31. if (event & APR_POLLNVAL)
  32. rv |= POLLNVAL;
  33. return rv;
  34. }
  35. static fspr_int16_t get_revent(fspr_int16_t event)
  36. {
  37. fspr_int16_t rv = 0;
  38. if (event & POLLIN)
  39. rv |= APR_POLLIN;
  40. if (event & POLLPRI)
  41. rv |= APR_POLLPRI;
  42. if (event & POLLOUT)
  43. rv |= APR_POLLOUT;
  44. if (event & POLLERR)
  45. rv |= APR_POLLERR;
  46. if (event & POLLHUP)
  47. rv |= APR_POLLHUP;
  48. if (event & POLLNVAL)
  49. rv |= APR_POLLNVAL;
  50. return rv;
  51. }
  52. #endif /* POLL_USES_POLL || POLLSET_USES_POLL */
  53. #ifdef POLL_USES_POLL
  54. #define SMALL_POLLSET_LIMIT 8
  55. APR_DECLARE(fspr_status_t) fspr_poll(fspr_pollfd_t *aprset, fspr_int32_t num,
  56. fspr_int32_t *nsds,
  57. fspr_interval_time_t timeout)
  58. {
  59. int i, num_to_poll;
  60. #ifdef HAVE_VLA
  61. /* XXX: I trust that this is a segv when insufficient stack exists? */
  62. struct pollfd pollset[num];
  63. #elif defined(HAVE_ALLOCA)
  64. struct pollfd *pollset = alloca(sizeof(struct pollfd) * num);
  65. if (!pollset)
  66. return APR_ENOMEM;
  67. #else
  68. struct pollfd tmp_pollset[SMALL_POLLSET_LIMIT];
  69. struct pollfd *pollset;
  70. if (num <= SMALL_POLLSET_LIMIT) {
  71. pollset = tmp_pollset;
  72. }
  73. else {
  74. /* This does require O(n) to copy the descriptors to the internal
  75. * mapping.
  76. */
  77. pollset = malloc(sizeof(struct pollfd) * num);
  78. /* The other option is adding an fspr_pool_abort() fn to invoke
  79. * the pool's out of memory handler
  80. */
  81. if (!pollset)
  82. return APR_ENOMEM;
  83. }
  84. #endif
  85. for (i = 0; i < num; i++) {
  86. if (aprset[i].desc_type == APR_POLL_SOCKET) {
  87. pollset[i].fd = aprset[i].desc.s->socketdes;
  88. }
  89. else if (aprset[i].desc_type == APR_POLL_FILE) {
  90. pollset[i].fd = aprset[i].desc.f->filedes;
  91. }
  92. else {
  93. break;
  94. }
  95. pollset[i].events = get_event(aprset[i].reqevents);
  96. }
  97. num_to_poll = i;
  98. if (timeout > 0) {
  99. timeout /= 1000; /* convert microseconds to milliseconds */
  100. }
  101. i = poll(pollset, num_to_poll, timeout);
  102. (*nsds) = i;
  103. if (i > 0) { /* poll() sets revents only if an event was signalled;
  104. * we don't promise to set rtnevents unless an event
  105. * was signalled
  106. */
  107. for (i = 0; i < num; i++) {
  108. aprset[i].rtnevents = get_revent(pollset[i].revents);
  109. }
  110. }
  111. #if !defined(HAVE_VLA) && !defined(HAVE_ALLOCA)
  112. if (num > SMALL_POLLSET_LIMIT) {
  113. free(pollset);
  114. }
  115. #endif
  116. if ((*nsds) < 0) {
  117. return fspr_get_netos_error();
  118. }
  119. if ((*nsds) == 0) {
  120. return APR_TIMEUP;
  121. }
  122. return APR_SUCCESS;
  123. }
  124. #endif /* POLL_USES_POLL */
  125. #ifdef POLLSET_USES_POLL
  126. struct fspr_pollset_t
  127. {
  128. fspr_pool_t *pool;
  129. fspr_uint32_t nelts;
  130. fspr_uint32_t nalloc;
  131. struct pollfd *pollset;
  132. fspr_pollfd_t *query_set;
  133. fspr_pollfd_t *result_set;
  134. };
  135. APR_DECLARE(fspr_status_t) fspr_pollset_create(fspr_pollset_t **pollset,
  136. fspr_uint32_t size,
  137. fspr_pool_t *p,
  138. fspr_uint32_t flags)
  139. {
  140. if (flags & APR_POLLSET_THREADSAFE) {
  141. *pollset = NULL;
  142. return APR_ENOTIMPL;
  143. }
  144. *pollset = fspr_palloc(p, sizeof(**pollset));
  145. (*pollset)->nelts = 0;
  146. (*pollset)->nalloc = size;
  147. (*pollset)->pool = p;
  148. (*pollset)->pollset = fspr_palloc(p, size * sizeof(struct pollfd));
  149. (*pollset)->query_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  150. (*pollset)->result_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  151. return APR_SUCCESS;
  152. }
  153. APR_DECLARE(fspr_status_t) fspr_pollset_destroy(fspr_pollset_t *pollset)
  154. {
  155. return APR_SUCCESS;
  156. }
  157. APR_DECLARE(fspr_status_t) fspr_pollset_add(fspr_pollset_t *pollset,
  158. const fspr_pollfd_t *descriptor)
  159. {
  160. if (pollset->nelts == pollset->nalloc) {
  161. return APR_ENOMEM;
  162. }
  163. pollset->query_set[pollset->nelts] = *descriptor;
  164. if (descriptor->desc_type == APR_POLL_SOCKET) {
  165. pollset->pollset[pollset->nelts].fd = descriptor->desc.s->socketdes;
  166. }
  167. else {
  168. pollset->pollset[pollset->nelts].fd = descriptor->desc.f->filedes;
  169. }
  170. pollset->pollset[pollset->nelts].events =
  171. get_event(descriptor->reqevents);
  172. pollset->nelts++;
  173. return APR_SUCCESS;
  174. }
  175. APR_DECLARE(fspr_status_t) fspr_pollset_remove(fspr_pollset_t *pollset,
  176. const fspr_pollfd_t *descriptor)
  177. {
  178. fspr_uint32_t i;
  179. for (i = 0; i < pollset->nelts; i++) {
  180. if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  181. /* Found an instance of the fd: remove this and any other copies */
  182. fspr_uint32_t dst = i;
  183. fspr_uint32_t old_nelts = pollset->nelts;
  184. pollset->nelts--;
  185. for (i++; i < old_nelts; i++) {
  186. if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  187. pollset->nelts--;
  188. }
  189. else {
  190. pollset->pollset[dst] = pollset->pollset[i];
  191. pollset->query_set[dst] = pollset->query_set[i];
  192. dst++;
  193. }
  194. }
  195. return APR_SUCCESS;
  196. }
  197. }
  198. return APR_NOTFOUND;
  199. }
  200. APR_DECLARE(fspr_status_t) fspr_pollset_poll(fspr_pollset_t *pollset,
  201. fspr_interval_time_t timeout,
  202. fspr_int32_t *num,
  203. const fspr_pollfd_t **descriptors)
  204. {
  205. int rv;
  206. fspr_uint32_t i, j;
  207. if (timeout > 0) {
  208. timeout /= 1000;
  209. }
  210. rv = poll(pollset->pollset, pollset->nelts, timeout);
  211. (*num) = rv;
  212. if (rv < 0) {
  213. return fspr_get_netos_error();
  214. }
  215. if (rv == 0) {
  216. return APR_TIMEUP;
  217. }
  218. j = 0;
  219. for (i = 0; i < pollset->nelts; i++) {
  220. if (pollset->pollset[i].revents != 0) {
  221. pollset->result_set[j] = pollset->query_set[i];
  222. pollset->result_set[j].rtnevents =
  223. get_revent(pollset->pollset[i].revents);
  224. j++;
  225. }
  226. }
  227. if (descriptors)
  228. *descriptors = pollset->result_set;
  229. return APR_SUCCESS;
  230. }
  231. #endif /* POLLSET_USES_POLL */