#include "gn/exec_process.h"
#include <stddef.h>
#include <memory>
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "util/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#include "base/win/scoped_handle.h"
#include "base/win/scoped_process_information.h"
#include "base/win/win_util.h"
#else
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include "base/posix/eintr_wrapper.h"
#include "base/posix/file_descriptor_shuffle.h"
#endif
namespace internal {
#if defined(OS_WIN)
bool ExecProcess(const base::CommandLine& cmdline,
const base::FilePath& startup_dir,
std::string* std_out,
std::string* std_err,
int* exit_code) {
return ExecProcess(cmdline.GetCommandLineString(), startup_dir, std_out,
std_err, exit_code);
}
bool ExecProcess(const std::u16string& cmdline_str,
const base::FilePath& startup_dir,
std::string* std_out,
std::string* std_err,
int* exit_code) {
SECURITY_ATTRIBUTES sa_attr;
sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
sa_attr.bInheritHandle = TRUE;
sa_attr.lpSecurityDescriptor = nullptr;
HANDLE out_read = nullptr;
HANDLE out_write = nullptr;
if (!CreatePipe(&out_read, &out_write, &sa_attr, 0)) {
NOTREACHED() << "Failed to create pipe";
return false;
}
base::win::ScopedHandle scoped_out_read(out_read);
base::win::ScopedHandle scoped_out_write(out_write);
HANDLE err_read = nullptr;
HANDLE err_write = nullptr;
if (!CreatePipe(&err_read, &err_write, &sa_attr, 0)) {
NOTREACHED() << "Failed to create pipe";
return false;
}
base::win::ScopedHandle scoped_err_read(err_read);
base::win::ScopedHandle scoped_err_write(err_write);
if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) {
NOTREACHED() << "Failed to disable pipe inheritance";
return false;
}
if (!SetHandleInformation(err_read, HANDLE_FLAG_INHERIT, 0)) {
NOTREACHED() << "Failed to disable pipe inheritance";
return false;
}
STARTUPINFO start_info = {};
start_info.cb = sizeof(STARTUPINFO);
start_info.hStdOutput = out_write;
start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
start_info.dwFlags |= STARTF_USESTDHANDLES;
std::u16string cmdline_writable = cmdline_str;
PROCESS_INFORMATION temp_process_info = {};
if (!CreateProcess(
nullptr, base::ToWCharT(&cmdline_writable[0]), nullptr, nullptr,
TRUE,
NORMAL_PRIORITY_CLASS, nullptr, base::ToWCharT(&startup_dir.value()),
&start_info, &temp_process_info)) {
return false;
}
base::win::ScopedProcessInformation proc_info(temp_process_info);
scoped_out_write.Close();
scoped_err_write.Close();
const int kBufferSize = 1024;
char buffer[kBufferSize];
for (;;) {
DWORD bytes_read = 0;
BOOL success =
ReadFile(out_read, buffer, kBufferSize, &bytes_read, nullptr);
if (!success || bytes_read == 0)
break;
std_out->append(buffer, bytes_read);
}
WaitForSingleObject(proc_info.process_handle(), INFINITE);
DWORD dw_exit_code;
GetExitCodeProcess(proc_info.process_handle(), &dw_exit_code);
*exit_code = static_cast<int>(dw_exit_code);
return true;
}
#else
bool ReadFromPipe(int fd, std::string* output) {
char buffer[256];
int bytes_read = HANDLE_EINTR(read(fd, buffer, sizeof(buffer)));
if (bytes_read == -1) {
return errno == EAGAIN || errno == EWOULDBLOCK;
} else if (bytes_read <= 0) {
return false;
}
output->append(buffer, bytes_read);
return true;
}
bool WaitForExit(int pid, int* exit_code) {
int status;
if (waitpid(pid, &status, 0) < 0) {
PLOG(ERROR) << "waitpid";
return false;
}
if (WIFEXITED(status)) {
*exit_code = WEXITSTATUS(status);
return true;
} else if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGTERM ||
WTERMSIG(status) == SIGHUP)
return false;
}
return false;
}
bool ExecProcess(const base::CommandLine& cmdline,
const base::FilePath& startup_dir,
std::string* std_out,
std::string* std_err,
int* exit_code) {
*exit_code = EXIT_FAILURE;
std::vector<std::string> argv = cmdline.argv();
int out_fd[2], err_fd[2];
pid_t pid;
base::InjectiveMultimap fd_shuffle1, fd_shuffle2;
std::unique_ptr<char*[]> argv_cstr(new char*[argv.size() + 1]);
fd_shuffle1.reserve(3);
fd_shuffle2.reserve(3);
if (pipe(out_fd) < 0)
return false;
base::ScopedFD out_read(out_fd[0]), out_write(out_fd[1]);
if (pipe(err_fd) < 0)
return false;
base::ScopedFD err_read(err_fd[0]), err_write(err_fd[1]);
if (out_read.get() >= FD_SETSIZE || err_read.get() >= FD_SETSIZE)
return false;
switch (pid = fork()) {
case -1:
return false;
case 0:
{
#if defined(OS_MAC)
sigignore(SIGTRAP);
#endif
int dev_null = open("/dev/null", O_WRONLY);
if (dev_null < 0)
_exit(127);
fd_shuffle1.push_back(
base::InjectionArc(out_write.get(), STDOUT_FILENO, true));
fd_shuffle1.push_back(
base::InjectionArc(err_write.get(), STDERR_FILENO, true));
fd_shuffle1.push_back(base::InjectionArc(dev_null, STDIN_FILENO, true));
for (size_t i = 0; i < fd_shuffle1.size(); ++i)
fd_shuffle2.push_back(fd_shuffle1[i]);
if (!ShuffleFileDescriptors(&fd_shuffle1))
_exit(127);
base::SetCurrentDirectory(startup_dir);
for (size_t i = 0; i < argv.size(); i++)
argv_cstr[i] = const_cast<char*>(argv[i].c_str());
argv_cstr[argv.size()] = nullptr;
execvp(argv_cstr[0], argv_cstr.get());
_exit(127);
break;
}
default:
{
out_write.reset();
err_write.reset();
bool out_open = true, err_open = true;
while (out_open || err_open) {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(out_read.get(), &read_fds);
FD_SET(err_read.get(), &read_fds);
int res =
HANDLE_EINTR(select(std::max(out_read.get(), err_read.get()) + 1,
&read_fds, nullptr, nullptr, nullptr));
if (res <= 0)
break;
if (FD_ISSET(out_read.get(), &read_fds))
out_open = ReadFromPipe(out_read.get(), std_out);
if (FD_ISSET(err_read.get(), &read_fds))
err_open = ReadFromPipe(err_read.get(), std_err);
}
return WaitForExit(pid, exit_code);
}
}
return false;
}
#endif
}