compat_threads.c 5.7 KB

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