pipe-windows.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "pipe.h"
  21. struct os_process_pipe {
  22. bool read_pipe;
  23. HANDLE handle;
  24. HANDLE handle_err;
  25. HANDLE process;
  26. };
  27. static bool create_pipe(HANDLE *input, HANDLE *output)
  28. {
  29. SECURITY_ATTRIBUTES sa = {0};
  30. sa.nLength = sizeof(sa);
  31. sa.bInheritHandle = true;
  32. if (!CreatePipe(input, output, &sa, 0)) {
  33. return false;
  34. }
  35. return true;
  36. }
  37. static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
  38. 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,
  57. flags, NULL, NULL, &si, &pi);
  58. if (success) {
  59. *process = pi.hProcess;
  60. CloseHandle(pi.hThread);
  61. } else {
  62. // Not logging the full command line is intentional
  63. // as it may contain stream keys etc.
  64. blog(LOG_ERROR, "CreateProcessW failed: %lu",
  65. GetLastError());
  66. }
  67. bfree(cmd_line_w);
  68. }
  69. return success;
  70. }
  71. os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
  72. const char *type)
  73. {
  74. os_process_pipe_t *pp = NULL;
  75. bool read_pipe;
  76. HANDLE process;
  77. HANDLE output;
  78. HANDLE err_input, err_output;
  79. HANDLE input;
  80. bool success;
  81. if (!cmd_line || !type) {
  82. return NULL;
  83. }
  84. if (*type != 'r' && *type != 'w') {
  85. return NULL;
  86. }
  87. if (!create_pipe(&input, &output)) {
  88. return NULL;
  89. }
  90. if (!create_pipe(&err_input, &err_output)) {
  91. return NULL;
  92. }
  93. read_pipe = *type == 'r';
  94. success = !!SetHandleInformation(read_pipe ? input : output,
  95. HANDLE_FLAG_INHERIT, false);
  96. if (!success) {
  97. goto error;
  98. }
  99. success = !!SetHandleInformation(err_input, HANDLE_FLAG_INHERIT, false);
  100. if (!success) {
  101. goto error;
  102. }
  103. success = create_process(cmd_line, read_pipe ? NULL : input,
  104. read_pipe ? output : NULL, err_output,
  105. &process);
  106. if (!success) {
  107. goto error;
  108. }
  109. pp = bmalloc(sizeof(*pp));
  110. pp->handle = read_pipe ? input : output;
  111. pp->read_pipe = read_pipe;
  112. pp->process = process;
  113. pp->handle_err = err_input;
  114. CloseHandle(read_pipe ? output : input);
  115. CloseHandle(err_output);
  116. return pp;
  117. error:
  118. CloseHandle(output);
  119. CloseHandle(input);
  120. return NULL;
  121. }
  122. int os_process_pipe_destroy(os_process_pipe_t *pp)
  123. {
  124. int ret = 0;
  125. if (pp) {
  126. DWORD code;
  127. CloseHandle(pp->handle);
  128. CloseHandle(pp->handle_err);
  129. WaitForSingleObject(pp->process, INFINITE);
  130. if (GetExitCodeProcess(pp->process, &code))
  131. ret = (int)code;
  132. CloseHandle(pp->process);
  133. bfree(pp);
  134. }
  135. return ret;
  136. }
  137. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  138. {
  139. DWORD bytes_read;
  140. bool success;
  141. if (!pp) {
  142. return 0;
  143. }
  144. if (!pp->read_pipe) {
  145. return 0;
  146. }
  147. success = !!ReadFile(pp->handle, data, (DWORD)len, &bytes_read, NULL);
  148. if (success && bytes_read) {
  149. return bytes_read;
  150. }
  151. return 0;
  152. }
  153. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
  154. size_t len)
  155. {
  156. DWORD bytes_read;
  157. bool success;
  158. if (!pp || !pp->handle_err) {
  159. return 0;
  160. }
  161. success =
  162. !!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
  163. if (success && bytes_read) {
  164. return bytes_read;
  165. } else
  166. bytes_read = GetLastError();
  167. return 0;
  168. }
  169. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
  170. size_t len)
  171. {
  172. DWORD bytes_written;
  173. bool success;
  174. if (!pp) {
  175. return 0;
  176. }
  177. if (pp->read_pipe) {
  178. return 0;
  179. }
  180. success =
  181. !!WriteFile(pp->handle, data, (DWORD)len, &bytes_written, NULL);
  182. if (success && bytes_written) {
  183. return bytes_written;
  184. }
  185. return 0;
  186. }