compat_threads.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. tor_cond_t *
  39. tor_cond_new(void)
  40. {
  41. tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
  42. if (tor_cond_init(cond)<0)
  43. tor_free(cond);
  44. return cond;
  45. }
  46. void
  47. tor_cond_free(tor_cond_t *c)
  48. {
  49. if (!c)
  50. return;
  51. tor_cond_uninit(c);
  52. tor_free(c);
  53. }
  54. /** Identity of the "main" thread */
  55. static unsigned long main_thread_id = -1;
  56. /** Start considering the current thread to be the 'main thread'. This has
  57. * no effect on anything besides in_main_thread(). */
  58. void
  59. set_main_thread(void)
  60. {
  61. main_thread_id = tor_get_thread_id();
  62. }
  63. /** Return true iff called from the main thread. */
  64. int
  65. in_main_thread(void)
  66. {
  67. return main_thread_id == tor_get_thread_id();
  68. }
  69. #ifdef HAVE_EVENTFD
  70. static int
  71. eventfd_alert(int fd)
  72. {
  73. uint64_t u = 1;
  74. int r = write(fd, (void*)&u, sizeof(u));
  75. if (r < 0 && errno != EAGAIN)
  76. return -1;
  77. return 0;
  78. }
  79. static int
  80. eventfd_drain(int fd)
  81. {
  82. uint64_t u = 0;
  83. int r = read(fd, (void*)&u, sizeof(u));
  84. if (r < 0 && errno != EAGAIN)
  85. return -1;
  86. return 0;
  87. }
  88. #endif
  89. #ifdef HAVE_PIPE
  90. static int
  91. pipe_alert(int fd)
  92. {
  93. ssize_t r = write(fd, "x", 1);
  94. if (r < 0 && errno != EAGAIN)
  95. return -1;
  96. return 0;
  97. }
  98. static int
  99. pipe_drain(int fd)
  100. {
  101. char buf[32];
  102. ssize_t r;
  103. while ((r = read(fd, buf, sizeof(buf))) >= 0)
  104. ;
  105. if (r == 0 || errno != EAGAIN)
  106. return -1;
  107. return 0;
  108. }
  109. #endif
  110. static int
  111. sock_alert(tor_socket_t fd)
  112. {
  113. ssize_t r = send(fd, "x", 1, 0);
  114. if (r < 0 && !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  115. return -1;
  116. return 0;
  117. }
  118. static int
  119. sock_drain(tor_socket_t fd)
  120. {
  121. char buf[32];
  122. ssize_t r;
  123. while ((r = recv(fd, buf, sizeof(buf), 0)) >= 0)
  124. ;
  125. if (r == 0 || !ERRNO_IS_EAGAIN(tor_socket_errno(fd)))
  126. return -1;
  127. return 0;
  128. }
  129. /** Allocate a new set of alert sockets. DOCDOC */
  130. int
  131. alert_sockets_create(alert_sockets_t *socks_out)
  132. {
  133. tor_socket_t socks[2];
  134. #ifdef HAVE_EVENTFD
  135. #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  136. socks[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
  137. #else
  138. socks[0] = -1;
  139. #endif
  140. if (socks[0] < 0) {
  141. socks[0] = eventfd(0,0);
  142. if (socks[0] >= 0) {
  143. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  144. set_socket_nonblocking(socks[0]) < 0) {
  145. close(socks[0]);
  146. return -1;
  147. }
  148. }
  149. }
  150. if (socks[0] >= 0) {
  151. socks_out->read_fd = socks_out->write_fd = socks[0];
  152. socks_out->alert_fn = eventfd_alert;
  153. socks_out->drain_fn = eventfd_drain;
  154. return 0;
  155. }
  156. #endif
  157. #ifdef HAVE_PIPE2
  158. if (pipe2(socks, O_NONBLOCK|O_CLOEXEC) == 0) {
  159. socks_out->read_fd = socks[0];
  160. socks_out->write_fd = socks[1];
  161. socks_out->alert_fn = pipe_alert;
  162. socks_out->drain_fn = pipe_drain;
  163. return 0;
  164. }
  165. #endif
  166. #ifdef HAVE_PIPE
  167. if (pipe(socks) == 0) {
  168. if (fcntl(socks[0], F_SETFD, FD_CLOEXEC) < 0 ||
  169. fcntl(socks[1], F_SETFD, FD_CLOEXEC) < 0 ||
  170. set_socket_nonblocking(socks[0]) < 0 ||
  171. set_socket_nonblocking(socks[1]) < 0) {
  172. close(socks[0]);
  173. close(socks[1]);
  174. return -1;
  175. }
  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. if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == 0) {
  184. set_socket_nonblocking(socks[0]);
  185. set_socket_nonblocking(socks[1]);
  186. socks_out->alert_fn = sock_alert;
  187. socks_out->drain_fn = sock_drain;
  188. return 0;
  189. }
  190. return -1;
  191. }