compat_threads.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. /** Release all storage and system resources held by <b>m</b>. */
  30. void
  31. tor_mutex_free(tor_mutex_t *m)
  32. {
  33. if (!m)
  34. return;
  35. tor_mutex_uninit(m);
  36. tor_free(m);
  37. }
  38. /** Allocate and return a new condition variable. */
  39. tor_cond_t *
  40. tor_cond_new(void)
  41. {
  42. tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
  43. if (tor_cond_init(cond)<0)
  44. tor_free(cond);
  45. return cond;
  46. }
  47. /** Free all storage held in <b>c</b>. */
  48. void
  49. tor_cond_free(tor_cond_t *c)
  50. {
  51. if (!c)
  52. return;
  53. tor_cond_uninit(c);
  54. tor_free(c);
  55. }
  56. /** Identity of the "main" thread */
  57. static unsigned long main_thread_id = -1;
  58. /** Start considering the current thread to be the 'main thread'. This has
  59. * no effect on anything besides in_main_thread(). */
  60. void
  61. set_main_thread(void)
  62. {
  63. main_thread_id = tor_get_thread_id();
  64. }
  65. /** Return true iff called from the main thread. */
  66. int
  67. in_main_thread(void)
  68. {
  69. return main_thread_id == tor_get_thread_id();
  70. }
  71. #ifdef HAVE_EVENTFD
  72. static int
  73. eventfd_alert(int fd)
  74. {
  75. uint64_t u = 1;
  76. int r = write(fd, (void*)&u, sizeof(u));
  77. if (r < 0 && errno != EAGAIN)
  78. return -1;
  79. return 0;
  80. }
  81. static int
  82. eventfd_drain(int fd)
  83. {
  84. uint64_t u = 0;
  85. int r = read(fd, (void*)&u, sizeof(u));
  86. if (r < 0 && errno != EAGAIN)
  87. return -1;
  88. return 0;
  89. }
  90. #endif
  91. #ifdef HAVE_PIPE
  92. static int
  93. pipe_alert(int fd)
  94. {
  95. ssize_t r = write(fd, "x", 1);
  96. if (r < 0 && errno != EAGAIN)
  97. return -1;
  98. return 0;
  99. }
  100. static int
  101. pipe_drain(int fd)
  102. {
  103. char buf[32];
  104. ssize_t r;
  105. while ((r = read(fd, buf, sizeof(buf))) >= 0)
  106. ;
  107. if (r == 0 || errno != EAGAIN)
  108. return -1;
  109. return 0;
  110. }
  111. #endif
  112. static int
  113. sock_alert(tor_socket_t fd)
  114. {
  115. ssize_t r = send(fd, "x", 1, 0);
  116. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  117. return -1;
  118. return 0;
  119. }
  120. static int
  121. sock_drain(tor_socket_t fd)
  122. {
  123. char buf[32];
  124. ssize_t r;
  125. while ((r = recv(fd, buf, sizeof(buf), 0)) >= 0)
  126. ;
  127. if (r == 0 || !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  128. return -1;
  129. return 0;
  130. }
  131. /** Allocate a new set of alert sockets, and set the appropriate function
  132. * pointers, in <b>socks_out</b>. */
  133. int
  134. alert_sockets_create(alert_sockets_t *socks_out)
  135. {
  136. tor_socket_t socks[2];
  137. #ifdef HAVE_EVENTFD
  138. /* First, we try the Linux eventfd() syscall. This gives a 64-bit counter
  139. * associated with a single file descriptor. */
  140. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  141. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  142. #else
  143. socks[0] = -1;
  144. #endif
  145. if (socks[0] < 0) {
  146. socks[0] = eventfd(0,0);
  147. if (socks[0] >= 0) {
  148. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  149. set_socket_nonblocking(socks[0]) < 0) {
  150. close(socks[0]);
  151. return -1;
  152. }
  153. }
  154. }
  155. if (socks[0] >= 0) {
  156. socks_out->read_fd = socks_out->write_fd = socks[0];
  157. socks_out->alert_fn = eventfd_alert;
  158. socks_out->drain_fn = eventfd_drain;
  159. return 0;
  160. }
  161. #endif
  162. #ifdef HAVE_PIPE2
  163. /* Now we're going to try pipes. First type the pipe2() syscall, if we
  164. * have it, so we can save some calls... */
  165. if (pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  166. socks_out->read_fd = socks[0];
  167. socks_out->write_fd = socks[1];
  168. socks_out->alert_fn = pipe_alert;
  169. socks_out->drain_fn = pipe_drain;
  170. return 0;
  171. }
  172. #endif
  173. #ifdef HAVE_PIPE
  174. /* Now try the regular pipe() syscall. Pipes have a bit lower overhead than
  175. * socketpairs, fwict. */
  176. if (pipe(socks) == 0) {
  177. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  178. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  179. set_socket_nonblocking(socks[0]) < 0 ||
  180. set_socket_nonblocking(socks[1]) < 0) {
  181. close(socks[0]);
  182. close(socks[1]);
  183. return -1;
  184. }
  185. socks_out->read_fd = socks[0];
  186. socks_out->write_fd = socks[1];
  187. socks_out->alert_fn = pipe_alert;
  188. socks_out->drain_fn = pipe_drain;
  189. return 0;
  190. }
  191. #endif
  192. /* If nothing else worked, fall back on socketpair(). */
  193. if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  194. if (set_socket_nonblocking(socks[0]) < 0 ||
  195. set_socket_nonblocking(socks[1])) {
  196. tor_close_socket(socks[0]);
  197. tor_close_socket(socks[1]);
  198. return -1;
  199. }
  200. socks_out->read_fd = socks[0];
  201. socks_out->write_fd = socks[1];
  202. socks_out->alert_fn = sock_alert;
  203. socks_out->drain_fn = sock_drain;
  204. return 0;
  205. }
  206. return -1;
  207. }
  208. /** Close the sockets in <b>socks</b>. */
  209. void
  210. alert_sockets_close(alert_sockets_t *socks)
  211. {
  212. if (socks->alert_fn == sock_alert) {
  213. /* they are sockets. */
  214. tor_close_socket(socks->read_fd);
  215. tor_close_socket(socks->write_fd);
  216. } else {
  217. close(socks->read_fd);
  218. if (socks->write_fd != socks->read_fd)
  219. close(socks->write_fd);
  220. }
  221. socks->read_fd = socks->write_fd = -1;
  222. }