pipe-windows.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2014 Hugh 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. }
  62. bfree(cmd_line_w);
  63. }
  64. return success;
  65. }
  66. os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
  67. const char *type)
  68. {
  69. os_process_pipe_t *pp = NULL;
  70. bool read_pipe;
  71. HANDLE process;
  72. HANDLE output;
  73. HANDLE err_input, err_output;
  74. HANDLE input;
  75. bool success;
  76. if (!cmd_line || !type) {
  77. return NULL;
  78. }
  79. if (*type != 'r' && *type != 'w') {
  80. return NULL;
  81. }
  82. if (!create_pipe(&input, &output)) {
  83. return NULL;
  84. }
  85. if (!create_pipe(&err_input, &err_output)) {
  86. return NULL;
  87. }
  88. read_pipe = *type == 'r';
  89. success = !!SetHandleInformation(read_pipe ? input : output,
  90. HANDLE_FLAG_INHERIT, false);
  91. if (!success) {
  92. goto error;
  93. }
  94. success = !!SetHandleInformation(err_input, HANDLE_FLAG_INHERIT, false);
  95. if (!success) {
  96. goto error;
  97. }
  98. success = create_process(cmd_line, read_pipe ? NULL : input,
  99. read_pipe ? output : NULL, err_output,
  100. &process);
  101. if (!success) {
  102. goto error;
  103. }
  104. pp = bmalloc(sizeof(*pp));
  105. pp->handle = read_pipe ? input : output;
  106. pp->read_pipe = read_pipe;
  107. pp->process = process;
  108. pp->handle_err = err_input;
  109. CloseHandle(read_pipe ? output : input);
  110. CloseHandle(err_output);
  111. return pp;
  112. error:
  113. CloseHandle(output);
  114. CloseHandle(input);
  115. return NULL;
  116. }
  117. int os_process_pipe_destroy(os_process_pipe_t *pp)
  118. {
  119. int ret = 0;
  120. if (pp) {
  121. DWORD code;
  122. CloseHandle(pp->handle);
  123. CloseHandle(pp->handle_err);
  124. WaitForSingleObject(pp->process, INFINITE);
  125. if (GetExitCodeProcess(pp->process, &code))
  126. ret = (int)code;
  127. CloseHandle(pp->process);
  128. bfree(pp);
  129. }
  130. return ret;
  131. }
  132. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  133. {
  134. DWORD bytes_read;
  135. bool success;
  136. if (!pp) {
  137. return 0;
  138. }
  139. if (!pp->read_pipe) {
  140. return 0;
  141. }
  142. success = !!ReadFile(pp->handle, data, (DWORD)len, &bytes_read, NULL);
  143. if (success && bytes_read) {
  144. return bytes_read;
  145. }
  146. return 0;
  147. }
  148. size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
  149. size_t len)
  150. {
  151. DWORD bytes_read;
  152. bool success;
  153. if (!pp || !pp->handle_err) {
  154. return 0;
  155. }
  156. success =
  157. !!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
  158. if (success && bytes_read) {
  159. return bytes_read;
  160. } else
  161. bytes_read = GetLastError();
  162. return 0;
  163. }
  164. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
  165. size_t len)
  166. {
  167. DWORD bytes_written;
  168. bool success;
  169. if (!pp) {
  170. return 0;
  171. }
  172. if (pp->read_pipe) {
  173. return 0;
  174. }
  175. success =
  176. !!WriteFile(pp->handle, data, (DWORD)len, &bytes_written, NULL);
  177. if (success && bytes_written) {
  178. return bytes_written;
  179. }
  180. return 0;
  181. }