pipe-windows.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 process;
  25. };
  26. static bool create_pipe(HANDLE *input, HANDLE *output)
  27. {
  28. SECURITY_ATTRIBUTES sa = {0};
  29. sa.nLength = sizeof(sa);
  30. sa.bInheritHandle = true;
  31. if (!CreatePipe(input, output, &sa, 0)) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
  37. HANDLE stdout_handle, HANDLE *process)
  38. {
  39. PROCESS_INFORMATION pi = {0};
  40. wchar_t *cmd_line_w = NULL;
  41. STARTUPINFOW si = {0};
  42. bool success = false;
  43. si.cb = sizeof(si);
  44. si.dwFlags = STARTF_USESTDHANDLES | STARTF_FORCEOFFFEEDBACK;
  45. si.hStdInput = stdin_handle;
  46. si.hStdOutput = stdout_handle;
  47. os_utf8_to_wcs_ptr(cmd_line, 0, &cmd_line_w);
  48. if (cmd_line_w) {
  49. success = !!CreateProcessW(NULL, cmd_line_w, NULL, NULL, true,
  50. CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
  51. if (success) {
  52. *process = pi.hProcess;
  53. CloseHandle(pi.hThread);
  54. }
  55. bfree(cmd_line_w);
  56. }
  57. return success;
  58. }
  59. os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
  60. const char *type)
  61. {
  62. os_process_pipe_t *pp = NULL;
  63. bool read_pipe;
  64. HANDLE process;
  65. HANDLE output;
  66. HANDLE input;
  67. bool success;
  68. if (!cmd_line || !type) {
  69. return NULL;
  70. }
  71. if (*type != 'r' && *type != 'w') {
  72. return NULL;
  73. }
  74. if (!create_pipe(&input, &output)) {
  75. return NULL;
  76. }
  77. read_pipe = *type == 'r';
  78. success = !!SetHandleInformation(read_pipe ? input : output,
  79. HANDLE_FLAG_INHERIT, false);
  80. if (!success) {
  81. goto error;
  82. }
  83. success = create_process(cmd_line, read_pipe ? NULL : input,
  84. read_pipe ? output : NULL, &process);
  85. if (!success) {
  86. goto error;
  87. }
  88. pp = bmalloc(sizeof(*pp));
  89. pp->handle = read_pipe ? input : output;
  90. pp->read_pipe = read_pipe;
  91. pp->process = process;
  92. CloseHandle(read_pipe ? output : input);
  93. return pp;
  94. error:
  95. CloseHandle(output);
  96. CloseHandle(input);
  97. return NULL;
  98. }
  99. int os_process_pipe_destroy(os_process_pipe_t *pp)
  100. {
  101. int ret = 0;
  102. if (pp) {
  103. DWORD code;
  104. CloseHandle(pp->handle);
  105. WaitForSingleObject(pp->process, INFINITE);
  106. if (GetExitCodeProcess(pp->process, &code))
  107. ret = (int)code;
  108. CloseHandle(pp->process);
  109. bfree(pp);
  110. }
  111. return ret;
  112. }
  113. size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
  114. {
  115. DWORD bytes_read;
  116. bool success;
  117. if (!pp) {
  118. return 0;
  119. }
  120. if (!pp->read_pipe) {
  121. return 0;
  122. }
  123. success = !!ReadFile(pp->handle, data, (DWORD)len, &bytes_read, NULL);
  124. if (success && bytes_read) {
  125. return bytes_read;
  126. }
  127. return 0;
  128. }
  129. size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
  130. size_t len)
  131. {
  132. DWORD bytes_written;
  133. bool success;
  134. if (!pp) {
  135. return 0;
  136. }
  137. if (pp->read_pipe) {
  138. return 0;
  139. }
  140. success = !!WriteFile(pp->handle, data, (DWORD)len, &bytes_written,
  141. NULL);
  142. if (success && bytes_written) {
  143. return bytes_written;
  144. }
  145. return 0;
  146. }