subprocess.h 4.2 KB

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