abort_socket.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2009, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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 <stdlib.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/socket.h>
  21. #include <poll.h>
  22. #include "cutils/abort_socket.h"
  23. struct asocket *asocket_init(int fd) {
  24. int abort_fd[2];
  25. int flags;
  26. struct asocket *s;
  27. /* set primary socket to non-blocking */
  28. flags = fcntl(fd, F_GETFL);
  29. if (flags == -1)
  30. return NULL;
  31. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
  32. return NULL;
  33. /* create pipe with non-blocking write, so that asocket_close() cannot
  34. block */
  35. if (pipe(abort_fd))
  36. return NULL;
  37. flags = fcntl(abort_fd[1], F_GETFL);
  38. if (flags == -1)
  39. return NULL;
  40. if (fcntl(abort_fd[1], F_SETFL, flags | O_NONBLOCK))
  41. return NULL;
  42. s = malloc(sizeof(struct asocket));
  43. if (!s)
  44. return NULL;
  45. s->fd = fd;
  46. s->abort_fd[0] = abort_fd[0];
  47. s->abort_fd[1] = abort_fd[1];
  48. return s;
  49. }
  50. int asocket_connect(struct asocket *s, const struct sockaddr *addr,
  51. socklen_t addrlen, int timeout) {
  52. int ret;
  53. do {
  54. ret = connect(s->fd, addr, addrlen);
  55. } while (ret && errno == EINTR);
  56. if (ret && errno == EINPROGRESS) {
  57. /* ready to poll() */
  58. socklen_t retlen;
  59. struct pollfd pfd[2];
  60. pfd[0].fd = s->fd;
  61. pfd[0].events = POLLOUT;
  62. pfd[0].revents = 0;
  63. pfd[1].fd = s->abort_fd[0];
  64. pfd[1].events = POLLIN;
  65. pfd[1].revents = 0;
  66. do {
  67. ret = poll(pfd, 2, timeout);
  68. } while (ret < 0 && errno == EINTR);
  69. if (ret < 0)
  70. return -1;
  71. else if (ret == 0) {
  72. /* timeout */
  73. errno = ETIMEDOUT;
  74. return -1;
  75. }
  76. if (pfd[1].revents) {
  77. /* abort due to asocket_abort() */
  78. errno = ECANCELED;
  79. return -1;
  80. }
  81. if (pfd[0].revents) {
  82. if (pfd[0].revents & POLLOUT) {
  83. /* connect call complete, read return code */
  84. retlen = sizeof(ret);
  85. if (getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen))
  86. return -1;
  87. /* got connect() return code */
  88. if (ret) {
  89. errno = ret;
  90. }
  91. } else {
  92. /* some error event on this fd */
  93. errno = ECONNABORTED;
  94. return -1;
  95. }
  96. }
  97. }
  98. return ret;
  99. }
  100. int asocket_accept(struct asocket *s, struct sockaddr *addr,
  101. socklen_t *addrlen, int timeout) {
  102. int ret;
  103. struct pollfd pfd[2];
  104. pfd[0].fd = s->fd;
  105. pfd[0].events = POLLIN;
  106. pfd[0].revents = 0;
  107. pfd[1].fd = s->abort_fd[0];
  108. pfd[1].events = POLLIN;
  109. pfd[1].revents = 0;
  110. do {
  111. ret = poll(pfd, 2, timeout);
  112. } while (ret < 0 && errno == EINTR);
  113. if (ret < 0)
  114. return -1;
  115. else if (ret == 0) {
  116. /* timeout */
  117. errno = ETIMEDOUT;
  118. return -1;
  119. }
  120. if (pfd[1].revents) {
  121. /* abort due to asocket_abort() */
  122. errno = ECANCELED;
  123. return -1;
  124. }
  125. if (pfd[0].revents) {
  126. if (pfd[0].revents & POLLIN) {
  127. /* ready to accept() without blocking */
  128. do {
  129. ret = accept(s->fd, addr, addrlen);
  130. } while (ret < 0 && errno == EINTR);
  131. } else {
  132. /* some error event on this fd */
  133. errno = ECONNABORTED;
  134. return -1;
  135. }
  136. }
  137. return ret;
  138. }
  139. int asocket_read(struct asocket *s, void *buf, size_t count, int timeout) {
  140. int ret;
  141. struct pollfd pfd[2];
  142. pfd[0].fd = s->fd;
  143. pfd[0].events = POLLIN;
  144. pfd[0].revents = 0;
  145. pfd[1].fd = s->abort_fd[0];
  146. pfd[1].events = POLLIN;
  147. pfd[1].revents = 0;
  148. do {
  149. ret = poll(pfd, 2, timeout);
  150. } while (ret < 0 && errno == EINTR);
  151. if (ret < 0)
  152. return -1;
  153. else if (ret == 0) {
  154. /* timeout */
  155. errno = ETIMEDOUT;
  156. return -1;
  157. }
  158. if (pfd[1].revents) {
  159. /* abort due to asocket_abort() */
  160. errno = ECANCELED;
  161. return -1;
  162. }
  163. if (pfd[0].revents) {
  164. if (pfd[0].revents & POLLIN) {
  165. /* ready to read() without blocking */
  166. do {
  167. ret = read(s->fd, buf, count);
  168. } while (ret < 0 && errno == EINTR);
  169. } else {
  170. /* some error event on this fd */
  171. errno = ECONNABORTED;
  172. return -1;
  173. }
  174. }
  175. return ret;
  176. }
  177. int asocket_write(struct asocket *s, const void *buf, size_t count,
  178. int timeout) {
  179. int ret;
  180. struct pollfd pfd[2];
  181. pfd[0].fd = s->fd;
  182. pfd[0].events = POLLOUT;
  183. pfd[0].revents = 0;
  184. pfd[1].fd = s->abort_fd[0];
  185. pfd[1].events = POLLIN;
  186. pfd[1].revents = 0;
  187. do {
  188. ret = poll(pfd, 2, timeout);
  189. } while (ret < 0 && errno == EINTR);
  190. if (ret < 0)
  191. return -1;
  192. else if (ret == 0) {
  193. /* timeout */
  194. errno = ETIMEDOUT;
  195. return -1;
  196. }
  197. if (pfd[1].revents) {
  198. /* abort due to asocket_abort() */
  199. errno = ECANCELED;
  200. return -1;
  201. }
  202. if (pfd[0].revents) {
  203. if (pfd[0].revents & POLLOUT) {
  204. /* ready to write() without blocking */
  205. do {
  206. ret = write(s->fd, buf, count);
  207. } while (ret < 0 && errno == EINTR);
  208. } else {
  209. /* some error event on this fd */
  210. errno = ECONNABORTED;
  211. return -1;
  212. }
  213. }
  214. return ret;
  215. }
  216. void asocket_abort(struct asocket *s) {
  217. int ret;
  218. char buf = 0;
  219. /* Prevent further use of fd, without yet releasing the fd */
  220. shutdown(s->fd, SHUT_RDWR);
  221. /* wake up calls blocked at poll() */
  222. do {
  223. ret = write(s->abort_fd[1], &buf, 1);
  224. } while (ret < 0 && errno == EINTR);
  225. }
  226. void asocket_destroy(struct asocket *s) {
  227. struct asocket s_copy = *s;
  228. /* Clients should *not* be using these fd's after calling
  229. asocket_destroy(), but in case they do, set to -1 so they cannot use a
  230. stale fd */
  231. s->fd = -1;
  232. s->abort_fd[0] = -1;
  233. s->abort_fd[1] = -1;
  234. /* Call asocket_abort() in case there are still threads blocked on this
  235. socket. Clients should not rely on this behavior - it is racy because we
  236. are about to close() these sockets - clients should instead make sure
  237. all threads are done with the socket before calling asocket_destory().
  238. */
  239. asocket_abort(&s_copy);
  240. /* enough safety checks, close and release memory */
  241. close(s_copy.abort_fd[1]);
  242. close(s_copy.abort_fd[0]);
  243. close(s_copy.fd);
  244. free(s);
  245. }