compat_threads.c 6.5 KB

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