pipe-windows.c 3.4 KB

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