1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "subprocess.h"
24 Subprocess::Subprocess(bool use_console) : child_(NULL) , overlapped_(),
26 use_console_(use_console) {
29 Subprocess::~Subprocess() {
31 if (!CloseHandle(pipe_))
32 Win32Fatal("CloseHandle");
34 // Reap child if forgotten.
39 HANDLE Subprocess::SetupPipe(HANDLE ioport) {
41 snprintf(pipe_name, sizeof(pipe_name),
42 "\\\\.\\pipe\\ninja_pid%lu_sp%p", GetCurrentProcessId(), this);
44 pipe_ = ::CreateNamedPipeA(pipe_name,
45 PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
47 PIPE_UNLIMITED_INSTANCES,
48 0, 0, INFINITE, NULL);
49 if (pipe_ == INVALID_HANDLE_VALUE)
50 Win32Fatal("CreateNamedPipe");
52 if (!CreateIoCompletionPort(pipe_, ioport, (ULONG_PTR)this, 0))
53 Win32Fatal("CreateIoCompletionPort");
55 memset(&overlapped_, 0, sizeof(overlapped_));
56 if (!ConnectNamedPipe(pipe_, &overlapped_) &&
57 GetLastError() != ERROR_IO_PENDING) {
58 Win32Fatal("ConnectNamedPipe");
61 // Get the write end of the pipe as a handle inheritable across processes.
62 HANDLE output_write_handle = CreateFile(pipe_name, GENERIC_WRITE, 0,
63 NULL, OPEN_EXISTING, 0, NULL);
64 HANDLE output_write_child;
65 if (!DuplicateHandle(GetCurrentProcess(), output_write_handle,
66 GetCurrentProcess(), &output_write_child,
67 0, TRUE, DUPLICATE_SAME_ACCESS)) {
68 Win32Fatal("DuplicateHandle");
70 CloseHandle(output_write_handle);
72 return output_write_child;
75 bool Subprocess::Start(SubprocessSet* set, const string& command) {
76 HANDLE child_pipe = SetupPipe(set->ioport_);
78 SECURITY_ATTRIBUTES security_attributes;
79 memset(&security_attributes, 0, sizeof(SECURITY_ATTRIBUTES));
80 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
81 security_attributes.bInheritHandle = TRUE;
82 // Must be inheritable so subprocesses can dup to children.
83 HANDLE nul = CreateFile("NUL", GENERIC_READ,
84 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
85 &security_attributes, OPEN_EXISTING, 0, NULL);
86 if (nul == INVALID_HANDLE_VALUE)
87 Fatal("couldn't open nul");
89 STARTUPINFOA startup_info;
90 memset(&startup_info, 0, sizeof(startup_info));
91 startup_info.cb = sizeof(STARTUPINFO);
93 startup_info.dwFlags = STARTF_USESTDHANDLES;
94 startup_info.hStdInput = nul;
95 startup_info.hStdOutput = child_pipe;
96 startup_info.hStdError = child_pipe;
98 // In the console case, child_pipe is still inherited by the child and closed
99 // when the subprocess finishes, which then notifies ninja.
101 PROCESS_INFORMATION process_info;
102 memset(&process_info, 0, sizeof(process_info));
104 // Ninja handles ctrl-c, except for subprocesses in console pools.
105 DWORD process_flags = use_console_ ? 0 : CREATE_NEW_PROCESS_GROUP;
107 // Do not prepend 'cmd /c' on Windows, this breaks command
108 // lines greater than 8,191 chars.
109 if (!CreateProcessA(NULL, (char*)command.c_str(), NULL, NULL,
110 /* inherit handles */ TRUE, process_flags,
112 &startup_info, &process_info)) {
113 DWORD error = GetLastError();
114 if (error == ERROR_FILE_NOT_FOUND) {
115 // File (program) not found error is treated as a normal build
118 CloseHandle(child_pipe);
122 // child_ is already NULL;
123 buf_ = "CreateProcess failed: The system cannot find the file "
127 Win32Fatal("CreateProcess"); // pass all other errors to Win32Fatal
131 // Close pipe channel only used by the child.
133 CloseHandle(child_pipe);
136 CloseHandle(process_info.hThread);
137 child_ = process_info.hProcess;
142 void Subprocess::OnPipeReady() {
144 if (!GetOverlappedResult(pipe_, &overlapped_, &bytes, TRUE)) {
145 if (GetLastError() == ERROR_BROKEN_PIPE) {
150 Win32Fatal("GetOverlappedResult");
153 if (is_reading_ && bytes)
154 buf_.append(overlapped_buf_, bytes);
156 memset(&overlapped_, 0, sizeof(overlapped_));
158 if (!::ReadFile(pipe_, overlapped_buf_, sizeof(overlapped_buf_),
159 &bytes, &overlapped_)) {
160 if (GetLastError() == ERROR_BROKEN_PIPE) {
165 if (GetLastError() != ERROR_IO_PENDING)
166 Win32Fatal("ReadFile");
169 // Even if we read any bytes in the readfile call, we'll enter this
170 // function again later and get them at that point.
173 ExitStatus Subprocess::Finish() {
177 // TODO: add error handling for all of these.
178 WaitForSingleObject(child_, INFINITE);
181 GetExitCodeProcess(child_, &exit_code);
186 return exit_code == 0 ? ExitSuccess :
187 exit_code == CONTROL_C_EXIT ? ExitInterrupted :
191 bool Subprocess::Done() const {
192 return pipe_ == NULL;
195 const string& Subprocess::GetOutput() const {
199 HANDLE SubprocessSet::ioport_;
201 SubprocessSet::SubprocessSet() {
202 ioport_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
204 Win32Fatal("CreateIoCompletionPort");
205 if (!SetConsoleCtrlHandler(NotifyInterrupted, TRUE))
206 Win32Fatal("SetConsoleCtrlHandler");
209 SubprocessSet::~SubprocessSet() {
212 SetConsoleCtrlHandler(NotifyInterrupted, FALSE);
213 CloseHandle(ioport_);
216 BOOL WINAPI SubprocessSet::NotifyInterrupted(DWORD dwCtrlType) {
217 if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT) {
218 if (!PostQueuedCompletionStatus(ioport_, 0, 0, NULL))
219 Win32Fatal("PostQueuedCompletionStatus");
226 Subprocess *SubprocessSet::Add(const string& command, bool use_console) {
227 Subprocess *subprocess = new Subprocess(use_console);
228 if (!subprocess->Start(this, command)) {
232 if (subprocess->child_)
233 running_.push_back(subprocess);
235 finished_.push(subprocess);
239 bool SubprocessSet::DoWork() {
242 OVERLAPPED* overlapped;
244 if (!GetQueuedCompletionStatus(ioport_, &bytes_read, (PULONG_PTR)&subproc,
245 &overlapped, INFINITE)) {
246 if (GetLastError() != ERROR_BROKEN_PIPE)
247 Win32Fatal("GetQueuedCompletionStatus");
250 if (!subproc) // A NULL subproc indicates that we were interrupted and is
251 // delivered by NotifyInterrupted above.
254 subproc->OnPipeReady();
256 if (subproc->Done()) {
257 vector<Subprocess*>::iterator end =
258 remove(running_.begin(), running_.end(), subproc);
259 if (running_.end() != end) {
260 finished_.push(subproc);
261 running_.resize(end - running_.begin());
268 Subprocess* SubprocessSet::NextFinished() {
269 if (finished_.empty())
271 Subprocess* subproc = finished_.front();
276 void SubprocessSet::Clear() {
277 for (vector<Subprocess*>::iterator i = running_.begin();
278 i != running_.end(); ++i) {
279 // Since the foreground process is in our process group, it will receive a
280 // CTRL_C_EVENT or CTRL_BREAK_EVENT at the same time as us.
281 if ((*i)->child_ && !(*i)->use_console_) {
282 if (!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT,
283 GetProcessId((*i)->child_))) {
284 Win32Fatal("GenerateConsoleCtrlEvent");
288 for (vector<Subprocess*>::iterator i = running_.begin();
289 i != running_.end(); ++i)