pipe-windows.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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, HANDLE stdout_handle, HANDLE stderr_handle,
  39. HANDLE *process)
  40. {
  41. PROCESS_INFORMATION pi = {0};
  42. wchar_t *cmd_line_w = NULL;
  43. STARTUPINFOW si = {0};
  44. bool success = false;
  45. si.cb = sizeof(si);
  46. si.dwFlags = STARTF_USESTDHANDLES | STARTF_FORCEOFFFEEDBACK;
  47. si.hStdInput = stdin_handle;
  48. si.hStdOutput = stdout_handle;
  49. si.hStdError = stderr_handle;
  50. DWORD flags = 0;
  51. #ifndef SHOW_SUBPROCESSES
  52. flags = CREATE_NO_WINDOW;
  53. #endif
  54. os_utf8_to_wcs_ptr(cmd_line, 0, &cmd_line_w);
  55. if (cmd_line_w) {
  56. success = !!CreateProcessW(NULL, cmd_line_w, NULL, NULL, true, flags, NULL, NULL, &si, &pi);
  57. if (success) {
  58. *process = pi.hProcess;
  59. CloseHandle(pi.hThread);
  60. } else {
  61. // Not logging the full command line is intentional
  62. // as it may contain stream keys etc.
  63. blog(LOG_ERROR, "CreateProcessW failed: %lu", GetLastError());
  64. }
  65. bfree(cmd_line_w);
  66. }
  67. return success;
  68. }
  69. os_process_pipe_t *os_process_pipe_create(const char *cmd_line, const char *type)
  70. {
  71. os_process_pipe_t *pp = NULL;
  72. bool read_pipe;
  73. HANDLE process;
  74. HANDLE output;
  75. HANDLE err_input, err_output;
  76. HANDLE input;
  77. bool success;
  78. if (!cmd_line || !type) {
  79. return NULL;
  80. }
  81. if (*type != 'r' && *type != 'w') {
  82. return NULL;
  83. }
  84. if (!create_pipe(&input, &output)) {
  85. return NULL;
  86. }
  87. if (!create_pipe(&err_input, &err_output)) {
  88. return NULL;
  89. }
  90. read_pipe = *type == 'r';
  91. success = !!SetHandleInformation(read_pipe ? input : output, HANDLE_FLAG_INHERIT, false);
  92. if (!success) {
  93. goto error;
  94. }
  95. success = !!SetHandleInformation(err_input, HANDLE_FLAG_INHERIT, false);
  96. if (!success) {
  97. goto error;
  98. }
  99. success = create_process(cmd_line, read_pipe ? NULL : input, read_pipe ? output : NULL, err_output, &process);
  100. if (!success) {
  101. goto error;
  102. }
  103. pp = bmalloc(sizeof(*pp));
  104. pp->handle = read_pipe ? input : output;
  105. pp->read_pipe = read_pipe;
  106. pp->process = process;
  107. pp->handle_err = err_input;
  108. CloseHandle(read_pipe ? output : input);
  109. CloseHandle(err_output);
  110. return pp;
  111. error:
  112. CloseHandle(output);
  113. CloseHandle(input);
  114. return NULL;
  115. }
  116. static inline void add_backslashes(struct dstr *str, size_t count)
  117. {
  118. while (count--)
  119. dstr_cat_ch(str, '\\');
  120. }
  121. os_process_pipe_t *os_process_pipe_create2(const os_process_args_t *args, const char *type)
  122. {
  123. struct dstr cmd_line = {0};
  124. /* Convert list to command line as Windows does not have any API that
  125. * allows us to just pass argc/argv. */
  126. char **argv = os_process_args_get_argv(args);
  127. /* Based on Python subprocess module implementation. */
  128. while (*argv) {
  129. size_t bs_count = 0;
  130. const char *arg = *argv;
  131. bool needs_quotes = strlen(arg) == 0 || strstr(arg, " ") != NULL || strstr(arg, "\t") != NULL;
  132. if (cmd_line.len)
  133. dstr_cat_ch(&cmd_line, ' ');
  134. if (needs_quotes)
  135. dstr_cat_ch(&cmd_line, '"');
  136. while (*arg) {
  137. if (*arg == '\\') {
  138. bs_count++;
  139. } else if (*arg == '"') {
  140. add_backslashes(&cmd_line, bs_count * 2);
  141. dstr_cat(&cmd_line, "\\\"");
  142. bs_count = 0;
  143. } else {
  144. if (bs_count) {
  145. add_backslashes(&cmd_line, bs_count);
  146. bs_count = 0;
  147. }
  148. dstr_cat_ch(&cmd_line, *arg);
  149. }
  150. arg++;
  151. }
  152. if (bs_count)
  153. add_backslashes(&cmd_line, bs_count);
  154. if (needs_quotes) {
  155. add_backslashes(&cmd_line, bs_count);
  156. dstr_cat_ch(&cmd_line, '"');
  157. }
  158. argv++;
  159. }
  160. os_process_pipe_t *ret = os_process_pipe_create(cmd_line.array, type);
  161. dstr_free(&cmd_line);
  162. return ret;
  163. }
  164. int os_process_pipe_destroy(os_process_pipe_t *pp)
  165. {
  166. int ret = 0;
  167. if (pp) {
  168. DWORD code;
  169. CloseHandle(pp->handle);
  170. CloseHandle(pp->handle_err);
  171. WaitForSingleObject(pp->process, INFINITE);
  172. if (GetExitCodeProcess(pp->process, &code))
  173. ret = (int)code;
  174. CloseHandle(pp->process);
  175. bfree(pp);
  176. }
  177. return ret;
  178. }
  179. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  180. {
  181. DWORD bytes_read;
  182. bool success;
  183. if (!pp) {
  184. return 0;
  185. }
  186. if (!pp->read_pipe) {
  187. return 0;
  188. }
  189. success = !!ReadFile(pp->handle, data, (DWORD)len, &bytes_read, NULL);
  190. if (success && bytes_read) {
  191. return bytes_read;
  192. }
  193. return 0;
  194. }
  195. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
  196. {
  197. DWORD bytes_read;
  198. bool success;
  199. if (!pp || !pp->handle_err) {
  200. return 0;
  201. }
  202. success = !!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
  203. if (success && bytes_read) {
  204. return bytes_read;
  205. } else
  206. bytes_read = GetLastError();
  207. return 0;
  208. }
  209. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data, size_t len)
  210. {
  211. DWORD bytes_written;
  212. bool success;
  213. if (!pp) {
  214. return 0;
  215. }
  216. if (pp->read_pipe) {
  217. return 0;
  218. }
  219. success = !!WriteFile(pp->handle, data, (DWORD)len, &bytes_written, NULL);
  220. if (success && bytes_written) {
  221. return bytes_written;
  222. }
  223. return 0;
  224. }