pipe-windows.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define WIN32_LEAN_AND_MEAN
  17. #include <windows.h>
  18. #include "platform.h"
  19. #include "bmem.h"
  20. #include "dstr.h"
  21. #include "pipe.h"
  22. struct os_process_pipe {
  23. bool read_pipe;
  24. HANDLE handle;
  25. HANDLE handle_err;
  26. HANDLE process;
  27. };
  28. static bool create_pipe(HANDLE *input, HANDLE *output)
  29. {
  30. SECURITY_ATTRIBUTES sa = {0};
  31. sa.nLength = sizeof(sa);
  32. sa.bInheritHandle = true;
  33. if (!CreatePipe(input, output, &sa, 0)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
  39. HANDLE stdout_handle, HANDLE stderr_handle,
  40. HANDLE *process)
  41. {
  42. PROCESS_INFORMATION pi = {0};
  43. wchar_t *cmd_line_w = NULL;
  44. STARTUPINFOW si = {0};
  45. bool success = false;
  46. si.cb = sizeof(si);
  47. si.dwFlags = STARTF_USESTDHANDLES | STARTF_FORCEOFFFEEDBACK;
  48. si.hStdInput = stdin_handle;
  49. si.hStdOutput = stdout_handle;
  50. si.hStdError = stderr_handle;
  51. DWORD flags = 0;
  52. #ifndef SHOW_SUBPROCESSES
  53. flags = CREATE_NO_WINDOW;
  54. #endif
  55. os_utf8_to_wcs_ptr(cmd_line, 0, &cmd_line_w);
  56. if (cmd_line_w) {
  57. success = !!CreateProcessW(NULL, cmd_line_w, NULL, NULL, true,
  58. flags, NULL, NULL, &si, &pi);
  59. if (success) {
  60. *process = pi.hProcess;
  61. CloseHandle(pi.hThread);
  62. } else {
  63. // Not logging the full command line is intentional
  64. // as it may contain stream keys etc.
  65. blog(LOG_ERROR, "CreateProcessW failed: %lu",
  66. GetLastError());
  67. }
  68. bfree(cmd_line_w);
  69. }
  70. return success;
  71. }
  72. os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
  73. const char *type)
  74. {
  75. os_process_pipe_t *pp = NULL;
  76. bool read_pipe;
  77. HANDLE process;
  78. HANDLE output;
  79. HANDLE err_input, err_output;
  80. HANDLE input;
  81. bool success;
  82. if (!cmd_line || !type) {
  83. return NULL;
  84. }
  85. if (*type != 'r' && *type != 'w') {
  86. return NULL;
  87. }
  88. if (!create_pipe(&input, &output)) {
  89. return NULL;
  90. }
  91. if (!create_pipe(&err_input, &err_output)) {
  92. return NULL;
  93. }
  94. read_pipe = *type == 'r';
  95. success = !!SetHandleInformation(read_pipe ? input : output,
  96. HANDLE_FLAG_INHERIT, false);
  97. if (!success) {
  98. goto error;
  99. }
  100. success = !!SetHandleInformation(err_input, HANDLE_FLAG_INHERIT, false);
  101. if (!success) {
  102. goto error;
  103. }
  104. success = create_process(cmd_line, read_pipe ? NULL : input,
  105. read_pipe ? output : NULL, err_output,
  106. &process);
  107. if (!success) {
  108. goto error;
  109. }
  110. pp = bmalloc(sizeof(*pp));
  111. pp->handle = read_pipe ? input : output;
  112. pp->read_pipe = read_pipe;
  113. pp->process = process;
  114. pp->handle_err = err_input;
  115. CloseHandle(read_pipe ? output : input);
  116. CloseHandle(err_output);
  117. return pp;
  118. error:
  119. CloseHandle(output);
  120. CloseHandle(input);
  121. return NULL;
  122. }
  123. static inline void add_backslashes(struct dstr *str, size_t count)
  124. {
  125. while (count--)
  126. dstr_cat_ch(str, '\\');
  127. }
  128. os_process_pipe_t *os_process_pipe_create2(const os_process_args_t *args,
  129. const char *type)
  130. {
  131. struct dstr cmd_line = {0};
  132. /* Convert list to command line as Windows does not have any API that
  133. * allows us to just pass argc/argv. */
  134. char **argv = os_process_args_get_argv(args);
  135. /* Based on Python subprocess module implementation. */
  136. while (*argv) {
  137. size_t bs_count = 0;
  138. const char *arg = *argv;
  139. bool needs_quotes = strlen(arg) == 0 ||
  140. strstr(arg, " ") != NULL ||
  141. strstr(arg, "\t") != NULL;
  142. if (cmd_line.len)
  143. dstr_cat_ch(&cmd_line, ' ');
  144. if (needs_quotes)
  145. dstr_cat_ch(&cmd_line, '"');
  146. while (*arg) {
  147. if (*arg == '\\') {
  148. bs_count++;
  149. } else if (*arg == '"') {
  150. add_backslashes(&cmd_line, bs_count * 2);
  151. dstr_cat(&cmd_line, "\\\"");
  152. bs_count = 0;
  153. } else {
  154. if (bs_count) {
  155. add_backslashes(&cmd_line, bs_count);
  156. bs_count = 0;
  157. }
  158. dstr_cat_ch(&cmd_line, *arg);
  159. }
  160. arg++;
  161. }
  162. if (bs_count)
  163. add_backslashes(&cmd_line, bs_count);
  164. if (needs_quotes) {
  165. add_backslashes(&cmd_line, bs_count);
  166. dstr_cat_ch(&cmd_line, '"');
  167. }
  168. argv++;
  169. }
  170. os_process_pipe_t *ret = os_process_pipe_create(cmd_line.array, type);
  171. dstr_free(&cmd_line);
  172. return ret;
  173. }
  174. int os_process_pipe_destroy(os_process_pipe_t *pp)
  175. {
  176. int ret = 0;
  177. if (pp) {
  178. DWORD code;
  179. CloseHandle(pp->handle);
  180. CloseHandle(pp->handle_err);
  181. WaitForSingleObject(pp->process, INFINITE);
  182. if (GetExitCodeProcess(pp->process, &code))
  183. ret = (int)code;
  184. CloseHandle(pp->process);
  185. bfree(pp);
  186. }
  187. return ret;
  188. }
  189. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  190. {
  191. DWORD bytes_read;
  192. bool success;
  193. if (!pp) {
  194. return 0;
  195. }
  196. if (!pp->read_pipe) {
  197. return 0;
  198. }
  199. success = !!ReadFile(pp->handle, data, (DWORD)len, &bytes_read, NULL);
  200. if (success && bytes_read) {
  201. return bytes_read;
  202. }
  203. return 0;
  204. }
  205. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
  206. size_t len)
  207. {
  208. DWORD bytes_read;
  209. bool success;
  210. if (!pp || !pp->handle_err) {
  211. return 0;
  212. }
  213. success =
  214. !!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
  215. if (success && bytes_read) {
  216. return bytes_read;
  217. } else
  218. bytes_read = GetLastError();
  219. return 0;
  220. }
  221. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
  222. size_t len)
  223. {
  224. DWORD bytes_written;
  225. bool success;
  226. if (!pp) {
  227. return 0;
  228. }
  229. if (pp->read_pipe) {
  230. return 0;
  231. }
  232. success =
  233. !!WriteFile(pp->handle, data, (DWORD)len, &bytes_written, NULL);
  234. if (success && bytes_written) {
  235. return bytes_written;
  236. }
  237. return 0;
  238. }