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