pipe-windows.c 4.4 KB

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