blob: 0e8bbc80a44fa46b93c40990ec2fe51ce5f09f0e [file] [log] [blame]
Reid Spencerb88212e2004-09-15 05:49:50 +00001//===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +00002//
Reid Spencer76b83a12004-08-29 19:20:41 +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.
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +00007//
Reid Spencer76b83a12004-08-29 19:20:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Program class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "Windows.h"
Reid Spencerab97f222006-06-07 23:18:34 +000015#include <cstdio>
Reid Spencerab97f222006-06-07 23:18:34 +000016#include <fcntl.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include <io.h>
18#include <malloc.h>
Reid Spencerb88212e2004-09-15 05:49:50 +000019
Reid Spencer76b83a12004-08-29 19:20:41 +000020//===----------------------------------------------------------------------===//
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +000021//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencerb88212e2004-09-15 05:49:50 +000022//=== and must not be UNIX code
Reid Spencer76b83a12004-08-29 19:20:41 +000023//===----------------------------------------------------------------------===//
24
Mikhail Glushenkov88e98fe2009-09-22 15:40:32 +000025namespace {
26 struct Win32ProcessInfo {
27 HANDLE hProcess;
28 DWORD dwProcessId;
29 };
30}
31
Reid Spencerb88212e2004-09-15 05:49:50 +000032namespace llvm {
33using namespace sys;
34
35// This function just uses the PATH environment variable to find the program.
Rafael Espindola404ae772013-06-12 21:11:50 +000036Path sys::FindProgramByName(const std::string& progName) {
Reid Spencerb88212e2004-09-15 05:49:50 +000037 // Check some degenerate cases
38 if (progName.length() == 0) // no program
39 return Path();
40 Path temp;
Jeff Cohen215db902005-07-08 02:48:42 +000041 if (!temp.set(progName)) // invalid name
Reid Spencerb88212e2004-09-15 05:49:50 +000042 return Path();
Mikhail Glushenkova581d8a2010-11-02 20:32:39 +000043 // Return paths with slashes verbatim.
44 if (progName.find('\\') != std::string::npos ||
45 progName.find('/') != std::string::npos)
Reid Spencerb88212e2004-09-15 05:49:50 +000046 return temp;
47
Mikhail Glushenkova581d8a2010-11-02 20:32:39 +000048 // At this point, the file name is valid and does not contain slashes.
Reid Spencerb88212e2004-09-15 05:49:50 +000049 // Let Windows search for it.
50 char buffer[MAX_PATH];
51 char *dummy = NULL;
52 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
53 buffer, &dummy);
54
55 // See if it wasn't found.
56 if (len == 0)
57 return Path();
58
59 // See if we got the entire path.
60 if (len < MAX_PATH)
61 return Path(buffer);
62
63 // Buffer was too small; grow and retry.
64 while (true) {
65 char *b = reinterpret_cast<char *>(_alloca(len+1));
66 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
67
68 // It is unlikely the search failed, but it's always possible some file
69 // was added or removed since the last search, so be paranoid...
70 if (len2 == 0)
71 return Path();
72 else if (len2 <= len)
73 return Path(b);
74
75 len = len2;
76 }
77}
78
Reid Spencer42bcf6e2006-08-21 06:02:44 +000079static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
Jeff Cohen4220bf52005-02-20 02:43:04 +000080 HANDLE h;
81 if (path == 0) {
82 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
83 GetCurrentProcess(), &h,
84 0, TRUE, DUPLICATE_SAME_ACCESS);
85 return h;
86 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +000087
Matthijs Kooijman616e4842008-06-12 10:47:18 +000088 const char *fname;
89 if (path->isEmpty())
Jeff Cohen4220bf52005-02-20 02:43:04 +000090 fname = "NUL";
Matthijs Kooijman616e4842008-06-12 10:47:18 +000091 else
Chris Lattnerc521f542009-08-23 22:45:37 +000092 fname = path->c_str();
Jeff Cohen4220bf52005-02-20 02:43:04 +000093
94 SECURITY_ATTRIBUTES sa;
95 sa.nLength = sizeof(sa);
96 sa.lpSecurityDescriptor = 0;
97 sa.bInheritHandle = TRUE;
98
99 h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
Jeff Cohen18cf7eb2005-02-20 02:48:51 +0000100 &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
Jeff Cohen4220bf52005-02-20 02:43:04 +0000101 FILE_ATTRIBUTE_NORMAL, NULL);
102 if (h == INVALID_HANDLE_VALUE) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000103 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
Jeff Cohen4220bf52005-02-20 02:43:04 +0000104 (fd ? "input: " : "output: "));
105 }
Jeff Cohena531d042007-03-05 05:22:08 +0000106
Jeff Cohen4220bf52005-02-20 02:43:04 +0000107 return h;
108}
109
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000110/// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
111/// CreateProcess.
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000112static bool ArgNeedsQuotes(const char *Str) {
NAKAMURA Takumi3e600a22011-02-05 08:53:12 +0000113 return Str[0] == '\0' || strpbrk(Str, "\t \"&\'()*<>\\`^|") != 0;
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000114}
115
Reid Kleckner74679a92013-04-22 19:03:55 +0000116/// CountPrecedingBackslashes - Returns the number of backslashes preceding Cur
117/// in the C string Start.
118static unsigned int CountPrecedingBackslashes(const char *Start,
119 const char *Cur) {
120 unsigned int Count = 0;
121 --Cur;
122 while (Cur >= Start && *Cur == '\\') {
123 ++Count;
124 --Cur;
125 }
126 return Count;
127}
128
129/// EscapePrecedingEscapes - Append a backslash to Dst for every backslash
130/// preceding Cur in the Start string. Assumes Dst has enough space.
131static char *EscapePrecedingEscapes(char *Dst, const char *Start,
132 const char *Cur) {
133 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Cur);
134 while (PrecedingEscapes > 0) {
135 *Dst++ = '\\';
136 --PrecedingEscapes;
137 }
138 return Dst;
139}
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000140
141/// ArgLenWithQuotes - Check whether argument needs to be quoted when calling
142/// CreateProcess and returns length of quoted arg with escaped quotes
143static unsigned int ArgLenWithQuotes(const char *Str) {
Reid Kleckner74679a92013-04-22 19:03:55 +0000144 const char *Start = Str;
Aaron Ballmanfd86e162013-05-01 02:53:14 +0000145 bool Quoted = ArgNeedsQuotes(Str);
146 unsigned int len = Quoted ? 2 : 0;
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000147
148 while (*Str != '\0') {
Reid Kleckner74679a92013-04-22 19:03:55 +0000149 if (*Str == '\"') {
150 // We need to add a backslash, but ensure that it isn't escaped.
151 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
152 len += PrecedingEscapes + 1;
153 }
154 // Note that we *don't* need to escape runs of backslashes that don't
155 // precede a double quote! See MSDN:
156 // http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000157
158 ++len;
159 ++Str;
160 }
161
Aaron Ballmanfd86e162013-05-01 02:53:14 +0000162 if (Quoted) {
163 // Make sure the closing quote doesn't get escaped by a trailing backslash.
164 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
165 len += PrecedingEscapes + 1;
166 }
167
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000168 return len;
169}
170
Rafael Espindola404ae772013-06-12 21:11:50 +0000171}
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000172
Rafael Espindola2bb2aa22013-06-12 21:16:07 +0000173static bool Execute(void *&Data,
Rafael Espindola404ae772013-06-12 21:11:50 +0000174 const Path& path,
175 const char** args,
176 const char** envp,
177 const Path** redirects,
178 unsigned memoryLimit,
179 std::string* ErrMsg) {
180 if (Data) {
181 Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data);
Mikhail Glushenkov88e98fe2009-09-22 15:40:32 +0000182 CloseHandle(wpi->hProcess);
183 delete wpi;
Rafael Espindola404ae772013-06-12 21:11:50 +0000184 Data = 0;
Daniel Dunbar7b446a02009-09-22 04:44:56 +0000185 }
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000186
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000187 if (!path.canExecute()) {
188 if (ErrMsg)
189 *ErrMsg = "program not executable";
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000190 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000191 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000192
193 // Windows wants a command line, not an array of args, to pass to the new
194 // process. We have to concatenate them all, while quoting the args that
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000195 // have embedded spaces (or are empty).
Reid Spencerb88212e2004-09-15 05:49:50 +0000196
197 // First, determine the length of the command line.
Jeff Cohen97a41e22005-02-16 04:43:45 +0000198 unsigned len = 0;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000199 for (unsigned i = 0; args[i]; i++) {
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000200 len += ArgLenWithQuotes(args[i]) + 1;
Reid Spencerb88212e2004-09-15 05:49:50 +0000201 }
202
203 // Now build the command line.
Anton Korobeynikova4f27602008-01-24 01:20:48 +0000204 char *command = reinterpret_cast<char *>(_alloca(len+1));
Reid Spencerb88212e2004-09-15 05:49:50 +0000205 char *p = command;
206
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000207 for (unsigned i = 0; args[i]; i++) {
208 const char *arg = args[i];
Reid Kleckner74679a92013-04-22 19:03:55 +0000209 const char *start = arg;
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000210
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000211 bool needsQuoting = ArgNeedsQuotes(arg);
Reid Spencerb88212e2004-09-15 05:49:50 +0000212 if (needsQuoting)
213 *p++ = '"';
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000214
215 while (*arg != '\0') {
Reid Kleckner74679a92013-04-22 19:03:55 +0000216 if (*arg == '\"') {
217 // Escape all preceding escapes (if any), and then escape the quote.
218 p = EscapePrecedingEscapes(p, start, arg);
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000219 *p++ = '\\';
Reid Kleckner74679a92013-04-22 19:03:55 +0000220 }
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000221
222 *p++ = *arg++;
223 }
224
Reid Kleckner74679a92013-04-22 19:03:55 +0000225 if (needsQuoting) {
226 // Make sure our quote doesn't get escaped by a trailing backslash.
227 p = EscapePrecedingEscapes(p, start, arg);
Reid Spencerb88212e2004-09-15 05:49:50 +0000228 *p++ = '"';
Reid Kleckner74679a92013-04-22 19:03:55 +0000229 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000230 *p++ = ' ';
231 }
232
233 *p = 0;
234
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000235 // The pointer to the environment block for the new process.
236 char *envblock = 0;
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000237
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000238 if (envp) {
239 // An environment block consists of a null-terminated block of
240 // null-terminated strings. Convert the array of environment variables to
241 // an environment block by concatenating them.
242
243 // First, determine the length of the environment block.
244 len = 0;
245 for (unsigned i = 0; envp[i]; i++)
246 len += strlen(envp[i]) + 1;
247
248 // Now build the environment block.
249 envblock = reinterpret_cast<char *>(_alloca(len+1));
250 p = envblock;
251
252 for (unsigned i = 0; envp[i]; i++) {
253 const char *ev = envp[i];
254 size_t len = strlen(ev) + 1;
255 memcpy(p, ev, len);
256 p += len;
257 }
258
259 *p = 0;
260 }
261
Reid Spencerb88212e2004-09-15 05:49:50 +0000262 // Create a child process.
263 STARTUPINFO si;
264 memset(&si, 0, sizeof(si));
265 si.cb = sizeof(si);
Jeff Cohen4220bf52005-02-20 02:43:04 +0000266 si.hStdInput = INVALID_HANDLE_VALUE;
267 si.hStdOutput = INVALID_HANDLE_VALUE;
268 si.hStdError = INVALID_HANDLE_VALUE;
Reid Spencerb88212e2004-09-15 05:49:50 +0000269
Jeff Cohen4220bf52005-02-20 02:43:04 +0000270 if (redirects) {
271 si.dwFlags = STARTF_USESTDHANDLES;
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000272
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000273 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
274 if (si.hStdInput == INVALID_HANDLE_VALUE) {
275 MakeErrMsg(ErrMsg, "can't redirect stdin");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000276 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000277 }
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +0000278 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000279 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
Jeff Cohen4220bf52005-02-20 02:43:04 +0000280 CloseHandle(si.hStdInput);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000281 MakeErrMsg(ErrMsg, "can't redirect stdout");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000282 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000283 }
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000284 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
285 // If stdout and stderr should go to the same place, redirect stderr
286 // to the handle already open for stdout.
287 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
288 GetCurrentProcess(), &si.hStdError,
289 0, TRUE, DUPLICATE_SAME_ACCESS);
290 } else {
291 // Just redirect stderr
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +0000292 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000293 if (si.hStdError == INVALID_HANDLE_VALUE) {
294 CloseHandle(si.hStdInput);
295 CloseHandle(si.hStdOutput);
296 MakeErrMsg(ErrMsg, "can't redirect stderr");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000297 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000298 }
Jeff Cohen4220bf52005-02-20 02:43:04 +0000299 }
300 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000301
Reid Spencerb88212e2004-09-15 05:49:50 +0000302 PROCESS_INFORMATION pi;
303 memset(&pi, 0, sizeof(pi));
304
Jeff Cohen4220bf52005-02-20 02:43:04 +0000305 fflush(stdout);
306 fflush(stderr);
Mikhail Glushenkov60cde5b2009-04-14 21:31:36 +0000307 BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, TRUE, 0,
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000308 envblock, NULL, &si, &pi);
Jeff Cohen4220bf52005-02-20 02:43:04 +0000309 DWORD err = GetLastError();
310
311 // Regardless of whether the process got created or not, we are done with
312 // the handles we created for it to inherit.
313 CloseHandle(si.hStdInput);
314 CloseHandle(si.hStdOutput);
315 CloseHandle(si.hStdError);
316
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000317 // Now return an error if the process didn't get created.
Chris Lattnerc521f542009-08-23 22:45:37 +0000318 if (!rc) {
Jeff Cohen4220bf52005-02-20 02:43:04 +0000319 SetLastError(err);
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000320 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
Chris Lattnerc521f542009-08-23 22:45:37 +0000321 path.str() + "'");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000322 return false;
Reid Spencerb88212e2004-09-15 05:49:50 +0000323 }
Mikhail Glushenkov88e98fe2009-09-22 15:40:32 +0000324 Win32ProcessInfo* wpi = new Win32ProcessInfo;
325 wpi->hProcess = pi.hProcess;
326 wpi->dwProcessId = pi.dwProcessId;
Rafael Espindola404ae772013-06-12 21:11:50 +0000327 Data = wpi;
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000328
Jeff Cohena531d042007-03-05 05:22:08 +0000329 // Make sure these get closed no matter what.
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000330 ScopedCommonHandle hThread(pi.hThread);
Jeff Cohena531d042007-03-05 05:22:08 +0000331
332 // Assign the process to a job if a memory limit is defined.
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000333 ScopedJobHandle hJob;
Jeff Cohena531d042007-03-05 05:22:08 +0000334 if (memoryLimit != 0) {
335 hJob = CreateJobObject(0, 0);
336 bool success = false;
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000337 if (hJob) {
Jeff Cohena531d042007-03-05 05:22:08 +0000338 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
339 memset(&jeli, 0, sizeof(jeli));
340 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
Jeff Cohen7157fe32007-03-05 05:45:08 +0000341 jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576;
Jeff Cohena531d042007-03-05 05:22:08 +0000342 if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
343 &jeli, sizeof(jeli))) {
344 if (AssignProcessToJobObject(hJob, pi.hProcess))
345 success = true;
346 }
347 }
348 if (!success) {
349 SetLastError(GetLastError());
350 MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
351 TerminateProcess(pi.hProcess, 1);
352 WaitForSingleObject(pi.hProcess, INFINITE);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000353 return false;
Jeff Cohena531d042007-03-05 05:22:08 +0000354 }
355 }
356
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000357 return true;
358}
359
Rafael Espindola404ae772013-06-12 21:11:50 +0000360static int WaitAux(void *&Data, const Path &path,
361 unsigned secondsToWait,
362 std::string* ErrMsg) {
363 if (Data == 0) {
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000364 MakeErrMsg(ErrMsg, "Process not started!");
365 return -1;
366 }
367
Rafael Espindola404ae772013-06-12 21:11:50 +0000368 Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data);
Mikhail Glushenkov88e98fe2009-09-22 15:40:32 +0000369 HANDLE hProcess = wpi->hProcess;
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000370
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000371 // Wait for the process to terminate.
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000372 DWORD millisecondsToWait = INFINITE;
373 if (secondsToWait > 0)
374 millisecondsToWait = secondsToWait * 1000;
375
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000376 if (WaitForSingleObject(hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
377 if (!TerminateProcess(hProcess, 1)) {
378 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program.");
Andrew Trickd5d07642011-05-21 00:56:46 +0000379 // -2 indicates a crash or timeout as opposed to failure to execute.
380 return -2;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000381 }
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000382 WaitForSingleObject(hProcess, INFINITE);
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000383 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000384
Reid Spencerb88212e2004-09-15 05:49:50 +0000385 // Get its exit status.
386 DWORD status;
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000387 BOOL rc = GetExitCodeProcess(hProcess, &status);
388 DWORD err = GetLastError();
Reid Spencerb88212e2004-09-15 05:49:50 +0000389
Jeff Cohen4220bf52005-02-20 02:43:04 +0000390 if (!rc) {
391 SetLastError(err);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000392 MakeErrMsg(ErrMsg, "Failed getting status for program.");
Andrew Trickd5d07642011-05-21 00:56:46 +0000393 // -2 indicates a crash or timeout as opposed to failure to execute.
394 return -2;
Jeff Cohen4220bf52005-02-20 02:43:04 +0000395 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000396
NAKAMURA Takumi64404a32011-11-29 07:47:04 +0000397 if (!status)
398 return 0;
399
400 // Pass 10(Warning) and 11(Error) to the callee as negative value.
401 if ((status & 0xBFFF0000U) == 0x80000000U)
402 return (int)status;
403
404 if (status & 0xFF)
405 return status & 0x7FFFFFFF;
406
407 return 1;
Reid Spencerb88212e2004-09-15 05:49:50 +0000408}
409
Rafael Espindola404ae772013-06-12 21:11:50 +0000410static int Wait(void *&Data, const Path &path,
411 unsigned secondsToWait,
412 std::string* ErrMsg) {
413 int Ret = WaitAux(Data, path, secondsToWait, ErrMsg);
414
415 Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data);
416 CloseHandle(wpi->hProcess);
417 delete wpi;
418 Data = 0;
419
420 return Ret;
421}
422
423namespace llvm {
Rafael Espindola0c24b202013-06-12 21:25:04 +0000424error_code sys::ChangeStdinToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000425 int result = _setmode( _fileno(stdin), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000426 if (result == -1)
427 return error_code(errno, generic_category());
428 return make_error_code(errc::success);
Reid Spencerab97f222006-06-07 23:18:34 +0000429}
430
Rafael Espindola0c24b202013-06-12 21:25:04 +0000431error_code sys::ChangeStdoutToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000432 int result = _setmode( _fileno(stdout), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000433 if (result == -1)
434 return error_code(errno, generic_category());
435 return make_error_code(errc::success);
Reid Spencerab97f222006-06-07 23:18:34 +0000436}
437
Rafael Espindola0c24b202013-06-12 21:25:04 +0000438error_code sys::ChangeStderrToBinary(){
Douglas Gregor10519372010-01-28 06:42:08 +0000439 int result = _setmode( _fileno(stderr), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000440 if (result == -1)
441 return error_code(errno, generic_category());
442 return make_error_code(errc::success);
Douglas Gregor10519372010-01-28 06:42:08 +0000443}
444
Rafael Espindolacd848c02013-04-11 14:06:34 +0000445bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) {
446 // The documented max length of the command line passed to CreateProcess.
447 static const size_t MaxCommandStringLength = 32768;
448 size_t ArgLength = 0;
449 for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
450 I != E; ++I) {
451 // Account for the trailing space for every arg but the last one and the
452 // trailing NULL of the last argument.
453 ArgLength += ArgLenWithQuotes(*I) + 1;
454 if (ArgLength > MaxCommandStringLength) {
455 return false;
456 }
457 }
458 return true;
459}
460
Reid Spencerb88212e2004-09-15 05:49:50 +0000461}