Skip to content

Commit fa82150

Browse files
cblichmanncopybara-github
authored andcommitted
Refactor forkserver_bin.cc into a sandbox2 namespace
This change moves the main logic into `sandbox2::ForkserverMain`, improves error logging for status messages, and slightly refactors the child process handling within the `ServeRequest` loop. PiperOrigin-RevId: 899569633 Change-Id: Ifb5f3ec4c208b68c0d3991500c54defefc43253d
1 parent 8443279 commit fa82150

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

sandboxed_api/sandbox2/forkserver_bin.cc

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <csignal>
1818
#include <cstdlib>
19-
#include <string>
2019

2120
#include "absl/base/log_severity.h"
2221
#include "absl/log/globals.h"
@@ -28,17 +27,22 @@
2827
#include "sandboxed_api/sandbox2/unwind/unwind.h"
2928
#include "sandboxed_api/util/raw_logging.h"
3029

31-
int main() {
30+
namespace sandbox2 {
31+
namespace {
32+
33+
int ForkserverMain() {
3234
SAPI_RAW_PCHECK(setpgid(0, 0) == 0, "setpgid(0, 0) failed");
33-
// Make sure the logs go stderr.
35+
36+
// Make sure the logs go stderr. We won't initialize the logging library.
3437
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
3538

3639
// Close all non-essential FDs to keep newly opened FD numbers consistent.
37-
absl::Status status = sandbox2::sanitizer::CloseAllFDsExcept(
38-
{0, 1, 2, sandbox2::Comms::kSandbox2ClientCommsFD});
39-
40-
if (!status.ok()) {
41-
SAPI_RAW_LOG(WARNING, "Closing non-essential FDs failed");
40+
if (absl::Status status = sanitizer::CloseAllFDsExcept(
41+
{0, 1, 2, Comms::kSandbox2ClientCommsFD});
42+
!status.ok()) {
43+
SAPI_RAW_LOG(WARNING, "Closing non-essential FDs failed: %.*s",
44+
static_cast<int>(status.message().size()),
45+
status.message().data());
4246
}
4347

4448
// Make the process' name easily recognizable with ps/pstree.
@@ -51,32 +55,43 @@ int main() {
5155
// the parent goes down (or if the GlobalForkServerComms is closed), which is
5256
// assured by prctl(PR_SET_PDEATHSIG, SIGKILL) being called in the
5357
// ForkServer::Initialize(). We don't want to change behavior of non-global
54-
// ForkServers, hence it's called here and not in the
55-
// ForkServer::Initialize().
56-
struct sigaction sa;
57-
sa.sa_handler = SIG_IGN;
58-
sa.sa_flags = 0;
59-
sigemptyset(&sa.sa_mask);
60-
if (sigaction(SIGTERM, &sa, nullptr) == -1) {
61-
SAPI_RAW_PLOG(WARNING, "sigaction(SIGTERM, sa_handler=SIG_IGN)");
58+
// ForkServers, hence it's called here and not in ForkServer::Initialize().
59+
{
60+
struct sigaction sa;
61+
sa.sa_handler = SIG_IGN;
62+
sa.sa_flags = 0;
63+
sigemptyset(&sa.sa_mask);
64+
if (sigaction(SIGTERM, &sa, nullptr) == -1) {
65+
SAPI_RAW_PLOG(WARNING, "sigaction(SIGTERM, sa_handler=SIG_IGN)");
66+
}
6267
}
6368

64-
sandbox2::Comms comms(sandbox2::Comms::kDefaultConnection);
65-
sandbox2::ForkServer fork_server(&comms);
66-
sandbox2::sanitizer::WaitForSanitizer();
69+
Comms comms(Comms::kDefaultConnection);
70+
ForkServer fork_server(&comms);
71+
sanitizer::WaitForSanitizer();
6772

6873
while (!fork_server.IsTerminated()) {
69-
pid_t child_pid = fork_server.ServeRequest();
70-
if (child_pid == 0) {
71-
sandbox2::Client client(&comms);
72-
client.SandboxMeHere();
73-
auto status = sandbox2::RunLibUnwindAndSymbolizer(&comms);
74-
if (!status.ok()) {
75-
SAPI_RAW_LOG(ERROR, "RunLibUnwindAndSymbolizer failed: %s",
76-
std::string(status.message()).c_str());
77-
}
78-
return status.ok() ? EXIT_SUCCESS : EXIT_FAILURE;
74+
if (fork_server.ServeRequest() != 0) {
75+
// Non-child process or error. Errors are logged internally.
76+
continue;
7977
}
78+
79+
Client client(&comms);
80+
client.SandboxMeHere();
81+
82+
if (absl::Status status = RunLibUnwindAndSymbolizer(&comms); !status.ok()) {
83+
SAPI_RAW_LOG(ERROR, "RunLibUnwindAndSymbolizer failed: %.*s",
84+
static_cast<int>(status.message().size()),
85+
status.message().data());
86+
return EXIT_FAILURE;
87+
}
88+
return EXIT_SUCCESS;
8089
}
8190
SAPI_RAW_VLOG(1, "ForkServer Comms closed. Exiting");
91+
return EXIT_SUCCESS;
8292
}
93+
94+
} // namespace
95+
} // namespace sandbox2
96+
97+
int main() { return sandbox2::ForkserverMain(); }

0 commit comments

Comments
 (0)