hs_pirprocess.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * \file hs_pirprocess.c
  3. * \brief Handle hidden service PIR helper processes.
  4. **/
  5. #include "core/or/or.h"
  6. #include "feature/hs/hs_pirprocess.h"
  7. #define SUBPROCESS_PRIVATE
  8. #include "lib/process/env.h"
  9. #include "lib/process/subprocess.h"
  10. #include "lib/evloop/compat_libevent.h"
  11. #include <event2/event.h>
  12. #ifdef HAVE_UNISTD_H
  13. #include <unistd.h>
  14. #endif
  15. typedef enum {
  16. PIRPROCESS_READSTATE_HEADER,
  17. PIRPROCESS_READSTATE_BODY
  18. } PIRProcessReadState;
  19. struct pir_process_st {
  20. process_handle_t *process;
  21. buf_t *stdin_buf;
  22. struct event *stdin_ev;
  23. struct event *stdout_ev;
  24. struct event *stderr_ev;
  25. char *loglabel;
  26. pir_process_msghandler_t msghandler;
  27. PIRProcessReadState readstate;
  28. size_t readoff, readleft;
  29. unsigned char hdrbuf[PIRPROCESS_HDR_SIZE];
  30. char *bodybuf;
  31. };
  32. /* This is called when the pir process has output for us. */
  33. static void
  34. pirprocess_stdoutcb(evutil_socket_t fd, short what, void *arg)
  35. {
  36. pir_process_t handle = (pir_process_t)arg;
  37. if (!(what & EV_READ)) {
  38. /* Not sure why we're here */
  39. return;
  40. }
  41. if (handle->readstate == PIRPROCESS_READSTATE_HEADER) {
  42. int res = read(fd, handle->hdrbuf + handle->readoff, handle->readleft);
  43. if (res <= 0) return;
  44. handle->readoff += res;
  45. handle->readleft -= res;
  46. if (handle->readleft == 0) {
  47. handle->readleft = ntohl(*(uint32_t*)
  48. (handle->hdrbuf+PIRPROCESS_HDR_SIZE-4));
  49. tor_free(handle->bodybuf);
  50. if (handle->readleft > 0) {
  51. handle->bodybuf = tor_malloc(handle->readleft);
  52. handle->readoff = 0;
  53. handle->readstate = PIRPROCESS_READSTATE_BODY;
  54. } else {
  55. (handle->msghandler)(handle->hdrbuf, NULL, 0);
  56. handle->readoff = 0;
  57. handle->readleft = PIRPROCESS_HDR_SIZE;
  58. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  59. }
  60. }
  61. } else if (handle->readstate == PIRPROCESS_READSTATE_BODY) {
  62. int res = read(fd, handle->bodybuf + handle->readoff,
  63. handle->readleft);
  64. if (res <= 0) return;
  65. handle->readoff += res;
  66. handle->readleft -= res;
  67. if (handle->readleft == 0) {
  68. /* Reading is complete */
  69. (handle->msghandler)(handle->hdrbuf, handle->bodybuf,
  70. handle->readoff);
  71. /* Prepare for the next output from the PIR server */
  72. tor_free(handle->bodybuf);
  73. handle->readoff = 0;
  74. handle->readleft = PIRPROCESS_HDR_SIZE;
  75. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  76. }
  77. }
  78. }
  79. /* This is called when the pir process is ready to read from its stdin. */
  80. static void
  81. pirprocess_stdincb(evutil_socket_t fd, short what, void *arg)
  82. {
  83. pir_process_t handle = (pir_process_t)arg;
  84. int res;
  85. size_t bufsize = buf_datalen(handle->stdin_buf);
  86. char *netbuf = NULL;
  87. if (!(what & EV_WRITE)) {
  88. /* Not sure why we're here */
  89. log_info(LD_DIRSERV,"PIRSERVER bailing");
  90. return;
  91. }
  92. if (bufsize == 0) {
  93. log_err(LD_DIRSERV,"PIRSERVER trying to write 0-length buffer");
  94. return;
  95. }
  96. netbuf = tor_malloc(bufsize);
  97. if (netbuf == NULL) {
  98. log_err(LD_DIRSERV,"PIRSERVER failed to allocate buffer");
  99. return;
  100. }
  101. /* One might think that just calling buf_flush_to_socket would be
  102. * the thing to do, but that function ends up calling sendto()
  103. * instead of write(), which doesn't work on pipes. So we do it
  104. * more manually. Using a bufferevent may be another option. */
  105. buf_peek(handle->stdin_buf, netbuf, bufsize);
  106. res = write(fd, netbuf, bufsize);
  107. tor_free(netbuf);
  108. if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  109. /* Try writing again later */
  110. event_add(handle->stdin_ev, NULL);
  111. return;
  112. }
  113. if (res <= 0) {
  114. /* Stop trying to write. */
  115. return;
  116. }
  117. buf_drain(handle->stdin_buf, res);
  118. bufsize -= res;
  119. if (bufsize > 0) {
  120. /* There's more to write */
  121. event_add(handle->stdin_ev, NULL);
  122. }
  123. }
  124. /* This is called when the pir process writes something to its stderr. */
  125. static void
  126. pirprocess_stderrcb(evutil_socket_t fd, short what, void *arg)
  127. {
  128. pir_process_t handle = (pir_process_t)arg;
  129. if (!(what & EV_READ)) {
  130. /* Not sure why we're here */
  131. return;
  132. }
  133. char buf[1000];
  134. int res = read(fd, buf, sizeof(buf)-1);
  135. if (res <= 0) return;
  136. buf[res] = '\0';
  137. log_info(LD_DIRSERV,"%s %s", handle->loglabel, escaped(buf));
  138. }
  139. void
  140. hs_pirprocess_close(pir_process_t *handlep)
  141. {
  142. pir_process_t handle;
  143. if (!handlep) return;
  144. if (!*handlep) return;
  145. handle = *handlep;
  146. if (handle->stdin_buf) {
  147. buf_free(handle->stdin_buf);
  148. }
  149. if (handle->stdin_ev) {
  150. event_free(handle->stdin_ev);
  151. }
  152. if (handle->stdout_ev) {
  153. event_free(handle->stdout_ev);
  154. }
  155. if (handle->stderr_ev) {
  156. event_free(handle->stderr_ev);
  157. }
  158. if (handle->process) {
  159. tor_process_handle_destroy(handle->process, 1);
  160. }
  161. if (handle->loglabel) {
  162. tor_free(handle->loglabel);
  163. }
  164. if (handle->bodybuf) {
  165. tor_free(handle->bodybuf);
  166. }
  167. tor_free(handle);
  168. *handlep = NULL;
  169. }
  170. void
  171. hs_pirprocess_poke(const char *path, const char *loglabel,
  172. pir_process_msghandler_t msghandler, pir_process_t *handlep)
  173. {
  174. int res;
  175. smartlist_t *env_vars = get_current_process_environment_variables();
  176. const char *argv[2];
  177. process_environment_t *env;
  178. pir_process_t handle;
  179. if (!handlep) return;
  180. if (*handlep) {
  181. handle = *handlep;
  182. if (handle->process &&
  183. handle->process->status == PROCESS_STATUS_RUNNING) {
  184. /* The PIR process appears to be open */
  185. return;
  186. }
  187. }
  188. /* Close everything we've got and start over */
  189. hs_pirprocess_close(handlep);
  190. if (!path) {
  191. /* We don't have a configured PIR server */
  192. return;
  193. }
  194. *handlep = tor_malloc_zero(sizeof(struct pir_process_st));
  195. handle = *handlep;
  196. /* Start the process */
  197. argv[0] = path;
  198. argv[1] = NULL;
  199. env = process_environment_make(env_vars);
  200. res = tor_spawn_background(path, argv, env, &(handle->process));
  201. SMARTLIST_FOREACH(env_vars, void *, x, tor_free(x));
  202. smartlist_free(env_vars);
  203. if (res != PROCESS_STATUS_RUNNING) {
  204. /* Launch failure */
  205. hs_pirprocess_close(handlep);
  206. return;
  207. }
  208. handle->loglabel = tor_strdup(loglabel);
  209. handle->msghandler = msghandler;
  210. /* Create a libevent event to listen to the PIR process' responses. */
  211. handle->stdout_ev = event_new(tor_libevent_get_base(),
  212. handle->process->stdout_pipe, EV_READ|EV_PERSIST,
  213. pirprocess_stdoutcb, handle);
  214. event_add(handle->stdout_ev, NULL);
  215. /* And one to listen to the PIR server's stderr. */
  216. handle->stderr_ev = event_new(tor_libevent_get_base(),
  217. handle->process->stderr_pipe, EV_READ|EV_PERSIST,
  218. pirprocess_stderrcb, handle);
  219. event_add(handle->stderr_ev, NULL);
  220. /* And one for writability to the pir process' stdin, but don't add
  221. * it just yet. Also create the buffer it will use. */
  222. handle->stdin_buf = buf_new();
  223. handle->stdin_ev = event_new(tor_libevent_get_base(),
  224. handle->process->stdin_pipe, EV_WRITE, pirprocess_stdincb,
  225. handle);
  226. handle->readstate = PIRPROCESS_READSTATE_HEADER;
  227. handle->readoff = 0;
  228. handle->readleft = PIRPROCESS_HDR_SIZE;
  229. handle->bodybuf = NULL;
  230. }
  231. int
  232. hs_pirprocess_send(pir_process_t handle, const unsigned char *buf,
  233. size_t len)
  234. {
  235. if (handle->process == NULL ||
  236. handle->process->status != PROCESS_STATUS_RUNNING) {
  237. /* We don't have a process to talk to */
  238. return -1;
  239. }
  240. /* Write the data to the stdin buffer */
  241. if (len > 0) {
  242. buf_add(handle->stdin_buf, (const char *)buf, len);
  243. event_add(handle->stdin_ev, NULL);
  244. }
  245. return len;
  246. }