alertsock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/net/alertsock.h"
  7. #include "lib/net/socket.h"
  8. #include "lib/log/util_bug.h"
  9. #ifdef HAVE_SYS_EVENTFD_H
  10. #include <sys/eventfd.h>
  11. #endif
  12. #ifdef HAVE_FCNTL_H
  13. #include <fcntl.h>
  14. #endif
  15. #ifdef HAVE_UNISTD_H
  16. #include <unistd.h>
  17. #endif
  18. #ifdef HAVE_SYS_SOCKET_H
  19. #include <sys/socket.h>
  20. #endif
  21. #ifdef _WIN32
  22. #include <winsock2.h>
  23. #endif
  24. #if defined(HAVE_EVENTFD) || defined(HAVE_PIPE)
  25. /* As write(), but retry on EINTR, and return the negative error code on
  26. * error. */
  27. static int
  28. write_ni(int fd, const void *buf, size_t n)
  29. {
  30. int r;
  31. again:
  32. r = (int) write(fd, buf, n);
  33. if (r < 0) {
  34. if (errno == EINTR)
  35. goto again;
  36. else
  37. return -errno;
  38. }
  39. return r;
  40. }
  41. /* As read(), but retry on EINTR, and return the negative error code on error.
  42. */
  43. static int
  44. read_ni(int fd, void *buf, size_t n)
  45. {
  46. int r;
  47. again:
  48. r = (int) read(fd, buf, n);
  49. if (r < 0) {
  50. if (errno == EINTR)
  51. goto again;
  52. else
  53. return -errno;
  54. }
  55. return r;
  56. }
  57. #endif /* defined(HAVE_EVENTFD) || defined(HAVE_PIPE) */
  58. /** As send(), but retry on EINTR, and return the negative error code on
  59. * error. */
  60. static int
  61. send_ni(int fd, const void *buf, size_t n, int flags)
  62. {
  63. int r;
  64. again:
  65. r = (int) send(fd, buf, n, flags);
  66. if (r < 0) {
  67. int error = tor_socket_errno(fd);
  68. if (ERRNO_IS_EINTR(error))
  69. goto again;
  70. else
  71. return -error;
  72. }
  73. return r;
  74. }
  75. /** As recv(), but retry on EINTR, and return the negative error code on
  76. * error. */
  77. static int
  78. recv_ni(int fd, void *buf, size_t n, int flags)
  79. {
  80. int r;
  81. again:
  82. r = (int) recv(fd, buf, n, flags);
  83. if (r < 0) {
  84. int error = tor_socket_errno(fd);
  85. if (ERRNO_IS_EINTR(error))
  86. goto again;
  87. else
  88. return -error;
  89. }
  90. return r;
  91. }
  92. #ifdef HAVE_EVENTFD
  93. /* Increment the event count on an eventfd <b>fd</b> */
  94. static int
  95. eventfd_alert(int fd)
  96. {
  97. uint64_t u = 1;
  98. int r = write_ni(fd, (void*)&u, sizeof(u));
  99. if (r < 0 && -r != EAGAIN)
  100. return -1;
  101. return 0;
  102. }
  103. /* Drain all events from an eventfd <b>fd</b>. */
  104. static int
  105. eventfd_drain(int fd)
  106. {
  107. uint64_t u = 0;
  108. int r = read_ni(fd, (void*)&u, sizeof(u));
  109. if (r < 0 && -r != EAGAIN)
  110. return r;
  111. return 0;
  112. }
  113. #endif /* defined(HAVE_EVENTFD) */
  114. #ifdef HAVE_PIPE
  115. /** Send a byte over a pipe. Return 0 on success or EAGAIN; -1 on error */
  116. static int
  117. pipe_alert(int fd)
  118. {
  119. ssize_t r = write_ni(fd, "x", 1);
  120. if (r < 0 && -r != EAGAIN)
  121. return (int)r;
  122. return 0;
  123. }
  124. /** Drain all input from a pipe <b>fd</b> and ignore it. Return 0 on
  125. * success, -1 on error. */
  126. static int
  127. pipe_drain(int fd)
  128. {
  129. char buf[32];
  130. ssize_t r;
  131. do {
  132. r = read_ni(fd, buf, sizeof(buf));
  133. } while (r > 0);
  134. if (r < 0 && errno != EAGAIN)
  135. return -errno;
  136. /* A value of r = 0 means EOF on the fd so successfully drained. */
  137. return 0;
  138. }
  139. #endif /* defined(HAVE_PIPE) */
  140. /** Send a byte on socket <b>fd</b>t. Return 0 on success or EAGAIN,
  141. * -1 on error. */
  142. static int
  143. sock_alert(tor_socket_t fd)
  144. {
  145. ssize_t r = send_ni(fd, "x", 1, 0);
  146. if (r < 0 && !ERRNO_IS_EAGAIN(-r))
  147. return (int)r;
  148. return 0;
  149. }
  150. /** Drain all the input from a socket <b>fd</b>, and ignore it. Return 0 on
  151. * success, -errno on error. */
  152. static int
  153. sock_drain(tor_socket_t fd)
  154. {
  155. char buf[32];
  156. ssize_t r;
  157. do {
  158. r = recv_ni(fd, buf, sizeof(buf), 0);
  159. } while (r > 0);
  160. if (r < 0 && !ERRNO_IS_EAGAIN(-r))
  161. return (int)r;
  162. /* A value of r = 0 means EOF on the fd so successfully drained. */
  163. return 0;
  164. }
  165. /** Allocate a new set of alert sockets, and set the appropriate function
  166. * pointers, in <b>socks_out</b>. */
  167. int
  168. alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
  169. {
  170. tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
  171. #ifdef HAVE_EVENTFD
  172. /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
  173. * associated with a single file descriptor. */
  174. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  175. if (!(flags & ASOCKS_NOEVENTFD2))
  176. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  177. #endif
  178. if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
  179. socks[0] = eventfd(0,0);
  180. if (socks[0] >= 0) {
  181. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  182. set_socket_nonblocking(socks[0]) < 0) {
  183. // LCOV_EXCL_START -- if eventfd succeeds, fcntl will.
  184. tor_assert_nonfatal_unreached();
  185. close(socks[0]);
  186. return -1;
  187. // LCOV_EXCL_STOP
  188. }
  189. }
  190. }
  191. if (socks[0] >= 0) {
  192. socks_out->read_fd = socks_out->write_fd = socks[0];
  193. socks_out->alert_fn = eventfd_alert;
  194. socks_out->drain_fn = eventfd_drain;
  195. return 0;
  196. }
  197. #endif /* defined(HAVE_EVENTFD) */
  198. #ifdef HAVE_PIPE2
  199. /* Now we're going to try pipes. First type the pipe2() syscall, if we
  200. * have it, so we can save some calls... */
  201. if (!(flags & ASOCKS_NOPIPE2) &&
  202. pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  203. socks_out->read_fd = socks[0];
  204. socks_out->write_fd = socks[1];
  205. socks_out->alert_fn = pipe_alert;
  206. socks_out->drain_fn = pipe_drain;
  207. return 0;
  208. }
  209. #endif /* defined(HAVE_PIPE2) */
  210. #ifdef HAVE_PIPE
  211. /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
  212. * socketpairs, fwict. */
  213. if (!(flags & ASOCKS_NOPIPE) &&
  214. pipe(socks) == 0) {
  215. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  216. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  217. set_socket_nonblocking(socks[0]) < 0 ||
  218. set_socket_nonblocking(socks[1]) < 0) {
  219. // LCOV_EXCL_START -- if pipe succeeds, you can fcntl the output
  220. tor_assert_nonfatal_unreached();
  221. close(socks[0]);
  222. close(socks[1]);
  223. return -1;
  224. // LCOV_EXCL_STOP
  225. }
  226. socks_out->read_fd = socks[0];
  227. socks_out->write_fd = socks[1];
  228. socks_out->alert_fn = pipe_alert;
  229. socks_out->drain_fn = pipe_drain;
  230. return 0;
  231. }
  232. #endif /* defined(HAVE_PIPE) */
  233. /* If nothing else worked, fall back on socketpair(). */
  234. if (!(flags & ASOCKS_NOSOCKETPAIR) &&
  235. tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  236. if (set_socket_nonblocking(socks[0]) < 0 ||
  237. set_socket_nonblocking(socks[1])) {
  238. // LCOV_EXCL_START -- if socketpair worked, you can make it nonblocking.
  239. tor_assert_nonfatal_unreached();
  240. tor_close_socket(socks[0]);
  241. tor_close_socket(socks[1]);
  242. return -1;
  243. // LCOV_EXCL_STOP
  244. }
  245. socks_out->read_fd = socks[0];
  246. socks_out->write_fd = socks[1];
  247. socks_out->alert_fn = sock_alert;
  248. socks_out->drain_fn = sock_drain;
  249. return 0;
  250. }
  251. return -1;
  252. }
  253. /** Close the sockets in <b>socks</b>. */
  254. void
  255. alert_sockets_close(alert_sockets_t *socks)
  256. {
  257. if (socks->alert_fn == sock_alert) {
  258. /* they are sockets. */
  259. tor_close_socket(socks->read_fd);
  260. tor_close_socket(socks->write_fd);
  261. } else {
  262. close(socks->read_fd);
  263. if (socks->write_fd != socks->read_fd)
  264. close(socks->write_fd);
  265. }
  266. socks->read_fd = socks->write_fd = -1;
  267. }