blob: a368407960c4bac4c10c55f3d35ecf209d9bc525 [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"
Rafael Espindolab0a5c962013-06-14 19:38:45 +000015#include "llvm/Support/FileSystem.h"
Reid Spencerab97f222006-06-07 23:18:34 +000016#include <cstdio>
Reid Spencerab97f222006-06-07 23:18:34 +000017#include <fcntl.h>
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include <io.h>
19#include <malloc.h>
Reid Spencerb88212e2004-09-15 05:49:50 +000020
Reid Spencer76b83a12004-08-29 19:20:41 +000021//===----------------------------------------------------------------------===//
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +000022//=== WARNING: Implementation here must contain only Win32 specific code
Reid Spencerb88212e2004-09-15 05:49:50 +000023//=== and must not be UNIX code
Reid Spencer76b83a12004-08-29 19:20:41 +000024//===----------------------------------------------------------------------===//
25
Mikhail Glushenkov88e98fe2009-09-22 15:40:32 +000026namespace {
27 struct Win32ProcessInfo {
28 HANDLE hProcess;
29 DWORD dwProcessId;
30 };
31}
32
Reid Spencerb88212e2004-09-15 05:49:50 +000033namespace llvm {
34using namespace sys;
35
36// This function just uses the PATH environment variable to find the program.
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000037std::string sys::FindProgramByName(const std::string &progName) {
Reid Spencerb88212e2004-09-15 05:49:50 +000038 // Check some degenerate cases
39 if (progName.length() == 0) // no program
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000040 return "";
Rafael Espindolab0a5c962013-06-14 19:38:45 +000041 std::string temp = progName;
Mikhail Glushenkova581d8a2010-11-02 20:32:39 +000042 // Return paths with slashes verbatim.
43 if (progName.find('\\') != std::string::npos ||
44 progName.find('/') != std::string::npos)
Rafael Espindolab0a5c962013-06-14 19:38:45 +000045 return temp;
Reid Spencerb88212e2004-09-15 05:49:50 +000046
Mikhail Glushenkova581d8a2010-11-02 20:32:39 +000047 // At this point, the file name is valid and does not contain slashes.
Reid Spencerb88212e2004-09-15 05:49:50 +000048 // Let Windows search for it.
49 char buffer[MAX_PATH];
50 char *dummy = NULL;
51 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
52 buffer, &dummy);
53
54 // See if it wasn't found.
55 if (len == 0)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000056 return "";
Reid Spencerb88212e2004-09-15 05:49:50 +000057
58 // See if we got the entire path.
59 if (len < MAX_PATH)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000060 return std::string(buffer);
Reid Spencerb88212e2004-09-15 05:49:50 +000061
62 // Buffer was too small; grow and retry.
63 while (true) {
64 char *b = reinterpret_cast<char *>(_alloca(len+1));
65 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
66
67 // It is unlikely the search failed, but it's always possible some file
68 // was added or removed since the last search, so be paranoid...
69 if (len2 == 0)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000070 return "";
Reid Spencerb88212e2004-09-15 05:49:50 +000071 else if (len2 <= len)
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +000072 return std::string(b);
Reid Spencerb88212e2004-09-15 05:49:50 +000073
74 len = len2;
75 }
76}
77
Rafael Espindolab0a5c962013-06-14 19:38:45 +000078static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
Jeff Cohen4220bf52005-02-20 02:43:04 +000079 HANDLE h;
80 if (path == 0) {
81 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
82 GetCurrentProcess(), &h,
83 0, TRUE, DUPLICATE_SAME_ACCESS);
84 return h;
85 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +000086
Rafael Espindolab0a5c962013-06-14 19:38:45 +000087 std::string fname;
88 if (path->empty())
Jeff Cohen4220bf52005-02-20 02:43:04 +000089 fname = "NUL";
Matthijs Kooijman616e4842008-06-12 10:47:18 +000090 else
Rafael Espindolab0a5c962013-06-14 19:38:45 +000091 fname = *path;
Jeff Cohen4220bf52005-02-20 02:43:04 +000092
93 SECURITY_ATTRIBUTES sa;
94 sa.nLength = sizeof(sa);
95 sa.lpSecurityDescriptor = 0;
96 sa.bInheritHandle = TRUE;
97
Rafael Espindolab0a5c962013-06-14 19:38:45 +000098 h = CreateFile(fname.c_str(), fd ? GENERIC_WRITE : GENERIC_READ,
99 FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
Jeff Cohen4220bf52005-02-20 02:43:04 +0000100 FILE_ATTRIBUTE_NORMAL, NULL);
101 if (h == INVALID_HANDLE_VALUE) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000102 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
Jeff Cohen4220bf52005-02-20 02:43:04 +0000103 (fd ? "input: " : "output: "));
104 }
Jeff Cohena531d042007-03-05 05:22:08 +0000105
Jeff Cohen4220bf52005-02-20 02:43:04 +0000106 return h;
107}
108
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000109/// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
110/// CreateProcess.
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000111static bool ArgNeedsQuotes(const char *Str) {
NAKAMURA Takumi3e600a22011-02-05 08:53:12 +0000112 return Str[0] == '\0' || strpbrk(Str, "\t \"&\'()*<>\\`^|") != 0;
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000113}
114
Reid Kleckner74679a92013-04-22 19:03:55 +0000115/// CountPrecedingBackslashes - Returns the number of backslashes preceding Cur
116/// in the C string Start.
117static unsigned int CountPrecedingBackslashes(const char *Start,
118 const char *Cur) {
119 unsigned int Count = 0;
120 --Cur;
121 while (Cur >= Start && *Cur == '\\') {
122 ++Count;
123 --Cur;
124 }
125 return Count;
126}
127
128/// EscapePrecedingEscapes - Append a backslash to Dst for every backslash
129/// preceding Cur in the Start string. Assumes Dst has enough space.
130static char *EscapePrecedingEscapes(char *Dst, const char *Start,
131 const char *Cur) {
132 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Cur);
133 while (PrecedingEscapes > 0) {
134 *Dst++ = '\\';
135 --PrecedingEscapes;
136 }
137 return Dst;
138}
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000139
140/// ArgLenWithQuotes - Check whether argument needs to be quoted when calling
141/// CreateProcess and returns length of quoted arg with escaped quotes
142static unsigned int ArgLenWithQuotes(const char *Str) {
Reid Kleckner74679a92013-04-22 19:03:55 +0000143 const char *Start = Str;
Aaron Ballmanfd86e162013-05-01 02:53:14 +0000144 bool Quoted = ArgNeedsQuotes(Str);
145 unsigned int len = Quoted ? 2 : 0;
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000146
147 while (*Str != '\0') {
Reid Kleckner74679a92013-04-22 19:03:55 +0000148 if (*Str == '\"') {
149 // We need to add a backslash, but ensure that it isn't escaped.
150 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
151 len += PrecedingEscapes + 1;
152 }
153 // Note that we *don't* need to escape runs of backslashes that don't
154 // precede a double quote! See MSDN:
155 // http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000156
157 ++len;
158 ++Str;
159 }
160
Aaron Ballmanfd86e162013-05-01 02:53:14 +0000161 if (Quoted) {
162 // Make sure the closing quote doesn't get escaped by a trailing backslash.
163 unsigned PrecedingEscapes = CountPrecedingBackslashes(Start, Str);
164 len += PrecedingEscapes + 1;
165 }
166
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000167 return len;
168}
169
Rafael Espindola404ae772013-06-12 21:11:50 +0000170}
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000171
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000172static bool Execute(void **Data,
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000173 StringRef Program,
Rafael Espindola404ae772013-06-12 21:11:50 +0000174 const char** args,
175 const char** envp,
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000176 const StringRef** redirects,
Rafael Espindola404ae772013-06-12 21:11:50 +0000177 unsigned memoryLimit,
178 std::string* ErrMsg) {
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000179 if (!sys::fs::can_execute(Program)) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000180 if (ErrMsg)
181 *ErrMsg = "program not executable";
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000182 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000183 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000184
185 // Windows wants a command line, not an array of args, to pass to the new
186 // process. We have to concatenate them all, while quoting the args that
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000187 // have embedded spaces (or are empty).
Reid Spencerb88212e2004-09-15 05:49:50 +0000188
189 // First, determine the length of the command line.
Jeff Cohen97a41e22005-02-16 04:43:45 +0000190 unsigned len = 0;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000191 for (unsigned i = 0; args[i]; i++) {
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000192 len += ArgLenWithQuotes(args[i]) + 1;
Reid Spencerb88212e2004-09-15 05:49:50 +0000193 }
194
195 // Now build the command line.
Anton Korobeynikova4f27602008-01-24 01:20:48 +0000196 char *command = reinterpret_cast<char *>(_alloca(len+1));
Reid Spencerb88212e2004-09-15 05:49:50 +0000197 char *p = command;
198
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000199 for (unsigned i = 0; args[i]; i++) {
200 const char *arg = args[i];
Reid Kleckner74679a92013-04-22 19:03:55 +0000201 const char *start = arg;
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000202
Daniel Dunbar381b89d2009-08-02 20:41:09 +0000203 bool needsQuoting = ArgNeedsQuotes(arg);
Reid Spencerb88212e2004-09-15 05:49:50 +0000204 if (needsQuoting)
205 *p++ = '"';
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000206
207 while (*arg != '\0') {
Reid Kleckner74679a92013-04-22 19:03:55 +0000208 if (*arg == '\"') {
209 // Escape all preceding escapes (if any), and then escape the quote.
210 p = EscapePrecedingEscapes(p, start, arg);
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000211 *p++ = '\\';
Reid Kleckner74679a92013-04-22 19:03:55 +0000212 }
Anton Korobeynikovc2747d02010-03-28 15:07:02 +0000213
214 *p++ = *arg++;
215 }
216
Reid Kleckner74679a92013-04-22 19:03:55 +0000217 if (needsQuoting) {
218 // Make sure our quote doesn't get escaped by a trailing backslash.
219 p = EscapePrecedingEscapes(p, start, arg);
Reid Spencerb88212e2004-09-15 05:49:50 +0000220 *p++ = '"';
Reid Kleckner74679a92013-04-22 19:03:55 +0000221 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000222 *p++ = ' ';
223 }
224
225 *p = 0;
226
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000227 // The pointer to the environment block for the new process.
228 char *envblock = 0;
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000229
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000230 if (envp) {
231 // An environment block consists of a null-terminated block of
232 // null-terminated strings. Convert the array of environment variables to
233 // an environment block by concatenating them.
234
235 // First, determine the length of the environment block.
236 len = 0;
237 for (unsigned i = 0; envp[i]; i++)
238 len += strlen(envp[i]) + 1;
239
240 // Now build the environment block.
241 envblock = reinterpret_cast<char *>(_alloca(len+1));
242 p = envblock;
243
244 for (unsigned i = 0; envp[i]; i++) {
245 const char *ev = envp[i];
246 size_t len = strlen(ev) + 1;
247 memcpy(p, ev, len);
248 p += len;
249 }
250
251 *p = 0;
252 }
253
Reid Spencerb88212e2004-09-15 05:49:50 +0000254 // Create a child process.
255 STARTUPINFO si;
256 memset(&si, 0, sizeof(si));
257 si.cb = sizeof(si);
Jeff Cohen4220bf52005-02-20 02:43:04 +0000258 si.hStdInput = INVALID_HANDLE_VALUE;
259 si.hStdOutput = INVALID_HANDLE_VALUE;
260 si.hStdError = INVALID_HANDLE_VALUE;
Reid Spencerb88212e2004-09-15 05:49:50 +0000261
Jeff Cohen4220bf52005-02-20 02:43:04 +0000262 if (redirects) {
263 si.dwFlags = STARTF_USESTDHANDLES;
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000264
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000265 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
266 if (si.hStdInput == INVALID_HANDLE_VALUE) {
267 MakeErrMsg(ErrMsg, "can't redirect stdin");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000268 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000269 }
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +0000270 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000271 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
Jeff Cohen4220bf52005-02-20 02:43:04 +0000272 CloseHandle(si.hStdInput);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000273 MakeErrMsg(ErrMsg, "can't redirect stdout");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000274 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000275 }
Matthijs Kooijman1cc695e2008-06-12 12:53:35 +0000276 if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) {
277 // If stdout and stderr should go to the same place, redirect stderr
278 // to the handle already open for stdout.
279 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
280 GetCurrentProcess(), &si.hStdError,
281 0, TRUE, DUPLICATE_SAME_ACCESS);
282 } else {
283 // Just redirect stderr
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +0000284 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000285 if (si.hStdError == INVALID_HANDLE_VALUE) {
286 CloseHandle(si.hStdInput);
287 CloseHandle(si.hStdOutput);
288 MakeErrMsg(ErrMsg, "can't redirect stderr");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000289 return false;
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000290 }
Jeff Cohen4220bf52005-02-20 02:43:04 +0000291 }
292 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000293
Reid Spencerb88212e2004-09-15 05:49:50 +0000294 PROCESS_INFORMATION pi;
295 memset(&pi, 0, sizeof(pi));
296
Jeff Cohen4220bf52005-02-20 02:43:04 +0000297 fflush(stdout);
298 fflush(stderr);
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000299 std::string ProgramStr = Program;
300 BOOL rc = CreateProcess(ProgramStr.c_str(), command, NULL, NULL, TRUE, 0,
Argyrios Kyrtzidis24f99982008-06-15 03:54:39 +0000301 envblock, NULL, &si, &pi);
Jeff Cohen4220bf52005-02-20 02:43:04 +0000302 DWORD err = GetLastError();
303
304 // Regardless of whether the process got created or not, we are done with
305 // the handles we created for it to inherit.
306 CloseHandle(si.hStdInput);
307 CloseHandle(si.hStdOutput);
308 CloseHandle(si.hStdError);
309
Reid Spencer42bcf6e2006-08-21 06:02:44 +0000310 // Now return an error if the process didn't get created.
Chris Lattnerc521f542009-08-23 22:45:37 +0000311 if (!rc) {
Jeff Cohen4220bf52005-02-20 02:43:04 +0000312 SetLastError(err);
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000313 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000314 ProgramStr + "'");
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000315 return false;
Reid Spencerb88212e2004-09-15 05:49:50 +0000316 }
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000317 if (Data) {
318 Win32ProcessInfo* wpi = new Win32ProcessInfo;
319 wpi->hProcess = pi.hProcess;
320 wpi->dwProcessId = pi.dwProcessId;
321 *Data = wpi;
322 }
Mikhail Glushenkov4a91b762009-09-08 19:50:27 +0000323
Jeff Cohena531d042007-03-05 05:22:08 +0000324 // Make sure these get closed no matter what.
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000325 ScopedCommonHandle hThread(pi.hThread);
Jeff Cohena531d042007-03-05 05:22:08 +0000326
327 // Assign the process to a job if a memory limit is defined.
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000328 ScopedJobHandle hJob;
Jeff Cohena531d042007-03-05 05:22:08 +0000329 if (memoryLimit != 0) {
330 hJob = CreateJobObject(0, 0);
331 bool success = false;
Michael J. Spencer513f1b62011-12-12 06:03:33 +0000332 if (hJob) {
Jeff Cohena531d042007-03-05 05:22:08 +0000333 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
334 memset(&jeli, 0, sizeof(jeli));
335 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
Jeff Cohen7157fe32007-03-05 05:45:08 +0000336 jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576;
Jeff Cohena531d042007-03-05 05:22:08 +0000337 if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
338 &jeli, sizeof(jeli))) {
339 if (AssignProcessToJobObject(hJob, pi.hProcess))
340 success = true;
341 }
342 }
343 if (!success) {
344 SetLastError(GetLastError());
345 MakeErrMsg(ErrMsg, std::string("Unable to set memory limit"));
346 TerminateProcess(pi.hProcess, 1);
347 WaitForSingleObject(pi.hProcess, INFINITE);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000348 return false;
Jeff Cohena531d042007-03-05 05:22:08 +0000349 }
350 }
351
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000352 // Don't leak the handle if the caller doesn't want it.
353 if (!Data)
354 CloseHandle(pi.hProcess);
355
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000356 return true;
357}
358
Rafael Espindolaf7c67012013-06-14 18:12:13 +0000359static int WaitAux(Win32ProcessInfo *wpi, unsigned secondsToWait,
360 std::string *ErrMsg) {
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000361 // Wait for the process to terminate.
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000362 HANDLE hProcess = wpi->hProcess;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000363 DWORD millisecondsToWait = INFINITE;
364 if (secondsToWait > 0)
365 millisecondsToWait = secondsToWait * 1000;
366
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000367 if (WaitForSingleObject(hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
368 if (!TerminateProcess(hProcess, 1)) {
369 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program.");
Andrew Trickd5d07642011-05-21 00:56:46 +0000370 // -2 indicates a crash or timeout as opposed to failure to execute.
371 return -2;
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000372 }
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000373 WaitForSingleObject(hProcess, INFINITE);
Jeff Cohen7ae0bc72004-12-20 03:24:56 +0000374 }
Mikhail Glushenkov3a62efb2009-04-14 21:31:14 +0000375
Reid Spencerb88212e2004-09-15 05:49:50 +0000376 // Get its exit status.
377 DWORD status;
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000378 BOOL rc = GetExitCodeProcess(hProcess, &status);
379 DWORD err = GetLastError();
Reid Spencerb88212e2004-09-15 05:49:50 +0000380
Jeff Cohen4220bf52005-02-20 02:43:04 +0000381 if (!rc) {
382 SetLastError(err);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +0000383 MakeErrMsg(ErrMsg, "Failed getting status for program.");
Andrew Trickd5d07642011-05-21 00:56:46 +0000384 // -2 indicates a crash or timeout as opposed to failure to execute.
385 return -2;
Jeff Cohen4220bf52005-02-20 02:43:04 +0000386 }
Reid Spencerb88212e2004-09-15 05:49:50 +0000387
NAKAMURA Takumi64404a32011-11-29 07:47:04 +0000388 if (!status)
389 return 0;
390
391 // Pass 10(Warning) and 11(Error) to the callee as negative value.
392 if ((status & 0xBFFF0000U) == 0x80000000U)
393 return (int)status;
394
395 if (status & 0xFF)
396 return status & 0x7FFFFFFF;
397
398 return 1;
Reid Spencerb88212e2004-09-15 05:49:50 +0000399}
400
Rafael Espindolab0a5c962013-06-14 19:38:45 +0000401static int Wait(void *&Data, StringRef Program, unsigned secondsToWait,
Reid Kleckner6e6a0f52013-06-13 15:27:17 +0000402 std::string *ErrMsg) {
403 Win32ProcessInfo *wpi = reinterpret_cast<Win32ProcessInfo *>(Data);
Rafael Espindolaf7c67012013-06-14 18:12:13 +0000404 int Ret = WaitAux(wpi, secondsToWait, ErrMsg);
Rafael Espindola404ae772013-06-12 21:11:50 +0000405
Rafael Espindola404ae772013-06-12 21:11:50 +0000406 CloseHandle(wpi->hProcess);
407 delete wpi;
408 Data = 0;
409
410 return Ret;
411}
412
413namespace llvm {
Rafael Espindola0c24b202013-06-12 21:25:04 +0000414error_code sys::ChangeStdinToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000415 int result = _setmode( _fileno(stdin), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000416 if (result == -1)
417 return error_code(errno, generic_category());
418 return make_error_code(errc::success);
Reid Spencerab97f222006-06-07 23:18:34 +0000419}
420
Rafael Espindola0c24b202013-06-12 21:25:04 +0000421error_code sys::ChangeStdoutToBinary(){
Reid Spencerab97f222006-06-07 23:18:34 +0000422 int result = _setmode( _fileno(stdout), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000423 if (result == -1)
424 return error_code(errno, generic_category());
425 return make_error_code(errc::success);
Reid Spencerab97f222006-06-07 23:18:34 +0000426}
427
Rafael Espindola0c24b202013-06-12 21:25:04 +0000428error_code sys::ChangeStderrToBinary(){
Douglas Gregor10519372010-01-28 06:42:08 +0000429 int result = _setmode( _fileno(stderr), _O_BINARY );
Michael J. Spencera2755f82011-12-13 23:16:49 +0000430 if (result == -1)
431 return error_code(errno, generic_category());
432 return make_error_code(errc::success);
Douglas Gregor10519372010-01-28 06:42:08 +0000433}
434
Rafael Espindolacd848c02013-04-11 14:06:34 +0000435bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef<const char*> Args) {
436 // The documented max length of the command line passed to CreateProcess.
437 static const size_t MaxCommandStringLength = 32768;
438 size_t ArgLength = 0;
439 for (ArrayRef<const char*>::iterator I = Args.begin(), E = Args.end();
440 I != E; ++I) {
441 // Account for the trailing space for every arg but the last one and the
442 // trailing NULL of the last argument.
443 ArgLength += ArgLenWithQuotes(*I) + 1;
444 if (ArgLength > MaxCommandStringLength) {
445 return false;
446 }
447 }
448 return true;
449}
450
Reid Spencerb88212e2004-09-15 05:49:50 +0000451}