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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/transaction.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
|
|
|
constexpr absl::Duration TransactionBase::kDefaultTimeLimit;
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status TransactionBase::RunTransactionFunctionInSandbox(
|
|
|
|
const std::function<absl::Status()>& f) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Run Main(), invoking Init() if this hasn't been yet done.
|
2020-02-18 21:27:08 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(sandbox_->Init());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Set the wall-time limit for this transaction run, and clean it up
|
|
|
|
// afterwards, no matter what the result.
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(
|
|
|
|
sandbox_->SetWallTimeLimit(absl::Seconds(GetTimeLimit())));
|
2019-03-19 00:21:48 +08:00
|
|
|
struct TimeCleanup {
|
|
|
|
~TimeCleanup() {
|
2020-08-07 15:30:04 +08:00
|
|
|
capture->sandbox_->SetWallTimeLimit(absl::ZeroDuration()).IgnoreError();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
TransactionBase* capture;
|
2020-02-18 21:27:08 +08:00
|
|
|
} sandbox_cleanup = {this};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-02-18 21:27:08 +08:00
|
|
|
if (!initialized_) {
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(Init());
|
2020-02-18 21:27:08 +08:00
|
|
|
initialized_ = true;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return f();
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status TransactionBase::RunTransactionLoop(
|
|
|
|
const std::function<absl::Status()>& f) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Try to run Main() for a few times, return error if none of the tries
|
|
|
|
// succeeded.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status status;
|
2020-02-18 21:27:08 +08:00
|
|
|
for (int i = 0; i <= retry_count_; ++i) {
|
2019-03-19 00:21:48 +08:00
|
|
|
status = RunTransactionFunctionInSandbox(f);
|
|
|
|
if (status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-02-18 21:27:08 +08:00
|
|
|
sandbox_->Terminate();
|
|
|
|
initialized_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-18 21:27:08 +08:00
|
|
|
LOG(ERROR) << "Tried " << (retry_count_ + 1) << " times to run the "
|
2019-03-19 00:21:48 +08:00
|
|
|
<< "transaction, but it failed. SAPI error: '" << status
|
|
|
|
<< "'. Latest sandbox error: '"
|
2020-02-18 21:27:08 +08:00
|
|
|
<< sandbox_->AwaitResult().ToString() << "'";
|
2019-03-19 00:21:48 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionBase::~TransactionBase() {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!initialized_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (absl::Status status = Finish(); !status.ok()) {
|
|
|
|
LOG(ERROR) << "Transaction finalizer returned an error: " << status;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sapi
|