Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_ |
| 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_ |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 19 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 20 | #include <memory> |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 23 | #include <base/bind.h> |
Alex Deymo | 0bb2341 | 2015-06-19 00:04:46 -0700 | [diff] [blame] | 24 | #include <base/location.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 26 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 27 | #include "update_engine/update_manager/evaluation_context.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 28 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 29 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 30 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 31 | template <typename R, typename... Args> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 32 | EvalStatus UpdateManager::EvaluatePolicy( |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 33 | EvaluationContext* ec, |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 34 | EvalStatus (Policy::*policy_method)( |
| 35 | EvaluationContext*, State*, std::string*, R*, Args...) const, |
| 36 | R* result, |
| 37 | Args... args) { |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 38 | // If expiration timeout fired, dump the context and reset expiration. |
| 39 | // IMPORTANT: We must still proceed with evaluation of the policy in this |
| 40 | // case, so that the evaluation time (and corresponding reevaluation timeouts) |
| 41 | // are readjusted. |
| 42 | if (ec->is_expired()) { |
| 43 | LOG(WARNING) << "Request timed out, evaluation context: " |
| 44 | << ec->DumpContext(); |
| 45 | ec->ResetExpiration(); |
| 46 | } |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 47 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 48 | // Reset the evaluation context. |
| 49 | ec->ResetEvaluation(); |
| 50 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 51 | const std::string policy_name = policy_->PolicyRequestName(policy_method); |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 52 | |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 53 | // First try calling the actual policy. |
| 54 | std::string error; |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 55 | EvalStatus status = (policy_.get()->*policy_method)( |
| 56 | ec, state_.get(), &error, result, args...); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 57 | // If evaluating the main policy failed, defer to the default policy. |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 58 | if (status == EvalStatus::kFailed) { |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 59 | LOG(WARNING) << "Evaluating policy failed: " << error |
| 60 | << "\nEvaluation context: " << ec->DumpContext(); |
| 61 | error.clear(); |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 62 | status = (default_policy_.*policy_method)( |
| 63 | ec, state_.get(), &error, result, args...); |
Gilad Arnold | fd45a73 | 2014-08-07 15:53:46 -0700 | [diff] [blame] | 64 | if (status == EvalStatus::kFailed) { |
| 65 | LOG(WARNING) << "Evaluating default policy failed: " << error; |
| 66 | } else if (status == EvalStatus::kAskMeAgainLater) { |
| 67 | LOG(ERROR) |
| 68 | << "Default policy would block; this is a bug, forcing failure."; |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 69 | status = EvalStatus::kFailed; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 72 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 73 | return status; |
| 74 | } |
| 75 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 76 | template <typename R, typename... Args> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 77 | void UpdateManager::OnPolicyReadyToEvaluate( |
Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 78 | std::shared_ptr<EvaluationContext> ec, |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 79 | base::Callback<void(EvalStatus status, const R& result)> callback, |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 80 | EvalStatus (Policy::*policy_method)( |
| 81 | EvaluationContext*, State*, std::string*, R*, Args...) const, |
Alex Deymo | e75e025 | 2014-04-08 14:00:11 -0700 | [diff] [blame] | 82 | Args... args) { |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 83 | // Evaluate the policy. |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 84 | R result; |
Alex Vakulenko | 9c155d2 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 85 | EvalStatus status = EvaluatePolicy(ec.get(), policy_method, &result, args...); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 86 | |
| 87 | if (status != EvalStatus::kAskMeAgainLater) { |
| 88 | // AsyncPolicyRequest finished. |
| 89 | callback.Run(status, result); |
| 90 | return; |
| 91 | } |
Alex Deymo | 53556ec | 2014-03-17 10:05:57 -0700 | [diff] [blame] | 92 | |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 93 | // Re-schedule the policy request based on used variables. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 94 | base::Closure reeval_callback = |
| 95 | base::Bind(&UpdateManager::OnPolicyReadyToEvaluate<R, Args...>, |
| 96 | base::Unretained(this), |
| 97 | ec, |
| 98 | callback, |
| 99 | policy_method, |
| 100 | args...); |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 101 | if (ec->RunOnValueChangeOrTimeout(reeval_callback)) |
| 102 | return; // Reevaluation scheduled successfully. |
| 103 | |
| 104 | // Scheduling a reevaluation can fail because policy method didn't use any |
| 105 | // non-const variable nor there's any time-based event that will change the |
| 106 | // status of evaluation. Alternatively, this may indicate an error in the use |
| 107 | // of the scheduling interface. |
| 108 | LOG(ERROR) << "Failed to schedule a reevaluation of policy " |
| 109 | << policy_->PolicyRequestName(policy_method) << "; this is a bug."; |
| 110 | callback.Run(status, result); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 113 | template <typename R, typename... ActualArgs, typename... ExpectedArgs> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 114 | EvalStatus UpdateManager::PolicyRequest( |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 115 | EvalStatus (Policy::*policy_method)( |
| 116 | EvaluationContext*, State*, std::string*, R*, ExpectedArgs...) const, |
| 117 | R* result, |
| 118 | ActualArgs... args) { |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 119 | auto ec = std::make_shared<EvaluationContext>(evaluation_timeout_); |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 120 | // A PolicyRequest always consists on a single evaluation on a new |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 121 | // EvaluationContext. |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 122 | // IMPORTANT: To ensure that ActualArgs can be converted to ExpectedArgs, we |
| 123 | // explicitly instantiate EvaluatePolicy with the latter in lieu of the |
| 124 | // former. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 125 | EvalStatus ret = EvaluatePolicy<R, ExpectedArgs...>( |
| 126 | ec.get(), policy_method, result, args...); |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 127 | // Sync policy requests must not block, if they do then this is an error. |
| 128 | DCHECK(EvalStatus::kAskMeAgainLater != ret); |
| 129 | LOG_IF(WARNING, EvalStatus::kAskMeAgainLater == ret) |
Gilad Arnold | f9f85d6 | 2014-06-19 18:07:01 -0700 | [diff] [blame] | 130 | << "Sync request used with an async policy; this is a bug"; |
Gilad Arnold | 897b5e5 | 2014-05-21 09:37:18 -0700 | [diff] [blame] | 131 | return ret; |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 134 | template <typename R, typename... ActualArgs, typename... ExpectedArgs> |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 135 | void UpdateManager::AsyncPolicyRequest( |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 136 | base::Callback<void(EvalStatus, const R& result)> callback, |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 137 | EvalStatus (Policy::*policy_method)( |
| 138 | EvaluationContext*, State*, std::string*, R*, ExpectedArgs...) const, |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 139 | ActualArgs... args) { |
Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 140 | auto ec = std::make_shared<EvaluationContext>( |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 141 | evaluation_timeout_, |
| 142 | expiration_timeout_, |
| 143 | std::unique_ptr<base::Callback<void(EvaluationContext*)>>( |
| 144 | new base::Callback<void(EvaluationContext*)>( |
| 145 | base::Bind(&UpdateManager::UnregisterEvalContext, |
| 146 | weak_ptr_factory_.GetWeakPtr())))); |
Amin Hassani | a2c8b92 | 2019-08-14 19:41:03 -0700 | [diff] [blame] | 147 | if (!ec_repo_.insert(ec).second) { |
Gilad Arnold | 83ffdda | 2014-08-08 13:30:31 -0700 | [diff] [blame] | 148 | LOG(ERROR) << "Failed to register evaluation context; this is a bug."; |
| 149 | } |
| 150 | |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 151 | // IMPORTANT: To ensure that ActualArgs can be converted to ExpectedArgs, we |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 152 | // explicitly instantiate UpdateManager::OnPolicyReadyToEvaluate with the |
Gilad Arnold | 13a8243 | 2014-05-19 12:52:44 -0700 | [diff] [blame] | 153 | // latter in lieu of the former. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 154 | base::Closure eval_callback = |
| 155 | base::Bind(&UpdateManager::OnPolicyReadyToEvaluate<R, ExpectedArgs...>, |
| 156 | base::Unretained(this), |
| 157 | ec, |
| 158 | callback, |
| 159 | policy_method, |
| 160 | args...); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 161 | brillo::MessageLoop::current()->PostTask(FROM_HERE, eval_callback); |
Alex Deymo | 7b948f0 | 2014-03-10 17:01:10 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 164 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 165 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 166 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_INL_H_ |