Declare global forkserver start mode flag in header file so it can be overridden in code more easily.

PiperOrigin-RevId: 346588150
Change-Id: I1013b928a45bab164e7db28d49be8504353dc064
This commit is contained in:
Sandboxed API Team 2020-12-09 10:53:06 -08:00 committed by Copybara-Service
parent 7ce4b24cb5
commit c7d8e83d60
3 changed files with 58 additions and 46 deletions

View File

@ -240,13 +240,13 @@ target_link_libraries(sandbox2_global_forkserver
sandbox2::util
sapi::base
sapi::embed_file
sapi::flags
sapi::raw_logging
PUBLIC absl::core_headers
absl::synchronization
sandbox2::comms
sandbox2::fork_client
sandbox2::forkserver_proto
sapi::flags
)
# sandboxed_api/sandbox2:start_global_forkserver_lib_constructor

View File

@ -22,7 +22,6 @@
#include <syscall.h>
#include <unistd.h>
#include <bitset>
#include <csignal>
#include <cstdlib>
#include <string>
@ -45,40 +44,6 @@
#include "sandboxed_api/util/raw_logging.h"
namespace sandbox2 {
namespace {
enum class GlobalForkserverStartMode {
kOnDemand,
// MUST be the last element
kNumGlobalForkserverStartModes,
};
class GlobalForkserverStartModeSet {
public:
static constexpr size_t kSize = static_cast<size_t>(
GlobalForkserverStartMode::kNumGlobalForkserverStartModes);
GlobalForkserverStartModeSet() {}
explicit GlobalForkserverStartModeSet(GlobalForkserverStartMode value) {
value_[static_cast<size_t>(value)] = true;
}
GlobalForkserverStartModeSet& operator|=(GlobalForkserverStartMode value) {
value_[static_cast<size_t>(value)] = true;
return *this;
}
GlobalForkserverStartModeSet operator|(
GlobalForkserverStartMode value) const {
GlobalForkserverStartModeSet rv(*this);
rv |= value;
return rv;
}
bool contains(GlobalForkserverStartMode value) const {
return value_[static_cast<size_t>(value)];
}
bool empty() { return value_.none(); }
private:
std::bitset<kSize> value_;
};
bool AbslParseFlag(absl::string_view text, GlobalForkserverStartModeSet* out,
std::string* error) {
@ -98,6 +63,7 @@ bool AbslParseFlag(absl::string_view text, GlobalForkserverStartModeSet* out,
return true;
}
namespace {
std::string ToString(GlobalForkserverStartMode mode) {
switch (mode) {
case GlobalForkserverStartMode::kOnDemand:
@ -106,6 +72,16 @@ std::string ToString(GlobalForkserverStartMode mode) {
return "unknown";
}
}
bool ValidateStartMode(const char*, const std::string& flag) {
GlobalForkserverStartModeSet unused;
std::string error;
if (!AbslParseFlag(flag, &unused, &error)) {
SAPI_RAW_LOG(ERROR, "%s", error);
return false;
}
return true;
}
} // namespace
std::string AbslUnparseFlag(GlobalForkserverStartModeSet in) {
std::vector<std::string> str_modes;
@ -120,16 +96,7 @@ std::string AbslUnparseFlag(GlobalForkserverStartModeSet in) {
}
return absl::StrJoin(str_modes, ",");
}
bool ValidateStartMode(const char*, const std::string& flag) {
GlobalForkserverStartModeSet unused;
std::string error;
if (!AbslParseFlag(flag, &unused, &error)) {
SAPI_RAW_LOG(ERROR, "%s", error);
return false;
}
return true;
}
} // namespace
} // namespace sandbox2
ABSL_FLAG(string, sandbox2_forkserver_start_mode, "ondemand",

View File

@ -20,7 +20,12 @@
#include <sys/types.h>
#include <bitset>
#include <string>
#include "absl/base/thread_annotations.h"
#include "sandboxed_api/util/flag.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "sandboxed_api/sandbox2/comms.h"
#include "sandboxed_api/sandbox2/fork_client.h"
@ -53,6 +58,46 @@ class GlobalForkClient {
ForkClient fork_client_;
};
enum class GlobalForkserverStartMode {
kOnDemand,
// MUST be the last element
kNumGlobalForkserverStartModes,
};
class GlobalForkserverStartModeSet {
public:
static constexpr size_t kSize = static_cast<size_t>(
GlobalForkserverStartMode::kNumGlobalForkserverStartModes);
GlobalForkserverStartModeSet() {}
explicit GlobalForkserverStartModeSet(GlobalForkserverStartMode value) {
value_[static_cast<size_t>(value)] = true;
}
GlobalForkserverStartModeSet& operator|=(GlobalForkserverStartMode value) {
value_[static_cast<size_t>(value)] = true;
return *this;
}
GlobalForkserverStartModeSet operator|(
GlobalForkserverStartMode value) const {
GlobalForkserverStartModeSet rv(*this);
rv |= value;
return rv;
}
bool contains(GlobalForkserverStartMode value) const {
return value_[static_cast<size_t>(value)];
}
bool empty() { return value_.none(); }
private:
std::bitset<kSize> value_;
};
bool AbslParseFlag(absl::string_view text, GlobalForkserverStartModeSet* out,
std::string* error);
std::string AbslUnparseFlag(GlobalForkserverStartModeSet in);
} // namespace sandbox2
ABSL_DECLARE_FLAG(string, sandbox2_forkserver_start_mode);
#endif // SANDBOXED_API_SANDBOX2_GLOBAL_FORKCLIENT_H_