blob: db87d8ed60ed847cad342dac61cc50a4eca59dc9 [file] [log] [blame]
Reid Spencer91886b72004-09-15 05:47:40 +00001//===- Win32/Process.cpp - Win32 Process Implementation ------- -*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer91886b72004-09-15 05:47:40 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer91886b72004-09-15 05:47:40 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Process class.
11//
12//===----------------------------------------------------------------------===//
13
David Majnemer61eae2e2013-10-07 01:00:07 +000014#include "llvm/Support/Allocator.h"
Alp Toker552f2f72014-06-03 03:01:03 +000015#include "llvm/Support/ErrorHandling.h"
Rafael Espindola5c4f8292014-06-11 19:05:50 +000016#include "llvm/Support/WindowsError.h"
Chandler Carruth10b09152014-01-07 12:37:13 +000017#include <malloc.h>
18
19// The Windows.h header must be after LLVM and standard headers.
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000020#include "WindowsSupport.h"
Chandler Carruth10b09152014-01-07 12:37:13 +000021
Daniel Dunbar9b92e2b2011-09-23 23:23:36 +000022#include <direct.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include <io.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include <psapi.h>
David Majnemer61eae2e2013-10-07 01:00:07 +000025#include <shellapi.h>
Jeff Cohen7ae0bc72004-12-20 03:24:56 +000026
Reid Spencer187b4ad2006-06-01 19:03:21 +000027#ifdef __MINGW32__
28 #if (HAVE_LIBPSAPI != 1)
29 #error "libpsapi.a should be present"
30 #endif
David Majnemer61eae2e2013-10-07 01:00:07 +000031 #if (HAVE_LIBSHELL32 != 1)
32 #error "libshell32.a should be present"
33 #endif
Reid Spencer187b4ad2006-06-01 19:03:21 +000034#else
David Majnemerf636cf42013-10-06 20:44:34 +000035 #pragma comment(lib, "psapi.lib")
David Majnemer61eae2e2013-10-07 01:00:07 +000036 #pragma comment(lib, "shell32.lib")
Reid Spencer187b4ad2006-06-01 19:03:21 +000037#endif
Reid Spencer91886b72004-09-15 05:47:40 +000038
39//===----------------------------------------------------------------------===//
Michael J. Spencer447762d2010-11-29 18:16:10 +000040//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencer91886b72004-09-15 05:47:40 +000041//=== and must not be UNIX code
42//===----------------------------------------------------------------------===//
43
Jeff Cohen07e22ba2005-02-19 03:01:13 +000044#ifdef __MINGW32__
Jeff Cohen53fbecc2004-12-23 03:44:40 +000045// This ban should be lifted when MinGW 1.0+ has defined this value.
46# define _HEAPOK (-2)
47#endif
48
Chandler Carruth5473dfb2012-12-31 11:45:20 +000049using namespace llvm;
Reid Spencer91886b72004-09-15 05:47:40 +000050using namespace sys;
Chandler Carruth97683aa2012-12-31 11:17:50 +000051
52process::id_type self_process::get_id() {
Aaron Ballmanf0b53842013-06-08 20:29:03 +000053 return GetCurrentProcessId();
Chandler Carruth97683aa2012-12-31 11:17:50 +000054}
55
Chandler Carruthef7f9682013-01-04 23:19:55 +000056static TimeValue getTimeValueFromFILETIME(FILETIME Time) {
57 ULARGE_INTEGER TimeInteger;
58 TimeInteger.LowPart = Time.dwLowDateTime;
59 TimeInteger.HighPart = Time.dwHighDateTime;
60
61 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
62 return TimeValue(
63 static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000),
64 static_cast<TimeValue::NanoSecondsType>(
65 (TimeInteger.QuadPart % 10000000) * 100));
66}
67
68TimeValue self_process::get_user_time() const {
69 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
70 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
71 &UserTime) == 0)
72 return TimeValue();
73
74 return getTimeValueFromFILETIME(UserTime);
75}
76
77TimeValue self_process::get_system_time() const {
78 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
79 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
80 &UserTime) == 0)
81 return TimeValue();
82
83 return getTimeValueFromFILETIME(KernelTime);
84}
85
Alp Tokerd71b6df2014-05-19 16:13:28 +000086// This function retrieves the page size using GetNativeSystemInfo() and is
87// present solely so it can be called once to initialize the self_process member
88// below.
Chandler Carruth15dcad92012-12-31 23:23:35 +000089static unsigned getPageSize() {
Alp Tokerd71b6df2014-05-19 16:13:28 +000090 // GetNativeSystemInfo() provides the physical page size which may differ
91 // from GetSystemInfo() in 32-bit applications running under WOW64.
Reid Spencer91886b72004-09-15 05:47:40 +000092 SYSTEM_INFO info;
Alp Tokerd71b6df2014-05-19 16:13:28 +000093 GetNativeSystemInfo(&info);
NAKAMURA Takumi7a042342013-09-04 14:12:26 +000094 // FIXME: FileOffset in MapViewOfFile() should be aligned to not dwPageSize,
95 // but dwAllocationGranularity.
Reid Spencer91886b72004-09-15 05:47:40 +000096 return static_cast<unsigned>(info.dwPageSize);
97}
98
Chandler Carruth15dcad92012-12-31 23:23:35 +000099// This constructor guaranteed to be run exactly once on a single thread, and
100// sets up various process invariants that can be queried cheaply from then on.
101self_process::self_process() : PageSize(getPageSize()) {
Reid Spencer91886b72004-09-15 05:47:40 +0000102}
103
Chandler Carruth15dcad92012-12-31 23:23:35 +0000104
Michael J. Spencer447762d2010-11-29 18:16:10 +0000105size_t
Reid Spencerac38f3a2004-12-20 00:59:28 +0000106Process::GetMallocUsage()
107{
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000108 _HEAPINFO hinfo;
109 hinfo._pentry = NULL;
110
111 size_t size = 0;
112
113 while (_heapwalk(&hinfo) == _HEAPOK)
114 size += hinfo._size;
115
116 return size;
Reid Spencerac38f3a2004-12-20 00:59:28 +0000117}
118
Chandler Carruthef7f9682013-01-04 23:19:55 +0000119void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
120 TimeValue &sys_time) {
Reid Spencerac38f3a2004-12-20 00:59:28 +0000121 elapsed = TimeValue::now();
122
Chandler Carruthef7f9682013-01-04 23:19:55 +0000123 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
124 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
125 &UserTime) == 0)
126 return;
Reid Spencerac38f3a2004-12-20 00:59:28 +0000127
Chandler Carruthef7f9682013-01-04 23:19:55 +0000128 user_time = getTimeValueFromFILETIME(UserTime);
Chandler Carruthb79a7aa2013-01-04 23:46:04 +0000129 sys_time = getTimeValueFromFILETIME(KernelTime);
Reid Spencerac38f3a2004-12-20 00:59:28 +0000130}
131
Reid Spencercf15b872004-12-27 06:17:27 +0000132// Some LLVM programs such as bugpoint produce core files as a normal part of
Aaron Ballmandcd57572013-08-16 14:33:07 +0000133// their operation. To prevent the disk from filling up, this configuration
134// item does what's necessary to prevent their generation.
Reid Spencercf15b872004-12-27 06:17:27 +0000135void Process::PreventCoreFiles() {
Aaron Ballmandcd57572013-08-16 14:33:07 +0000136 // Windows does have the concept of core files, called minidumps. However,
137 // disabling minidumps for a particular application extends past the lifetime
138 // of that application, which is the incorrect behavior for this API.
139 // Additionally, the APIs require elevated privileges to disable and re-
140 // enable minidumps, which makes this untenable. For more information, see
141 // WerAddExcludedApplication and WerRemoveExcludedApplication (Vista and
142 // later).
143 //
144 // Windows also has modal pop-up message boxes. As this method is used by
145 // bugpoint, preventing these pop-ups is additionally important.
Jeff Cohen81549a52005-02-18 07:05:18 +0000146 SetErrorMode(SEM_FAILCRITICALERRORS |
147 SEM_NOGPFAULTERRORBOX |
148 SEM_NOOPENFILEERRORBOX);
Reid Spencercf15b872004-12-27 06:17:27 +0000149}
150
Rui Ueyama471d0c52013-09-10 19:45:51 +0000151/// Returns the environment variable \arg Name's value as a string encoded in
152/// UTF-8. \arg Name is assumed to be in UTF-8 encoding.
153Optional<std::string> Process::GetEnv(StringRef Name) {
154 // Convert the argument to UTF-16 to pass it to _wgetenv().
155 SmallVector<wchar_t, 128> NameUTF16;
Ahmed Charlesce30de92014-03-05 05:04:00 +0000156 if (windows::UTF8ToUTF16(Name, NameUTF16))
Rui Ueyama471d0c52013-09-10 19:45:51 +0000157 return None;
158
159 // Environment variable can be encoded in non-UTF8 encoding, and there's no
160 // way to know what the encoding is. The only reliable way to look up
161 // multibyte environment variable is to use GetEnvironmentVariableW().
David Majnemer61eae2e2013-10-07 01:00:07 +0000162 SmallVector<wchar_t, MAX_PATH> Buf;
163 size_t Size = MAX_PATH;
164 do {
David Majnemerf07777c2013-10-07 21:57:07 +0000165 Buf.reserve(Size);
166 Size =
167 GetEnvironmentVariableW(NameUTF16.data(), Buf.data(), Buf.capacity());
David Majnemer61eae2e2013-10-07 01:00:07 +0000168 if (Size == 0)
169 return None;
170
Rui Ueyama471d0c52013-09-10 19:45:51 +0000171 // Try again with larger buffer.
David Majnemer61eae2e2013-10-07 01:00:07 +0000172 } while (Size > Buf.capacity());
173 Buf.set_size(Size);
Rui Ueyama471d0c52013-09-10 19:45:51 +0000174
175 // Convert the result from UTF-16 to UTF-8.
David Majnemer61eae2e2013-10-07 01:00:07 +0000176 SmallVector<char, MAX_PATH> Res;
Ahmed Charlesce30de92014-03-05 05:04:00 +0000177 if (windows::UTF16ToUTF8(Buf.data(), Size, Res))
Rui Ueyama471d0c52013-09-10 19:45:51 +0000178 return None;
David Majnemerf07777c2013-10-07 21:57:07 +0000179 return std::string(Res.data());
Rui Ueyama471d0c52013-09-10 19:45:51 +0000180}
181
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000182static std::error_code windows_error(DWORD E) {
Rafael Espindola5c4f8292014-06-11 19:05:50 +0000183 return mapWindowsError(E);
Rafael Espindolaa813d602014-06-11 03:58:34 +0000184}
185
Hans Wennborg21f0f132014-07-16 00:52:11 +0000186static void AllocateAndPush(const SmallVectorImpl<char> &S,
187 SmallVectorImpl<const char *> &Vector,
188 SpecificBumpPtrAllocator<char> &Allocator) {
189 char *Buffer = Allocator.Allocate(S.size() + 1);
190 ::memcpy(Buffer, S.data(), S.size());
191 Buffer[S.size()] = '\0';
192 Vector.push_back(Buffer);
193}
194
195/// Convert Arg from UTF-16 to UTF-8 and push it onto Args.
196static std::error_code
197ConvertAndPushArg(const wchar_t *Arg, SmallVectorImpl<const char *> &Args,
198 SpecificBumpPtrAllocator<char> &Allocator) {
199 SmallVector<char, MAX_PATH> ArgString;
200 if (std::error_code ec = windows::UTF16ToUTF8(Arg, wcslen(Arg), ArgString))
201 return ec;
202 AllocateAndPush(ArgString, Args, Allocator);
203 return std::error_code();
204}
205
206/// \brief Perform wildcard expansion of Arg, or just push it into Args if it
207/// doesn't have wildcards or doesn't match any files.
208static std::error_code
209WildcardExpand(const wchar_t *Arg, SmallVectorImpl<const char *> &Args,
210 SpecificBumpPtrAllocator<char> &Allocator) {
211 if (!wcspbrk(Arg, L"*?")) {
212 // Arg does not contain any wildcard characters. This is the common case.
213 return ConvertAndPushArg(Arg, Args, Allocator);
214 }
215
Hans Wennborge34a71a2014-07-24 21:09:45 +0000216 if (wcscmp(Arg, L"/?") == 0 || wcscmp(Arg, L"-?") == 0) {
217 // Don't wildcard expand /?. Always treat it as an option.
218 return ConvertAndPushArg(Arg, Args, Allocator);
219 }
220
Hans Wennborg21f0f132014-07-16 00:52:11 +0000221 // Extract any directory part of the argument.
222 SmallVector<char, MAX_PATH> Dir;
223 if (std::error_code ec = windows::UTF16ToUTF8(Arg, wcslen(Arg), Dir))
224 return ec;
225 sys::path::remove_filename(Dir);
226 const int DirSize = Dir.size();
227
228 // Search for matching files.
229 WIN32_FIND_DATAW FileData;
230 HANDLE FindHandle = FindFirstFileW(Arg, &FileData);
231 if (FindHandle == INVALID_HANDLE_VALUE) {
232 return ConvertAndPushArg(Arg, Args, Allocator);
233 }
234
235 std::error_code ec;
236 do {
237 SmallVector<char, MAX_PATH> FileName;
238 ec = windows::UTF16ToUTF8(FileData.cFileName, wcslen(FileData.cFileName),
239 FileName);
240 if (ec)
241 break;
242
243 // Push the filename onto Dir, and remove it afterwards.
244 llvm::sys::path::append(Dir, StringRef(FileName.data(), FileName.size()));
245 AllocateAndPush(Dir, Args, Allocator);
246 Dir.resize(DirSize);
247 } while (FindNextFileW(FindHandle, &FileData));
248
249 FindClose(FindHandle);
250 return ec;
251}
252
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000253std::error_code
David Majnemer61eae2e2013-10-07 01:00:07 +0000254Process::GetArgumentVector(SmallVectorImpl<const char *> &Args,
255 ArrayRef<const char *>,
256 SpecificBumpPtrAllocator<char> &ArgAllocator) {
Hans Wennborg21f0f132014-07-16 00:52:11 +0000257 int ArgCount;
258 wchar_t **UnicodeCommandLine =
259 CommandLineToArgvW(GetCommandLineW(), &ArgCount);
David Majnemer61eae2e2013-10-07 01:00:07 +0000260 if (!UnicodeCommandLine)
261 return windows_error(::GetLastError());
262
Hans Wennborg21f0f132014-07-16 00:52:11 +0000263 Args.reserve(ArgCount);
264 std::error_code ec;
David Majnemer61eae2e2013-10-07 01:00:07 +0000265
Hans Wennborg21f0f132014-07-16 00:52:11 +0000266 for (int i = 0; i < ArgCount; ++i) {
267 ec = WildcardExpand(UnicodeCommandLine[i], Args, ArgAllocator);
David Majnemer61eae2e2013-10-07 01:00:07 +0000268 if (ec)
269 break;
David Majnemer61eae2e2013-10-07 01:00:07 +0000270 }
David Majnemer61eae2e2013-10-07 01:00:07 +0000271
Hans Wennborg21f0f132014-07-16 00:52:11 +0000272 LocalFree(UnicodeCommandLine);
273 return ec;
David Majnemer61eae2e2013-10-07 01:00:07 +0000274}
275
David Majnemer121a1742014-10-06 23:16:18 +0000276std::error_code Process::FixupStandardFileDescriptors() {
277 return std::error_code();
278}
279
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000280bool Process::StandardInIsUserInput() {
Dan Gohmane5929232009-09-11 20:46:33 +0000281 return FileDescriptorIsDisplayed(0);
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000282}
283
284bool Process::StandardOutIsDisplayed() {
Dan Gohmane5929232009-09-11 20:46:33 +0000285 return FileDescriptorIsDisplayed(1);
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000286}
287
288bool Process::StandardErrIsDisplayed() {
Dan Gohmane5929232009-09-11 20:46:33 +0000289 return FileDescriptorIsDisplayed(2);
290}
291
292bool Process::FileDescriptorIsDisplayed(int fd) {
Bill Wendling2b079652012-07-19 00:06:06 +0000293 DWORD Mode; // Unused
NAKAMURA Takumi23ebef12010-11-10 08:37:47 +0000294 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
Jeff Cohenb90c31f2005-01-01 22:54:05 +0000295}
296
Douglas Gregor15436612009-05-11 18:05:52 +0000297unsigned Process::StandardOutColumns() {
298 unsigned Columns = 0;
299 CONSOLE_SCREEN_BUFFER_INFO csbi;
300 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
301 Columns = csbi.dwSize.X;
302 return Columns;
303}
304
305unsigned Process::StandardErrColumns() {
306 unsigned Columns = 0;
307 CONSOLE_SCREEN_BUFFER_INFO csbi;
308 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
309 Columns = csbi.dwSize.X;
310 return Columns;
311}
312
Daniel Dunbar712de822012-07-20 18:29:38 +0000313// The terminal always has colors.
Benjamin Kramerdfaa0f32012-07-20 19:49:33 +0000314bool Process::FileDescriptorHasColors(int fd) {
Daniel Dunbar712de822012-07-20 18:29:38 +0000315 return FileDescriptorIsDisplayed(fd);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000316}
317
318bool Process::StandardOutHasColors() {
Daniel Dunbar712de822012-07-20 18:29:38 +0000319 return FileDescriptorHasColors(1);
320}
321
322bool Process::StandardErrHasColors() {
323 return FileDescriptorHasColors(2);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000324}
Torok Edwin63e44bb2009-06-04 08:18:25 +0000325
Nico Rieck92d649a2013-09-11 00:36:48 +0000326static bool UseANSI = false;
327void Process::UseANSIEscapeCodes(bool enable) {
328 UseANSI = enable;
329}
330
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000331namespace {
332class DefaultColors
333{
334 private:
335 WORD defaultColor;
336 public:
337 DefaultColors()
338 :defaultColor(GetCurrentColor()) {}
339 static unsigned GetCurrentColor() {
340 CONSOLE_SCREEN_BUFFER_INFO csbi;
341 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
342 return csbi.wAttributes;
343 return 0;
344 }
345 WORD operator()() const { return defaultColor; }
346};
347
348DefaultColors defaultColors;
349}
350
351bool Process::ColorNeedsFlush() {
Nico Rieck92d649a2013-09-11 00:36:48 +0000352 return !UseANSI;
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000353}
354
355const char *Process::OutputBold(bool bg) {
Nico Rieck92d649a2013-09-11 00:36:48 +0000356 if (UseANSI) return "\033[1m";
357
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000358 WORD colors = DefaultColors::GetCurrentColor();
359 if (bg)
360 colors |= BACKGROUND_INTENSITY;
361 else
362 colors |= FOREGROUND_INTENSITY;
363 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
364 return 0;
365}
366
367const char *Process::OutputColor(char code, bool bold, bool bg) {
Nico Rieck92d649a2013-09-11 00:36:48 +0000368 if (UseANSI) return colorcodes[bg?1:0][bold?1:0][code&7];
369
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000370 WORD colors;
371 if (bg) {
372 colors = ((code&1) ? BACKGROUND_RED : 0) |
373 ((code&2) ? BACKGROUND_GREEN : 0 ) |
374 ((code&4) ? BACKGROUND_BLUE : 0);
375 if (bold)
376 colors |= BACKGROUND_INTENSITY;
377 } else {
378 colors = ((code&1) ? FOREGROUND_RED : 0) |
379 ((code&2) ? FOREGROUND_GREEN : 0 ) |
380 ((code&4) ? FOREGROUND_BLUE : 0);
381 if (bold)
382 colors |= FOREGROUND_INTENSITY;
383 }
384 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
385 return 0;
386}
387
Benjamin Kramer13d16f32012-04-16 08:56:50 +0000388static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
389 CONSOLE_SCREEN_BUFFER_INFO info;
390 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
391 return info.wAttributes;
392}
393
394const char *Process::OutputReverse() {
Nico Rieck92d649a2013-09-11 00:36:48 +0000395 if (UseANSI) return "\033[7m";
396
Benjamin Kramer13d16f32012-04-16 08:56:50 +0000397 const WORD attributes
398 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
399
400 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
401 FOREGROUND_RED | FOREGROUND_INTENSITY;
402 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
403 BACKGROUND_RED | BACKGROUND_INTENSITY;
404 const WORD color_mask = foreground_mask | background_mask;
405
406 WORD new_attributes =
407 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
408 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
409 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
410 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
411 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
412 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
413 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
414 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
415 0;
416 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
417
418 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
419 return 0;
420}
421
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000422const char *Process::ResetColor() {
Nico Rieck92d649a2013-09-11 00:36:48 +0000423 if (UseANSI) return "\033[0m";
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000424 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
425 return 0;
426}
Aaron Ballman78440732014-02-04 14:49:21 +0000427
428unsigned Process::GetRandomNumber() {
Aaron Ballman3f5e8b82014-02-11 02:47:33 +0000429 HCRYPTPROV HCPC;
430 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
431 CRYPT_VERIFYCONTEXT))
Alp Toker552f2f72014-06-03 03:01:03 +0000432 report_fatal_error("Could not acquire a cryptographic context");
Aaron Ballman3f5e8b82014-02-11 02:47:33 +0000433
434 ScopedCryptContext CryptoProvider(HCPC);
435 unsigned Ret;
436 if (!::CryptGenRandom(CryptoProvider, sizeof(Ret),
437 reinterpret_cast<BYTE *>(&Ret)))
Alp Toker552f2f72014-06-03 03:01:03 +0000438 report_fatal_error("Could not generate a random number");
Aaron Ballman3f5e8b82014-02-11 02:47:33 +0000439 return Ret;
Aaron Ballman78440732014-02-04 14:49:21 +0000440}