compat_threads.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 = 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 = 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 = 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 = 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(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. while ((r = read(fd, buf, sizeof(buf))) >= 0)
  159. ;
  160. if (r == 0 || errno != EAGAIN)
  161. return -1;
  162. return 0;
  163. }
  164. #endif
  165. static int
  166. sock_alert(tor_socket_t fd)
  167. {
  168. ssize_t r = send_ni(fd, "x", 1, 0);
  169. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  170. return -1;
  171. return 0;
  172. }
  173. static int
  174. sock_drain(tor_socket_t fd)
  175. {
  176. char buf[32];
  177. ssize_t r;
  178. while ((r = recv_ni(fd, buf, sizeof(buf), 0)) >= 0)
  179. ;
  180. if (r == 0 || !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  181. return -1;
  182. return 0;
  183. }
  184. /** Allocate a new set of alert sockets, and set the appropriate function
  185. * pointers, in <b>socks_out</b>. */
  186. int
  187. alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
  188. {
  189. tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
  190. #ifdef HAVE_EVENTFD
  191. /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
  192. * associated with a single file descriptor. */
  193. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  194. if (!(flags & ASOCKS_NOEVENTFD2))
  195. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  196. #endif
  197. if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
  198. socks[0] = eventfd(0,0);
  199. if (socks[0] >= 0) {
  200. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  201. set_socket_nonblocking(socks[0]) < 0) {
  202. close(socks[0]);
  203. return -1;
  204. }
  205. }
  206. }
  207. if (socks[0] >= 0) {
  208. socks_out->read_fd = socks_out->write_fd = socks[0];
  209. socks_out->alert_fn = eventfd_alert;
  210. socks_out->drain_fn = eventfd_drain;
  211. return 0;
  212. }
  213. #endif
  214. #ifdef HAVE_PIPE2
  215. /* Now we're going to try pipes. First type the pipe2() syscall, if we
  216. * have it, so we can save some calls... */
  217. if (!(flags & ASOCKS_NOPIPE2) &&
  218. pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  219. socks_out->read_fd = socks[0];
  220. socks_out->write_fd = socks[1];
  221. socks_out->alert_fn = pipe_alert;
  222. socks_out->drain_fn = pipe_drain;
  223. return 0;
  224. }
  225. #endif
  226. #ifdef HAVE_PIPE
  227. /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
  228. * socketpairs, fwict. */
  229. if (!(flags & ASOCKS_NOPIPE) &&
  230. pipe(socks) == 0) {
  231. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  232. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  233. set_socket_nonblocking(socks[0]) < 0 ||
  234. set_socket_nonblocking(socks[1]) < 0) {
  235. close(socks[0]);
  236. close(socks[1]);
  237. return -1;
  238. }
  239. socks_out->read_fd = socks[0];
  240. socks_out->write_fd = socks[1];
  241. socks_out->alert_fn = pipe_alert;
  242. socks_out->drain_fn = pipe_drain;
  243. return 0;
  244. }
  245. #endif
  246. /* If nothing else worked, fall back on socketpair(). */
  247. if (!(flags & ASOCKS_NOSOCKETPAIR) &&
  248. tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  249. if (set_socket_nonblocking(socks[0]) < 0 ||
  250. set_socket_nonblocking(socks[1])) {
  251. tor_close_socket(socks[0]);
  252. tor_close_socket(socks[1]);
  253. return -1;
  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. }