Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 1 | //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===// |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Program class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 14 | #include "Win32.h" |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 15 | #include <cstdio> |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 16 | #include <malloc.h> |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 17 | #include <io.h> |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 21 | //=== WARNING: Implementation here must contain only Win32 specific code |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 22 | //=== and must not be UNIX code |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
NAKAMURA Takumi | 4cec6e2 | 2010-09-17 11:14:18 +0000 | [diff] [blame] | 25 | #ifdef __MINGW32__ |
| 26 | // Ancient mingw32's w32api might not have this declaration. |
| 27 | extern "C" |
| 28 | BOOL WINAPI SetInformationJobObject(HANDLE hJob, |
| 29 | JOBOBJECTINFOCLASS JobObjectInfoClass, |
| 30 | LPVOID lpJobObjectInfo, |
| 31 | DWORD cbJobObjectInfoLength); |
| 32 | #endif |
| 33 | |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | struct Win32ProcessInfo { |
| 36 | HANDLE hProcess; |
| 37 | DWORD dwProcessId; |
| 38 | }; |
| 39 | } |
| 40 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 41 | namespace llvm { |
| 42 | using namespace sys; |
| 43 | |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 44 | Program::Program() : Data_(0) {} |
| 45 | |
| 46 | Program::~Program() { |
| 47 | if (Data_) { |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 48 | Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data_); |
| 49 | CloseHandle(wpi->hProcess); |
| 50 | delete wpi; |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 51 | Data_ = 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | unsigned Program::GetPid() const { |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 56 | Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data_); |
| 57 | return wpi->dwProcessId; |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 60 | // This function just uses the PATH environment variable to find the program. |
| 61 | Path |
| 62 | Program::FindProgramByName(const std::string& progName) { |
| 63 | |
| 64 | // Check some degenerate cases |
| 65 | if (progName.length() == 0) // no program |
| 66 | return Path(); |
| 67 | Path temp; |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 68 | if (!temp.set(progName)) // invalid name |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 69 | return Path(); |
Jeff Cohen | edb9d6b | 2005-07-08 02:48:42 +0000 | [diff] [blame] | 70 | if (temp.canExecute()) // already executable as is |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 71 | return temp; |
| 72 | |
| 73 | // At this point, the file name is valid and its not executable. |
| 74 | // Let Windows search for it. |
| 75 | char buffer[MAX_PATH]; |
| 76 | char *dummy = NULL; |
| 77 | DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH, |
| 78 | buffer, &dummy); |
| 79 | |
| 80 | // See if it wasn't found. |
| 81 | if (len == 0) |
| 82 | return Path(); |
| 83 | |
| 84 | // See if we got the entire path. |
| 85 | if (len < MAX_PATH) |
| 86 | return Path(buffer); |
| 87 | |
| 88 | // Buffer was too small; grow and retry. |
| 89 | while (true) { |
| 90 | char *b = reinterpret_cast<char *>(_alloca(len+1)); |
| 91 | DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy); |
| 92 | |
| 93 | // It is unlikely the search failed, but it's always possible some file |
| 94 | // was added or removed since the last search, so be paranoid... |
| 95 | if (len2 == 0) |
| 96 | return Path(); |
| 97 | else if (len2 <= len) |
| 98 | return Path(b); |
| 99 | |
| 100 | len = len2; |
| 101 | } |
| 102 | } |
| 103 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 104 | static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) { |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 105 | HANDLE h; |
| 106 | if (path == 0) { |
| 107 | DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd), |
| 108 | GetCurrentProcess(), &h, |
| 109 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 110 | return h; |
| 111 | } |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 112 | |
Matthijs Kooijman | 905261e | 2008-06-12 10:47:18 +0000 | [diff] [blame] | 113 | const char *fname; |
| 114 | if (path->isEmpty()) |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 115 | fname = "NUL"; |
Matthijs Kooijman | 905261e | 2008-06-12 10:47:18 +0000 | [diff] [blame] | 116 | else |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 117 | fname = path->c_str(); |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 118 | |
| 119 | SECURITY_ATTRIBUTES sa; |
| 120 | sa.nLength = sizeof(sa); |
| 121 | sa.lpSecurityDescriptor = 0; |
| 122 | sa.bInheritHandle = TRUE; |
| 123 | |
| 124 | h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ, |
Jeff Cohen | 45a1b26 | 2005-02-20 02:48:51 +0000 | [diff] [blame] | 125 | &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS, |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 126 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 127 | if (h == INVALID_HANDLE_VALUE) { |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 128 | MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " + |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 129 | (fd ? "input: " : "output: ")); |
| 130 | } |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 131 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 132 | return h; |
| 133 | } |
| 134 | |
Daniel Dunbar | 9a0b574 | 2009-08-02 20:41:09 +0000 | [diff] [blame] | 135 | /// ArgNeedsQuotes - Check whether argument needs to be quoted when calling |
| 136 | /// CreateProcess. |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 137 | static bool ArgNeedsQuotes(const char *Str) { |
Daniel Dunbar | 9a0b574 | 2009-08-02 20:41:09 +0000 | [diff] [blame] | 138 | return Str[0] == '\0' || strchr(Str, ' ') != 0; |
| 139 | } |
| 140 | |
Anton Korobeynikov | 8124429 | 2010-03-28 15:07:02 +0000 | [diff] [blame] | 141 | |
| 142 | /// ArgLenWithQuotes - Check whether argument needs to be quoted when calling |
| 143 | /// CreateProcess and returns length of quoted arg with escaped quotes |
| 144 | static unsigned int ArgLenWithQuotes(const char *Str) { |
| 145 | unsigned int len = ArgNeedsQuotes(Str) ? 2 : 0; |
| 146 | |
| 147 | while (*Str != '\0') { |
| 148 | if (*Str == '\"') |
| 149 | ++len; |
| 150 | |
| 151 | ++len; |
| 152 | ++Str; |
| 153 | } |
| 154 | |
| 155 | return len; |
| 156 | } |
| 157 | |
| 158 | |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 159 | bool |
| 160 | Program::Execute(const Path& path, |
| 161 | const char** args, |
| 162 | const char** envp, |
| 163 | const Path** redirects, |
| 164 | unsigned memoryLimit, |
| 165 | std::string* ErrMsg) { |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 166 | if (Data_) { |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 167 | Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data_); |
| 168 | CloseHandle(wpi->hProcess); |
| 169 | delete wpi; |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 170 | Data_ = 0; |
| 171 | } |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 172 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 173 | if (!path.canExecute()) { |
| 174 | if (ErrMsg) |
| 175 | *ErrMsg = "program not executable"; |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 176 | return false; |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 177 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 178 | |
| 179 | // Windows wants a command line, not an array of args, to pass to the new |
| 180 | // process. We have to concatenate them all, while quoting the args that |
Daniel Dunbar | 9a0b574 | 2009-08-02 20:41:09 +0000 | [diff] [blame] | 181 | // have embedded spaces (or are empty). |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 182 | |
| 183 | // First, determine the length of the command line. |
Jeff Cohen | e5f7e65 | 2005-02-16 04:43:45 +0000 | [diff] [blame] | 184 | unsigned len = 0; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 185 | for (unsigned i = 0; args[i]; i++) { |
Anton Korobeynikov | 8124429 | 2010-03-28 15:07:02 +0000 | [diff] [blame] | 186 | len += ArgLenWithQuotes(args[i]) + 1; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Now build the command line. |
Anton Korobeynikov | 58ea52a | 2008-01-24 01:20:48 +0000 | [diff] [blame] | 190 | char *command = reinterpret_cast<char *>(_alloca(len+1)); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 191 | char *p = command; |
| 192 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 193 | for (unsigned i = 0; args[i]; i++) { |
| 194 | const char *arg = args[i]; |
Anton Korobeynikov | 8124429 | 2010-03-28 15:07:02 +0000 | [diff] [blame] | 195 | |
Daniel Dunbar | 9a0b574 | 2009-08-02 20:41:09 +0000 | [diff] [blame] | 196 | bool needsQuoting = ArgNeedsQuotes(arg); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 197 | if (needsQuoting) |
| 198 | *p++ = '"'; |
Anton Korobeynikov | 8124429 | 2010-03-28 15:07:02 +0000 | [diff] [blame] | 199 | |
| 200 | while (*arg != '\0') { |
| 201 | if (*arg == '\"') |
| 202 | *p++ = '\\'; |
| 203 | |
| 204 | *p++ = *arg++; |
| 205 | } |
| 206 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 207 | if (needsQuoting) |
| 208 | *p++ = '"'; |
| 209 | *p++ = ' '; |
| 210 | } |
| 211 | |
| 212 | *p = 0; |
| 213 | |
Argyrios Kyrtzidis | f1844d2 | 2008-06-15 03:54:39 +0000 | [diff] [blame] | 214 | // The pointer to the environment block for the new process. |
| 215 | char *envblock = 0; |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 216 | |
Argyrios Kyrtzidis | f1844d2 | 2008-06-15 03:54:39 +0000 | [diff] [blame] | 217 | if (envp) { |
| 218 | // An environment block consists of a null-terminated block of |
| 219 | // null-terminated strings. Convert the array of environment variables to |
| 220 | // an environment block by concatenating them. |
| 221 | |
| 222 | // First, determine the length of the environment block. |
| 223 | len = 0; |
| 224 | for (unsigned i = 0; envp[i]; i++) |
| 225 | len += strlen(envp[i]) + 1; |
| 226 | |
| 227 | // Now build the environment block. |
| 228 | envblock = reinterpret_cast<char *>(_alloca(len+1)); |
| 229 | p = envblock; |
| 230 | |
| 231 | for (unsigned i = 0; envp[i]; i++) { |
| 232 | const char *ev = envp[i]; |
| 233 | size_t len = strlen(ev) + 1; |
| 234 | memcpy(p, ev, len); |
| 235 | p += len; |
| 236 | } |
| 237 | |
| 238 | *p = 0; |
| 239 | } |
| 240 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 241 | // Create a child process. |
| 242 | STARTUPINFO si; |
| 243 | memset(&si, 0, sizeof(si)); |
| 244 | si.cb = sizeof(si); |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 245 | si.hStdInput = INVALID_HANDLE_VALUE; |
| 246 | si.hStdOutput = INVALID_HANDLE_VALUE; |
| 247 | si.hStdError = INVALID_HANDLE_VALUE; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 248 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 249 | if (redirects) { |
| 250 | si.dwFlags = STARTF_USESTDHANDLES; |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 251 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 252 | si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg); |
| 253 | if (si.hStdInput == INVALID_HANDLE_VALUE) { |
| 254 | MakeErrMsg(ErrMsg, "can't redirect stdin"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 255 | return false; |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 256 | } |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 257 | si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 258 | if (si.hStdOutput == INVALID_HANDLE_VALUE) { |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 259 | CloseHandle(si.hStdInput); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 260 | MakeErrMsg(ErrMsg, "can't redirect stdout"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 261 | return false; |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 262 | } |
Matthijs Kooijman | cf45ca0 | 2008-06-12 12:53:35 +0000 | [diff] [blame] | 263 | if (redirects[1] && redirects[2] && *(redirects[1]) == *(redirects[2])) { |
| 264 | // If stdout and stderr should go to the same place, redirect stderr |
| 265 | // to the handle already open for stdout. |
| 266 | DuplicateHandle(GetCurrentProcess(), si.hStdOutput, |
| 267 | GetCurrentProcess(), &si.hStdError, |
| 268 | 0, TRUE, DUPLICATE_SAME_ACCESS); |
| 269 | } else { |
| 270 | // Just redirect stderr |
Anton Korobeynikov | 7d51544 | 2006-09-01 20:35:17 +0000 | [diff] [blame] | 271 | si.hStdError = RedirectIO(redirects[2], 2, ErrMsg); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 272 | if (si.hStdError == INVALID_HANDLE_VALUE) { |
| 273 | CloseHandle(si.hStdInput); |
| 274 | CloseHandle(si.hStdOutput); |
| 275 | MakeErrMsg(ErrMsg, "can't redirect stderr"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 276 | return false; |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 277 | } |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 280 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 281 | PROCESS_INFORMATION pi; |
| 282 | memset(&pi, 0, sizeof(pi)); |
| 283 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 284 | fflush(stdout); |
| 285 | fflush(stderr); |
Mikhail Glushenkov | 0526653 | 2009-04-14 21:31:36 +0000 | [diff] [blame] | 286 | BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, TRUE, 0, |
Argyrios Kyrtzidis | f1844d2 | 2008-06-15 03:54:39 +0000 | [diff] [blame] | 287 | envblock, NULL, &si, &pi); |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 288 | DWORD err = GetLastError(); |
| 289 | |
| 290 | // Regardless of whether the process got created or not, we are done with |
| 291 | // the handles we created for it to inherit. |
| 292 | CloseHandle(si.hStdInput); |
| 293 | CloseHandle(si.hStdOutput); |
| 294 | CloseHandle(si.hStdError); |
| 295 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 296 | // Now return an error if the process didn't get created. |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 297 | if (!rc) { |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 298 | SetLastError(err); |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 299 | MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") + |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 300 | path.str() + "'"); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 301 | return false; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 302 | } |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 303 | Win32ProcessInfo* wpi = new Win32ProcessInfo; |
| 304 | wpi->hProcess = pi.hProcess; |
| 305 | wpi->dwProcessId = pi.dwProcessId; |
| 306 | Data_ = wpi; |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 307 | |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 308 | // Make sure these get closed no matter what. |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 309 | AutoHandle hThread(pi.hThread); |
| 310 | |
| 311 | // Assign the process to a job if a memory limit is defined. |
| 312 | AutoHandle hJob(0); |
| 313 | if (memoryLimit != 0) { |
| 314 | hJob = CreateJobObject(0, 0); |
| 315 | bool success = false; |
| 316 | if (hJob != 0) { |
| 317 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli; |
| 318 | memset(&jeli, 0, sizeof(jeli)); |
| 319 | jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY; |
Jeff Cohen | 413bc82 | 2007-03-05 05:45:08 +0000 | [diff] [blame] | 320 | jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576; |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 321 | if (SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, |
| 322 | &jeli, sizeof(jeli))) { |
| 323 | if (AssignProcessToJobObject(hJob, pi.hProcess)) |
| 324 | success = true; |
| 325 | } |
| 326 | } |
| 327 | if (!success) { |
| 328 | SetLastError(GetLastError()); |
| 329 | MakeErrMsg(ErrMsg, std::string("Unable to set memory limit")); |
| 330 | TerminateProcess(pi.hProcess, 1); |
| 331 | WaitForSingleObject(pi.hProcess, INFINITE); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 332 | return false; |
Jeff Cohen | 0a18267 | 2007-03-05 05:22:08 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
| 339 | int |
Dan Gohman | e5f77cd | 2010-10-29 16:54:25 +0000 | [diff] [blame^] | 340 | Program::Wait(const Path &path, |
| 341 | unsigned secondsToWait, |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 342 | std::string* ErrMsg) { |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 343 | if (Data_ == 0) { |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 344 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 345 | return -1; |
| 346 | } |
| 347 | |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 348 | Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data_); |
| 349 | HANDLE hProcess = wpi->hProcess; |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 350 | |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 351 | // Wait for the process to terminate. |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 352 | DWORD millisecondsToWait = INFINITE; |
| 353 | if (secondsToWait > 0) |
| 354 | millisecondsToWait = secondsToWait * 1000; |
| 355 | |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 356 | if (WaitForSingleObject(hProcess, millisecondsToWait) == WAIT_TIMEOUT) { |
| 357 | if (!TerminateProcess(hProcess, 1)) { |
| 358 | MakeErrMsg(ErrMsg, "Failed to terminate timed-out program."); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 359 | return -1; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 360 | } |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 361 | WaitForSingleObject(hProcess, INFINITE); |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 362 | } |
Mikhail Glushenkov | bd50a16 | 2009-04-14 21:31:14 +0000 | [diff] [blame] | 363 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 364 | // Get its exit status. |
| 365 | DWORD status; |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 366 | BOOL rc = GetExitCodeProcess(hProcess, &status); |
| 367 | DWORD err = GetLastError(); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 368 | |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 369 | if (!rc) { |
| 370 | SetLastError(err); |
Mikhail Glushenkov | 3140619 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 371 | MakeErrMsg(ErrMsg, "Failed getting status for program."); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 372 | return -1; |
Jeff Cohen | 875d08e | 2005-02-20 02:43:04 +0000 | [diff] [blame] | 373 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 374 | |
| 375 | return status; |
| 376 | } |
| 377 | |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 378 | bool |
| 379 | Program::Kill(std::string* ErrMsg) { |
Daniel Dunbar | 57d6903 | 2009-09-22 04:44:56 +0000 | [diff] [blame] | 380 | if (Data_ == 0) { |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 381 | MakeErrMsg(ErrMsg, "Process not started!"); |
| 382 | return true; |
| 383 | } |
| 384 | |
Mikhail Glushenkov | 92d3236 | 2009-09-22 15:40:32 +0000 | [diff] [blame] | 385 | Win32ProcessInfo* wpi = reinterpret_cast<Win32ProcessInfo*>(Data_); |
| 386 | HANDLE hProcess = wpi->hProcess; |
Mikhail Glushenkov | 8add269 | 2009-09-09 09:51:47 +0000 | [diff] [blame] | 387 | if (TerminateProcess(hProcess, 1) == 0) { |
| 388 | MakeErrMsg(ErrMsg, "The process couldn't be killed!"); |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | return false; |
Mikhail Glushenkov | a607202 | 2009-09-08 19:50:27 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 395 | bool Program::ChangeStdinToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 396 | int result = _setmode( _fileno(stdin), _O_BINARY ); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 397 | return result == -1; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 400 | bool Program::ChangeStdoutToBinary(){ |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 401 | int result = _setmode( _fileno(stdout), _O_BINARY ); |
Reid Spencer | 4ce5dc6 | 2006-08-21 06:02:44 +0000 | [diff] [blame] | 402 | return result == -1; |
Reid Spencer | 32f5553 | 2006-06-07 23:18:34 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Douglas Gregor | 21569cd | 2010-01-28 06:42:08 +0000 | [diff] [blame] | 405 | bool Program::ChangeStderrToBinary(){ |
| 406 | int result = _setmode( _fileno(stderr), _O_BINARY ); |
| 407 | return result == -1; |
| 408 | } |
| 409 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 410 | } |