/test/runner-win.c

http://github.com/joyent/libuv · C · 292 lines · 196 code · 64 blank · 32 comment · 48 complexity · 67dbce6c6d7c43b96ee223357ec57d5d MD5 · raw file

  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. /* Disable stdio output buffering. */
  51. setvbuf(stdout, NULL, _IONBF, 0);
  52. setvbuf(stderr, NULL, _IONBF, 0);
  53. strcpy(executable_path, argv[0]);
  54. }
  55. int process_start(char *name, char *part, process_info_t *p, int is_helper) {
  56. HANDLE file = INVALID_HANDLE_VALUE;
  57. HANDLE nul = INVALID_HANDLE_VALUE;
  58. WCHAR path[MAX_PATH], filename[MAX_PATH];
  59. WCHAR image[MAX_PATH + 1];
  60. WCHAR args[MAX_PATH * 2];
  61. STARTUPINFOW si;
  62. PROCESS_INFORMATION pi;
  63. DWORD result;
  64. if (GetTempPathW(sizeof(path) / sizeof(WCHAR), (WCHAR*)&path) == 0)
  65. goto error;
  66. if (GetTempFileNameW((WCHAR*)&path, L"uv", 0, (WCHAR*)&filename) == 0)
  67. goto error;
  68. file = CreateFileW((WCHAR*)filename,
  69. GENERIC_READ | GENERIC_WRITE,
  70. 0,
  71. NULL,
  72. CREATE_ALWAYS,
  73. FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
  74. NULL);
  75. if (file == INVALID_HANDLE_VALUE)
  76. goto error;
  77. if (!SetHandleInformation(file, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
  78. goto error;
  79. nul = CreateFileA("nul",
  80. GENERIC_READ,
  81. FILE_SHARE_READ | FILE_SHARE_WRITE,
  82. NULL,
  83. OPEN_EXISTING,
  84. FILE_ATTRIBUTE_NORMAL,
  85. NULL);
  86. if (nul == INVALID_HANDLE_VALUE)
  87. goto error;
  88. if (!SetHandleInformation(nul, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
  89. goto error;
  90. result = GetModuleFileNameW(NULL, (WCHAR*)&image, sizeof(image) / sizeof(WCHAR));
  91. if (result == 0 || result == sizeof(image))
  92. goto error;
  93. if (part) {
  94. if (_snwprintf((WCHAR*)args,
  95. sizeof(args) / sizeof(WCHAR),
  96. L"\"%s\" %S %S",
  97. image,
  98. name,
  99. part) < 0) {
  100. goto error;
  101. }
  102. } else {
  103. if (_snwprintf((WCHAR*)args,
  104. sizeof(args) / sizeof(WCHAR),
  105. L"\"%s\" %S",
  106. image,
  107. name) < 0) {
  108. goto error;
  109. }
  110. }
  111. memset((void*)&si, 0, sizeof(si));
  112. si.cb = sizeof(si);
  113. si.dwFlags = STARTF_USESTDHANDLES;
  114. si.hStdInput = nul;
  115. si.hStdOutput = file;
  116. si.hStdError = file;
  117. if (!CreateProcessW(image, args, NULL, NULL, TRUE,
  118. 0, NULL, NULL, &si, &pi))
  119. goto error;
  120. CloseHandle(pi.hThread);
  121. SetHandleInformation(nul, HANDLE_FLAG_INHERIT, 0);
  122. SetHandleInformation(file, HANDLE_FLAG_INHERIT, 0);
  123. p->stdio_in = nul;
  124. p->stdio_out = file;
  125. p->process = pi.hProcess;
  126. p->name = part;
  127. return 0;
  128. error:
  129. if (file != INVALID_HANDLE_VALUE)
  130. CloseHandle(file);
  131. if (nul != INVALID_HANDLE_VALUE)
  132. CloseHandle(nul);
  133. return -1;
  134. }
  135. /* Timeout is is msecs. Set timeout < 0 to never time out. */
  136. /* Returns 0 when all processes are terminated, -2 on timeout. */
  137. int process_wait(process_info_t *vec, int n, int timeout) {
  138. int i;
  139. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  140. DWORD timeout_api, result;
  141. /* If there's nothing to wait for, return immediately. */
  142. if (n == 0)
  143. return 0;
  144. ASSERT(n <= MAXIMUM_WAIT_OBJECTS);
  145. for (i = 0; i < n; i++)
  146. handles[i] = vec[i].process;
  147. if (timeout >= 0) {
  148. timeout_api = (DWORD)timeout;
  149. } else {
  150. timeout_api = INFINITE;
  151. }
  152. result = WaitForMultipleObjects(n, handles, TRUE, timeout_api);
  153. if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + n) {
  154. /* All processes are terminated. */
  155. return 0;
  156. }
  157. if (result == WAIT_TIMEOUT) {
  158. return -2;
  159. }
  160. return -1;
  161. }
  162. long int process_output_size(process_info_t *p) {
  163. LARGE_INTEGER size;
  164. if (!GetFileSizeEx(p->stdio_out, &size))
  165. return -1;
  166. return (long int)size.QuadPart;
  167. }
  168. int process_copy_output(process_info_t *p, int fd) {
  169. DWORD read;
  170. char buf[1024];
  171. if (SetFilePointer(p->stdio_out, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
  172. return -1;
  173. while (ReadFile(p->stdio_out, (void*)&buf, sizeof(buf), &read, NULL) &&
  174. read > 0)
  175. write(fd, buf, read);
  176. if (GetLastError() != ERROR_HANDLE_EOF)
  177. return -1;
  178. return 0;
  179. }
  180. char* process_get_name(process_info_t *p) {
  181. return p->name;
  182. }
  183. int process_terminate(process_info_t *p) {
  184. if (!TerminateProcess(p->process, 1))
  185. return -1;
  186. return 0;
  187. }
  188. int process_reap(process_info_t *p) {
  189. DWORD exitCode;
  190. if (!GetExitCodeProcess(p->process, &exitCode))
  191. return -1;
  192. return (int)exitCode;
  193. }
  194. void process_cleanup(process_info_t *p) {
  195. CloseHandle(p->process);
  196. CloseHandle(p->stdio_in);
  197. CloseHandle(p->stdio_out);
  198. }
  199. static int clear_line() {
  200. HANDLE handle;
  201. CONSOLE_SCREEN_BUFFER_INFO info;
  202. COORD coord;
  203. DWORD written;
  204. handle = (HANDLE)_get_osfhandle(fileno(stderr));
  205. if (handle == INVALID_HANDLE_VALUE)
  206. return -1;
  207. if (!GetConsoleScreenBufferInfo(handle, &info))
  208. return -1;
  209. coord = info.dwCursorPosition;
  210. if (coord.Y <= 0)
  211. return -1;
  212. coord.X = 0;
  213. if (!SetConsoleCursorPosition(handle, coord))
  214. return -1;
  215. if (!FillConsoleOutputCharacterW(handle, 0x20, info.dwSize.X, coord, &written))
  216. return -1;
  217. return 0;
  218. }
  219. void rewind_cursor() {
  220. if (clear_line() == -1) {
  221. /* If clear_line fails (stdout is not a console), print a newline. */
  222. fprintf(stderr, "\n");
  223. }
  224. }
  225. /* Pause the calling thread for a number of milliseconds. */
  226. void uv_sleep(int msec) {
  227. Sleep(msec);
  228. }