#include <google/protobuf/compiler/subprocess.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#ifndef _WIN32
#include <errno.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/wait.h>
#endif
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/substitute.h>
#include <google/protobuf/message.h>
#include <google/protobuf/io/io_win32.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace {
char* portable_strdup(const char* s) { … }
}
#ifdef _WIN32
static void CloseHandleOrDie(HANDLE handle) {
if (!CloseHandle(handle)) {
GOOGLE_LOG(FATAL) << "CloseHandle: "
<< Subprocess::Win32ErrorMessage(GetLastError());
}
}
Subprocess::Subprocess()
: process_start_error_(ERROR_SUCCESS),
child_handle_(nullptr),
child_stdin_(nullptr),
child_stdout_(nullptr) {}
Subprocess::~Subprocess() {
if (child_stdin_ != nullptr) {
CloseHandleOrDie(child_stdin_);
}
if (child_stdout_ != nullptr) {
CloseHandleOrDie(child_stdout_);
}
}
void Subprocess::Start(const std::string& program, SearchMode search_mode) {
HANDLE stdin_pipe_read;
HANDLE stdin_pipe_write;
HANDLE stdout_pipe_read;
HANDLE stdout_pipe_write;
if (!CreatePipe(&stdin_pipe_read, &stdin_pipe_write, nullptr, 0)) {
GOOGLE_LOG(FATAL) << "CreatePipe: " << Win32ErrorMessage(GetLastError());
}
if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, nullptr, 0)) {
GOOGLE_LOG(FATAL) << "CreatePipe: " << Win32ErrorMessage(GetLastError());
}
if (!SetHandleInformation(stdin_pipe_read, HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT)) {
GOOGLE_LOG(FATAL) << "SetHandleInformation: "
<< Win32ErrorMessage(GetLastError());
}
if (!SetHandleInformation(stdout_pipe_write, HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT)) {
GOOGLE_LOG(FATAL) << "SetHandleInformation: "
<< Win32ErrorMessage(GetLastError());
}
STARTUPINFOW startup_info;
ZeroMemory(&startup_info, sizeof(startup_info));
startup_info.cb = sizeof(startup_info);
startup_info.dwFlags = STARTF_USESTDHANDLES;
startup_info.hStdInput = stdin_pipe_read;
startup_info.hStdOutput = stdout_pipe_write;
startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if (startup_info.hStdError == INVALID_HANDLE_VALUE) {
GOOGLE_LOG(FATAL) << "GetStdHandle: " << Win32ErrorMessage(GetLastError());
}
std::wstring wprogram;
if (!io::win32::strings::utf8_to_wcs(program.c_str(), &wprogram)) {
GOOGLE_LOG(FATAL) << "utf8_to_wcs: " << Win32ErrorMessage(GetLastError());
}
std::string command_line = "cmd.exe /c \"" + program + "\"";
std::wstring wcommand_line;
if (!io::win32::strings::utf8_to_wcs(command_line.c_str(), &wcommand_line)) {
GOOGLE_LOG(FATAL) << "utf8_to_wcs: " << Win32ErrorMessage(GetLastError());
}
wchar_t *wcommand_line_copy = _wcsdup(wcommand_line.c_str());
PROCESS_INFORMATION process_info;
if (CreateProcessW((search_mode == SEARCH_PATH) ? nullptr : wprogram.c_str(),
(search_mode == SEARCH_PATH) ? wcommand_line_copy : NULL,
nullptr,
nullptr,
TRUE,
0,
nullptr,
nullptr,
&startup_info, &process_info)) {
child_handle_ = process_info.hProcess;
CloseHandleOrDie(process_info.hThread);
child_stdin_ = stdin_pipe_write;
child_stdout_ = stdout_pipe_read;
} else {
process_start_error_ = GetLastError();
CloseHandleOrDie(stdin_pipe_write);
CloseHandleOrDie(stdout_pipe_read);
}
CloseHandleOrDie(stdin_pipe_read);
CloseHandleOrDie(stdout_pipe_write);
free(wcommand_line_copy);
}
bool Subprocess::Communicate(const Message& input, Message* output,
std::string* error) {
if (process_start_error_ != ERROR_SUCCESS) {
*error = Win32ErrorMessage(process_start_error_);
return false;
}
GOOGLE_CHECK(child_handle_ != nullptr) << "Must call Start() first.";
std::string input_data;
if (!input.SerializeToString(&input_data)) {
*error = "Failed to serialize request.";
return false;
}
std::string output_data;
int input_pos = 0;
while (child_stdout_ != nullptr) {
HANDLE handles[2];
int handle_count = 0;
if (child_stdin_ != nullptr) {
handles[handle_count++] = child_stdin_;
}
if (child_stdout_ != nullptr) {
handles[handle_count++] = child_stdout_;
}
DWORD wait_result =
WaitForMultipleObjects(handle_count, handles, FALSE, INFINITE);
HANDLE signaled_handle = nullptr;
if (wait_result >= WAIT_OBJECT_0 &&
wait_result < WAIT_OBJECT_0 + handle_count) {
signaled_handle = handles[wait_result - WAIT_OBJECT_0];
} else if (wait_result == WAIT_FAILED) {
GOOGLE_LOG(FATAL) << "WaitForMultipleObjects: "
<< Win32ErrorMessage(GetLastError());
} else {
GOOGLE_LOG(FATAL) << "WaitForMultipleObjects: Unexpected return code: "
<< wait_result;
}
if (signaled_handle == child_stdin_) {
DWORD n;
if (!WriteFile(child_stdin_, input_data.data() + input_pos,
input_data.size() - input_pos, &n, nullptr)) {
input_pos = input_data.size();
} else {
input_pos += n;
}
if (input_pos == input_data.size()) {
CloseHandleOrDie(child_stdin_);
child_stdin_ = nullptr;
}
} else if (signaled_handle == child_stdout_) {
char buffer[4096];
DWORD n;
if (!ReadFile(child_stdout_, buffer, sizeof(buffer), &n, nullptr)) {
CloseHandleOrDie(child_stdout_);
child_stdout_ = nullptr;
} else {
output_data.append(buffer, n);
}
}
}
if (child_stdin_ != nullptr) {
CloseHandleOrDie(child_stdin_);
child_stdin_ = nullptr;
}
DWORD wait_result = WaitForSingleObject(child_handle_, INFINITE);
if (wait_result == WAIT_FAILED) {
GOOGLE_LOG(FATAL) << "WaitForSingleObject: "
<< Win32ErrorMessage(GetLastError());
} else if (wait_result != WAIT_OBJECT_0) {
GOOGLE_LOG(FATAL) << "WaitForSingleObject: Unexpected return code: "
<< wait_result;
}
DWORD exit_code;
if (!GetExitCodeProcess(child_handle_, &exit_code)) {
GOOGLE_LOG(FATAL) << "GetExitCodeProcess: "
<< Win32ErrorMessage(GetLastError());
}
CloseHandleOrDie(child_handle_);
child_handle_ = nullptr;
if (exit_code != 0) {
*error = strings::Substitute("Plugin failed with status code $0.", exit_code);
return false;
}
if (!output->ParseFromString(output_data)) {
*error = "Plugin output is unparseable: " + CEscape(output_data);
return false;
}
return true;
}
std::string Subprocess::Win32ErrorMessage(DWORD error_code) {
char* message;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPSTR)&message,
0, nullptr);
std::string result = message;
LocalFree(message);
return result;
}
#else
Subprocess::Subprocess()
: … { … }
Subprocess::~Subprocess() { … }
void Subprocess::Start(const std::string& program, SearchMode search_mode) { … }
bool Subprocess::Communicate(const Message& input, Message* output,
std::string* error) { … }
#endif
}
}
}