alertsock.c 7.3 KB

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