1*ec63e07aSXin Li // Copyright 2019 Google LLC 2*ec63e07aSXin Li // 3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License"); 4*ec63e07aSXin Li // you may not use this file except in compliance with the License. 5*ec63e07aSXin Li // You may obtain a copy of the License at 6*ec63e07aSXin Li // 7*ec63e07aSXin Li // https://www.apache.org/licenses/LICENSE-2.0 8*ec63e07aSXin Li // 9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software 10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS, 11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*ec63e07aSXin Li // See the License for the specific language governing permissions and 13*ec63e07aSXin Li // limitations under the License. 14*ec63e07aSXin Li 15*ec63e07aSXin Li #include "sandboxed_api/transaction.h" 16*ec63e07aSXin Li 17*ec63e07aSXin Li #include <functional> 18*ec63e07aSXin Li #include <memory> 19*ec63e07aSXin Li 20*ec63e07aSXin Li #include "absl/log/log.h" 21*ec63e07aSXin Li #include "absl/status/status.h" 22*ec63e07aSXin Li #include "absl/time/time.h" 23*ec63e07aSXin Li #include "sandboxed_api/util/status_macros.h" 24*ec63e07aSXin Li 25*ec63e07aSXin Li namespace sapi { 26*ec63e07aSXin Li 27*ec63e07aSXin Li constexpr absl::Duration TransactionBase::kDefaultTimeLimit; 28*ec63e07aSXin Li RunTransactionFunctionInSandbox(const std::function<absl::Status ()> & f)29*ec63e07aSXin Liabsl::Status TransactionBase::RunTransactionFunctionInSandbox( 30*ec63e07aSXin Li const std::function<absl::Status()>& f) { 31*ec63e07aSXin Li // Run Main(), invoking Init() if this hasn't been yet done. 32*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(sandbox_->Init()); 33*ec63e07aSXin Li 34*ec63e07aSXin Li // Set the wall-time limit for this transaction run, and clean it up 35*ec63e07aSXin Li // afterwards, no matter what the result. 36*ec63e07aSXin Li SAPI_RETURN_IF_ERROR( 37*ec63e07aSXin Li sandbox_->SetWallTimeLimit(absl::Seconds(GetTimeLimit()))); 38*ec63e07aSXin Li struct TimeCleanup { 39*ec63e07aSXin Li ~TimeCleanup() { 40*ec63e07aSXin Li capture->sandbox_->SetWallTimeLimit(absl::ZeroDuration()).IgnoreError(); 41*ec63e07aSXin Li } 42*ec63e07aSXin Li TransactionBase* capture; 43*ec63e07aSXin Li } sandbox_cleanup = {this}; 44*ec63e07aSXin Li 45*ec63e07aSXin Li if (!initialized_) { 46*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(Init()); 47*ec63e07aSXin Li initialized_ = true; 48*ec63e07aSXin Li } 49*ec63e07aSXin Li 50*ec63e07aSXin Li return f(); 51*ec63e07aSXin Li } 52*ec63e07aSXin Li RunTransactionLoop(const std::function<absl::Status ()> & f)53*ec63e07aSXin Liabsl::Status TransactionBase::RunTransactionLoop( 54*ec63e07aSXin Li const std::function<absl::Status()>& f) { 55*ec63e07aSXin Li // Try to run Main() for a few times, return error if none of the tries 56*ec63e07aSXin Li // succeeded. 57*ec63e07aSXin Li absl::Status status; 58*ec63e07aSXin Li for (int i = 0; i <= retry_count_; ++i) { 59*ec63e07aSXin Li status = RunTransactionFunctionInSandbox(f); 60*ec63e07aSXin Li if (status.ok()) { 61*ec63e07aSXin Li return status; 62*ec63e07aSXin Li } 63*ec63e07aSXin Li sandbox_->Terminate(); 64*ec63e07aSXin Li initialized_ = false; 65*ec63e07aSXin Li } 66*ec63e07aSXin Li 67*ec63e07aSXin Li LOG(ERROR) << "Tried " << (retry_count_ + 1) << " times to run the " 68*ec63e07aSXin Li << "transaction, but it failed. SAPI error: '" << status 69*ec63e07aSXin Li << "'. Latest sandbox error: '" 70*ec63e07aSXin Li << sandbox_->AwaitResult().ToString() << "'"; 71*ec63e07aSXin Li return status; 72*ec63e07aSXin Li } 73*ec63e07aSXin Li ~TransactionBase()74*ec63e07aSXin LiTransactionBase::~TransactionBase() { 75*ec63e07aSXin Li if (!initialized_) { 76*ec63e07aSXin Li return; 77*ec63e07aSXin Li } 78*ec63e07aSXin Li if (absl::Status status = Finish(); !status.ok()) { 79*ec63e07aSXin Li LOG(ERROR) << "Transaction finalizer returned an error: " << status; 80*ec63e07aSXin Li } 81*ec63e07aSXin Li } 82*ec63e07aSXin Li 83*ec63e07aSXin Li } // namespace sapi 84