process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file process.c
  7. * \brief Module for working with other processes.
  8. **/
  9. #define PROCESS_PRIVATE
  10. #include "lib/container/buffers.h"
  11. #include "lib/net/buffers_net.h"
  12. #include "lib/container/smartlist.h"
  13. #include "lib/log/log.h"
  14. #include "lib/log/util_bug.h"
  15. #include "lib/process/process.h"
  16. #include "lib/process/process_unix.h"
  17. #include "lib/process/process_win32.h"
  18. #include "lib/process/env.h"
  19. #ifdef HAVE_STDDEF_H
  20. #include <stddef.h>
  21. #endif
  22. /** A list of all <b>process_t</b> instances currently allocated. */
  23. static smartlist_t *processes;
  24. /**
  25. * Boolean. If true, then Tor may call execve or CreateProcess via
  26. * tor_spawn_background.
  27. **/
  28. static int may_spawn_background_process = 1;
  29. /** Structure to represent a child process. */
  30. struct process_t {
  31. /** Process status. */
  32. process_status_t status;
  33. /** Which protocol is the process using? */
  34. process_protocol_t protocol;
  35. /** Which function to call when we have data ready from stdout? */
  36. process_read_callback_t stdout_read_callback;
  37. /** Which function to call when we have data ready from stderr? */
  38. process_read_callback_t stderr_read_callback;
  39. /** Which function call when our process terminated? */
  40. process_exit_callback_t exit_callback;
  41. /** Our exit code when the process have terminated. */
  42. process_exit_code_t exit_code;
  43. /** Name of the command we want to execute (for example: /bin/ls). */
  44. char *command;
  45. /** The arguments used for the new process. The format here is one argument
  46. * per element of the smartlist_t. On Windows these arguments are combined
  47. * together using the <b>tor_join_win_cmdline</b> function. On Unix the
  48. * process name (argv[0]) and the trailing NULL is added automatically before
  49. * the process is executed. */
  50. smartlist_t *arguments;
  51. /** The environment used for the new process. */
  52. smartlist_t *environment;
  53. /** Buffer to store data from stdout when it is read. */
  54. buf_t *stdout_buffer;
  55. /** Buffer to store data from stderr when it is read. */
  56. buf_t *stderr_buffer;
  57. /** Buffer to store data to stdin before it is written. */
  58. buf_t *stdin_buffer;
  59. /** Do we need to store some custom data with the process? */
  60. void *data;
  61. #ifndef _WIN32
  62. /** Our Unix process handle. */
  63. process_unix_t *unix_process;
  64. #else
  65. /** Our Win32 process handle. */
  66. process_win32_t *win32_process;
  67. #endif
  68. };
  69. /** Convert a given process status in <b>status</b> to its string
  70. * representation. */
  71. const char *
  72. process_status_to_string(process_status_t status)
  73. {
  74. switch (status) {
  75. case PROCESS_STATUS_NOT_RUNNING:
  76. return "not running";
  77. case PROCESS_STATUS_RUNNING:
  78. return "running";
  79. case PROCESS_STATUS_ERROR:
  80. return "error";
  81. }
  82. /* LCOV_EXCL_START */
  83. tor_assert_unreached();
  84. return NULL;
  85. /* LCOV_EXCL_STOP */
  86. }
  87. /** Convert a given process protocol in <b>protocol</b> to its string
  88. * representation. */
  89. const char *
  90. process_protocol_to_string(process_protocol_t protocol)
  91. {
  92. switch (protocol) {
  93. case PROCESS_PROTOCOL_LINE:
  94. return "Line";
  95. case PROCESS_PROTOCOL_RAW:
  96. return "Raw";
  97. }
  98. /* LCOV_EXCL_START */
  99. tor_assert_unreached();
  100. return NULL;
  101. /* LCOV_EXCL_STOP */
  102. }
  103. /**
  104. * Turn off may_spawn_background_process, so that all future calls to
  105. * tor_spawn_background are guaranteed to fail.
  106. **/
  107. void
  108. tor_disable_spawning_background_processes(void)
  109. {
  110. may_spawn_background_process = 0;
  111. }
  112. /** Initialize the Process subsystem. This function initializes the Process
  113. * subsystem's global state. For cleaning up, <b>process_free_all()</b> should
  114. * be called. */
  115. void
  116. process_init(void)
  117. {
  118. processes = smartlist_new();
  119. #ifdef _WIN32
  120. process_win32_init();
  121. #endif
  122. }
  123. /** Free up all resources that is handled by the Process subsystem. Note that
  124. * this call does not terminate already running processes. */
  125. void
  126. process_free_all(void)
  127. {
  128. #ifdef _WIN32
  129. process_win32_deinit();
  130. #endif
  131. SMARTLIST_FOREACH(processes, process_t *, x, process_free(x));
  132. smartlist_free(processes);
  133. }
  134. /** Get a list of all processes. This function returns a smartlist of
  135. * <b>process_t</b> containing all the currently allocated processes. */
  136. const smartlist_t *
  137. process_get_all_processes(void)
  138. {
  139. return processes;
  140. }
  141. /** Allocate and initialize a new process. This function returns a newly
  142. * allocated and initialized process data, which can be used to configure and
  143. * later run a subprocess of Tor. Use the various <b>process_set_*()</b>
  144. * methods to configure it and run the process using <b>process_exec()</b>. Use
  145. * <b>command</b> to specify the path to the command to run. You can either
  146. * specify an absolute path to the command or relative where Tor will use the
  147. * underlying operating system's functionality for finding the command to run.
  148. * */
  149. process_t *
  150. process_new(const char *command)
  151. {
  152. tor_assert(command);
  153. process_t *process;
  154. process = tor_malloc_zero(sizeof(process_t));
  155. /* Set our command. */
  156. process->command = tor_strdup(command);
  157. /* By default we are not running. */
  158. process->status = PROCESS_STATUS_NOT_RUNNING;
  159. /* Prepare process environment. */
  160. process->arguments = smartlist_new();
  161. process->environment = smartlist_new();
  162. /* Prepare the buffers. */
  163. process->stdout_buffer = buf_new();
  164. process->stderr_buffer = buf_new();
  165. process->stdin_buffer = buf_new();
  166. #ifndef _WIN32
  167. /* Prepare our Unix process handle. */
  168. process->unix_process = process_unix_new();
  169. #else
  170. /* Prepare our Win32 process handle. */
  171. process->win32_process = process_win32_new();
  172. #endif
  173. smartlist_add(processes, process);
  174. return process;
  175. }
  176. /** Deallocate the given process in <b>process</b>. */
  177. void
  178. process_free_(process_t *process)
  179. {
  180. if (! process)
  181. return;
  182. /* Cleanup parameters. */
  183. tor_free(process->command);
  184. /* Cleanup arguments and environment. */
  185. SMARTLIST_FOREACH(process->arguments, char *, x, tor_free(x));
  186. smartlist_free(process->arguments);
  187. SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
  188. smartlist_free(process->environment);
  189. /* Cleanup the buffers. */
  190. buf_free(process->stdout_buffer);
  191. buf_free(process->stderr_buffer);
  192. buf_free(process->stdin_buffer);
  193. #ifndef _WIN32
  194. /* Cleanup our Unix process handle. */
  195. process_unix_free(process->unix_process);
  196. #else
  197. /* Cleanup our Win32 process handle. */
  198. process_win32_free(process->win32_process);
  199. #endif
  200. smartlist_remove(processes, process);
  201. tor_free(process);
  202. }
  203. /** Execute the given process. This function executes the given process as a
  204. * subprocess of Tor. Returns <b>PROCESS_STATUS_RUNNING</b> upon success. */
  205. process_status_t
  206. process_exec(process_t *process)
  207. {
  208. tor_assert(process);
  209. if (BUG(may_spawn_background_process == 0))
  210. return PROCESS_STATUS_ERROR;
  211. process_status_t status = PROCESS_STATUS_NOT_RUNNING;
  212. log_info(LD_PROCESS, "Starting new process: %s", process->command);
  213. #ifndef _WIN32
  214. status = process_unix_exec(process);
  215. #else
  216. status = process_win32_exec(process);
  217. #endif
  218. /* Update our state. */
  219. process_set_status(process, status);
  220. if (status != PROCESS_STATUS_RUNNING) {
  221. log_warn(LD_PROCESS, "Failed to start process: %s",
  222. process_get_command(process));
  223. }
  224. return status;
  225. }
  226. /** Terminate the given process. Returns true on success,
  227. * otherwise false. */
  228. bool
  229. process_terminate(process_t *process)
  230. {
  231. tor_assert(process);
  232. /* Terminating a non-running process isn't going to work. */
  233. if (process_get_status(process) != PROCESS_STATUS_RUNNING)
  234. return false;
  235. log_debug(LD_PROCESS, "Terminating process");
  236. #ifndef _WIN32
  237. return process_unix_terminate(process);
  238. #else
  239. return process_win32_terminate(process);
  240. #endif
  241. }
  242. /** Returns the unique process identifier for the given <b>process</b>. */
  243. process_pid_t
  244. process_get_pid(process_t *process)
  245. {
  246. tor_assert(process);
  247. #ifndef _WIN32
  248. return process_unix_get_pid(process);
  249. #else
  250. return process_win32_get_pid(process);
  251. #endif
  252. }
  253. /** Set the callback function for output from the child process's standard out
  254. * handle. This function sets the callback function which is called every time
  255. * the child process have written output to its standard out file handle.
  256. *
  257. * Use <b>process_set_protocol(process, PROCESS_PROTOCOL_LINE)</b> if you want
  258. * the callback to only contain complete "\n" or "\r\n" terminated lines. */
  259. void
  260. process_set_stdout_read_callback(process_t *process,
  261. process_read_callback_t callback)
  262. {
  263. tor_assert(process);
  264. process->stdout_read_callback = callback;
  265. }
  266. /** Set the callback function for output from the child process's standard
  267. * error handle. This function sets the callback function which is called
  268. * every time the child process have written output to its standard error file
  269. * handle.
  270. *
  271. * Use <b>process_set_protocol(process, PROCESS_PROTOCOL_LINE)</b> if you want
  272. * the callback to only contain complete "\n" or "\r\n" terminated lines. */
  273. void
  274. process_set_stderr_read_callback(process_t *process,
  275. process_read_callback_t callback)
  276. {
  277. tor_assert(process);
  278. process->stderr_read_callback = callback;
  279. }
  280. /** Set the callback function for process exit notification. The
  281. * <b>callback</b> function will be called every time your child process have
  282. * terminated. */
  283. void
  284. process_set_exit_callback(process_t *process,
  285. process_exit_callback_t callback)
  286. {
  287. tor_assert(process);
  288. process->exit_callback = callback;
  289. }
  290. /** Get the current command of the given process. */
  291. const char *
  292. process_get_command(const process_t *process)
  293. {
  294. tor_assert(process);
  295. return process->command;
  296. }
  297. void
  298. process_set_protocol(process_t *process, process_protocol_t protocol)
  299. {
  300. tor_assert(process);
  301. process->protocol = protocol;
  302. }
  303. /** Get the currently used protocol of the given process. */
  304. process_protocol_t
  305. process_get_protocol(const process_t *process)
  306. {
  307. tor_assert(process);
  308. return process->protocol;
  309. }
  310. /** Set opague pointer to data. This function allows you to store a pointer to
  311. * your own data in the given process. Use <b>process_get_data()</b> in the
  312. * various callback functions to retrieve the data again.
  313. *
  314. * Note that the given process does NOT take ownership of the data and you are
  315. * responsible for freeing up any resources allocated by the given data.
  316. * */
  317. void
  318. process_set_data(process_t *process, void *data)
  319. {
  320. tor_assert(process);
  321. process->data = data;
  322. }
  323. /** Get the opaque pointer to callback data from the given process. This
  324. * function allows you get the data you stored with <b>process_set_data()</b>
  325. * in the different callback functions. */
  326. void *
  327. process_get_data(const process_t *process)
  328. {
  329. tor_assert(process);
  330. return process->data;
  331. }
  332. /** Set the status of a given process. */
  333. void
  334. process_set_status(process_t *process, process_status_t status)
  335. {
  336. tor_assert(process);
  337. process->status = status;
  338. }
  339. /** Get the status of the given process. */
  340. process_status_t
  341. process_get_status(const process_t *process)
  342. {
  343. tor_assert(process);
  344. return process->status;
  345. }
  346. /** Append an argument to the list of arguments in the given process. */
  347. void
  348. process_append_argument(process_t *process, const char *argument)
  349. {
  350. tor_assert(process);
  351. tor_assert(argument);
  352. smartlist_add(process->arguments, tor_strdup(argument));
  353. }
  354. /** Returns a list of arguments (excluding the command itself) from the
  355. * given process. */
  356. const smartlist_t *
  357. process_get_arguments(const process_t *process)
  358. {
  359. tor_assert(process);
  360. return process->arguments;
  361. }
  362. /** Returns a newly allocated Unix style argument vector. Use <b>tor_free()</b>
  363. * to deallocate it after use. */
  364. char **
  365. process_get_argv(const process_t *process)
  366. {
  367. tor_assert(process);
  368. /** Generate a Unix style process argument vector from our process's
  369. * arguments smartlist_t. */
  370. char **argv = NULL;
  371. char *filename = process->command;
  372. const smartlist_t *arguments = process->arguments;
  373. const size_t size = smartlist_len(arguments);
  374. /* Make space for the process filename as argv[0] and a trailing NULL. */
  375. argv = tor_malloc_zero(sizeof(char *) * (size + 2));
  376. /* Set our filename as first argument. */
  377. argv[0] = filename;
  378. /* Put in the rest of the values from arguments. */
  379. SMARTLIST_FOREACH_BEGIN(arguments, char *, arg_val) {
  380. tor_assert(arg_val != NULL);
  381. argv[arg_val_sl_idx + 1] = arg_val;
  382. } SMARTLIST_FOREACH_END(arg_val);
  383. return argv;
  384. }
  385. /** This function clears the internal environment and copies over every string
  386. * from <b>env</b> as the new environment. */
  387. void
  388. process_reset_environment(process_t *process, const smartlist_t *env)
  389. {
  390. tor_assert(process);
  391. tor_assert(env);
  392. /* Cleanup old environment. */
  393. SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
  394. smartlist_free(process->environment);
  395. process->environment = smartlist_new();
  396. SMARTLIST_FOREACH(env, char *, x,
  397. smartlist_add(process->environment, tor_strdup(x)));
  398. }
  399. /** Set the given <b>key</b>/<b>value</b> pair as environment variable in the
  400. * given process. */
  401. void
  402. process_set_environment(process_t *process,
  403. const char *key,
  404. const char *value)
  405. {
  406. tor_assert(process);
  407. tor_assert(key);
  408. tor_assert(value);
  409. smartlist_add_asprintf(process->environment, "%s=%s", key, value);
  410. }
  411. /** Returns a newly allocated <b>process_environment_t</b> containing the
  412. * environment variables for the given process. */
  413. process_environment_t *
  414. process_get_environment(const process_t *process)
  415. {
  416. tor_assert(process);
  417. return process_environment_make(process->environment);
  418. }
  419. #ifndef _WIN32
  420. /** Get the internal handle for the Unix backend. */
  421. process_unix_t *
  422. process_get_unix_process(const process_t *process)
  423. {
  424. tor_assert(process);
  425. tor_assert(process->unix_process);
  426. return process->unix_process;
  427. }
  428. #else
  429. /** Get the internal handle for Windows backend. */
  430. process_win32_t *
  431. process_get_win32_process(const process_t *process)
  432. {
  433. tor_assert(process);
  434. tor_assert(process->win32_process);
  435. return process->win32_process;
  436. }
  437. #endif
  438. /** Write <b>size</b> bytes of <b>data</b> to the given process's standard
  439. * input. */
  440. void
  441. process_write(process_t *process,
  442. const uint8_t *data, size_t size)
  443. {
  444. tor_assert(process);
  445. tor_assert(data);
  446. buf_add(process->stdin_buffer, (char *)data, size);
  447. process_write_stdin(process, process->stdin_buffer);
  448. }
  449. /** As tor_vsnprintf(), but write the data to the given process's standard
  450. * input. */
  451. void
  452. process_vprintf(process_t *process,
  453. const char *format, va_list args)
  454. {
  455. tor_assert(process);
  456. tor_assert(format);
  457. int size;
  458. char *data;
  459. size = tor_vasprintf(&data, format, args);
  460. process_write(process, (uint8_t *)data, size);
  461. tor_free(data);
  462. }
  463. /** As tor_snprintf(), but write the data to the given process's standard
  464. * input. */
  465. void
  466. process_printf(process_t *process,
  467. const char *format, ...)
  468. {
  469. tor_assert(process);
  470. tor_assert(format);
  471. va_list ap;
  472. va_start(ap, format);
  473. process_vprintf(process, format, ap);
  474. va_end(ap);
  475. }
  476. /** This function is called by the Process backend when a given process have
  477. * data that is ready to be read from the child process's standard output
  478. * handle. */
  479. void
  480. process_notify_event_stdout(process_t *process)
  481. {
  482. tor_assert(process);
  483. int ret;
  484. ret = process_read_stdout(process, process->stdout_buffer);
  485. if (ret > 0)
  486. process_read_data(process,
  487. process->stdout_buffer,
  488. process->stdout_read_callback);
  489. }
  490. /** This function is called by the Process backend when a given process have
  491. * data that is ready to be read from the child process's standard error
  492. * handle. */
  493. void
  494. process_notify_event_stderr(process_t *process)
  495. {
  496. tor_assert(process);
  497. int ret;
  498. ret = process_read_stderr(process, process->stderr_buffer);
  499. if (ret > 0)
  500. process_read_data(process,
  501. process->stderr_buffer,
  502. process->stderr_read_callback);
  503. }
  504. /** This function is called by the Process backend when a given process is
  505. * allowed to begin writing data to the standard input of the child process. */
  506. void
  507. process_notify_event_stdin(process_t *process)
  508. {
  509. tor_assert(process);
  510. process_write_stdin(process, process->stdin_buffer);
  511. }
  512. /** This function is called by the Process backend when a given process have
  513. * terminated. The exit status code is passed in <b>exit_code</b>. We mark the
  514. * process as no longer running and calls the <b>exit_callback</b> with
  515. * information about the process termination. The given <b>process</b> is
  516. * free'd iff the exit_callback returns true. */
  517. void
  518. process_notify_event_exit(process_t *process, process_exit_code_t exit_code)
  519. {
  520. tor_assert(process);
  521. log_debug(LD_PROCESS,
  522. "Process terminated with exit code: %"PRIu64, exit_code);
  523. /* Update our state. */
  524. process_set_status(process, PROCESS_STATUS_NOT_RUNNING);
  525. process->exit_code = exit_code;
  526. /* Call our exit callback, if it exists. */
  527. bool free_process_handle = false;
  528. /* The exit callback will tell us if we should process_free() our handle. */
  529. if (process->exit_callback)
  530. free_process_handle = process->exit_callback(process, exit_code);
  531. if (free_process_handle)
  532. process_free(process);
  533. }
  534. /** This function is called whenever the Process backend have notified us that
  535. * there is data to be read from its standard out handle. Returns the number of
  536. * bytes that have been put into the given buffer. */
  537. MOCK_IMPL(STATIC int, process_read_stdout, (process_t *process, buf_t *buffer))
  538. {
  539. tor_assert(process);
  540. tor_assert(buffer);
  541. #ifndef _WIN32
  542. return process_unix_read_stdout(process, buffer);
  543. #else
  544. return process_win32_read_stdout(process, buffer);
  545. #endif
  546. }
  547. /** This function is called whenever the Process backend have notified us that
  548. * there is data to be read from its standard error handle. Returns the number
  549. * of bytes that have been put into the given buffer. */
  550. MOCK_IMPL(STATIC int, process_read_stderr, (process_t *process, buf_t *buffer))
  551. {
  552. tor_assert(process);
  553. tor_assert(buffer);
  554. #ifndef _WIN32
  555. return process_unix_read_stderr(process, buffer);
  556. #else
  557. return process_win32_read_stderr(process, buffer);
  558. #endif
  559. }
  560. /** This function calls the backend function for the given process whenever
  561. * there is data to be written to the backends' file handles. */
  562. MOCK_IMPL(STATIC void, process_write_stdin,
  563. (process_t *process, buf_t *buffer))
  564. {
  565. tor_assert(process);
  566. tor_assert(buffer);
  567. #ifndef _WIN32
  568. process_unix_write(process, buffer);
  569. #else
  570. process_win32_write(process, buffer);
  571. #endif
  572. }
  573. /** This function calls the protocol handlers based on the value of
  574. * <b>process_get_protocol(process)</b>. Currently we call
  575. * <b>process_read_buffer()</b> for <b>PROCESS_PROTOCOL_RAW</b> and
  576. * <b>process_read_lines()</b> for <b>PROCESS_PROTOCOL_LINE</b>. */
  577. STATIC void
  578. process_read_data(process_t *process,
  579. buf_t *buffer,
  580. process_read_callback_t callback)
  581. {
  582. tor_assert(process);
  583. tor_assert(buffer);
  584. switch (process_get_protocol(process)) {
  585. case PROCESS_PROTOCOL_RAW:
  586. process_read_buffer(process, buffer, callback);
  587. break;
  588. case PROCESS_PROTOCOL_LINE:
  589. process_read_lines(process, buffer, callback);
  590. break;
  591. default:
  592. /* LCOV_EXCL_START */
  593. tor_assert_unreached();
  594. return;
  595. /* LCOV_EXCL_STOP */
  596. }
  597. }
  598. /** This function takes the content of the given <b>buffer</b> and passes it to
  599. * the given <b>callback</b> function, but ensures that an additional zero byte
  600. * is added to the end of the data such that the given callback implementation
  601. * can threat the content as a ASCIIZ string. */
  602. STATIC void
  603. process_read_buffer(process_t *process,
  604. buf_t *buffer,
  605. process_read_callback_t callback)
  606. {
  607. tor_assert(process);
  608. tor_assert(buffer);
  609. const size_t size = buf_datalen(buffer);
  610. /* We allocate an extra byte for the zero byte in the end. */
  611. char *data = tor_malloc_zero(size + 1);
  612. buf_get_bytes(buffer, data, size);
  613. log_debug(LD_PROCESS, "Read data from process");
  614. if (callback)
  615. callback(process, data, size);
  616. tor_free(data);
  617. }
  618. /** This function tries to extract complete lines from the given <b>buffer</b>
  619. * and calls the given <b>callback</b> function whenever it has a complete
  620. * line. Before calling <b>callback</b> we remove the trailing "\n" or "\r\n"
  621. * from the line. If we are unable to extract a complete line we leave the data
  622. * in the buffer for next call. */
  623. STATIC void
  624. process_read_lines(process_t *process,
  625. buf_t *buffer,
  626. process_read_callback_t callback)
  627. {
  628. tor_assert(process);
  629. tor_assert(buffer);
  630. const size_t size = buf_datalen(buffer) + 1;
  631. size_t line_size = 0;
  632. char *data = tor_malloc_zero(size);
  633. int ret;
  634. while (true) {
  635. line_size = size;
  636. ret = buf_get_line(buffer, data, &line_size);
  637. /* A complete line should always be smaller than the size of our
  638. * buffer. */
  639. tor_assert(ret != -1);
  640. /* Remove \n from the end of the line. */
  641. if (line_size >= 1 && data[line_size - 1] == '\n') {
  642. data[line_size - 1] = '\0';
  643. --line_size;
  644. }
  645. /* Remove \r from the end of the line. */
  646. if (line_size >= 1 && data[line_size - 1] == '\r') {
  647. data[line_size - 1] = '\0';
  648. --line_size;
  649. }
  650. if (ret == 1) {
  651. log_debug(LD_PROCESS, "Read line from process: \"%s\"", data);
  652. if (callback)
  653. callback(process, data, line_size);
  654. /* We have read a whole line, let's see if there is more lines to read.
  655. * */
  656. continue;
  657. }
  658. /* No complete line for us to read. We are done for now. */
  659. tor_assert_nonfatal(ret == 0);
  660. break;
  661. }
  662. tor_free(data);
  663. }