1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "subprocess.h"
#include <stdio.h>
#include <algorithm>
#include "util.h"
namespace {
void Win32Fatal(const char* function) {
Fatal("%s: %s", function, GetLastErrorString().c_str());
}
} // anonymous namespace
Subprocess::Subprocess() : child_(NULL) , overlapped_(), is_reading_(false) {
}
Subprocess::~Subprocess() {
// Reap child if forgotten.
if (child_)
Finish();
}
HANDLE Subprocess::SetupPipe(HANDLE ioport) {
char pipe_name[100];
snprintf(pipe_name, sizeof(pipe_name),
"\\\\.\\pipe\\ninja_pid%u_sp%p", GetCurrentProcessId(), this);
pipe_ = ::CreateNamedPipeA(pipe_name,
PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE,
PIPE_UNLIMITED_INSTANCES,
0, 0, INFINITE, NULL);
if (pipe_ == INVALID_HANDLE_VALUE)
Win32Fatal("CreateNamedPipe");
if (!CreateIoCompletionPort(pipe_, ioport, (ULONG_PTR)this, 0))
Win32Fatal("CreateIoCompletionPort");
memset(&overlapped_, 0, sizeof(overlapped_));
if (!ConnectNamedPipe(pipe_, &overlapped_) &&
GetLastError() != ERROR_IO_PENDING) {
Win32Fatal("ConnectNamedPipe");
}
// Get the write end of the pipe as a handle inheritable across processes.
HANDLE output_write_handle = CreateFile(pipe_name, GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
HANDLE output_write_child;
if (!DuplicateHandle(GetCurrentProcess(), output_write_handle,
GetCurrentProcess(), &output_write_child,
0, TRUE, DUPLICATE_SAME_ACCESS)) {
Win32Fatal("DuplicateHandle");
}
CloseHandle(output_write_handle);
return output_write_child;
}
bool Subprocess::Start(SubprocessSet* set, const string& command) {
HANDLE child_pipe = SetupPipe(set->ioport_);
STARTUPINFOA startup_info;
memset(&startup_info, 0, sizeof(startup_info));
startup_info.cb = sizeof(STARTUPINFO);
startup_info.dwFlags = STARTF_USESTDHANDLES;
startup_info.hStdOutput = child_pipe;
// TODO: what does this hook up stdin to?
startup_info.hStdInput = NULL;
// TODO: is it ok to reuse pipe like this?
startup_info.hStdError = child_pipe;
PROCESS_INFORMATION process_info;
memset(&process_info, 0, sizeof(process_info));
// Do not prepend 'cmd /c' on Windows, this breaks command
// lines greater than 8,191 chars.
if (!CreateProcessA(NULL, (char*)command.c_str(), NULL, NULL,
/* inherit handles */ TRUE, 0,
NULL, NULL,
&startup_info, &process_info)) {
DWORD error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND) { // file (program) not found error is treated as a normal build action failure
if (child_pipe)
CloseHandle(child_pipe);
CloseHandle(pipe_);
pipe_ = NULL;
// child_ is already NULL;
buf_ = "CreateProcess failed: The system cannot find the file specified.\n";
return true;
} else {
Win32Fatal("CreateProcess"); // pass all other errors to Win32Fatal
}
}
// Close pipe channel only used by the child.
if (child_pipe)
CloseHandle(child_pipe);
CloseHandle(process_info.hThread);
child_ = process_info.hProcess;
return true;
}
void Subprocess::OnPipeReady() {
DWORD bytes;
if (!GetOverlappedResult(pipe_, &overlapped_, &bytes, TRUE)) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
CloseHandle(pipe_);
pipe_ = NULL;
return;
}
Win32Fatal("GetOverlappedResult");
}
if (is_reading_ && bytes)
buf_.append(overlapped_buf_, bytes);
memset(&overlapped_, 0, sizeof(overlapped_));
is_reading_ = true;
if (!::ReadFile(pipe_, overlapped_buf_, sizeof(overlapped_buf_),
&bytes, &overlapped_)) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
CloseHandle(pipe_);
pipe_ = NULL;
return;
}
if (GetLastError() != ERROR_IO_PENDING)
Win32Fatal("ReadFile");
}
// Even if we read any bytes in the readfile call, we'll enter this
// function again later and get them at that point.
}
bool Subprocess::Finish() {
if (! child_) {
return false;
}
// TODO: add error handling for all of these.
WaitForSingleObject(child_, INFINITE);
DWORD exit_code = 0;
GetExitCodeProcess(child_, &exit_code);
CloseHandle(child_);
child_ = NULL;
return exit_code == 0;
}
bool Subprocess::Done() const {
return pipe_ == NULL;
}
const string& Subprocess::GetOutput() const {
return buf_;
}
SubprocessSet::SubprocessSet() {
ioport_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
if (!ioport_)
Win32Fatal("CreateIoCompletionPort");
}
SubprocessSet::~SubprocessSet() {
CloseHandle(ioport_);
}
void SubprocessSet::Add(Subprocess* subprocess) {
if (subprocess->child_)
running_.push_back(subprocess);
else
finished_.push(subprocess);
}
void SubprocessSet::DoWork() {
DWORD bytes_read;
Subprocess* subproc;
OVERLAPPED* overlapped;
if (!GetQueuedCompletionStatus(ioport_, &bytes_read, (PULONG_PTR)&subproc,
&overlapped, INFINITE)) {
if (GetLastError() != ERROR_BROKEN_PIPE)
Win32Fatal("GetQueuedCompletionStatus");
}
subproc->OnPipeReady();
if (subproc->Done()) {
vector<Subprocess*>::iterator end =
std::remove(running_.begin(), running_.end(), subproc);
if (running_.end() != end) {
finished_.push(subproc);
running_.resize(end - running_.begin());
}
}
}
Subprocess* SubprocessSet::NextFinished() {
if (finished_.empty())
return NULL;
Subprocess* subproc = finished_.front();
finished_.pop();
return subproc;
}
|