compat_threads.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #ifdef HAVE_EVENTFD
  82. static int
  83. eventfd_alert(int fd)
  84. {
  85. uint64_t u = 1;
  86. int r = write(fd, (void*)&u, sizeof(u));
  87. if (r < 0 && errno != EAGAIN)
  88. return -1;
  89. return 0;
  90. }
  91. static int
  92. eventfd_drain(int fd)
  93. {
  94. uint64_t u = 0;
  95. int r = read(fd, (void*)&u, sizeof(u));
  96. if (r < 0 && errno != EAGAIN)
  97. return -1;
  98. return 0;
  99. }
  100. #endif
  101. #ifdef HAVE_PIPE
  102. static int
  103. pipe_alert(int fd)
  104. {
  105. ssize_t r = write(fd, "x", 1);
  106. if (r < 0 && errno != EAGAIN)
  107. return -1;
  108. return 0;
  109. }
  110. static int
  111. pipe_drain(int fd)
  112. {
  113. char buf[32];
  114. ssize_t r;
  115. while ((r = read(fd, buf, sizeof(buf))) >= 0)
  116. ;
  117. if (r == 0 || errno != EAGAIN)
  118. return -1;
  119. return 0;
  120. }
  121. #endif
  122. static int
  123. sock_alert(tor_socket_t fd)
  124. {
  125. ssize_t r = send(fd, "x", 1, 0);
  126. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  127. return -1;
  128. return 0;
  129. }
  130. static int
  131. sock_drain(tor_socket_t fd)
  132. {
  133. char buf[32];
  134. ssize_t r;
  135. while ((r = recv(fd, buf, sizeof(buf), 0)) >= 0)
  136. ;
  137. if (r == 0 || !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  138. return -1;
  139. return 0;
  140. }
  141. /** Allocate a new set of alert sockets, and set the appropriate function
  142. * pointers, in <b>socks_out</b>. */
  143. int
  144. alert_sockets_create(alert_sockets_t *socks_out, uint32_t flags)
  145. {
  146. tor_socket_t socks[2] = { TOR_INVALID_SOCKET, TOR_INVALID_SOCKET };
  147. #ifdef HAVE_EVENTFD
  148. /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
  149. * associated with a single file descriptor. */
  150. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  151. if (!(flags & ASOCKS_NOEVENTFD2))
  152. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  153. #endif
  154. if (socks[0] < 0 && !(flags & ASOCKS_NOEVENTFD)) {
  155. socks[0] = eventfd(0,0);
  156. if (socks[0] >= 0) {
  157. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  158. set_socket_nonblocking(socks[0]) < 0) {
  159. close(socks[0]);
  160. return -1;
  161. }
  162. }
  163. }
  164. if (socks[0] >= 0) {
  165. socks_out->read_fd = socks_out->write_fd = socks[0];
  166. socks_out->alert_fn = eventfd_alert;
  167. socks_out->drain_fn = eventfd_drain;
  168. return 0;
  169. }
  170. #endif
  171. #ifdef HAVE_PIPE2
  172. /* Now we're going to try pipes. First type the pipe2() syscall, if we
  173. * have it, so we can save some calls... */
  174. if (!(flags & ASOCKS_NOPIPE2) &&
  175. pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  176. socks_out->read_fd = socks[0];
  177. socks_out->write_fd = socks[1];
  178. socks_out->alert_fn = pipe_alert;
  179. socks_out->drain_fn = pipe_drain;
  180. return 0;
  181. }
  182. #endif
  183. #ifdef HAVE_PIPE
  184. /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
  185. * socketpairs, fwict. */
  186. if (!(flags & ASOCKS_NOPIPE) &&
  187. pipe(socks) == 0) {
  188. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  189. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  190. set_socket_nonblocking(socks[0]) < 0 ||
  191. set_socket_nonblocking(socks[1]) < 0) {
  192. close(socks[0]);
  193. close(socks[1]);
  194. return -1;
  195. }
  196. socks_out->read_fd = socks[0];
  197. socks_out->write_fd = socks[1];
  198. socks_out->alert_fn = pipe_alert;
  199. socks_out->drain_fn = pipe_drain;
  200. return 0;
  201. }
  202. #endif
  203. /* If nothing else worked, fall back on socketpair(). */
  204. if (!(flags & ASOCKS_NOSOCKETPAIR) &&
  205. tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  206. if (set_socket_nonblocking(socks[0]) < 0 ||
  207. set_socket_nonblocking(socks[1])) {
  208. tor_close_socket(socks[0]);
  209. tor_close_socket(socks[1]);
  210. return -1;
  211. }
  212. socks_out->read_fd = socks[0];
  213. socks_out->write_fd = socks[1];
  214. socks_out->alert_fn = sock_alert;
  215. socks_out->drain_fn = sock_drain;
  216. return 0;
  217. }
  218. return -1;
  219. }
  220. /** Close the sockets in <b>socks</b>. */
  221. void
  222. alert_sockets_close(alert_sockets_t *socks)
  223. {
  224. if (socks->alert_fn == sock_alert) {
  225. /* they are sockets. */
  226. tor_close_socket(socks->read_fd);
  227. tor_close_socket(socks->write_fd);
  228. } else {
  229. close(socks->read_fd);
  230. if (socks->write_fd != socks->read_fd)
  231. close(socks->write_fd);
  232. }
  233. socks->read_fd = socks->write_fd = -1;
  234. }