runner-win.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. const char* tap;
  41. tap = getenv("UV_TAP_OUTPUT");
  42. tap_output = (tap != NULL && atoi(tap) > 0);
  43. /* Disable the "application crashed" popup. */
  44. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
  45. SEM_NOOPENFILEERRORBOX);
  46. #if !defined(__MINGW32__)
  47. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
  48. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
  49. #endif
  50. _setmode(0, _O_BINARY);
  51. _setmode(1, _O_BINARY);
  52. _setmode(2, _O_BINARY);
  53. /* Disable stdio output buffering. */
  54. setvbuf(stdout, NULL, _IONBF, 0);
  55. setvbuf(stderr, NULL, _IONBF, 0);
  56. strcpy(executable_path, argv[0]);
  57. }
  58. int process_start(char *name, char *part, process_info_t *p, int is_helper) {
  59. HANDLE file = INVALID_HANDLE_VALUE;
  60. HANDLE nul = INVALID_HANDLE_VALUE;
  61. WCHAR path[MAX_PATH], filename[MAX_PATH];
  62. WCHAR image[MAX_PATH + 1];
  63. WCHAR args[MAX_PATH * 2];
  64. STARTUPINFOW si;
  65. PROCESS_INFORMATION pi;
  66. DWORD result;
  67. if (GetTempPathW(sizeof(path) / sizeof(WCHAR), (WCHAR*)&path) == 0)
  68. goto error;
  69. if (GetTempFileNameW((WCHAR*)&path, L"uv", 0, (WCHAR*)&filename) == 0)
  70. goto error;
  71. file = CreateFileW((WCHAR*)filename,
  72. GENERIC_READ | GENERIC_WRITE,
  73. 0,
  74. NULL,
  75. CREATE_ALWAYS,
  76. FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
  77. NULL);
  78. if (file == INVALID_HANDLE_VALUE)
  79. goto error;
  80. if (!SetHandleInformation(file, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
  81. goto error;
  82. nul = CreateFileA("nul",
  83. GENERIC_READ,
  84. FILE_SHARE_READ | FILE_SHARE_WRITE,
  85. NULL,
  86. OPEN_EXISTING,
  87. FILE_ATTRIBUTE_NORMAL,
  88. NULL);
  89. if (nul == INVALID_HANDLE_VALUE)
  90. goto error;
  91. if (!SetHandleInformation(nul, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
  92. goto error;
  93. result = GetModuleFileNameW(NULL,
  94. (WCHAR*) &image,
  95. sizeof(image) / sizeof(WCHAR));
  96. if (result == 0 || result == sizeof(image))
  97. goto error;
  98. if (part) {
  99. if (_snwprintf((WCHAR*)args,
  100. sizeof(args) / sizeof(WCHAR),
  101. L"\"%s\" %S %S",
  102. image,
  103. name,
  104. part) < 0) {
  105. goto error;
  106. }
  107. } else {
  108. if (_snwprintf((WCHAR*)args,
  109. sizeof(args) / sizeof(WCHAR),
  110. L"\"%s\" %S",
  111. image,
  112. name) < 0) {
  113. goto error;
  114. }
  115. }
  116. memset((void*)&si, 0, sizeof(si));
  117. si.cb = sizeof(si);
  118. si.dwFlags = STARTF_USESTDHANDLES;
  119. si.hStdInput = nul;
  120. si.hStdOutput = file;
  121. si.hStdError = file;
  122. if (!CreateProcessW(image, args, NULL, NULL, TRUE,
  123. 0, NULL, NULL, &si, &pi))
  124. goto error;
  125. CloseHandle(pi.hThread);
  126. SetHandleInformation(nul, HANDLE_FLAG_INHERIT, 0);
  127. SetHandleInformation(file, HANDLE_FLAG_INHERIT, 0);
  128. p->stdio_in = nul;
  129. p->stdio_out = file;
  130. p->process = pi.hProcess;
  131. p->name = part;
  132. return 0;
  133. error:
  134. if (file != INVALID_HANDLE_VALUE)
  135. CloseHandle(file);
  136. if (nul != INVALID_HANDLE_VALUE)
  137. CloseHandle(nul);
  138. return -1;
  139. }
  140. /* Timeout is is msecs. Set timeout < 0 to never time out. */
  141. /* Returns 0 when all processes are terminated, -2 on timeout. */
  142. int process_wait(process_info_t *vec, int n, int timeout) {
  143. int i;
  144. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  145. DWORD timeout_api, result;
  146. /* If there's nothing to wait for, return immediately. */
  147. if (n == 0)
  148. return 0;
  149. ASSERT(n <= MAXIMUM_WAIT_OBJECTS);
  150. for (i = 0; i < n; i++)
  151. handles[i] = vec[i].process;
  152. if (timeout >= 0) {
  153. timeout_api = (DWORD)timeout;
  154. } else {
  155. timeout_api = INFINITE;
  156. }
  157. result = WaitForMultipleObjects(n, handles, TRUE, timeout_api);
  158. if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n) {
  159. /* All processes are terminated. */
  160. return 0;
  161. }
  162. if (result == WAIT_TIMEOUT) {
  163. return -2;
  164. }
  165. return -1;
  166. }
  167. long int process_output_size(process_info_t *p) {
  168. LARGE_INTEGER size;
  169. if (!GetFileSizeEx(p->stdio_out, &size))
  170. return -1;
  171. return (long int)size.QuadPart;
  172. }
  173. int process_copy_output(process_info_t *p, int fd) {
  174. DWORD read;
  175. char buf[1024];
  176. char *line, *start;
  177. if (SetFilePointer(p->stdio_out,
  178. 0,
  179. 0,
  180. FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
  181. return -1;
  182. }
  183. if (tap_output)
  184. write(fd, "#", 1);
  185. while (ReadFile(p->stdio_out, (void*)&buf, sizeof(buf), &read, NULL) &&
  186. read > 0) {
  187. if (tap_output) {
  188. start = buf;
  189. while ((line = strchr(start, '\n')) != NULL) {
  190. write(fd, start, line - start + 1);
  191. write(fd, "#", 1);
  192. start = line + 1;
  193. }
  194. if (start < buf + read)
  195. write(fd, start, buf + read - start);
  196. } else {
  197. write(fd, buf, read);
  198. }
  199. }
  200. if (tap_output)
  201. write(fd, "\n", 1);
  202. if (GetLastError() != ERROR_HANDLE_EOF)
  203. return -1;
  204. return 0;
  205. }
  206. int process_read_last_line(process_info_t *p,
  207. char * buffer,
  208. size_t buffer_len) {
  209. DWORD size;
  210. DWORD read;
  211. DWORD start;
  212. OVERLAPPED overlapped;
  213. ASSERT(buffer_len > 0);
  214. size = GetFileSize(p->stdio_out, NULL);
  215. if (size == INVALID_FILE_SIZE)
  216. return -1;
  217. if (size == 0) {
  218. buffer[0] = '\0';
  219. return 1;
  220. }
  221. memset(&overlapped, 0, sizeof overlapped);
  222. if (size >= buffer_len)
  223. overlapped.Offset = size - buffer_len - 1;
  224. if (!ReadFile(p->stdio_out, buffer, buffer_len - 1, &read, &overlapped))
  225. return -1;
  226. for (start = read - 1; start >= 0; start--) {
  227. if (buffer[start] == '\n' || buffer[start] == '\r')
  228. break;
  229. }
  230. if (start > 0)
  231. memmove(buffer, buffer + start, read - start);
  232. buffer[read - start] = '\0';
  233. return 0;
  234. }
  235. char* process_get_name(process_info_t *p) {
  236. return p->name;
  237. }
  238. int process_terminate(process_info_t *p) {
  239. if (!TerminateProcess(p->process, 1))
  240. return -1;
  241. return 0;
  242. }
  243. int process_reap(process_info_t *p) {
  244. DWORD exitCode;
  245. if (!GetExitCodeProcess(p->process, &exitCode))
  246. return -1;
  247. return (int)exitCode;
  248. }
  249. void process_cleanup(process_info_t *p) {
  250. CloseHandle(p->process);
  251. CloseHandle(p->stdio_in);
  252. CloseHandle(p->stdio_out);
  253. }
  254. static int clear_line() {
  255. HANDLE handle;
  256. CONSOLE_SCREEN_BUFFER_INFO info;
  257. COORD coord;
  258. DWORD written;
  259. handle = (HANDLE)_get_osfhandle(fileno(stderr));
  260. if (handle == INVALID_HANDLE_VALUE)
  261. return -1;
  262. if (!GetConsoleScreenBufferInfo(handle, &info))
  263. return -1;
  264. coord = info.dwCursorPosition;
  265. if (coord.Y <= 0)
  266. return -1;
  267. coord.X = 0;
  268. if (!SetConsoleCursorPosition(handle, coord))
  269. return -1;
  270. if (!FillConsoleOutputCharacterW(handle,
  271. 0x20,
  272. info.dwSize.X,
  273. coord,
  274. &written)) {
  275. return -1;
  276. }
  277. return 0;
  278. }
  279. void rewind_cursor() {
  280. if (clear_line() == -1) {
  281. /* If clear_line fails (stdout is not a console), print a newline. */
  282. fprintf(stderr, "\n");
  283. }
  284. }
  285. /* Pause the calling thread for a number of milliseconds. */
  286. void uv_sleep(int msec) {
  287. Sleep(msec);
  288. }