process.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  9. * All Rights Reserved.
  10. *
  11. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  12. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person
  15. * obtaining a copy of this software and associated documentation
  16. * files (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy,
  18. * modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is
  20. * furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. * ***** END LICENSE BLOCK *****
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include <io.h>
  39. #include <stdio.h>
  40. #include <windows.h>
  41. #include "common.h"
  42. #include "process.h"
  43. void die_windows_error(const char *fmt, ...) {
  44. char *msg;
  45. va_list ap;
  46. va_start(ap, fmt);
  47. vfprintf(stderr, fmt, ap);
  48. va_end(ap);
  49. if (!FormatMessage(
  50. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
  51. GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  52. (LPSTR)&msg, 0, NULL)) {
  53. msg = "(failed to retrieve Windows error message)";
  54. }
  55. fprintf(stderr, ": %s\n", msg);
  56. exit(1);
  57. }
  58. static char *make_command_line(const char *const *argv) {
  59. int i;
  60. size_t len = 1; /* initial quotes */
  61. char *buf;
  62. char *dest;
  63. /* calculate the length of the required buffer, making worst
  64. case assumptions for simplicity */
  65. for (i = 0;;) {
  66. /* each character could need escaping */
  67. len += strlen(argv[i]) * 2;
  68. if (!argv[++i]) {
  69. break;
  70. }
  71. len += 3; /* quotes, space, quotes */
  72. }
  73. len += 2; /* final quotes and the terminating zero */
  74. dest = buf = malloc(len);
  75. if (!buf) {
  76. die("allocating memory for subprocess command line");
  77. }
  78. /* Here we perform the inverse of the CommandLineToArgvW
  79. function. Note that its rules are slightly crazy: A
  80. sequence of backslashes only act to escape if followed by
  81. double quotes. A sequence of backslashes not followed by
  82. double quotes is untouched. */
  83. for (i = 0;;) {
  84. const char *src = argv[i];
  85. int backslashes = 0;
  86. *dest++ = '\"';
  87. for (;;) {
  88. switch (*src) {
  89. case 0:
  90. goto done;
  91. case '\"':
  92. for (; backslashes; backslashes--) {
  93. *dest++ = '\\';
  94. }
  95. *dest++ = '\\';
  96. *dest++ = '\"';
  97. break;
  98. case '\\':
  99. backslashes++;
  100. *dest++ = '\\';
  101. break;
  102. default:
  103. backslashes = 0;
  104. *dest++ = *src;
  105. break;
  106. }
  107. src++;
  108. }
  109. done:
  110. for (; backslashes; backslashes--) {
  111. *dest++ = '\\';
  112. }
  113. *dest++ = '\"';
  114. if (!argv[++i]) {
  115. break;
  116. }
  117. *dest++ = ' ';
  118. }
  119. *dest++ = 0;
  120. return buf;
  121. }
  122. void pipeline(const char *const *argv, struct pipeline *pl) {
  123. HANDLE in_read_handle, in_write_handle;
  124. SECURITY_ATTRIBUTES sec_attr;
  125. PROCESS_INFORMATION proc_info;
  126. STARTUPINFO start_info;
  127. char *cmdline = make_command_line(argv);
  128. sec_attr.nLength = sizeof sec_attr;
  129. sec_attr.bInheritHandle = TRUE;
  130. sec_attr.lpSecurityDescriptor = NULL;
  131. if (!CreatePipe(&in_read_handle, &in_write_handle, &sec_attr, 0)) {
  132. die_windows_error("CreatePipe");
  133. }
  134. if (!SetHandleInformation(in_write_handle, HANDLE_FLAG_INHERIT, 0)) {
  135. die_windows_error("SetHandleInformation");
  136. }
  137. /* when in Rome... */
  138. ZeroMemory(&proc_info, sizeof proc_info);
  139. ZeroMemory(&start_info, sizeof start_info);
  140. start_info.cb = sizeof start_info;
  141. start_info.dwFlags |= STARTF_USESTDHANDLES;
  142. if ((start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE)) ==
  143. INVALID_HANDLE_VALUE ||
  144. (start_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE)) ==
  145. INVALID_HANDLE_VALUE) {
  146. die_windows_error("GetStdHandle");
  147. }
  148. start_info.hStdInput = in_read_handle;
  149. if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
  150. &start_info, &proc_info)) {
  151. die_windows_error("CreateProcess");
  152. }
  153. free(cmdline);
  154. if (!CloseHandle(proc_info.hThread)) {
  155. die_windows_error("CloseHandle for thread");
  156. }
  157. if (!CloseHandle(in_read_handle)) {
  158. die_windows_error("CloseHandle");
  159. }
  160. pl->proc_handle = proc_info.hProcess;
  161. pl->infd = _open_osfhandle((intptr_t)in_write_handle, 0);
  162. }
  163. int finish_pipeline(struct pipeline *pl) {
  164. DWORD code;
  165. if (close(pl->infd)) {
  166. die_errno(errno, "close");
  167. }
  168. for (;;) {
  169. if (!GetExitCodeProcess(pl->proc_handle, &code)) {
  170. die_windows_error("GetExitCodeProcess");
  171. }
  172. if (code != STILL_ACTIVE) {
  173. break;
  174. }
  175. if (WaitForSingleObject(pl->proc_handle, INFINITE) == WAIT_FAILED) {
  176. die_windows_error("WaitForSingleObject");
  177. }
  178. }
  179. if (!CloseHandle(pl->proc_handle)) {
  180. die_windows_error("CloseHandle for process");
  181. }
  182. return code;
  183. }