runner-win.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include <fcntl.h>
  22. #include <io.h>
  23. #include <malloc.h>
  24. #include <stdio.h>
  25. #include <process.h>
  26. #if !defined(__MINGW32__)
  27. # include <crtdbg.h>
  28. #endif
  29. #include "task.h"
  30. #include "runner.h"
  31. /*
  32. * Define the stuff that MinGW doesn't have
  33. */
  34. #ifndef GetFileSizeEx
  35. WINBASEAPI BOOL WINAPI GetFileSizeEx(HANDLE hFile,
  36. PLARGE_INTEGER lpFileSize);
  37. #endif
  38. /* Do platform-specific initialization. */
  39. void platform_init(int argc, char **argv) {
  40. /* Disable the "application crashed" popup. */
  41. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
  42. SEM_NOOPENFILEERRORBOX);
  43. #if !defined(__MINGW32__)
  44. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
  45. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
  46. #endif
  47. _setmode(0, _O_BINARY);
  48. _setmode(1, _O_BINARY);
  49. _setmode(2, _O_BINARY);
  50. #ifdef _MSC_VER
  51. _set_fmode(_O_BINARY);
  52. #else
  53. _fmode = _O_BINARY;
  54. #endif
  55. /* Disable stdio output buffering. */
  56. setvbuf(stdout, NULL, _IONBF, 0);
  57. setvbuf(stderr, NULL, _IONBF, 0);
  58. strcpy(executable_path, argv[0]);
  59. }
  60. int process_start(char *name, char *part, process_info_t *p, int is_helper) {
  61. HANDLE file = INVALID_HANDLE_VALUE;
  62. HANDLE nul = INVALID_HANDLE_VALUE;
  63. WCHAR path[MAX_PATH], filename[MAX_PATH];
  64. WCHAR image[MAX_PATH + 1];
  65. WCHAR args[MAX_PATH * 2];
  66. STARTUPINFOW si;
  67. PROCESS_INFORMATION pi;
  68. DWORD result;
  69. if (!is_helper) {
  70. /* Give the helpers time to settle. Race-y, fix this. */
  71. uv_sleep(250);
  72. }
  73. if (GetTempPathW(sizeof(path) / sizeof(WCHAR), (WCHAR*)&path) == 0)
  74. goto error;
  75. if (GetTempFileNameW((WCHAR*)&path, L"uv", 0, (WCHAR*)&filename) == 0)
  76. goto error;
  77. file = CreateFileW((WCHAR*)filename,
  78. GENERIC_READ | GENERIC_WRITE,
  79. 0,
  80. NULL,
  81. CREATE_ALWAYS,
  82. FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
  83. NULL);
  84. if (file == INVALID_HANDLE_VALUE)
  85. goto error;
  86. if (!SetHandleInformation(file, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
  87. goto error;
  88. nul = CreateFileA("nul",
  89. GENERIC_READ,
  90. FILE_SHARE_READ | FILE_SHARE_WRITE,
  91. NULL,
  92. OPEN_EXISTING,
  93. FILE_ATTRIBUTE_NORMAL,
  94. NULL);
  95. if (nul == INVALID_HANDLE_VALUE)
  96. goto error;
  97. if (!SetHandleInformation(nul, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
  98. goto error;
  99. result = GetModuleFileNameW(NULL,
  100. (WCHAR*) &image,
  101. sizeof(image) / sizeof(WCHAR));
  102. if (result == 0 || result == sizeof(image))
  103. goto error;
  104. if (part) {
  105. if (_snwprintf((WCHAR*)args,
  106. sizeof(args) / sizeof(WCHAR),
  107. L"\"%s\" %S %S",
  108. image,
  109. name,
  110. part) < 0) {
  111. goto error;
  112. }
  113. } else {
  114. if (_snwprintf((WCHAR*)args,
  115. sizeof(args) / sizeof(WCHAR),
  116. L"\"%s\" %S",
  117. image,
  118. name) < 0) {
  119. goto error;
  120. }
  121. }
  122. memset((void*)&si, 0, sizeof(si));
  123. si.cb = sizeof(si);
  124. si.dwFlags = STARTF_USESTDHANDLES;
  125. si.hStdInput = nul;
  126. si.hStdOutput = file;
  127. si.hStdError = file;
  128. if (!CreateProcessW(image, args, NULL, NULL, TRUE,
  129. 0, NULL, NULL, &si, &pi))
  130. goto error;
  131. CloseHandle(pi.hThread);
  132. SetHandleInformation(nul, HANDLE_FLAG_INHERIT, 0);
  133. SetHandleInformation(file, HANDLE_FLAG_INHERIT, 0);
  134. p->stdio_in = nul;
  135. p->stdio_out = file;
  136. p->process = pi.hProcess;
  137. p->name = part;
  138. return 0;
  139. error:
  140. if (file != INVALID_HANDLE_VALUE)
  141. CloseHandle(file);
  142. if (nul != INVALID_HANDLE_VALUE)
  143. CloseHandle(nul);
  144. return -1;
  145. }
  146. /* Timeout is in msecs. Set timeout < 0 to never time out. Returns 0 when all
  147. * processes are terminated, -2 on timeout. */
  148. int process_wait(process_info_t *vec, int n, int timeout) {
  149. int i;
  150. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  151. DWORD timeout_api, result;
  152. /* If there's nothing to wait for, return immediately. */
  153. if (n == 0)
  154. return 0;
  155. ASSERT(n <= MAXIMUM_WAIT_OBJECTS);
  156. for (i = 0; i < n; i++)
  157. handles[i] = vec[i].process;
  158. if (timeout >= 0) {
  159. timeout_api = (DWORD)timeout;
  160. } else {
  161. timeout_api = INFINITE;
  162. }
  163. result = WaitForMultipleObjects(n, handles, TRUE, timeout_api);
  164. if (result < WAIT_OBJECT_0 + n) {
  165. /* All processes are terminated. */
  166. return 0;
  167. }
  168. if (result == WAIT_TIMEOUT) {
  169. return -2;
  170. }
  171. return -1;
  172. }
  173. long int process_output_size(process_info_t *p) {
  174. LARGE_INTEGER size;
  175. if (!GetFileSizeEx(p->stdio_out, &size))
  176. return -1;
  177. return (long int)size.QuadPart;
  178. }
  179. int process_copy_output(process_info_t* p, FILE* stream) {
  180. char buf[1024];
  181. int fd, r;
  182. fd = _open_osfhandle((intptr_t)p->stdio_out, _O_RDONLY | _O_TEXT);
  183. if (fd == -1)
  184. return -1;
  185. r = _lseek(fd, 0, SEEK_SET);
  186. if (r < 0)
  187. return -1;
  188. while ((r = _read(fd, buf, sizeof(buf))) != 0)
  189. print_lines(buf, r, stream);
  190. _close(fd);
  191. return 0;
  192. }
  193. int process_read_last_line(process_info_t *p,
  194. char * buffer,
  195. size_t buffer_len) {
  196. DWORD size;
  197. DWORD read;
  198. DWORD start;
  199. OVERLAPPED overlapped;
  200. ASSERT(buffer_len > 0);
  201. size = GetFileSize(p->stdio_out, NULL);
  202. if (size == INVALID_FILE_SIZE)
  203. return -1;
  204. if (size == 0) {
  205. buffer[0] = '\0';
  206. return 1;
  207. }
  208. memset(&overlapped, 0, sizeof overlapped);
  209. if (size >= buffer_len)
  210. overlapped.Offset = size - buffer_len - 1;
  211. if (!ReadFile(p->stdio_out, buffer, buffer_len - 1, &read, &overlapped))
  212. return -1;
  213. start = read;
  214. while (start-- > 0) {
  215. if (buffer[start] == '\n' || buffer[start] == '\r')
  216. break;
  217. }
  218. if (start > 0)
  219. memmove(buffer, buffer + start, read - start);
  220. buffer[read - start] = '\0';
  221. return 0;
  222. }
  223. char* process_get_name(process_info_t *p) {
  224. return p->name;
  225. }
  226. int process_terminate(process_info_t *p) {
  227. if (!TerminateProcess(p->process, 1))
  228. return -1;
  229. return 0;
  230. }
  231. int process_reap(process_info_t *p) {
  232. DWORD exitCode;
  233. if (!GetExitCodeProcess(p->process, &exitCode))
  234. return -1;
  235. return (int)exitCode;
  236. }
  237. void process_cleanup(process_info_t *p) {
  238. CloseHandle(p->process);
  239. CloseHandle(p->stdio_in);
  240. }
  241. static int clear_line(void) {
  242. HANDLE handle;
  243. CONSOLE_SCREEN_BUFFER_INFO info;
  244. COORD coord;
  245. DWORD written;
  246. handle = (HANDLE)_get_osfhandle(fileno(stderr));
  247. if (handle == INVALID_HANDLE_VALUE)
  248. return -1;
  249. if (!GetConsoleScreenBufferInfo(handle, &info))
  250. return -1;
  251. coord = info.dwCursorPosition;
  252. if (coord.Y <= 0)
  253. return -1;
  254. coord.X = 0;
  255. if (!SetConsoleCursorPosition(handle, coord))
  256. return -1;
  257. if (!FillConsoleOutputCharacterW(handle,
  258. 0x20,
  259. info.dwSize.X,
  260. coord,
  261. &written)) {
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. void rewind_cursor() {
  267. if (clear_line() == -1) {
  268. /* If clear_line fails (stdout is not a console), print a newline. */
  269. fprintf(stderr, "\n");
  270. }
  271. }