waitio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_file_io.h"
  17. #include "fspr_arch_networkio.h"
  18. #include "fspr_poll.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_support.h"
  21. /* The only case where we don't use wait_for_io_or_timeout is on
  22. * pre-BONE BeOS, so this check should be sufficient and simpler */
  23. #if !BEOS_R5
  24. #define USE_WAIT_FOR_IO
  25. #endif
  26. #ifdef USE_WAIT_FOR_IO
  27. #ifdef WAITIO_USES_POLL
  28. #ifdef HAVE_POLL_H
  29. #include <poll.h>
  30. #endif
  31. #ifdef HAVE_SYS_POLL_H
  32. #include <sys/poll.h>
  33. #endif
  34. fspr_status_t fspr_wait_for_io_or_timeout(fspr_file_t *f, fspr_socket_t *s,
  35. int for_read)
  36. {
  37. struct pollfd pfd;
  38. int rc, timeout;
  39. timeout = f ? f->timeout / 1000 : s->timeout / 1000;
  40. pfd.fd = f ? f->filedes : s->socketdes;
  41. pfd.events = for_read ? POLLIN : POLLOUT;
  42. do {
  43. rc = poll(&pfd, 1, timeout);
  44. } while (rc == -1 && errno == EINTR);
  45. if (rc == 0) {
  46. return APR_TIMEUP;
  47. }
  48. else if (rc > 0) {
  49. return APR_SUCCESS;
  50. }
  51. else {
  52. return errno;
  53. }
  54. }
  55. #else /* !WAITIO_USES_POLL */
  56. fspr_status_t fspr_wait_for_io_or_timeout(fspr_file_t *f, fspr_socket_t *s,
  57. int for_read)
  58. {
  59. fspr_interval_time_t timeout;
  60. fspr_pollfd_t pfd;
  61. int type = for_read ? APR_POLLIN : APR_POLLOUT;
  62. fspr_pollset_t *pollset;
  63. fspr_status_t status;
  64. /* TODO - timeout should be less each time through this loop */
  65. if (f) {
  66. pfd.desc_type = APR_POLL_FILE;
  67. pfd.desc.f = f;
  68. pollset = f->pollset;
  69. if (pollset == NULL) {
  70. status = fspr_pollset_create(&(f->pollset), 1, f->pool, 0);
  71. if (status != APR_SUCCESS) {
  72. return status;
  73. }
  74. pollset = f->pollset;
  75. }
  76. timeout = f->timeout;
  77. }
  78. else {
  79. pfd.desc_type = APR_POLL_SOCKET;
  80. pfd.desc.s = s;
  81. pollset = s->pollset;
  82. timeout = s->timeout;
  83. }
  84. pfd.reqevents = type;
  85. /* Remove the object if it was in the pollset, then add in the new
  86. * object with the correct reqevents value. Ignore the status result
  87. * on the remove, because it might not be in there (yet).
  88. */
  89. (void) fspr_pollset_remove(pollset, &pfd);
  90. /* ### check status code */
  91. (void) fspr_pollset_add(pollset, &pfd);
  92. do {
  93. int numdesc;
  94. const fspr_pollfd_t *pdesc;
  95. status = fspr_pollset_poll(pollset, timeout, &numdesc, &pdesc);
  96. if (numdesc == 1 && (pdesc[0].rtnevents & type) != 0) {
  97. return APR_SUCCESS;
  98. }
  99. } while (APR_STATUS_IS_EINTR(status));
  100. return status;
  101. }
  102. #endif /* WAITIO_USES_POLL */
  103. #endif /* USE_WAIT_FOR_IO */