compat_threads.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. /* As write(), but retry on EINTR */
  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. /* As read(), but retry on EINTR */
  93. static int
  94. read_ni(int fd, void *buf, size_t n)
  95. {
  96. int r;
  97. again:
  98. r = (int) read(fd, buf, n);
  99. if (r < 0 && errno == EINTR)
  100. goto again;
  101. return r;
  102. }
  103. #endif
  104. /** As send(), but retry on EINTR. */
  105. static int
  106. send_ni(int fd, const void *buf, size_t n, int flags)
  107. {
  108. int r;
  109. again:
  110. r = (int) send(fd, buf, n, flags);
  111. if (r < 0 && ERRNO_IS_EINTR(tor_socket_errno(fd)))
  112. goto again;
  113. return r;
  114. }
  115. /** As recv(), but retry on EINTR. */
  116. static int
  117. recv_ni(int fd, void *buf, size_t n, int flags)
  118. {
  119. int r;
  120. again:
  121. r = (int) recv(fd, buf, n, flags);
  122. if (r < 0 && ERRNO_IS_EINTR(tor_socket_errno(fd)))
  123. goto again;
  124. return r;
  125. }
  126. #ifdef HAVE_EVENTFD
  127. /* Increment the event count on an eventfd <b>fd</b> */
  128. static int
  129. eventfd_alert(int fd)
  130. {
  131. uint64_t u = 1;
  132. int r = write_ni(fd, (void*)&u, sizeof(u));
  133. if (r < 0 && errno != EAGAIN)
  134. return -1;
  135. return 0;
  136. }
  137. /* Drain all events from an eventfd <b>fd</b>. */
  138. static int
  139. eventfd_drain(int fd)
  140. {
  141. uint64_t u = 0;
  142. int r = read_ni(fd, (void*)&u, sizeof(u));
  143. if (r < 0 && errno != EAGAIN)
  144. return -1;
  145. return 0;
  146. }
  147. #endif
  148. #ifdef HAVE_PIPE
  149. /** Send a byte over a pipe. Return 0 on success or EAGAIN; -1 on error */
  150. static int
  151. pipe_alert(int fd)
  152. {
  153. ssize_t r = write_ni(fd, "x", 1);
  154. if (r < 0 && errno != EAGAIN)
  155. return -1;
  156. return 0;
  157. }
  158. /** Drain all input from a pipe <b>fd</b> and ignore it. Return 0 on
  159. * success, -1 on error. */
  160. static int
  161. pipe_drain(int fd)
  162. {
  163. char buf[32];
  164. ssize_t r;
  165. do {
  166. r = read_ni(fd, buf, sizeof(buf));
  167. } while (r > 0);
  168. if (r < 0 && errno != EAGAIN)
  169. return -1;
  170. /* A value of r = 0 means EOF on the fd so successfully drained. */
  171. return 0;
  172. }
  173. #endif
  174. /** Send a byte on socket <b>fd</b>t. Return 0 on success or EAGAIN,
  175. * -1 on error. */
  176. static int
  177. sock_alert(tor_socket_t fd)
  178. {
  179. ssize_t r = send_ni(fd, "x", 1, 0);
  180. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  181. return -1;
  182. return 0;
  183. }
  184. /** Drain all the input from a socket <b>fd</b>, and ignore it. Return 0 on
  185. * success, -1 on error. */
  186. static int
  187. sock_drain(tor_socket_t fd)
  188. {
  189. char buf[32];
  190. ssize_t r;
  191. do {
  192. r = recv_ni(fd, buf, sizeof(buf), 0);
  193. } while (r > 0);
  194. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  195. return -1;
  196. /* A value of r = 0 means EOF on the fd so successfully drained. */
  197. return 0;
  198. }
  199. /** Allocate a new set of alert sockets, and set the appropriate function
  200. * pointers, in <b>socks_out</b>. */
  201. int
  202. alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
  203. {
  204. tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
  205. #ifdef HAVE_EVENTFD
  206. /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
  207. * associated with a single file descriptor. */
  208. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  209. if (!(flags & ASOCKS_NOEVENTFD2))
  210. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  211. #endif
  212. if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
  213. socks[0] = eventfd(0,0);
  214. if (socks[0] >= 0) {
  215. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  216. set_socket_nonblocking(socks[0]) < 0) {
  217. close(socks[0]);
  218. return -1;
  219. }
  220. }
  221. }
  222. if (socks[0] >= 0) {
  223. socks_out->read_fd = socks_out->write_fd = socks[0];
  224. socks_out->alert_fn = eventfd_alert;
  225. socks_out->drain_fn = eventfd_drain;
  226. return 0;
  227. }
  228. #endif
  229. #ifdef HAVE_PIPE2
  230. /* Now we're going to try pipes. First type the pipe2() syscall, if we
  231. * have it, so we can save some calls... */
  232. if (!(flags & ASOCKS_NOPIPE2) &&
  233. pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  234. socks_out->read_fd = socks[0];
  235. socks_out->write_fd = socks[1];
  236. socks_out->alert_fn = pipe_alert;
  237. socks_out->drain_fn = pipe_drain;
  238. return 0;
  239. }
  240. #endif
  241. #ifdef HAVE_PIPE
  242. /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
  243. * socketpairs, fwict. */
  244. if (!(flags & ASOCKS_NOPIPE) &&
  245. pipe(socks) == 0) {
  246. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  247. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  248. set_socket_nonblocking(socks[0]) < 0 ||
  249. set_socket_nonblocking(socks[1]) < 0) {
  250. close(socks[0]);
  251. close(socks[1]);
  252. return -1;
  253. }
  254. socks_out->read_fd = socks[0];
  255. socks_out->write_fd = socks[1];
  256. socks_out->alert_fn = pipe_alert;
  257. socks_out->drain_fn = pipe_drain;
  258. return 0;
  259. }
  260. #endif
  261. /* If nothing else worked, fall back on socketpair(). */
  262. if (!(flags & ASOCKS_NOSOCKETPAIR) &&
  263. tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  264. if (set_socket_nonblocking(socks[0]) < 0 ||
  265. set_socket_nonblocking(socks[1])) {
  266. tor_close_socket(socks[0]);
  267. tor_close_socket(socks[1]);
  268. return -1;
  269. }
  270. socks_out->read_fd = socks[0];
  271. socks_out->write_fd = socks[1];
  272. socks_out->alert_fn = sock_alert;
  273. socks_out->drain_fn = sock_drain;
  274. return 0;
  275. }
  276. return -1;
  277. }
  278. /** Close the sockets in <b>socks</b>. */
  279. void
  280. alert_sockets_close(alert_sockets_t *socks)
  281. {
  282. if (socks->alert_fn == sock_alert) {
  283. /* they are sockets. */
  284. tor_close_socket(socks->read_fd);
  285. tor_close_socket(socks->write_fd);
  286. } else {
  287. close(socks->read_fd);
  288. if (socks->write_fd != socks->read_fd)
  289. close(socks->write_fd);
  290. }
  291. socks->read_fd = socks->write_fd = -1;
  292. }