1 //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Program class.
12 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only Win32 specific code
22 //=== and must not be UNIX code
23 //===----------------------------------------------------------------------===//
28 // This function just uses the PATH environment variable to find the program.
30 Program::FindProgramByName(const std::string& progName) {
32 // Check some degenerate cases
33 if (progName.length() == 0) // no program
36 if (!temp.set(progName)) // invalid name
38 if (temp.canExecute()) // already executable as is
41 // At this point, the file name is valid and its not executable.
42 // Let Windows search for it.
43 char buffer[MAX_PATH];
45 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
48 // See if it wasn't found.
52 // See if we got the entire path.
56 // Buffer was too small; grow and retry.
58 char *b = reinterpret_cast<char *>(_alloca(len+1));
59 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
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...
72 static HANDLE RedirectIO(const Path *path, int fd) {
75 DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
76 GetCurrentProcess(), &h,
77 0, TRUE, DUPLICATE_SAME_ACCESS);
81 const char *fname = path->toString().c_str();
85 SECURITY_ATTRIBUTES sa;
86 sa.nLength = sizeof(sa);
87 sa.lpSecurityDescriptor = 0;
88 sa.bInheritHandle = TRUE;
90 h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
91 &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
92 FILE_ATTRIBUTE_NORMAL, NULL);
93 if (h == INVALID_HANDLE_VALUE) {
94 ThrowError(std::string(fname) + ": Can't open file for " +
95 (fd ? "input: " : "output: "));
101 Program::ExecuteAndWait(const Path& path,
104 const Path** redirects,
105 unsigned secondsToWait) {
106 if (!path.canExecute())
107 throw path.toString() + " is not executable";
109 // Windows wants a command line, not an array of args, to pass to the new
110 // process. We have to concatenate them all, while quoting the args that
111 // have embedded spaces.
113 // First, determine the length of the command line.
115 for (unsigned i = 0; args[i]; i++) {
116 len += strlen(args[i]) + 1;
117 if (strchr(args[i], ' '))
121 // Now build the command line.
122 char *command = reinterpret_cast<char *>(_alloca(len));
125 for (unsigned i = 0; args[i]; i++) {
126 const char *arg = args[i];
127 size_t len = strlen(arg);
128 bool needsQuoting = strchr(arg, ' ') != 0;
140 // Create a child process.
142 memset(&si, 0, sizeof(si));
144 si.hStdInput = INVALID_HANDLE_VALUE;
145 si.hStdOutput = INVALID_HANDLE_VALUE;
146 si.hStdError = INVALID_HANDLE_VALUE;
149 si.dwFlags = STARTF_USESTDHANDLES;
152 si.hStdInput = RedirectIO(redirects[0], 0);
153 si.hStdOutput = RedirectIO(redirects[1], 1);
154 if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
155 si.hStdError = RedirectIO(redirects[2], 2);
157 DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
158 GetCurrentProcess(), &si.hStdError,
159 0, TRUE, DUPLICATE_SAME_ACCESS);
162 CloseHandle(si.hStdInput);
163 CloseHandle(si.hStdOutput);
164 CloseHandle(si.hStdError);
169 PROCESS_INFORMATION pi;
170 memset(&pi, 0, sizeof(pi));
174 BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
175 envp, NULL, &si, &pi);
176 DWORD err = GetLastError();
178 // Regardless of whether the process got created or not, we are done with
179 // the handles we created for it to inherit.
180 CloseHandle(si.hStdInput);
181 CloseHandle(si.hStdOutput);
182 CloseHandle(si.hStdError);
184 // Now throw an error if the process didn't get created.
188 ThrowError(std::string("Couldn't execute program '") +
189 path.toString() + "'");
192 // Wait for it to terminate.
193 DWORD millisecondsToWait = INFINITE;
194 if (secondsToWait > 0)
195 millisecondsToWait = secondsToWait * 1000;
197 if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
198 if (!TerminateProcess(pi.hProcess, 1)) {
199 ThrowError(std::string("Failed to terminate timed-out program '") +
200 path.toString() + "'");
202 WaitForSingleObject(pi.hProcess, INFINITE);
205 // Get its exit status.
207 rc = GetExitCodeProcess(pi.hProcess, &status);
208 err = GetLastError();
210 // Done with the handles; go close them.
211 CloseHandle(pi.hProcess);
212 CloseHandle(pi.hThread);
216 ThrowError(std::string("Failed getting status for program '") +
217 path.toString() + "'");
223 void Program::ChangeStdinToBinary(){
224 int result = _setmode( _fileno(stdin), _O_BINARY );
226 throw std::string("Cannot set input mode on stdin to binary.");
229 void Program::ChangeStdoutToBinary(){
230 int result = _setmode( _fileno(stdout), _O_BINARY );
232 throw std::string("Cannot set output mode on stdout to binary.");