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