subprocess.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file subprocess.h
  7. * \brief Header for subprocess.c
  8. **/
  9. #ifndef TOR_SUBPROCESS_H
  10. #define TOR_SUBPROCESS_H
  11. #include "lib/cc/torint.h"
  12. #include "lib/testsupport/testsupport.h"
  13. #include <stddef.h>
  14. #ifdef _WIN32
  15. #include <windows.h>
  16. #endif
  17. struct smartlist_t;
  18. void tor_disable_spawning_background_processes(void);
  19. typedef struct process_handle_t process_handle_t;
  20. struct process_environment_t;
  21. int tor_spawn_background(const char *const filename, const char **argv,
  22. struct process_environment_t *env,
  23. process_handle_t **process_handle_out);
  24. #define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
  25. /** Status of an I/O stream. */
  26. enum stream_status {
  27. IO_STREAM_OKAY,
  28. IO_STREAM_EAGAIN,
  29. IO_STREAM_TERM,
  30. IO_STREAM_CLOSED
  31. };
  32. const char *stream_status_to_string(enum stream_status stream_status);
  33. enum stream_status get_string_from_pipe(int fd, char *buf, size_t count);
  34. /* Values of process_handle_t.status. */
  35. #define PROCESS_STATUS_NOTRUNNING 0
  36. #define PROCESS_STATUS_RUNNING 1
  37. #define PROCESS_STATUS_ERROR -1
  38. #ifdef SUBPROCESS_PRIVATE
  39. struct waitpid_callback_t;
  40. /** Structure to represent the state of a process with which Tor is
  41. * communicating. The contents of this structure are private to util.c */
  42. struct process_handle_t {
  43. /** One of the PROCESS_STATUS_* values */
  44. int status;
  45. #ifdef _WIN32
  46. HANDLE stdin_pipe;
  47. HANDLE stdout_pipe;
  48. HANDLE stderr_pipe;
  49. PROCESS_INFORMATION pid;
  50. #else /* !(defined(_WIN32)) */
  51. int stdin_pipe;
  52. int stdout_pipe;
  53. int stderr_pipe;
  54. pid_t pid;
  55. /** If the process has not given us a SIGCHLD yet, this has the
  56. * waitpid_callback_t that gets invoked once it has. Otherwise this
  57. * contains NULL. */
  58. struct waitpid_callback_t *waitpid_cb;
  59. /** The exit status reported by waitpid. */
  60. int waitpid_exit_status;
  61. #endif /* defined(_WIN32) */
  62. };
  63. #endif /* defined(SUBPROCESS_PRIVATE) */
  64. /* Return values of tor_get_exit_code() */
  65. #define PROCESS_EXIT_RUNNING 1
  66. #define PROCESS_EXIT_EXITED 0
  67. #define PROCESS_EXIT_ERROR -1
  68. int tor_get_exit_code(process_handle_t *process_handle,
  69. int block, int *exit_code);
  70. int tor_split_lines(struct smartlist_t *sl, char *buf, int len);
  71. #ifdef _WIN32
  72. ssize_t tor_read_all_handle(HANDLE h, char *buf, size_t count,
  73. const process_handle_t *process);
  74. #else
  75. ssize_t tor_read_all_handle(int fd, char *buf, size_t count,
  76. const process_handle_t *process,
  77. int *eof);
  78. #endif /* defined(_WIN32) */
  79. ssize_t tor_read_all_from_process_stdout(
  80. const process_handle_t *process_handle, char *buf, size_t count);
  81. ssize_t tor_read_all_from_process_stderr(
  82. const process_handle_t *process_handle, char *buf, size_t count);
  83. char *tor_join_win_cmdline(const char *argv[]);
  84. int tor_process_get_pid(process_handle_t *process_handle);
  85. #ifdef _WIN32
  86. HANDLE tor_process_get_stdout_pipe(process_handle_t *process_handle);
  87. #else
  88. int tor_process_get_stdout_pipe(process_handle_t *process_handle);
  89. #endif
  90. #ifdef _WIN32
  91. MOCK_DECL(struct smartlist_t *, tor_get_lines_from_handle,(HANDLE *handle,
  92. enum stream_status *stream_status));
  93. #else
  94. MOCK_DECL(struct smartlist_t *, tor_get_lines_from_handle,(int fd,
  95. enum stream_status *stream_status));
  96. #endif /* defined(_WIN32) */
  97. int tor_terminate_process(process_handle_t *process_handle);
  98. MOCK_DECL(void, tor_process_handle_destroy,(process_handle_t *process_handle,
  99. int also_terminate_process));
  100. #ifdef SUBPROCESS_PRIVATE
  101. /* Prototypes for private functions only used by util.c (and unit tests) */
  102. #ifndef _WIN32
  103. STATIC int format_helper_exit_status(unsigned char child_state,
  104. int saved_errno, char *hex_errno);
  105. /* Space for hex values of child state, a slash, saved_errno (with
  106. leading minus) and newline (no null) */
  107. #define HEX_ERRNO_SIZE (sizeof(char) * 2 + 1 + \
  108. 1 + sizeof(int) * 2 + 1)
  109. #endif /* !defined(_WIN32) */
  110. #endif /* defined(SUBPROCESS_PRIVATE) */
  111. #endif