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