blob: 87f9e64e4c59317081a833e8d4a49b3f7d29b3f0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
Mikhail Glushenkova5140f72009-04-14 21:31:14 +00002//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Mikhail Glushenkova5140f72009-04-14 21:31:14 +00007//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Program class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Win32.h"
15#include <cstdio>
16#include <malloc.h>
17#include <io.h>
18#include <fcntl.h>
19
20//===----------------------------------------------------------------------===//
Mikhail Glushenkova5140f72009-04-14 21:31:14 +000021//=== WARNING: Implementation here must contain only Win32 specific code
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022//=== and must not be UNIX code
23//===----------------------------------------------------------------------===//
24
25namespace llvm {
26using namespace sys;
27
Mikhail Glushenkov460b0172009-09-08 19:50:55 +000028Program::Program() : Data_(0) {}
Daniel Dunbar1b399062009-08-03 05:02:46 +000029
30Program::~Program() {
Mikhail Glushenkov460b0172009-09-08 19:50:55 +000031 if (Data_) {
32 HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
Mikhail Glushenkova2801682009-09-08 19:50:27 +000033 CloseHandle(hProcess);
Mikhail Glushenkov460b0172009-09-08 19:50:55 +000034 Data_ = 0;
Mikhail Glushenkova2801682009-09-08 19:50:27 +000035 }
Daniel Dunbar1b399062009-08-03 05:02:46 +000036}
37
Mikhail Glushenkovc5e639c2009-09-08 19:51:39 +000038unsigned Program::GetPid() const {
Mikhail Glushenkov460b0172009-09-08 19:50:55 +000039 HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
40 return GetProcessId(hProcess);
41}
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043// This function just uses the PATH environment variable to find the program.
44Path
45Program::FindProgramByName(const std::string& progName) {
46
47 // Check some degenerate cases
48 if (progName.length() == 0) // no program
49 return Path();
50 Path temp;
51 if (!temp.set(progName)) // invalid name
52 return Path();
53 if (temp.canExecute()) // already executable as is
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
87static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
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 Glushenkova5140f72009-04-14 21:31:14 +000095
Matthijs Kooijman641150d2008-06-12 10:47:18 +000096 const char *fname;
97 if (path->isEmpty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 fname = "NUL";
Matthijs Kooijman641150d2008-06-12 10:47:18 +000099 else
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000100 fname = path->c_str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
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,
108 &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
109 FILE_ATTRIBUTE_NORMAL, NULL);
110 if (h == INVALID_HANDLE_VALUE) {
111 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
112 (fd ? "input: " : "output: "));
113 }
114
115 return h;
116}
117
118#ifdef __MINGW32__
119 // Due to unknown reason, mingw32's w32api doesn't have this declaration.
120 extern "C"
121 BOOL WINAPI SetInformationJobObject(HANDLE hJob,
122 JOBOBJECTINFOCLASS JobObjectInfoClass,
123 LPVOID lpJobObjectInfo,
124 DWORD cbJobObjectInfoLength);
125#endif
Mikhail Glushenkova5140f72009-04-14 21:31:14 +0000126
Daniel Dunbaraad7ba12009-08-02 20:41:09 +0000127/// ArgNeedsQuotes - Check whether argument needs to be quoted when calling
128/// CreateProcess.
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000129static bool ArgNeedsQuotes(const char *Str) {
Daniel Dunbaraad7ba12009-08-02 20:41:09 +0000130 return Str[0] == '\0' || strchr(Str, ' ') != 0;
131}
132
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000133bool
134Program::Execute(const Path& path,
135 const char** args,
136 const char** envp,
137 const Path** redirects,
138 unsigned memoryLimit,
139 std::string* ErrMsg) {
Mikhail Glushenkov460b0172009-09-08 19:50:55 +0000140 if (Data_) {
141 HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
142 CloseHandle(Data_);
143 Data_ = 0;
Daniel Dunbar1b399062009-08-03 05:02:46 +0000144 }
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 if (!path.canExecute()) {
147 if (ErrMsg)
148 *ErrMsg = "program not executable";
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000149 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 }
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 Dunbaraad7ba12009-08-02 20:41:09 +0000154 // have embedded spaces (or are empty).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155
156 // First, determine the length of the command line.
157 unsigned len = 0;
158 for (unsigned i = 0; args[i]; i++) {
159 len += strlen(args[i]) + 1;
Daniel Dunbaraad7ba12009-08-02 20:41:09 +0000160 if (ArgNeedsQuotes(args[i]))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 len += 2;
162 }
163
164 // Now build the command line.
Anton Korobeynikov27f1ebc2008-01-24 01:20:48 +0000165 char *command = reinterpret_cast<char *>(_alloca(len+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 char *p = command;
167
168 for (unsigned i = 0; args[i]; i++) {
169 const char *arg = args[i];
170 size_t len = strlen(arg);
Daniel Dunbaraad7ba12009-08-02 20:41:09 +0000171 bool needsQuoting = ArgNeedsQuotes(arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 if (needsQuoting)
173 *p++ = '"';
174 memcpy(p, arg, len);
175 p += len;
176 if (needsQuoting)
177 *p++ = '"';
178 *p++ = ' ';
179 }
180
181 *p = 0;
182
Argiris Kirtzidis715b3872008-06-15 03:54:39 +0000183 // The pointer to the environment block for the new process.
184 char *envblock = 0;
Mikhail Glushenkova5140f72009-04-14 21:31:14 +0000185
Argiris Kirtzidis715b3872008-06-15 03:54:39 +0000186 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 // Create a child process.
211 STARTUPINFO si;
212 memset(&si, 0, sizeof(si));
213 si.cb = sizeof(si);
214 si.hStdInput = INVALID_HANDLE_VALUE;
215 si.hStdOutput = INVALID_HANDLE_VALUE;
216 si.hStdError = INVALID_HANDLE_VALUE;
217
218 if (redirects) {
219 si.dwFlags = STARTF_USESTDHANDLES;
Mikhail Glushenkova5140f72009-04-14 21:31:14 +0000220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
222 if (si.hStdInput == INVALID_HANDLE_VALUE) {
223 MakeErrMsg(ErrMsg, "can't redirect stdin");
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000224 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 }
226 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
227 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
228 CloseHandle(si.hStdInput);
229 MakeErrMsg(ErrMsg, "can't redirect stdout");
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000230 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 }
Matthijs Kooijman0e7dc8a2008-06-12 12:53:35 +0000232 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
241 if (si.hStdError == INVALID_HANDLE_VALUE) {
242 CloseHandle(si.hStdInput);
243 CloseHandle(si.hStdOutput);
244 MakeErrMsg(ErrMsg, "can't redirect stderr");
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000245 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 }
248 }
Mikhail Glushenkova5140f72009-04-14 21:31:14 +0000249
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 PROCESS_INFORMATION pi;
251 memset(&pi, 0, sizeof(pi));
252
253 fflush(stdout);
254 fflush(stderr);
Mikhail Glushenkovaa9f1022009-04-14 21:31:36 +0000255 BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, TRUE, 0,
Argiris Kirtzidis715b3872008-06-15 03:54:39 +0000256 envblock, NULL, &si, &pi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 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
265 // Now return an error if the process didn't get created.
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000266 if (!rc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 SetLastError(err);
Mikhail Glushenkova5140f72009-04-14 21:31:14 +0000268 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000269 path.str() + "'");
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000270 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 }
Mikhail Glushenkov460b0172009-09-08 19:50:55 +0000272 Data_ = reinterpret_cast<void*>(pi.hProcess);
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 // Make sure these get closed no matter what.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 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;
286 jeli.ProcessMemoryLimit = uintptr_t(memoryLimit) * 1048576;
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 Glushenkovb23120b2009-07-18 21:43:12 +0000298 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 }
300 }
301
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000302 return true;
303}
304
305int
306Program::Wait(unsigned secondsToWait,
307 std::string* ErrMsg) {
Mikhail Glushenkov460b0172009-09-08 19:50:55 +0000308 if (Data_ == 0) {
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000309 MakeErrMsg(ErrMsg, "Process not started!");
310 return -1;
311 }
312
Mikhail Glushenkov460b0172009-09-08 19:50:55 +0000313 HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000314
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000315 // Wait for the process to terminate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 DWORD millisecondsToWait = INFINITE;
317 if (secondsToWait > 0)
318 millisecondsToWait = secondsToWait * 1000;
319
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000320 if (WaitForSingleObject(hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
321 if (!TerminateProcess(hProcess, 1)) {
322 MakeErrMsg(ErrMsg, "Failed to terminate timed-out program.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 return -1;
324 }
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000325 WaitForSingleObject(hProcess, INFINITE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 }
Mikhail Glushenkova5140f72009-04-14 21:31:14 +0000327
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // Get its exit status.
329 DWORD status;
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000330 BOOL rc = GetExitCodeProcess(hProcess, &status);
331 DWORD err = GetLastError();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
333 if (!rc) {
334 SetLastError(err);
Mikhail Glushenkovb23120b2009-07-18 21:43:12 +0000335 MakeErrMsg(ErrMsg, "Failed getting status for program.");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 return -1;
337 }
338
339 return status;
340}
341
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000342bool
343Program::Kill(std::string* ErrMsg) {
Mikhail Glushenkov460b0172009-09-08 19:50:55 +0000344 if (Data_ == 0) {
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000345 MakeErrMsg(ErrMsg, "Process not started!");
346 return true;
347 }
348
Mikhail Glushenkov460b0172009-09-08 19:50:55 +0000349 HANDLE hProcess = reinterpret_cast<HANDLE>(Data_);
Mikhail Glushenkova2801682009-09-08 19:50:27 +0000350 return TerminateProcess(hProcess, 1);
351}
352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353bool Program::ChangeStdinToBinary(){
354 int result = _setmode( _fileno(stdin), _O_BINARY );
355 return result == -1;
356}
357
358bool Program::ChangeStdoutToBinary(){
359 int result = _setmode( _fileno(stdout), _O_BINARY );
360 return result == -1;
361}
362
363}