pipe-windows.c 4.4 KB

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