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