compat_threads.c 6.7 KB

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