-
Notifications
You must be signed in to change notification settings - Fork 489
Restore basic functionality of the C++ process wrapper #1728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
scentini
wants to merge
5
commits into
bazelbuild:main
Choose a base branch
from
scentini:cc_determinism
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02ed874
Restore basic functionality of the C++ process wrapper
scentini e1c3803
Address failures
scentini c305b89
Address failures
scentini 4cb1402
Merge branch 'main' into cc_determinism
UebelAndre c990860
Apply suggestions from code review
UebelAndre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <algorithm> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include "util/process_wrapper/system.h" | ||
|
||
using CharType = process_wrapper::System::StrType::value_type; | ||
|
||
// A basic process wrapper whose only purpose is to preserve determinism when | ||
// building the true process wrapper. We do this by passing --remap-path-prefix=$pwd= | ||
// to the command line. | ||
int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) { | ||
using namespace process_wrapper; | ||
|
||
System::EnvironmentBlock environment_block; | ||
|
||
// Taking all environment variables from the current process | ||
// and sending them down to the child process | ||
for (int i = 0; envp[i] != nullptr; ++i) { | ||
environment_block.push_back(envp[i]); | ||
} | ||
|
||
System::StrType exec_path = argv[1]; | ||
|
||
System::Arguments arguments; | ||
|
||
for (int i = 2; i < argc; ++i) { | ||
arguments.push_back(argv[i]); | ||
} | ||
System::StrType pwd_prefix = | ||
PW_SYS_STR("--remap-path-prefix=") + System::GetWorkingDirectory() + PW_SYS_STR("="); | ||
arguments.push_back(pwd_prefix); | ||
|
||
return System::Exec(exec_path, arguments, environment_block); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef LIB_PROCESS_WRAPPER_SYSTEM_H_ | ||
#define LIB_PROCESS_WRAPPER_SYSTEM_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#if defined(_WIN32) && defined(UNICODE) | ||
#define PW_WIN_UNICODE | ||
#endif // defined(_WIN32) && defined(UNICODE) | ||
|
||
#if defined(PW_WIN_UNICODE) | ||
#define PW_SYS_STR(str) L##str | ||
#define PW_MAIN wmain | ||
#else | ||
#define PW_SYS_STR(str) str | ||
#define PW_MAIN main | ||
#endif | ||
|
||
namespace process_wrapper { | ||
|
||
class System { | ||
public: | ||
#if defined(PW_WIN_UNICODE) | ||
using StrType = std::wstring; | ||
#else | ||
using StrType = std::string; | ||
#endif // defined(PW_WIN_UNICODE) | ||
|
||
using StrVecType = std::vector<StrType>; | ||
using Arguments = StrVecType; | ||
using EnvironmentBlock = StrVecType; | ||
|
||
public: | ||
// Gets the working directory of the current process | ||
static StrType GetWorkingDirectory(); | ||
|
||
// Simple function to execute a process that inherits all the current | ||
// process handles. | ||
// Even if the function doesn't modify global state it is not reentrant | ||
// It is meant to be called once during the lifetime of the parent process | ||
static int Exec(const StrType& executable, const Arguments& arguments, | ||
const EnvironmentBlock& environment_block); | ||
}; | ||
|
||
} // namespace process_wrapper | ||
|
||
#endif // LIB_PROCESS_WRAPPER_SYSTEM_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include "util/process_wrapper/system.h" | ||
|
||
// posix headers | ||
#include <fcntl.h> | ||
#include <signal.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <unistd.h> | ||
|
||
#include <cerrno> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
namespace process_wrapper { | ||
|
||
namespace { | ||
|
||
class OutputPipe { | ||
public: | ||
static constexpr size_t kReadEndDesc = 0; | ||
static constexpr size_t kWriteEndDesc = 1; | ||
|
||
~OutputPipe() { | ||
CloseReadEnd(); | ||
CloseWriteEnd(); | ||
} | ||
|
||
int CreateEnds() { | ||
if (pipe(output_pipe_desc_) != 0) { | ||
std::cerr << "process wrapper error: failed to open the stdout pipes.\n"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
void DupWriteEnd(int newfd) { | ||
dup2(output_pipe_desc_[kWriteEndDesc], newfd); | ||
CloseReadEnd(); | ||
CloseWriteEnd(); | ||
} | ||
|
||
void CloseReadEnd() { Close(kReadEndDesc); } | ||
void CloseWriteEnd() { Close(kWriteEndDesc); } | ||
|
||
int ReadEndDesc() const { return output_pipe_desc_[kReadEndDesc]; } | ||
int WriteEndDesc() const { return output_pipe_desc_[kWriteEndDesc]; } | ||
|
||
private: | ||
void Close(size_t idx) { | ||
if (output_pipe_desc_[idx] > 0) { | ||
close(output_pipe_desc_[idx]); | ||
} | ||
output_pipe_desc_[idx] = -1; | ||
} | ||
int output_pipe_desc_[2] = {-1}; | ||
}; | ||
|
||
} // namespace | ||
|
||
System::StrType System::GetWorkingDirectory() { | ||
const size_t kMaxBufferLength = 4096; | ||
char cwd[kMaxBufferLength]; | ||
if (getcwd(cwd, sizeof(cwd)) == NULL) { | ||
return System::StrType{}; | ||
} | ||
return System::StrType{cwd}; | ||
} | ||
|
||
int System::Exec(const System::StrType &executable, | ||
const System::Arguments &arguments, | ||
const System::EnvironmentBlock &environment_block) { | ||
OutputPipe stdout_pipe; | ||
if (!stdout_pipe.CreateEnds()) { | ||
return -1; | ||
} | ||
OutputPipe stderr_pipe; | ||
if (!stderr_pipe.CreateEnds()) { | ||
return -1; | ||
} | ||
|
||
pid_t child_pid = fork(); | ||
if (child_pid < 0) { | ||
std::cerr << "process wrapper error: failed to fork the current process: " | ||
<< std::strerror(errno) << ".\n"; | ||
return -1; | ||
} else if (child_pid == 0) { | ||
std::vector<char *> argv; | ||
argv.push_back(const_cast<char *>(executable.c_str())); | ||
for (const StrType &argument : arguments) { | ||
argv.push_back(const_cast<char *>(argument.c_str())); | ||
} | ||
argv.push_back(nullptr); | ||
|
||
std::vector<char *> envp; | ||
for (const StrType &ev : environment_block) { | ||
envp.push_back(const_cast<char *>(ev.c_str())); | ||
} | ||
envp.push_back(nullptr); | ||
|
||
umask(022); | ||
execve(executable.c_str(), argv.data(), envp.data()); | ||
std::cerr << "process wrapper error: failed to exec the new process: " | ||
<< std::strerror(errno) << ".\n"; | ||
return -1; | ||
} | ||
|
||
int err, exit_status; | ||
do { | ||
err = waitpid(child_pid, &exit_status, 0); | ||
} while (err == -1 && errno == EINTR); | ||
|
||
if (WIFEXITED(exit_status)) { | ||
return WEXITSTATUS(exit_status); | ||
} else if (WIFSIGNALED(exit_status)) { | ||
raise(WTERMSIG(exit_status)); | ||
} else if (WIFSTOPPED(exit_status)) { | ||
raise(WSTOPSIG(exit_status)); | ||
} else { | ||
std::cerr << "process wrapper error: failed to parse exit code of the " | ||
"child process: " | ||
<< exit_status << ".\n"; | ||
} | ||
return -1; | ||
} | ||
|
||
} // namespace process_wrapper |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.