compat_threads.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compat_threads.c
  7. *
  8. * \brief Cross-platform threading and inter-thread communication logic.
  9. * (Platform-specific parts are written in the other compat_*threads
  10. * modules.)
  11. */
  12. #include "orconfig.h"
  13. #include <stdlib.h>
  14. #include "compat.h"
  15. #include "compat_threads.h"
  16. #include "util.h"
  17. #include "torlog.h"
  18. #ifdef HAVE_SYS_EVENTFD_H
  19. #include <sys/eventfd.h>
  20. #endif
  21. #ifdef HAVE_FCNTL_H
  22. #include <fcntl.h>
  23. #endif
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. /** Return a newly allocated, ready-for-use mutex. */
  28. tor_mutex_t *
  29. tor_mutex_new(void)
  30. {
  31. tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
  32. tor_mutex_init(m);
  33. return m;
  34. }
  35. /** Return a newly allocated, ready-for-use mutex. This one might be
  36. * non-recursive, if that's faster. */
  37. tor_mutex_t *
  38. tor_mutex_new_nonrecursive(void)
  39. {
  40. tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
  41. tor_mutex_init_nonrecursive(m);
  42. return m;
  43. }
  44. /** Release all storage and system resources held by <b>m</b>. */
  45. void
  46. tor_mutex_free(tor_mutex_t *m)
  47. {
  48. if (!m)
  49. return;
  50. tor_mutex_uninit(m);
  51. tor_free(m);
  52. }
  53. /** Allocate and return a new condition variable. */
  54. tor_cond_t *
  55. tor_cond_new(void)
  56. {
  57. tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
  58. if (tor_cond_init(cond)<0)
  59. tor_free(cond);
  60. return cond;
  61. }
  62. /** Free all storage held in <b>c</b>. */
  63. void
  64. tor_cond_free(tor_cond_t *c)
  65. {
  66. if (!c)
  67. return;
  68. tor_cond_uninit(c);
  69. tor_free(c);
  70. }
  71. /** Identity of the "main" thread */
  72. static unsigned long main_thread_id = -1;
  73. /** Start considering the current thread to be the 'main thread'. This has
  74. * no effect on anything besides in_main_thread(). */
  75. void
  76. set_main_thread(void)
  77. {
  78. main_thread_id = tor_get_thread_id();
  79. }
  80. /** Return true iff called from the main thread. */
  81. int
  82. in_main_thread(void)
  83. {
  84. return main_thread_id == tor_get_thread_id();
  85. }
  86. #if defined(HAVE_EVENTFD) || defined(HAVE_PIPE)
  87. /* As write(), but retry on EINTR */
  88. static int
  89. write_ni(int fd, const void *buf, size_t n)
  90. {
  91. int r;
  92. again:
  93. r = (int) write(fd, buf, n);
  94. if (r < 0 && errno == EINTR)
  95. goto again;
  96. return r;
  97. }
  98. /* As read(), but retry on EINTR */
  99. static int
  100. read_ni(int fd, void *buf, size_t n)
  101. {
  102. int r;
  103. again:
  104. r = (int) read(fd, buf, n);
  105. if (r < 0 && errno == EINTR)
  106. goto again;
  107. return r;
  108. }
  109. #endif
  110. /** As send(), but retry on EINTR. */
  111. static int
  112. send_ni(int fd, const void *buf, size_t n, int flags)
  113. {
  114. int r;
  115. again:
  116. r = (int) send(fd, buf, n, flags);
  117. if (r < 0 && ERRNO_IS_EINTR(tor_socket_errno(fd)))
  118. goto again;
  119. return r;
  120. }
  121. /** As recv(), but retry on EINTR. */
  122. static int
  123. recv_ni(int fd, void *buf, size_t n, int flags)
  124. {
  125. int r;
  126. again:
  127. r = (int) recv(fd, buf, n, flags);
  128. if (r < 0 && ERRNO_IS_EINTR(tor_socket_errno(fd)))
  129. goto again;
  130. return r;
  131. }
  132. #ifdef HAVE_EVENTFD
  133. /* Increment the event count on an eventfd <b>fd</b> */
  134. static int
  135. eventfd_alert(int fd)
  136. {
  137. uint64_t u = 1;
  138. int r = write_ni(fd, (void*)&u, sizeof(u));
  139. if (r < 0 && errno != EAGAIN)
  140. return -1;
  141. return 0;
  142. }
  143. /* Drain all events from an eventfd <b>fd</b>. */
  144. static int
  145. eventfd_drain(int fd)
  146. {
  147. uint64_t u = 0;
  148. int r = read_ni(fd, (void*)&u, sizeof(u));
  149. if (r < 0 && errno != EAGAIN)
  150. return -1;
  151. return 0;
  152. }
  153. #endif
  154. #ifdef HAVE_PIPE
  155. /** Send a byte over a pipe. Return 0 on success or EAGAIN; -1 on error */
  156. static int
  157. pipe_alert(int fd)
  158. {
  159. ssize_t r = write_ni(fd, "x", 1);
  160. if (r < 0 && errno != EAGAIN)
  161. return -1;
  162. return 0;
  163. }
  164. /** Drain all input from a pipe <b>fd</b> and ignore it. Return 0 on
  165. * success, -1 on error. */
  166. static int
  167. pipe_drain(int fd)
  168. {
  169. char buf[32];
  170. ssize_t r;
  171. do {
  172. r = read_ni(fd, buf, sizeof(buf));
  173. } while (r > 0);
  174. if (r < 0 && errno != EAGAIN)
  175. return -1;
  176. /* A value of r = 0 means EOF on the fd so successfully drained. */
  177. return 0;
  178. }
  179. #endif
  180. /** Send a byte on socket <b>fd</b>t. Return 0 on success or EAGAIN,
  181. * -1 on error. */
  182. static int
  183. sock_alert(tor_socket_t fd)
  184. {
  185. ssize_t r = send_ni(fd, "x", 1, 0);
  186. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  187. return -1;
  188. return 0;
  189. }
  190. /** Drain all the input from a socket <b>fd</b>, and ignore it. Return 0 on
  191. * success, -1 on error. */
  192. static int
  193. sock_drain(tor_socket_t fd)
  194. {
  195. char buf[32];
  196. ssize_t r;
  197. do {
  198. r = recv_ni(fd, buf, sizeof(buf), 0);
  199. } while (r > 0);
  200. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  201. return -1;
  202. /* A value of r = 0 means EOF on the fd so successfully drained. */
  203. return 0;
  204. }
  205. /** Allocate a new set of alert sockets, and set the appropriate function
  206. * pointers, in <b>socks_out</b>. */
  207. int
  208. alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
  209. {
  210. tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
  211. #ifdef HAVE_EVENTFD
  212. /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
  213. * associated with a single file descriptor. */
  214. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  215. if (!(flags & ASOCKS_NOEVENTFD2))
  216. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  217. #endif
  218. if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
  219. socks[0] = eventfd(0,0);
  220. if (socks[0] >= 0) {
  221. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  222. set_socket_nonblocking(socks[0]) < 0) {
  223. close(socks[0]);
  224. return -1;
  225. }
  226. }
  227. }
  228. if (socks[0] >= 0) {
  229. socks_out->read_fd = socks_out->write_fd = socks[0];
  230. socks_out->alert_fn = eventfd_alert;
  231. socks_out->drain_fn = eventfd_drain;
  232. return 0;
  233. }
  234. #endif
  235. #ifdef HAVE_PIPE2
  236. /* Now we're going to try pipes. First type the pipe2() syscall, if we
  237. * have it, so we can save some calls... */
  238. if (!(flags & ASOCKS_NOPIPE2) &&
  239. pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  240. socks_out->read_fd = socks[0];
  241. socks_out->write_fd = socks[1];
  242. socks_out->alert_fn = pipe_alert;
  243. socks_out->drain_fn = pipe_drain;
  244. return 0;
  245. }
  246. #endif
  247. #ifdef HAVE_PIPE
  248. /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
  249. * socketpairs, fwict. */
  250. if (!(flags & ASOCKS_NOPIPE) &&
  251. pipe(socks) == 0) {
  252. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  253. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  254. set_socket_nonblocking(socks[0]) < 0 ||
  255. set_socket_nonblocking(socks[1]) < 0) {
  256. close(socks[0]);
  257. close(socks[1]);
  258. return -1;
  259. }
  260. socks_out->read_fd = socks[0];
  261. socks_out->write_fd = socks[1];
  262. socks_out->alert_fn = pipe_alert;
  263. socks_out->drain_fn = pipe_drain;
  264. return 0;
  265. }
  266. #endif
  267. /* If nothing else worked, fall back on socketpair(). */
  268. if (!(flags & ASOCKS_NOSOCKETPAIR) &&
  269. tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  270. if (set_socket_nonblocking(socks[0]) < 0 ||
  271. set_socket_nonblocking(socks[1])) {
  272. tor_close_socket(socks[0]);
  273. tor_close_socket(socks[1]);
  274. return -1;
  275. }
  276. socks_out->read_fd = socks[0];
  277. socks_out->write_fd = socks[1];
  278. socks_out->alert_fn = sock_alert;
  279. socks_out->drain_fn = sock_drain;
  280. return 0;
  281. }
  282. return -1;
  283. }
  284. /** Close the sockets in <b>socks</b>. */
  285. void
  286. alert_sockets_close(alert_sockets_t *socks)
  287. {
  288. if (socks->alert_fn == sock_alert) {
  289. /* they are sockets. */
  290. tor_close_socket(socks->read_fd);
  291. tor_close_socket(socks->write_fd);
  292. } else {
  293. close(socks->read_fd);
  294. if (socks->write_fd != socks->read_fd)
  295. close(socks->write_fd);
  296. }
  297. socks->read_fd = socks->write_fd = -1;
  298. }