1
0

runner-win.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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, (WCHAR*)&image, sizeof(image) / sizeof(WCHAR));
  94. if (result == 0 || result == sizeof(image))
  95. goto error;
  96. if (part) {
  97. if (_snwprintf((WCHAR*)args,
  98. sizeof(args) / sizeof(WCHAR),
  99. L"\"%s\" %S %S",
  100. image,
  101. name,
  102. part) < 0) {
  103. goto error;
  104. }
  105. } else {
  106. if (_snwprintf((WCHAR*)args,
  107. sizeof(args) / sizeof(WCHAR),
  108. L"\"%s\" %S",
  109. image,
  110. name) < 0) {
  111. goto error;
  112. }
  113. }
  114. memset((void*)&si, 0, sizeof(si));
  115. si.cb = sizeof(si);
  116. si.dwFlags = STARTF_USESTDHANDLES;
  117. si.hStdInput = nul;
  118. si.hStdOutput = file;
  119. si.hStdError = file;
  120. if (!CreateProcessW(image, args, NULL, NULL, TRUE,
  121. 0, NULL, NULL, &si, &pi))
  122. goto error;
  123. CloseHandle(pi.hThread);
  124. SetHandleInformation(nul, HANDLE_FLAG_INHERIT, 0);
  125. SetHandleInformation(file, HANDLE_FLAG_INHERIT, 0);
  126. p->stdio_in = nul;
  127. p->stdio_out = file;
  128. p->process = pi.hProcess;
  129. p->name = part;
  130. return 0;
  131. error:
  132. if (file != INVALID_HANDLE_VALUE)
  133. CloseHandle(file);
  134. if (nul != INVALID_HANDLE_VALUE)
  135. CloseHandle(nul);
  136. return -1;
  137. }
  138. /* Timeout is is msecs. Set timeout < 0 to never time out. */
  139. /* Returns 0 when all processes are terminated, -2 on timeout. */
  140. int process_wait(process_info_t *vec, int n, int timeout) {
  141. int i;
  142. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  143. DWORD timeout_api, result;
  144. /* If there's nothing to wait for, return immediately. */
  145. if (n == 0)
  146. return 0;
  147. ASSERT(n <= MAXIMUM_WAIT_OBJECTS);
  148. for (i = 0; i < n; i++)
  149. handles[i] = vec[i].process;
  150. if (timeout >= 0) {
  151. timeout_api = (DWORD)timeout;
  152. } else {
  153. timeout_api = INFINITE;
  154. }
  155. result = WaitForMultipleObjects(n, handles, TRUE, timeout_api);
  156. if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n) {
  157. /* All processes are terminated. */
  158. return 0;
  159. }
  160. if (result == WAIT_TIMEOUT) {
  161. return -2;
  162. }
  163. return -1;
  164. }
  165. long int process_output_size(process_info_t *p) {
  166. LARGE_INTEGER size;
  167. if (!GetFileSizeEx(p->stdio_out, &size))
  168. return -1;
  169. return (long int)size.QuadPart;
  170. }
  171. int process_copy_output(process_info_t *p, int fd) {
  172. DWORD read;
  173. char buf[1024];
  174. char *line, *start;
  175. if (SetFilePointer(p->stdio_out, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
  176. return -1;
  177. if (tap_output)
  178. write(fd, "#", 1);
  179. while (ReadFile(p->stdio_out, (void*)&buf, sizeof(buf), &read, NULL) &&
  180. read > 0) {
  181. if (tap_output) {
  182. start = buf;
  183. while ((line = strchr(start, '\n')) != NULL) {
  184. write(fd, start, line - start + 1);
  185. write(fd, "#", 1);
  186. start = line + 1;
  187. }
  188. if (start < buf + read)
  189. write(fd, start, buf + read - start);
  190. } else {
  191. write(fd, buf, read);
  192. }
  193. }
  194. if (tap_output)
  195. write(fd, "\n", 1);
  196. if (GetLastError() != ERROR_HANDLE_EOF)
  197. return -1;
  198. return 0;
  199. }
  200. int process_read_last_line(process_info_t *p,
  201. char * buffer,
  202. size_t buffer_len) {
  203. DWORD size;
  204. DWORD read;
  205. DWORD start;
  206. OVERLAPPED overlapped;
  207. ASSERT(buffer_len > 0);
  208. size = GetFileSize(p->stdio_out, NULL);
  209. if (size == INVALID_FILE_SIZE)
  210. return -1;
  211. if (size == 0) {
  212. buffer[0] = '\0';
  213. return 1;
  214. }
  215. memset(&overlapped, 0, sizeof overlapped);
  216. if (size >= buffer_len)
  217. overlapped.Offset = size - buffer_len - 1;
  218. if (!ReadFile(p->stdio_out, buffer, buffer_len - 1, &read, &overlapped))
  219. return -1;
  220. for (start = read - 1; start >= 0; start--) {
  221. if (buffer[start] == '\n' || buffer[start] == '\r')
  222. break;
  223. }
  224. if (start > 0)
  225. memmove(buffer, buffer + start, read - start);
  226. buffer[read - start] = '\0';
  227. return 0;
  228. }
  229. char* process_get_name(process_info_t *p) {
  230. return p->name;
  231. }
  232. int process_terminate(process_info_t *p) {
  233. if (!TerminateProcess(p->process, 1))
  234. return -1;
  235. return 0;
  236. }
  237. int process_reap(process_info_t *p) {
  238. DWORD exitCode;
  239. if (!GetExitCodeProcess(p->process, &exitCode))
  240. return -1;
  241. return (int)exitCode;
  242. }
  243. void process_cleanup(process_info_t *p) {
  244. CloseHandle(p->process);
  245. CloseHandle(p->stdio_in);
  246. CloseHandle(p->stdio_out);
  247. }
  248. static int clear_line() {
  249. HANDLE handle;
  250. CONSOLE_SCREEN_BUFFER_INFO info;
  251. COORD coord;
  252. DWORD written;
  253. handle = (HANDLE)_get_osfhandle(fileno(stderr));
  254. if (handle == INVALID_HANDLE_VALUE)
  255. return -1;
  256. if (!GetConsoleScreenBufferInfo(handle, &info))
  257. return -1;
  258. coord = info.dwCursorPosition;
  259. if (coord.Y <= 0)
  260. return -1;
  261. coord.X = 0;
  262. if (!SetConsoleCursorPosition(handle, coord))
  263. return -1;
  264. if (!FillConsoleOutputCharacterW(handle, 0x20, info.dwSize.X, coord, &written))
  265. return -1;
  266. return 0;
  267. }
  268. void rewind_cursor() {
  269. if (clear_line() == -1) {
  270. /* If clear_line fails (stdout is not a console), print a newline. */
  271. fprintf(stderr, "\n");
  272. }
  273. }
  274. /* Pause the calling thread for a number of milliseconds. */
  275. void uv_sleep(int msec) {
  276. Sleep(msec);
  277. }