blob: e1ad155bb34d5fbffcf30d556f025914dec925d6 [file] [log] [blame]
Reid Spencerb016a372004-09-15 05:49:50 +00001//===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
Reid Spencer52a7efa2004-08-29 19:20:41 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencerb016a372004-09-15 05:49:50 +00005// This file was developed by Jeff Cohen and is distributed under the
Reid Spencer52a7efa2004-08-29 19:20:41 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of the Program class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencerb016a372004-09-15 05:49:50 +000014#include "Win32.h"
Reid Spencer32f55532006-06-07 23:18:34 +000015#include <cstdio>
Reid Spencerb016a372004-09-15 05:49:50 +000016#include <malloc.h>
Jeff Cohen875d08e2005-02-20 02:43:04 +000017#include <io.h>
Reid Spencer32f55532006-06-07 23:18:34 +000018#include <fcntl.h>
Reid Spencerb016a372004-09-15 05:49:50 +000019
Reid Spencer52a7efa2004-08-29 19:20:41 +000020//===----------------------------------------------------------------------===//
Reid Spencerb016a372004-09-15 05:49:50 +000021//=== WARNING: Implementation here must contain only Win32 specific code
22//=== and must not be UNIX code
Reid Spencer52a7efa2004-08-29 19:20:41 +000023//===----------------------------------------------------------------------===//
24
Reid Spencerb016a372004-09-15 05:49:50 +000025namespace llvm {
26using namespace sys;
27
28// This function just uses the PATH environment variable to find the program.
29Path
30Program::FindProgramByName(const std::string& progName) {
31
32 // Check some degenerate cases
33 if (progName.length() == 0) // no program
34 return Path();
35 Path temp;
Jeff Cohenedb9d6b2005-07-08 02:48:42 +000036 if (!temp.set(progName)) // invalid name
Reid Spencerb016a372004-09-15 05:49:50 +000037 return Path();
Jeff Cohenedb9d6b2005-07-08 02:48:42 +000038 if (temp.canExecute()) // already executable as is
Reid Spencerb016a372004-09-15 05:49:50 +000039 return temp;
40
41 // At this point, the file name is valid and its not executable.
42 // Let Windows search for it.
43 char buffer[MAX_PATH];
44 char *dummy = NULL;
45 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
46 buffer, &dummy);
47
48 // See if it wasn't found.
49 if (len == 0)
50 return Path();
51
52 // See if we got the entire path.
53 if (len < MAX_PATH)
54 return Path(buffer);
55
56 // Buffer was too small; grow and retry.
57 while (true) {
58 char *b = reinterpret_cast<char *>(_alloca(len+1));
59 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
60
61 // It is unlikely the search failed, but it's always possible some file
62 // was added or removed since the last search, so be paranoid...
63 if (len2 == 0)
64 return Path();
65 else if (len2 <= len)
66 return Path(b);
67
68 len = len2;
69 }
70}
71
Reid Spencer4ce5dc62006-08-21 06:02:44 +000072static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
Jeff Cohen875d08e2005-02-20 02:43:04 +000073 HANDLE h;
74 if (path == 0) {
75 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
76 GetCurrentProcess(), &h,
77 0, TRUE, DUPLICATE_SAME_ACCESS);
78 return h;
79 }
80
81 const char *fname = path->toString().c_str();
82 if (*fname == 0)
83 fname = "NUL";
84
85 SECURITY_ATTRIBUTES sa;
86 sa.nLength = sizeof(sa);
87 sa.lpSecurityDescriptor = 0;
88 sa.bInheritHandle = TRUE;
89
90 h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
Jeff Cohen45a1b262005-02-20 02:48:51 +000091 &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
Jeff Cohen875d08e2005-02-20 02:43:04 +000092 FILE_ATTRIBUTE_NORMAL, NULL);
93 if (h == INVALID_HANDLE_VALUE) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +000094 MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
Jeff Cohen875d08e2005-02-20 02:43:04 +000095 (fd ? "input: " : "output: "));
Reid Spencer4ce5dc62006-08-21 06:02:44 +000096 return h;
Jeff Cohen875d08e2005-02-20 02:43:04 +000097 }
98 return h;
99}
100
Reid Spencerb016a372004-09-15 05:49:50 +0000101int
102Program::ExecuteAndWait(const Path& path,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000103 const char** args,
104 const char** envp,
105 const Path** redirects,
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000106 unsigned secondsToWait,
107 std::string* ErrMsg) {
108 if (!path.canExecute()) {
109 if (ErrMsg)
110 *ErrMsg = "program not executable";
111 return -1;
112 }
Reid Spencerb016a372004-09-15 05:49:50 +0000113
114 // Windows wants a command line, not an array of args, to pass to the new
115 // process. We have to concatenate them all, while quoting the args that
116 // have embedded spaces.
117
118 // First, determine the length of the command line.
Jeff Cohene5f7e652005-02-16 04:43:45 +0000119 unsigned len = 0;
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000120 for (unsigned i = 0; args[i]; i++) {
121 len += strlen(args[i]) + 1;
122 if (strchr(args[i], ' '))
Reid Spencerb016a372004-09-15 05:49:50 +0000123 len += 2;
124 }
125
126 // Now build the command line.
127 char *command = reinterpret_cast<char *>(_alloca(len));
128 char *p = command;
129
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000130 for (unsigned i = 0; args[i]; i++) {
131 const char *arg = args[i];
Jeff Cohen01c55132005-04-11 03:44:22 +0000132 size_t len = strlen(arg);
Jeff Cohene5f7e652005-02-16 04:43:45 +0000133 bool needsQuoting = strchr(arg, ' ') != 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000134 if (needsQuoting)
135 *p++ = '"';
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000136 memcpy(p, arg, len);
137 p += len;
Reid Spencerb016a372004-09-15 05:49:50 +0000138 if (needsQuoting)
139 *p++ = '"';
140 *p++ = ' ';
141 }
142
143 *p = 0;
144
145 // Create a child process.
146 STARTUPINFO si;
147 memset(&si, 0, sizeof(si));
148 si.cb = sizeof(si);
Jeff Cohen875d08e2005-02-20 02:43:04 +0000149 si.hStdInput = INVALID_HANDLE_VALUE;
150 si.hStdOutput = INVALID_HANDLE_VALUE;
151 si.hStdError = INVALID_HANDLE_VALUE;
Reid Spencerb016a372004-09-15 05:49:50 +0000152
Jeff Cohen875d08e2005-02-20 02:43:04 +0000153 if (redirects) {
154 si.dwFlags = STARTF_USESTDHANDLES;
155
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000156 si.hStdInput = RedirectIO(redirects[0], 0, ErrMsg);
157 if (si.hStdInput == INVALID_HANDLE_VALUE) {
158 MakeErrMsg(ErrMsg, "can't redirect stdin");
159 return -1;
160 }
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000161 si.hStdOutput = RedirectIO(redirects[1], 1, ErrMsg);
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000162 if (si.hStdOutput == INVALID_HANDLE_VALUE) {
Jeff Cohen875d08e2005-02-20 02:43:04 +0000163 CloseHandle(si.hStdInput);
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000164 MakeErrMsg(ErrMsg, "can't redirect stdout");
165 return -1;
166 }
167 if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
Anton Korobeynikov7d515442006-09-01 20:35:17 +0000168 si.hStdError = RedirectIO(redirects[2], 2, ErrMsg);
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000169 if (si.hStdError == INVALID_HANDLE_VALUE) {
170 CloseHandle(si.hStdInput);
171 CloseHandle(si.hStdOutput);
172 MakeErrMsg(ErrMsg, "can't redirect stderr");
173 return -1;
174 }
175 } else {
176 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
177 GetCurrentProcess(), &si.hStdError,
178 0, TRUE, DUPLICATE_SAME_ACCESS);
Jeff Cohen875d08e2005-02-20 02:43:04 +0000179 }
180 }
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000181
Reid Spencerb016a372004-09-15 05:49:50 +0000182 PROCESS_INFORMATION pi;
183 memset(&pi, 0, sizeof(pi));
184
Jeff Cohen875d08e2005-02-20 02:43:04 +0000185 fflush(stdout);
186 fflush(stderr);
187 BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
188 envp, NULL, &si, &pi);
189 DWORD err = GetLastError();
190
191 // Regardless of whether the process got created or not, we are done with
192 // the handles we created for it to inherit.
193 CloseHandle(si.hStdInput);
194 CloseHandle(si.hStdOutput);
195 CloseHandle(si.hStdError);
196
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000197 // Now return an error if the process didn't get created.
Jeff Cohen875d08e2005-02-20 02:43:04 +0000198 if (!rc)
Reid Spencerb016a372004-09-15 05:49:50 +0000199 {
Jeff Cohen875d08e2005-02-20 02:43:04 +0000200 SetLastError(err);
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000201 MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
Reid Spencer707a27c2004-12-11 17:37:01 +0000202 path.toString() + "'");
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000203 return -1;
Reid Spencerb016a372004-09-15 05:49:50 +0000204 }
205
206 // Wait for it to terminate.
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000207 DWORD millisecondsToWait = INFINITE;
208 if (secondsToWait > 0)
209 millisecondsToWait = secondsToWait * 1000;
210
211 if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
212 if (!TerminateProcess(pi.hProcess, 1)) {
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000213 MakeErrMsg(ErrMsg, std::string("Failed to terminate timed-out program '")
214 + path.toString() + "'");
215 return -1;
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000216 }
217 WaitForSingleObject(pi.hProcess, INFINITE);
218 }
Reid Spencerb016a372004-09-15 05:49:50 +0000219
220 // Get its exit status.
221 DWORD status;
Jeff Cohen875d08e2005-02-20 02:43:04 +0000222 rc = GetExitCodeProcess(pi.hProcess, &status);
223 err = GetLastError();
Reid Spencerb016a372004-09-15 05:49:50 +0000224
225 // Done with the handles; go close them.
226 CloseHandle(pi.hProcess);
227 CloseHandle(pi.hThread);
228
Jeff Cohen875d08e2005-02-20 02:43:04 +0000229 if (!rc) {
230 SetLastError(err);
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000231 MakeErrMsg(ErrMsg, std::string("Failed getting status for program '") +
Reid Spencer707a27c2004-12-11 17:37:01 +0000232 path.toString() + "'");
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000233 return -1;
Jeff Cohen875d08e2005-02-20 02:43:04 +0000234 }
Reid Spencerb016a372004-09-15 05:49:50 +0000235
236 return status;
237}
238
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000239bool Program::ChangeStdinToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000240 int result = _setmode( _fileno(stdin), _O_BINARY );
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000241 return result == -1;
Reid Spencer32f55532006-06-07 23:18:34 +0000242}
243
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000244bool Program::ChangeStdoutToBinary(){
Reid Spencer32f55532006-06-07 23:18:34 +0000245 int result = _setmode( _fileno(stdout), _O_BINARY );
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000246 return result == -1;
Reid Spencer32f55532006-06-07 23:18:34 +0000247}
248
Reid Spencerb016a372004-09-15 05:49:50 +0000249}