compat_threads.c 7.5 KB

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