2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Implementation file for the sandbox2::Sandbox class.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
|
|
|
|
|
|
|
#include <csignal>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "absl/memory/memory.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-11-29 00:06:37 +08:00
|
|
|
#include "absl/time/time.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/monitor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
Sandbox2::~Sandbox2() {
|
|
|
|
if (monitor_thread_ && monitor_thread_->joinable()) {
|
|
|
|
monitor_thread_->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<Result> Sandbox2::AwaitResultWithTimeout(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::Duration timeout) {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
|
|
|
CHECK(monitor_thread_ != nullptr) << "Sandbox was already waited on";
|
|
|
|
|
2019-05-15 23:09:39 +08:00
|
|
|
auto done =
|
|
|
|
monitor_->done_notification_.WaitForNotificationWithTimeout(timeout);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!done) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::DeadlineExceededError("Sandbox did not finish within timeout");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
monitor_thread_->join();
|
|
|
|
|
|
|
|
CHECK(IsTerminated()) << "Monitor did not terminate";
|
|
|
|
|
|
|
|
// Reset the Monitor Thread object to its initial state, as to mark that this
|
|
|
|
// object cannot be used anymore to control behavior of the sandboxee (e.g.
|
|
|
|
// via signals).
|
|
|
|
monitor_thread_.reset(nullptr);
|
|
|
|
|
|
|
|
VLOG(1) << "Final execution status: " << monitor_->result_.ToString();
|
|
|
|
CHECK(monitor_->result_.final_status() != Result::UNSET);
|
|
|
|
return std::move(monitor_->result_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Sandbox2::AwaitResult() {
|
2020-04-02 22:42:17 +08:00
|
|
|
return AwaitResultWithTimeout(absl::InfiniteDuration()).value();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sandbox2::RunAsync() {
|
|
|
|
Launch();
|
|
|
|
|
|
|
|
// If the sandboxee setup failed we return 'false' here.
|
|
|
|
if (monitor_->IsDone() &&
|
|
|
|
monitor_->result_.final_status() == Result::SETUP_ERROR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:46:26 +08:00
|
|
|
void Sandbox2::NotifyMonitor() {
|
|
|
|
if (monitor_thread_ != nullptr) {
|
|
|
|
pthread_kill(monitor_thread_->native_handle(), SIGCHLD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
void Sandbox2::Kill() {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
|
|
|
|
2019-05-15 22:46:26 +08:00
|
|
|
monitor_->external_kill_request_flag_.clear(std::memory_order_relaxed);
|
|
|
|
NotifyMonitor();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox2::DumpStackTrace() {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
|
|
|
|
2019-05-15 22:46:26 +08:00
|
|
|
monitor_->dump_stack_request_flag_.clear(std::memory_order_relaxed);
|
|
|
|
NotifyMonitor();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sandbox2::IsTerminated() const {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
|
|
|
|
|
|
|
return monitor_->IsDone();
|
|
|
|
}
|
|
|
|
|
2019-11-29 00:06:37 +08:00
|
|
|
void Sandbox2::set_walltime_limit(absl::Duration limit) const {
|
|
|
|
if (limit == absl::ZeroDuration()) {
|
2019-05-15 22:46:26 +08:00
|
|
|
VLOG(1) << "Disarming walltime timer to ";
|
|
|
|
monitor_->deadline_millis_.store(0, std::memory_order_relaxed);
|
|
|
|
} else {
|
2019-11-29 00:06:37 +08:00
|
|
|
VLOG(1) << "Will set the walltime timer to " << limit;
|
|
|
|
absl::Time deadline = absl::Now() + limit;
|
2019-05-15 22:46:26 +08:00
|
|
|
monitor_->deadline_millis_.store(absl::ToUnixMillis(deadline),
|
|
|
|
std::memory_order_relaxed);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox2::Launch() {
|
|
|
|
monitor_ =
|
|
|
|
absl::make_unique<Monitor>(executor_.get(), policy_.get(), notify_.get());
|
|
|
|
monitor_thread_ =
|
|
|
|
absl::make_unique<std::thread>(&Monitor::Run, monitor_.get());
|
|
|
|
|
|
|
|
// Wait for the Monitor to set-up the sandboxee correctly (or fail while
|
|
|
|
// doing that). From here on, it is safe to use the IPC object for
|
|
|
|
// non-sandbox-related data exchange.
|
2019-05-15 23:09:39 +08:00
|
|
|
monitor_->setup_notification_.WaitForNotification();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sandbox2
|