pollset.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.h"
  17. #include "fspr_poll.h"
  18. #include "fspr_arch_networkio.h"
  19. struct fspr_pollset_t {
  20. fspr_pool_t *pool;
  21. fspr_uint32_t nelts;
  22. fspr_uint32_t nalloc;
  23. int *pollset;
  24. int num_read;
  25. int num_write;
  26. int num_except;
  27. int num_total;
  28. fspr_pollfd_t *query_set;
  29. fspr_pollfd_t *result_set;
  30. };
  31. APR_DECLARE(fspr_status_t) fspr_pollset_create(fspr_pollset_t **pollset,
  32. fspr_uint32_t size,
  33. fspr_pool_t *p,
  34. fspr_uint32_t flags)
  35. {
  36. *pollset = fspr_palloc(p, sizeof(**pollset));
  37. (*pollset)->pool = p;
  38. (*pollset)->nelts = 0;
  39. (*pollset)->nalloc = size;
  40. (*pollset)->pollset = fspr_palloc(p, size * sizeof(int) * 3);
  41. (*pollset)->query_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  42. (*pollset)->result_set = fspr_palloc(p, size * sizeof(fspr_pollfd_t));
  43. (*pollset)->num_read = -1;
  44. return APR_SUCCESS;
  45. }
  46. APR_DECLARE(fspr_status_t) fspr_pollset_destroy(fspr_pollset_t *pollset)
  47. {
  48. /* A no-op function for now. If we later implement /dev/poll
  49. * support, we'll need to close the /dev/poll fd here
  50. */
  51. return APR_SUCCESS;
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_pollset_add(fspr_pollset_t *pollset,
  54. const fspr_pollfd_t *descriptor)
  55. {
  56. if (pollset->nelts == pollset->nalloc) {
  57. return APR_ENOMEM;
  58. }
  59. pollset->query_set[pollset->nelts] = *descriptor;
  60. if (descriptor->desc_type != APR_POLL_SOCKET) {
  61. return APR_EBADF;
  62. }
  63. pollset->nelts++;
  64. pollset->num_read = -1;
  65. return APR_SUCCESS;
  66. }
  67. APR_DECLARE(fspr_status_t) fspr_pollset_remove(fspr_pollset_t *pollset,
  68. const fspr_pollfd_t *descriptor)
  69. {
  70. fspr_uint32_t i;
  71. for (i = 0; i < pollset->nelts; i++) {
  72. if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  73. /* Found an instance of the fd: remove this and any other copies */
  74. fspr_uint32_t dst = i;
  75. fspr_uint32_t old_nelts = pollset->nelts;
  76. pollset->nelts--;
  77. for (i++; i < old_nelts; i++) {
  78. if (descriptor->desc.s == pollset->query_set[i].desc.s) {
  79. pollset->nelts--;
  80. }
  81. else {
  82. pollset->pollset[dst] = pollset->pollset[i];
  83. pollset->query_set[dst] = pollset->query_set[i];
  84. dst++;
  85. }
  86. }
  87. pollset->num_read = -1;
  88. return APR_SUCCESS;
  89. }
  90. }
  91. return APR_NOTFOUND;
  92. }
  93. static void make_pollset(fspr_pollset_t *pollset)
  94. {
  95. int i;
  96. int pos = 0;
  97. pollset->num_read = 0;
  98. pollset->num_write = 0;
  99. pollset->num_except = 0;
  100. for (i = 0; i < pollset->nelts; i++) {
  101. if (pollset->query_set[i].reqevents & APR_POLLIN) {
  102. pollset->pollset[pos++] = pollset->query_set[i].desc.s->socketdes;
  103. pollset->num_read++;
  104. }
  105. }
  106. for (i = 0; i < pollset->nelts; i++) {
  107. if (pollset->query_set[i].reqevents & APR_POLLOUT) {
  108. pollset->pollset[pos++] = pollset->query_set[i].desc.s->socketdes;
  109. pollset->num_write++;
  110. }
  111. }
  112. for (i = 0; i < pollset->nelts; i++) {
  113. if (pollset->query_set[i].reqevents & APR_POLLPRI) {
  114. pollset->pollset[pos++] = pollset->query_set[i].desc.s->socketdes;
  115. pollset->num_except++;
  116. }
  117. }
  118. pollset->num_total = pollset->num_read + pollset->num_write + pollset->num_except;
  119. }
  120. APR_DECLARE(fspr_status_t) fspr_pollset_poll(fspr_pollset_t *pollset,
  121. fspr_interval_time_t timeout,
  122. fspr_int32_t *num,
  123. const fspr_pollfd_t **descriptors)
  124. {
  125. int rv;
  126. fspr_uint32_t i;
  127. int *pollresult;
  128. int read_pos, write_pos, except_pos;
  129. if (pollset->num_read < 0) {
  130. make_pollset(pollset);
  131. }
  132. pollresult = alloca(sizeof(int) * pollset->num_total);
  133. memcpy(pollresult, pollset->pollset, sizeof(int) * pollset->num_total);
  134. (*num) = 0;
  135. if (timeout > 0) {
  136. timeout /= 1000;
  137. }
  138. rv = select(pollresult, pollset->num_read, pollset->num_write, pollset->num_except, timeout);
  139. if (rv < 0) {
  140. return APR_FROM_OS_ERROR(sock_errno());
  141. }
  142. if (rv == 0) {
  143. return APR_TIMEUP;
  144. }
  145. read_pos = 0;
  146. write_pos = pollset->num_read;
  147. except_pos = pollset->num_read + pollset->num_write;
  148. for (i = 0; i < pollset->nelts; i++) {
  149. int rtnevents = 0;
  150. if (pollset->query_set[i].reqevents & APR_POLLIN) {
  151. if (pollresult[read_pos++] != -1) {
  152. rtnevents |= APR_POLLIN;
  153. }
  154. }
  155. if (pollset->query_set[i].reqevents & APR_POLLOUT) {
  156. if (pollresult[write_pos++] != -1) {
  157. rtnevents |= APR_POLLOUT;
  158. }
  159. }
  160. if (pollset->query_set[i].reqevents & APR_POLLPRI) {
  161. if (pollresult[except_pos++] != -1) {
  162. rtnevents |= APR_POLLPRI;
  163. }
  164. }
  165. if (rtnevents) {
  166. pollset->result_set[*num] = pollset->query_set[i];
  167. pollset->result_set[*num].rtnevents = rtnevents;
  168. (*num)++;
  169. }
  170. }
  171. if (descriptors) {
  172. *descriptors = pollset->result_set;
  173. }
  174. return APR_SUCCESS;
  175. }