| 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 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/chromeos_policy.h" | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 18 |  | 
| Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 19 | #include <algorithm> | 
| Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 20 | #include <set> | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 21 | #include <string> | 
|  | 22 |  | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 23 | #include <base/logging.h> | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 24 | #include <base/strings/string_util.h> | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 25 | #include <base/time/time.h> | 
|  | 26 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/error_code.h" | 
|  | 28 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 29 | #include "update_engine/update_manager/device_policy_provider.h" | 
|  | 30 | #include "update_engine/update_manager/policy_utils.h" | 
|  | 31 | #include "update_engine/update_manager/shill_provider.h" | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 32 |  | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 33 | using base::Time; | 
|  | 34 | using base::TimeDelta; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 35 | using chromeos_update_engine::ErrorCode; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 36 | using std::get; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 37 | using std::max; | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 38 | using std::min; | 
| Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 39 | using std::set; | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 40 | using std::string; | 
|  | 41 |  | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 42 | namespace { | 
|  | 43 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 44 | // Examines |err_code| and decides whether the URL index needs to be advanced, | 
|  | 45 | // the error count for the URL incremented, or none of the above. In the first | 
|  | 46 | // case, returns true; in the second case, increments |*url_num_error_p| and | 
|  | 47 | // returns false; otherwise just returns false. | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 48 | // | 
|  | 49 | // TODO(garnold) Adapted from PayloadState::UpdateFailed() (to be retired). | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 50 | bool HandleErrorCode(ErrorCode err_code, int* url_num_error_p) { | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 51 | err_code = chromeos_update_engine::utils::GetBaseErrorCode(err_code); | 
|  | 52 | switch (err_code) { | 
|  | 53 | // Errors which are good indicators of a problem with a particular URL or | 
|  | 54 | // the protocol used in the URL or entities in the communication channel | 
|  | 55 | // (e.g. proxies). We should try the next available URL in the next update | 
|  | 56 | // check to quickly recover from these errors. | 
|  | 57 | case ErrorCode::kPayloadHashMismatchError: | 
|  | 58 | case ErrorCode::kPayloadSizeMismatchError: | 
|  | 59 | case ErrorCode::kDownloadPayloadVerificationError: | 
|  | 60 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: | 
|  | 61 | case ErrorCode::kSignedDeltaPayloadExpectedError: | 
|  | 62 | case ErrorCode::kDownloadInvalidMetadataMagicString: | 
|  | 63 | case ErrorCode::kDownloadSignatureMissingInManifest: | 
|  | 64 | case ErrorCode::kDownloadManifestParseError: | 
|  | 65 | case ErrorCode::kDownloadMetadataSignatureError: | 
|  | 66 | case ErrorCode::kDownloadMetadataSignatureVerificationError: | 
|  | 67 | case ErrorCode::kDownloadMetadataSignatureMismatch: | 
|  | 68 | case ErrorCode::kDownloadOperationHashVerificationError: | 
|  | 69 | case ErrorCode::kDownloadOperationExecutionError: | 
|  | 70 | case ErrorCode::kDownloadOperationHashMismatch: | 
|  | 71 | case ErrorCode::kDownloadInvalidMetadataSize: | 
|  | 72 | case ErrorCode::kDownloadInvalidMetadataSignature: | 
|  | 73 | case ErrorCode::kDownloadOperationHashMissingError: | 
|  | 74 | case ErrorCode::kDownloadMetadataSignatureMissingError: | 
|  | 75 | case ErrorCode::kPayloadMismatchedType: | 
|  | 76 | case ErrorCode::kUnsupportedMajorPayloadVersion: | 
|  | 77 | case ErrorCode::kUnsupportedMinorPayloadVersion: | 
|  | 78 | LOG(INFO) << "Advancing download URL due to error " | 
|  | 79 | << chromeos_update_engine::utils::CodeToString(err_code) | 
|  | 80 | << " (" << static_cast<int>(err_code) << ")"; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 81 | return true; | 
|  | 82 |  | 
|  | 83 | // Errors which seem to be just transient network/communication related | 
|  | 84 | // failures and do not indicate any inherent problem with the URL itself. | 
|  | 85 | // So, we should keep the current URL but just increment the | 
|  | 86 | // failure count to give it more chances. This way, while we maximize our | 
|  | 87 | // chances of downloading from the URLs that appear earlier in the response | 
|  | 88 | // (because download from a local server URL that appears earlier in a | 
|  | 89 | // response is preferable than downloading from the next URL which could be | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 90 | // an Internet URL and thus could be more expensive). | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 91 | case ErrorCode::kError: | 
|  | 92 | case ErrorCode::kDownloadTransferError: | 
|  | 93 | case ErrorCode::kDownloadWriteError: | 
|  | 94 | case ErrorCode::kDownloadStateInitializationError: | 
| Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 95 | case ErrorCode::kOmahaErrorInHTTPResponse:  // Aggregate for HTTP errors. | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 96 | LOG(INFO) << "Incrementing URL failure count due to error " | 
|  | 97 | << chromeos_update_engine::utils::CodeToString(err_code) | 
|  | 98 | << " (" << static_cast<int>(err_code) << ")"; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 99 | *url_num_error_p += 1; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 100 | return false; | 
|  | 101 |  | 
|  | 102 | // Errors which are not specific to a URL and hence shouldn't result in | 
|  | 103 | // the URL being penalized. This can happen in two cases: | 
|  | 104 | // 1. We haven't started downloading anything: These errors don't cost us | 
|  | 105 | // anything in terms of actual payload bytes, so we should just do the | 
|  | 106 | // regular retries at the next update check. | 
|  | 107 | // 2. We have successfully downloaded the payload: In this case, the | 
|  | 108 | // payload attempt number would have been incremented and would take care | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 109 | // of the back-off at the next update check. | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 110 | // In either case, there's no need to update URL index or failure count. | 
|  | 111 | case ErrorCode::kOmahaRequestError: | 
|  | 112 | case ErrorCode::kOmahaResponseHandlerError: | 
|  | 113 | case ErrorCode::kPostinstallRunnerError: | 
|  | 114 | case ErrorCode::kFilesystemCopierError: | 
|  | 115 | case ErrorCode::kInstallDeviceOpenError: | 
|  | 116 | case ErrorCode::kKernelDeviceOpenError: | 
|  | 117 | case ErrorCode::kDownloadNewPartitionInfoError: | 
|  | 118 | case ErrorCode::kNewRootfsVerificationError: | 
|  | 119 | case ErrorCode::kNewKernelVerificationError: | 
|  | 120 | case ErrorCode::kPostinstallBootedFromFirmwareB: | 
|  | 121 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: | 
|  | 122 | case ErrorCode::kOmahaRequestEmptyResponseError: | 
|  | 123 | case ErrorCode::kOmahaRequestXMLParseError: | 
|  | 124 | case ErrorCode::kOmahaResponseInvalid: | 
|  | 125 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: | 
|  | 126 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: | 
|  | 127 | case ErrorCode::kOmahaUpdateDeferredForBackoff: | 
|  | 128 | case ErrorCode::kPostinstallPowerwashError: | 
|  | 129 | case ErrorCode::kUpdateCanceledByChannelChange: | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 130 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 131 | case ErrorCode::kFilesystemVerifierError: | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 132 | LOG(INFO) << "Not changing URL index or failure count due to error " | 
|  | 133 | << chromeos_update_engine::utils::CodeToString(err_code) | 
|  | 134 | << " (" << static_cast<int>(err_code) << ")"; | 
|  | 135 | return false; | 
|  | 136 |  | 
|  | 137 | case ErrorCode::kSuccess:                            // success code | 
|  | 138 | case ErrorCode::kUmaReportedMax:                     // not an error code | 
|  | 139 | case ErrorCode::kOmahaRequestHTTPResponseBase:       // aggregated already | 
|  | 140 | case ErrorCode::kDevModeFlag:                       // not an error code | 
|  | 141 | case ErrorCode::kResumedFlag:                        // not an error code | 
|  | 142 | case ErrorCode::kTestImageFlag:                      // not an error code | 
|  | 143 | case ErrorCode::kTestOmahaUrlFlag:                   // not an error code | 
|  | 144 | case ErrorCode::kSpecialFlags:                       // not an error code | 
|  | 145 | // These shouldn't happen. Enumerating these  explicitly here so that we | 
|  | 146 | // can let the compiler warn about new error codes that are added to | 
|  | 147 | // action_processor.h but not added here. | 
|  | 148 | LOG(WARNING) << "Unexpected error " | 
|  | 149 | << chromeos_update_engine::utils::CodeToString(err_code) | 
|  | 150 | << " (" << static_cast<int>(err_code) << ")"; | 
|  | 151 | // Note: Not adding a default here so as to let the compiler warn us of | 
|  | 152 | // any new enums that were added in the .h but not listed in this switch. | 
|  | 153 | } | 
|  | 154 | return false; | 
|  | 155 | } | 
|  | 156 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 157 | // Checks whether |url| can be used under given download restrictions. | 
|  | 158 | bool IsUrlUsable(const string& url, bool http_allowed) { | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 159 | return http_allowed || | 
|  | 160 | !base::StartsWith(url, "http://", | 
|  | 161 | base::CompareCase::INSENSITIVE_ASCII); | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
|  | 164 | }  // namespace | 
|  | 165 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 166 | namespace chromeos_update_manager { | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 167 |  | 
| Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 168 | const int ChromeOSPolicy::kTimeoutInitialInterval =  7 * 60; | 
| Alex Deymo | 14e7dde | 2015-10-20 14:46:33 -0700 | [diff] [blame] | 169 |  | 
|  | 170 | // TODO(deymo): Split the update_manager policies for Brillo and ChromeOS and | 
|  | 171 | // make the update check periodic interval configurable. | 
|  | 172 | #ifdef __ANDROID__ | 
|  | 173 | const int ChromeOSPolicy::kTimeoutPeriodicInterval = 5 * 60 * 60; | 
| Alex Deymo | dbe13b4 | 2015-11-06 11:15:08 -0800 | [diff] [blame] | 174 | const int ChromeOSPolicy::kTimeoutMaxBackoffInterval = 26 * 60 * 60; | 
| Alex Deymo | 14e7dde | 2015-10-20 14:46:33 -0700 | [diff] [blame] | 175 | #else | 
| Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 176 | const int ChromeOSPolicy::kTimeoutPeriodicInterval = 45 * 60; | 
| Alex Deymo | dbe13b4 | 2015-11-06 11:15:08 -0800 | [diff] [blame] | 177 | const int ChromeOSPolicy::kTimeoutMaxBackoffInterval = 4 * 60 * 60; | 
| Alex Deymo | 14e7dde | 2015-10-20 14:46:33 -0700 | [diff] [blame] | 178 | #endif  // __ANDROID__ | 
|  | 179 |  | 
| Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 180 | const int ChromeOSPolicy::kTimeoutRegularFuzz = 10 * 60; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 181 | const int ChromeOSPolicy::kAttemptBackoffMaxIntervalInDays = 16; | 
|  | 182 | const int ChromeOSPolicy::kAttemptBackoffFuzzInHours = 12; | 
| Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 183 | const int ChromeOSPolicy::kMaxP2PAttempts = 10; | 
|  | 184 | const int ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds = 5 * 24 * 60 * 60; | 
| Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 185 |  | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 186 | EvalStatus ChromeOSPolicy::UpdateCheckAllowed( | 
|  | 187 | EvaluationContext* ec, State* state, string* error, | 
|  | 188 | UpdateCheckParams* result) const { | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 189 | // Set the default return values. | 
|  | 190 | result->updates_enabled = true; | 
|  | 191 | result->target_channel.clear(); | 
| Gilad Arnold | d4b3032 | 2014-07-21 15:35:27 -0700 | [diff] [blame] | 192 | result->target_version_prefix.clear(); | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 193 | result->is_interactive = false; | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 194 |  | 
| Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 195 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 196 | UpdaterProvider* const updater_provider = state->updater_provider(); | 
| Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 197 | SystemProvider* const system_provider = state->system_provider(); | 
|  | 198 |  | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 199 | // Do not perform any updates if booted from removable device. This decision | 
|  | 200 | // is final. | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 201 | const unsigned int* num_slots_p = ec->GetValue( | 
|  | 202 | system_provider->var_num_slots()); | 
|  | 203 | if (!num_slots_p || *num_slots_p < 2) { | 
|  | 204 | LOG(INFO) << "Not enough slots for A/B updates, disabling update checks."; | 
| Gilad Arnold | bfc44f7 | 2014-07-09 14:41:39 -0700 | [diff] [blame] | 205 | result->updates_enabled = false; | 
|  | 206 | return EvalStatus::kSucceeded; | 
|  | 207 | } | 
|  | 208 |  | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 209 | const bool* device_policy_is_loaded_p = ec->GetValue( | 
|  | 210 | dp_provider->var_device_policy_is_loaded()); | 
|  | 211 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { | 
|  | 212 | // Check whether updates are disabled by policy. | 
|  | 213 | const bool* update_disabled_p = ec->GetValue( | 
|  | 214 | dp_provider->var_update_disabled()); | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 215 | if (update_disabled_p && *update_disabled_p) { | 
|  | 216 | LOG(INFO) << "Updates disabled by policy, blocking update checks."; | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 217 | return EvalStatus::kAskMeAgainLater; | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 218 | } | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 219 |  | 
| Gilad Arnold | d4b3032 | 2014-07-21 15:35:27 -0700 | [diff] [blame] | 220 | // Determine whether a target version prefix is dictated by policy. | 
|  | 221 | const string* target_version_prefix_p = ec->GetValue( | 
|  | 222 | dp_provider->var_target_version_prefix()); | 
|  | 223 | if (target_version_prefix_p) | 
|  | 224 | result->target_version_prefix = *target_version_prefix_p; | 
|  | 225 |  | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 226 | // Determine whether a target channel is dictated by policy. | 
|  | 227 | const bool* release_channel_delegated_p = ec->GetValue( | 
|  | 228 | dp_provider->var_release_channel_delegated()); | 
|  | 229 | if (release_channel_delegated_p && !(*release_channel_delegated_p)) { | 
|  | 230 | const string* release_channel_p = ec->GetValue( | 
|  | 231 | dp_provider->var_release_channel()); | 
|  | 232 | if (release_channel_p) | 
|  | 233 | result->target_channel = *release_channel_p; | 
|  | 234 | } | 
|  | 235 | } | 
|  | 236 |  | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 237 | // First, check to see if an interactive update was requested. | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 238 | const UpdateRequestStatus* forced_update_requested_p = ec->GetValue( | 
|  | 239 | updater_provider->var_forced_update_requested()); | 
|  | 240 | if (forced_update_requested_p && | 
|  | 241 | *forced_update_requested_p != UpdateRequestStatus::kNone) { | 
|  | 242 | result->is_interactive = | 
|  | 243 | (*forced_update_requested_p == UpdateRequestStatus::kInteractive); | 
|  | 244 | LOG(INFO) << "Forced update signaled (" | 
|  | 245 | << (result->is_interactive ?  "interactive" : "periodic") | 
|  | 246 | << "), allowing update check."; | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 247 | return EvalStatus::kSucceeded; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | // The logic thereafter applies to periodic updates. Bear in mind that we | 
|  | 251 | // should not return a final "no" if any of these criteria are not satisfied, | 
|  | 252 | // because the system may still update due to an interactive update request. | 
|  | 253 |  | 
|  | 254 | // Unofficial builds should not perform periodic update checks. | 
|  | 255 | const bool* is_official_build_p = ec->GetValue( | 
|  | 256 | system_provider->var_is_official_build()); | 
|  | 257 | if (is_official_build_p && !(*is_official_build_p)) { | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 258 | LOG(INFO) << "Unofficial build, blocking periodic update checks."; | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 259 | return EvalStatus::kAskMeAgainLater; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | // If OOBE is enabled, wait until it is completed. | 
|  | 263 | const bool* is_oobe_enabled_p = ec->GetValue( | 
|  | 264 | state->config_provider()->var_is_oobe_enabled()); | 
|  | 265 | if (is_oobe_enabled_p && *is_oobe_enabled_p) { | 
|  | 266 | const bool* is_oobe_complete_p = ec->GetValue( | 
|  | 267 | system_provider->var_is_oobe_complete()); | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 268 | if (is_oobe_complete_p && !(*is_oobe_complete_p)) { | 
|  | 269 | LOG(INFO) << "OOBE not completed, blocking update checks."; | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 270 | return EvalStatus::kAskMeAgainLater; | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 271 | } | 
| Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
|  | 274 | // Ensure that periodic update checks are timed properly. | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 275 | Time next_update_check; | 
|  | 276 | if (NextUpdateCheckTime(ec, state, error, &next_update_check) != | 
|  | 277 | EvalStatus::kSucceeded) { | 
|  | 278 | return EvalStatus::kFailed; | 
|  | 279 | } | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 280 | if (!ec->IsWallclockTimeGreaterThan(next_update_check)) { | 
|  | 281 | LOG(INFO) << "Periodic check interval not satisfied, blocking until " | 
|  | 282 | << chromeos_update_engine::utils::ToString(next_update_check); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 283 | return EvalStatus::kAskMeAgainLater; | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 284 | } | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 285 |  | 
|  | 286 | // It is time to check for an update. | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 287 | LOG(INFO) << "Allowing update check."; | 
| Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 288 | return EvalStatus::kSucceeded; | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 289 | } | 
|  | 290 |  | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 291 | EvalStatus ChromeOSPolicy::UpdateCanStart( | 
|  | 292 | EvaluationContext* ec, | 
|  | 293 | State* state, | 
|  | 294 | string* error, | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 295 | UpdateDownloadParams* result, | 
| Gilad Arnold | d78caf9 | 2014-09-24 09:28:14 -0700 | [diff] [blame] | 296 | const UpdateState update_state) const { | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 297 | // Set the default return values. Note that we set persisted values (backoff, | 
|  | 298 | // scattering) to the same values presented in the update state. The reason is | 
|  | 299 | // that preemptive returns, such as the case where an update check is due, | 
|  | 300 | // should not clear off the said values; rather, it is the deliberate | 
|  | 301 | // inference of new values that should cause them to be reset. | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 302 | result->update_can_start = false; | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 303 | result->cannot_start_reason = UpdateCannotStartReason::kUndefined; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 304 | result->download_url_idx = -1; | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 305 | result->download_url_allowed = true; | 
|  | 306 | result->download_url_num_errors = 0; | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 307 | result->p2p_downloading_allowed = false; | 
|  | 308 | result->p2p_sharing_allowed = false; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 309 | result->do_increment_failures = false; | 
|  | 310 | result->backoff_expiry = update_state.backoff_expiry; | 
|  | 311 | result->scatter_wait_period = update_state.scatter_wait_period; | 
|  | 312 | result->scatter_check_threshold = update_state.scatter_check_threshold; | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 313 |  | 
|  | 314 | // Make sure that we're not due for an update check. | 
|  | 315 | UpdateCheckParams check_result; | 
|  | 316 | EvalStatus check_status = UpdateCheckAllowed(ec, state, error, &check_result); | 
|  | 317 | if (check_status == EvalStatus::kFailed) | 
|  | 318 | return EvalStatus::kFailed; | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 319 | bool is_check_due = (check_status == EvalStatus::kSucceeded && | 
|  | 320 | check_result.updates_enabled == true); | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 321 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 322 | // Check whether backoff applies, and if not then which URL can be used for | 
|  | 323 | // downloading. These require scanning the download error log, and so they are | 
|  | 324 | // done together. | 
|  | 325 | UpdateBackoffAndDownloadUrlResult backoff_url_result; | 
|  | 326 | EvalStatus backoff_url_status = UpdateBackoffAndDownloadUrl( | 
|  | 327 | ec, state, error, &backoff_url_result, update_state); | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 328 | if (backoff_url_status == EvalStatus::kFailed) | 
|  | 329 | return EvalStatus::kFailed; | 
|  | 330 | result->download_url_idx = backoff_url_result.url_idx; | 
|  | 331 | result->download_url_num_errors = backoff_url_result.url_num_errors; | 
|  | 332 | result->do_increment_failures = backoff_url_result.do_increment_failures; | 
|  | 333 | result->backoff_expiry = backoff_url_result.backoff_expiry; | 
|  | 334 | bool is_backoff_active = | 
|  | 335 | (backoff_url_status == EvalStatus::kAskMeAgainLater) || | 
|  | 336 | !backoff_url_result.backoff_expiry.is_null(); | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 337 |  | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 338 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 339 | bool is_scattering_active = false; | 
|  | 340 | EvalStatus scattering_status = EvalStatus::kSucceeded; | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 341 |  | 
|  | 342 | const bool* device_policy_is_loaded_p = ec->GetValue( | 
|  | 343 | dp_provider->var_device_policy_is_loaded()); | 
|  | 344 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { | 
| Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 345 | // Check whether scattering applies to this update attempt. We should not be | 
|  | 346 | // scattering if this is an interactive update check, or if OOBE is enabled | 
|  | 347 | // but not completed. | 
|  | 348 | // | 
|  | 349 | // Note: current code further suppresses scattering if a "deadline" | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 350 | // attribute is found in the Omaha response. However, it appears that the | 
| Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 351 | // presence of this attribute is merely indicative of an OOBE update, during | 
|  | 352 | // which we suppress scattering anyway. | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 353 | bool is_scattering_applicable = false; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 354 | result->scatter_wait_period = kZeroInterval; | 
|  | 355 | result->scatter_check_threshold = 0; | 
|  | 356 | if (!update_state.is_interactive) { | 
| Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 357 | const bool* is_oobe_enabled_p = ec->GetValue( | 
|  | 358 | state->config_provider()->var_is_oobe_enabled()); | 
|  | 359 | if (is_oobe_enabled_p && !(*is_oobe_enabled_p)) { | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 360 | is_scattering_applicable = true; | 
| Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 361 | } else { | 
|  | 362 | const bool* is_oobe_complete_p = ec->GetValue( | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 363 | state->system_provider()->var_is_oobe_complete()); | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 364 | is_scattering_applicable = (is_oobe_complete_p && *is_oobe_complete_p); | 
| Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 365 | } | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | // Compute scattering values. | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 369 | if (is_scattering_applicable) { | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 370 | UpdateScatteringResult scatter_result; | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 371 | scattering_status = UpdateScattering(ec, state, error, &scatter_result, | 
|  | 372 | update_state); | 
|  | 373 | if (scattering_status == EvalStatus::kFailed) { | 
|  | 374 | return EvalStatus::kFailed; | 
|  | 375 | } else { | 
|  | 376 | result->scatter_wait_period = scatter_result.wait_period; | 
|  | 377 | result->scatter_check_threshold = scatter_result.check_threshold; | 
|  | 378 | if (scattering_status == EvalStatus::kAskMeAgainLater || | 
|  | 379 | scatter_result.is_scattering) | 
|  | 380 | is_scattering_active = true; | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 381 | } | 
|  | 382 | } | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 383 | } | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 384 |  | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 385 | // Find out whether P2P is globally enabled. | 
|  | 386 | bool p2p_enabled; | 
|  | 387 | EvalStatus p2p_enabled_status = P2PEnabled(ec, state, error, &p2p_enabled); | 
|  | 388 | if (p2p_enabled_status != EvalStatus::kSucceeded) | 
|  | 389 | return EvalStatus::kFailed; | 
|  | 390 |  | 
|  | 391 | // Is P2P is enabled, consider allowing it for downloading and/or sharing. | 
|  | 392 | if (p2p_enabled) { | 
|  | 393 | // Sharing via P2P is allowed if not disabled by Omaha. | 
|  | 394 | if (update_state.p2p_sharing_disabled) { | 
|  | 395 | LOG(INFO) << "Blocked P2P sharing because it is disabled by Omaha."; | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 396 | } else { | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 397 | result->p2p_sharing_allowed = true; | 
| Gilad Arnold | ef8d087 | 2014-10-03 14:14:06 -0700 | [diff] [blame] | 398 | } | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 399 |  | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 400 | // Downloading via P2P is allowed if not disabled by Omaha, an update is not | 
|  | 401 | // interactive, and other limits haven't been reached. | 
|  | 402 | if (update_state.p2p_downloading_disabled) { | 
|  | 403 | LOG(INFO) << "Blocked P2P downloading because it is disabled by Omaha."; | 
|  | 404 | } else if (update_state.is_interactive) { | 
|  | 405 | LOG(INFO) << "Blocked P2P downloading because update is interactive."; | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 406 | } else if (update_state.p2p_num_attempts >= kMaxP2PAttempts) { | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 407 | LOG(INFO) << "Blocked P2P downloading as it was attempted too many " | 
|  | 408 | "times."; | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 409 | } else if (!update_state.p2p_first_attempted.is_null() && | 
|  | 410 | ec->IsWallclockTimeGreaterThan( | 
|  | 411 | update_state.p2p_first_attempted + | 
|  | 412 | TimeDelta::FromSeconds(kMaxP2PAttemptsPeriodInSeconds))) { | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 413 | LOG(INFO) << "Blocked P2P downloading as its usage timespan exceeds " | 
|  | 414 | "limit."; | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 415 | } else { | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 416 | // P2P download is allowed; if backoff or scattering are active, be sure | 
|  | 417 | // to suppress them, yet prevent any download URL from being used. | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 418 | result->p2p_downloading_allowed = true; | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 419 | if (is_backoff_active || is_scattering_active) { | 
|  | 420 | is_backoff_active = is_scattering_active = false; | 
|  | 421 | result->download_url_allowed = false; | 
|  | 422 | } | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 423 | } | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 424 | } | 
|  | 425 |  | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 426 | // Check for various deterrents. | 
|  | 427 | if (is_check_due) { | 
|  | 428 | result->cannot_start_reason = UpdateCannotStartReason::kCheckDue; | 
|  | 429 | return EvalStatus::kSucceeded; | 
|  | 430 | } | 
|  | 431 | if (is_backoff_active) { | 
|  | 432 | result->cannot_start_reason = UpdateCannotStartReason::kBackoff; | 
|  | 433 | return backoff_url_status; | 
|  | 434 | } | 
|  | 435 | if (is_scattering_active) { | 
|  | 436 | result->cannot_start_reason = UpdateCannotStartReason::kScattering; | 
|  | 437 | return scattering_status; | 
|  | 438 | } | 
| Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 439 | if (result->download_url_idx < 0 && !result->p2p_downloading_allowed) { | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 440 | result->cannot_start_reason = UpdateCannotStartReason::kCannotDownload; | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 441 | return EvalStatus::kSucceeded; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 444 | // Update is good to go. | 
|  | 445 | result->update_can_start = true; | 
| Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 446 | return EvalStatus::kSucceeded; | 
|  | 447 | } | 
|  | 448 |  | 
| Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 449 | // TODO(garnold) Logic in this method is based on | 
|  | 450 | // ConnectionManager::IsUpdateAllowedOver(); be sure to deprecate the latter. | 
|  | 451 | // | 
|  | 452 | // TODO(garnold) The current logic generally treats the list of allowed | 
|  | 453 | // connections coming from the device policy as a whitelist, meaning that it | 
|  | 454 | // can only be used for enabling connections, but not disable them. Further, | 
|  | 455 | // certain connection types (like Bluetooth) cannot be enabled even by policy. | 
|  | 456 | // In effect, the only thing that device policy can change is to enable | 
|  | 457 | // updates over a cellular network (disabled by default). We may want to | 
|  | 458 | // revisit this semantics, allowing greater flexibility in defining specific | 
|  | 459 | // permissions over all types of networks. | 
| Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 460 | EvalStatus ChromeOSPolicy::UpdateDownloadAllowed( | 
| Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 461 | EvaluationContext* ec, | 
|  | 462 | State* state, | 
|  | 463 | string* error, | 
|  | 464 | bool* result) const { | 
|  | 465 | // Get the current connection type. | 
|  | 466 | ShillProvider* const shill_provider = state->shill_provider(); | 
|  | 467 | const ConnectionType* conn_type_p = ec->GetValue( | 
|  | 468 | shill_provider->var_conn_type()); | 
|  | 469 | POLICY_CHECK_VALUE_AND_FAIL(conn_type_p, error); | 
|  | 470 | ConnectionType conn_type = *conn_type_p; | 
|  | 471 |  | 
|  | 472 | // If we're tethering, treat it as a cellular connection. | 
|  | 473 | if (conn_type != ConnectionType::kCellular) { | 
|  | 474 | const ConnectionTethering* conn_tethering_p = ec->GetValue( | 
|  | 475 | shill_provider->var_conn_tethering()); | 
|  | 476 | POLICY_CHECK_VALUE_AND_FAIL(conn_tethering_p, error); | 
|  | 477 | if (*conn_tethering_p == ConnectionTethering::kConfirmed) | 
|  | 478 | conn_type = ConnectionType::kCellular; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | // By default, we allow updates for all connection types, with exceptions as | 
|  | 482 | // noted below. This also determines whether a device policy can override the | 
|  | 483 | // default. | 
|  | 484 | *result = true; | 
|  | 485 | bool device_policy_can_override = false; | 
|  | 486 | switch (conn_type) { | 
|  | 487 | case ConnectionType::kBluetooth: | 
|  | 488 | *result = false; | 
|  | 489 | break; | 
|  | 490 |  | 
|  | 491 | case ConnectionType::kCellular: | 
|  | 492 | *result = false; | 
|  | 493 | device_policy_can_override = true; | 
|  | 494 | break; | 
|  | 495 |  | 
|  | 496 | case ConnectionType::kUnknown: | 
|  | 497 | if (error) | 
|  | 498 | *error = "Unknown connection type"; | 
|  | 499 | return EvalStatus::kFailed; | 
|  | 500 |  | 
|  | 501 | default: | 
|  | 502 | break;  // Nothing to do. | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | // If update is allowed, we're done. | 
|  | 506 | if (*result) | 
|  | 507 | return EvalStatus::kSucceeded; | 
|  | 508 |  | 
|  | 509 | // Check whether the device policy specifically allows this connection. | 
| Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 510 | if (device_policy_can_override) { | 
|  | 511 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); | 
|  | 512 | const bool* device_policy_is_loaded_p = ec->GetValue( | 
|  | 513 | dp_provider->var_device_policy_is_loaded()); | 
|  | 514 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { | 
|  | 515 | const set<ConnectionType>* allowed_conn_types_p = ec->GetValue( | 
|  | 516 | dp_provider->var_allowed_connection_types_for_update()); | 
|  | 517 | if (allowed_conn_types_p) { | 
|  | 518 | if (allowed_conn_types_p->count(conn_type)) { | 
|  | 519 | *result = true; | 
|  | 520 | return EvalStatus::kSucceeded; | 
|  | 521 | } | 
| Gilad Arnold | 28d6be6 | 2014-06-30 14:04:04 -0700 | [diff] [blame] | 522 | } else if (conn_type == ConnectionType::kCellular) { | 
|  | 523 | // Local user settings can allow updates over cellular iff a policy was | 
|  | 524 | // loaded but no allowed connections were specified in it. | 
|  | 525 | const bool* update_over_cellular_allowed_p = ec->GetValue( | 
|  | 526 | state->updater_provider()->var_cellular_enabled()); | 
|  | 527 | if (update_over_cellular_allowed_p && *update_over_cellular_allowed_p) | 
|  | 528 | *result = true; | 
| Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 529 | } | 
|  | 530 | } | 
|  | 531 | } | 
|  | 532 |  | 
| Gilad Arnold | 28d6be6 | 2014-06-30 14:04:04 -0700 | [diff] [blame] | 533 | return (*result ? EvalStatus::kSucceeded : EvalStatus::kAskMeAgainLater); | 
| Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 536 | EvalStatus ChromeOSPolicy::P2PEnabled(EvaluationContext* ec, | 
|  | 537 | State* state, | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 538 | string* error, | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 539 | bool* result) const { | 
|  | 540 | bool enabled = false; | 
|  | 541 |  | 
|  | 542 | // Determine whether use of P2P is allowed by policy. Even if P2P is not | 
|  | 543 | // explicitly allowed, we allow it if the device is enterprise enrolled (that | 
|  | 544 | // is, missing or empty owner string). | 
|  | 545 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); | 
|  | 546 | const bool* device_policy_is_loaded_p = ec->GetValue( | 
|  | 547 | dp_provider->var_device_policy_is_loaded()); | 
|  | 548 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { | 
|  | 549 | const bool* policy_au_p2p_enabled_p = ec->GetValue( | 
|  | 550 | dp_provider->var_au_p2p_enabled()); | 
|  | 551 | if (policy_au_p2p_enabled_p) { | 
|  | 552 | enabled = *policy_au_p2p_enabled_p; | 
|  | 553 | } else { | 
|  | 554 | const string* policy_owner_p = ec->GetValue(dp_provider->var_owner()); | 
|  | 555 | if (!policy_owner_p || policy_owner_p->empty()) | 
|  | 556 | enabled = true; | 
|  | 557 | } | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | // Enable P2P, if so mandated by the updater configuration. This is additive | 
|  | 561 | // to whether or not P2P is enabled by device policy. | 
|  | 562 | if (!enabled) { | 
|  | 563 | const bool* updater_p2p_enabled_p = ec->GetValue( | 
|  | 564 | state->updater_provider()->var_p2p_enabled()); | 
|  | 565 | enabled = updater_p2p_enabled_p && *updater_p2p_enabled_p; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | *result = enabled; | 
|  | 569 | return EvalStatus::kSucceeded; | 
|  | 570 | } | 
|  | 571 |  | 
|  | 572 | EvalStatus ChromeOSPolicy::P2PEnabledChanged(EvaluationContext* ec, | 
|  | 573 | State* state, | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 574 | string* error, | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 575 | bool* result, | 
|  | 576 | bool prev_result) const { | 
|  | 577 | EvalStatus status = P2PEnabled(ec, state, error, result); | 
|  | 578 | if (status == EvalStatus::kSucceeded && *result == prev_result) | 
|  | 579 | return EvalStatus::kAskMeAgainLater; | 
|  | 580 | return status; | 
|  | 581 | } | 
|  | 582 |  | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 583 | EvalStatus ChromeOSPolicy::NextUpdateCheckTime(EvaluationContext* ec, | 
|  | 584 | State* state, string* error, | 
|  | 585 | Time* next_update_check) const { | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 586 | UpdaterProvider* const updater_provider = state->updater_provider(); | 
|  | 587 |  | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 588 | // Don't check for updates too often. We limit the update checks to once every | 
|  | 589 | // some interval. The interval is kTimeoutInitialInterval the first time and | 
|  | 590 | // kTimeoutPeriodicInterval for the subsequent update checks. If the update | 
|  | 591 | // check fails, we increase the interval between the update checks | 
|  | 592 | // exponentially until kTimeoutMaxBackoffInterval. Finally, to avoid having | 
|  | 593 | // many chromebooks running update checks at the exact same time, we add some | 
|  | 594 | // fuzz to the interval. | 
|  | 595 | const Time* updater_started_time = | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 596 | ec->GetValue(updater_provider->var_updater_started_time()); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 597 | POLICY_CHECK_VALUE_AND_FAIL(updater_started_time, error); | 
|  | 598 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 599 | const Time* last_checked_time = | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 600 | ec->GetValue(updater_provider->var_last_checked_time()); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 601 |  | 
|  | 602 | const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed()); | 
|  | 603 | POLICY_CHECK_VALUE_AND_FAIL(seed, error); | 
|  | 604 |  | 
|  | 605 | PRNG prng(*seed); | 
|  | 606 |  | 
| Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 607 | // If this is the first attempt, compute and return an initial value. | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 608 | if (!last_checked_time || *last_checked_time < *updater_started_time) { | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 609 | *next_update_check = *updater_started_time + FuzzedInterval( | 
|  | 610 | &prng, kTimeoutInitialInterval, kTimeoutRegularFuzz); | 
|  | 611 | return EvalStatus::kSucceeded; | 
|  | 612 | } | 
| Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 613 |  | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 614 | // Check whether the server is enforcing a poll interval; if not, this value | 
|  | 615 | // will be zero. | 
|  | 616 | const unsigned int* server_dictated_poll_interval = ec->GetValue( | 
|  | 617 | updater_provider->var_server_dictated_poll_interval()); | 
|  | 618 | POLICY_CHECK_VALUE_AND_FAIL(server_dictated_poll_interval, error); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 619 |  | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 620 | int interval = *server_dictated_poll_interval; | 
|  | 621 | int fuzz = 0; | 
|  | 622 |  | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 623 | // If no poll interval was dictated by server compute a back-off period, | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 624 | // starting from a predetermined base periodic interval and increasing | 
|  | 625 | // exponentially by the number of consecutive failed attempts. | 
|  | 626 | if (interval == 0) { | 
|  | 627 | const unsigned int* consecutive_failed_update_checks = ec->GetValue( | 
|  | 628 | updater_provider->var_consecutive_failed_update_checks()); | 
|  | 629 | POLICY_CHECK_VALUE_AND_FAIL(consecutive_failed_update_checks, error); | 
|  | 630 |  | 
|  | 631 | interval = kTimeoutPeriodicInterval; | 
|  | 632 | unsigned int num_failures = *consecutive_failed_update_checks; | 
|  | 633 | while (interval < kTimeoutMaxBackoffInterval && num_failures) { | 
|  | 634 | interval *= 2; | 
|  | 635 | num_failures--; | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 636 | } | 
|  | 637 | } | 
|  | 638 |  | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 639 | // We cannot back off longer than the predetermined maximum interval. | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 640 | if (interval > kTimeoutMaxBackoffInterval) | 
|  | 641 | interval = kTimeoutMaxBackoffInterval; | 
|  | 642 |  | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 643 | // We cannot back off shorter than the predetermined periodic interval. Also, | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 644 | // in this case set the fuzz to a predetermined regular value. | 
|  | 645 | if (interval <= kTimeoutPeriodicInterval) { | 
|  | 646 | interval = kTimeoutPeriodicInterval; | 
|  | 647 | fuzz = kTimeoutRegularFuzz; | 
|  | 648 | } | 
|  | 649 |  | 
|  | 650 | // If not otherwise determined, defer to a fuzz of +/-(interval / 2). | 
| Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 651 | if (fuzz == 0) | 
|  | 652 | fuzz = interval; | 
|  | 653 |  | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 654 | *next_update_check = *last_checked_time + FuzzedInterval( | 
| Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 655 | &prng, interval, fuzz); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 656 | return EvalStatus::kSucceeded; | 
|  | 657 | } | 
|  | 658 |  | 
|  | 659 | TimeDelta ChromeOSPolicy::FuzzedInterval(PRNG* prng, int interval, int fuzz) { | 
| Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 660 | DCHECK_GE(interval, 0); | 
|  | 661 | DCHECK_GE(fuzz, 0); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 662 | int half_fuzz = fuzz / 2; | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 663 | // This guarantees the output interval is non negative. | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 664 | int interval_min = max(interval - half_fuzz, 0); | 
| Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 665 | int interval_max = interval + half_fuzz; | 
|  | 666 | return TimeDelta::FromSeconds(prng->RandMinMax(interval_min, interval_max)); | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 667 | } | 
|  | 668 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 669 | EvalStatus ChromeOSPolicy::UpdateBackoffAndDownloadUrl( | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 670 | EvaluationContext* ec, State* state, string* error, | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 671 | UpdateBackoffAndDownloadUrlResult* result, | 
|  | 672 | const UpdateState& update_state) const { | 
|  | 673 | // Sanity checks. | 
|  | 674 | DCHECK_GE(update_state.download_errors_max, 0); | 
|  | 675 |  | 
|  | 676 | // Set default result values. | 
|  | 677 | result->do_increment_failures = false; | 
|  | 678 | result->backoff_expiry = update_state.backoff_expiry; | 
|  | 679 | result->url_idx = -1; | 
|  | 680 | result->url_num_errors = 0; | 
|  | 681 |  | 
|  | 682 | const bool* is_official_build_p = ec->GetValue( | 
|  | 683 | state->system_provider()->var_is_official_build()); | 
|  | 684 | bool is_official_build = (is_official_build_p ? *is_official_build_p : true); | 
|  | 685 |  | 
|  | 686 | // Check whether backoff is enabled. | 
|  | 687 | bool may_backoff = false; | 
|  | 688 | if (update_state.is_backoff_disabled) { | 
|  | 689 | LOG(INFO) << "Backoff disabled by Omaha."; | 
|  | 690 | } else if (update_state.is_interactive) { | 
|  | 691 | LOG(INFO) << "No backoff for interactive updates."; | 
|  | 692 | } else if (update_state.is_delta_payload) { | 
|  | 693 | LOG(INFO) << "No backoff for delta payloads."; | 
|  | 694 | } else if (!is_official_build) { | 
|  | 695 | LOG(INFO) << "No backoff for unofficial builds."; | 
|  | 696 | } else { | 
|  | 697 | may_backoff = true; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 698 | } | 
|  | 699 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 700 | // If previous backoff still in effect, block. | 
|  | 701 | if (may_backoff && !update_state.backoff_expiry.is_null() && | 
|  | 702 | !ec->IsWallclockTimeGreaterThan(update_state.backoff_expiry)) { | 
|  | 703 | LOG(INFO) << "Previous backoff has not expired, waiting."; | 
|  | 704 | return EvalStatus::kAskMeAgainLater; | 
|  | 705 | } | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 706 |  | 
|  | 707 | // Determine whether HTTP downloads are forbidden by policy. This only | 
|  | 708 | // applies to official system builds; otherwise, HTTP is always enabled. | 
|  | 709 | bool http_allowed = true; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 710 | if (is_official_build) { | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 711 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); | 
|  | 712 | const bool* device_policy_is_loaded_p = ec->GetValue( | 
|  | 713 | dp_provider->var_device_policy_is_loaded()); | 
|  | 714 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { | 
|  | 715 | const bool* policy_http_downloads_enabled_p = ec->GetValue( | 
|  | 716 | dp_provider->var_http_downloads_enabled()); | 
|  | 717 | http_allowed = (!policy_http_downloads_enabled_p || | 
|  | 718 | *policy_http_downloads_enabled_p); | 
|  | 719 | } | 
|  | 720 | } | 
|  | 721 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 722 | int url_idx = update_state.last_download_url_idx; | 
|  | 723 | if (url_idx < 0) | 
|  | 724 | url_idx = -1; | 
|  | 725 | bool do_advance_url = false; | 
|  | 726 | bool is_failure_occurred = false; | 
|  | 727 | Time err_time; | 
|  | 728 |  | 
|  | 729 | // Scan the relevant part of the download error log, tracking which URLs are | 
|  | 730 | // being used, and accounting the number of errors for each URL. Note that | 
|  | 731 | // this process may not traverse all errors provided, as it may decide to bail | 
|  | 732 | // out midway depending on the particular errors exhibited, the number of | 
|  | 733 | // failures allowed, etc. When this ends, |url_idx| will point to the last URL | 
|  | 734 | // used (-1 if starting fresh), |do_advance_url| will determine whether the | 
|  | 735 | // URL needs to be advanced, and |err_time| the point in time when the last | 
|  | 736 | // reported error occurred.  Additionally, if the error log indicates that an | 
|  | 737 | // update attempt has failed (abnormal), then |is_failure_occurred| will be | 
|  | 738 | // set to true. | 
|  | 739 | const int num_urls = update_state.download_urls.size(); | 
|  | 740 | int prev_url_idx = -1; | 
|  | 741 | int url_num_errors = update_state.last_download_url_num_errors; | 
|  | 742 | Time prev_err_time; | 
|  | 743 | bool is_first = true; | 
|  | 744 | for (const auto& err_tuple : update_state.download_errors) { | 
|  | 745 | // Do some sanity checks. | 
|  | 746 | int used_url_idx = get<0>(err_tuple); | 
|  | 747 | if (is_first && url_idx >= 0 && used_url_idx != url_idx) { | 
|  | 748 | LOG(WARNING) << "First URL in error log (" << used_url_idx | 
|  | 749 | << ") not as expected (" << url_idx << ")"; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 750 | } | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 751 | is_first = false; | 
|  | 752 | url_idx = used_url_idx; | 
|  | 753 | if (url_idx < 0 || url_idx >= num_urls) { | 
|  | 754 | LOG(ERROR) << "Download error log contains an invalid URL index (" | 
|  | 755 | << url_idx << ")"; | 
|  | 756 | return EvalStatus::kFailed; | 
|  | 757 | } | 
|  | 758 | err_time = get<2>(err_tuple); | 
|  | 759 | if (!(prev_err_time.is_null() || err_time >= prev_err_time)) { | 
|  | 760 | // TODO(garnold) Monotonicity cannot really be assumed when dealing with | 
|  | 761 | // wallclock-based timestamps. However, we're making a simplifying | 
|  | 762 | // assumption so as to keep the policy implementation straightforward, for | 
|  | 763 | // now. In general, we should convert all timestamp handling in the | 
|  | 764 | // UpdateManager to use monotonic time (instead of wallclock), including | 
|  | 765 | // the computation of various expiration times (backoff, scattering, etc). | 
|  | 766 | // The client will do whatever conversions necessary when | 
|  | 767 | // persisting/retrieving these values across reboots. See chromium:408794. | 
|  | 768 | LOG(ERROR) << "Download error timestamps not monotonically increasing."; | 
|  | 769 | return EvalStatus::kFailed; | 
|  | 770 | } | 
|  | 771 | prev_err_time = err_time; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 772 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 773 | // Ignore errors that happened before the last known failed attempt. | 
|  | 774 | if (!update_state.failures_last_updated.is_null() && | 
|  | 775 | err_time <= update_state.failures_last_updated) | 
|  | 776 | continue; | 
|  | 777 |  | 
|  | 778 | if (prev_url_idx >= 0) { | 
|  | 779 | if (url_idx < prev_url_idx) { | 
|  | 780 | LOG(ERROR) << "The URLs in the download error log have wrapped around (" | 
|  | 781 | << prev_url_idx << "->" << url_idx | 
|  | 782 | << "). This should not have happened and means that there's " | 
|  | 783 | "a bug. To be conservative, we record a failed attempt " | 
|  | 784 | "(invalidating the rest of the error log) and resume " | 
|  | 785 | "download from the first usable URL."; | 
|  | 786 | url_idx = -1; | 
|  | 787 | is_failure_occurred = true; | 
|  | 788 | break; | 
|  | 789 | } | 
|  | 790 |  | 
|  | 791 | if (url_idx > prev_url_idx) { | 
|  | 792 | url_num_errors = 0; | 
|  | 793 | do_advance_url = false; | 
|  | 794 | } | 
|  | 795 | } | 
|  | 796 |  | 
|  | 797 | if (HandleErrorCode(get<1>(err_tuple), &url_num_errors) || | 
|  | 798 | url_num_errors > update_state.download_errors_max) | 
|  | 799 | do_advance_url = true; | 
|  | 800 |  | 
|  | 801 | prev_url_idx = url_idx; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 802 | } | 
|  | 803 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 804 | // If required, advance to the next usable URL. If the URLs wraparound, we | 
|  | 805 | // mark an update attempt failure. Also be sure to set the download error | 
|  | 806 | // count to zero. | 
|  | 807 | if (url_idx < 0 || do_advance_url) { | 
|  | 808 | url_num_errors = 0; | 
|  | 809 | int start_url_idx = -1; | 
|  | 810 | do { | 
|  | 811 | if (++url_idx == num_urls) { | 
|  | 812 | url_idx = 0; | 
|  | 813 | // We only mark failure if an actual advancing of a URL was required. | 
|  | 814 | if (do_advance_url) | 
|  | 815 | is_failure_occurred = true; | 
|  | 816 | } | 
|  | 817 |  | 
|  | 818 | if (start_url_idx < 0) | 
|  | 819 | start_url_idx = url_idx; | 
|  | 820 | else if (url_idx == start_url_idx) | 
|  | 821 | url_idx = -1;  // No usable URL. | 
|  | 822 | } while (url_idx >= 0 && | 
|  | 823 | !IsUrlUsable(update_state.download_urls[url_idx], http_allowed)); | 
|  | 824 | } | 
|  | 825 |  | 
|  | 826 | // If we have a download URL but a failure was observed, compute a new backoff | 
|  | 827 | // expiry (if allowed). The backoff period is generally 2 ^ (num_failures - 1) | 
|  | 828 | // days, bounded by the size of int and kAttemptBackoffMaxIntervalInDays, and | 
|  | 829 | // fuzzed by kAttemptBackoffFuzzInHours hours. Backoff expiry is computed from | 
|  | 830 | // the latest recorded time of error. | 
|  | 831 | Time backoff_expiry; | 
|  | 832 | if (url_idx >= 0 && is_failure_occurred && may_backoff) { | 
|  | 833 | CHECK(!err_time.is_null()) | 
|  | 834 | << "We must have an error timestamp if a failure occurred!"; | 
|  | 835 | const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed()); | 
|  | 836 | POLICY_CHECK_VALUE_AND_FAIL(seed, error); | 
|  | 837 | PRNG prng(*seed); | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 838 | int exp = min(update_state.num_failures, | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 839 | static_cast<int>(sizeof(int)) * 8 - 2); | 
|  | 840 | TimeDelta backoff_interval = TimeDelta::FromDays( | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 841 | min(1 << exp, kAttemptBackoffMaxIntervalInDays)); | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 842 | TimeDelta backoff_fuzz = TimeDelta::FromHours(kAttemptBackoffFuzzInHours); | 
|  | 843 | TimeDelta wait_period = FuzzedInterval(&prng, backoff_interval.InSeconds(), | 
|  | 844 | backoff_fuzz.InSeconds()); | 
|  | 845 | backoff_expiry = err_time + wait_period; | 
|  | 846 |  | 
|  | 847 | // If the newly computed backoff already expired, nullify it. | 
|  | 848 | if (ec->IsWallclockTimeGreaterThan(backoff_expiry)) | 
|  | 849 | backoff_expiry = Time(); | 
|  | 850 | } | 
|  | 851 |  | 
|  | 852 | result->do_increment_failures = is_failure_occurred; | 
|  | 853 | result->backoff_expiry = backoff_expiry; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 854 | result->url_idx = url_idx; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 855 | result->url_num_errors = url_num_errors; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 856 | return EvalStatus::kSucceeded; | 
|  | 857 | } | 
|  | 858 |  | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 859 | EvalStatus ChromeOSPolicy::UpdateScattering( | 
|  | 860 | EvaluationContext* ec, | 
|  | 861 | State* state, | 
|  | 862 | string* error, | 
|  | 863 | UpdateScatteringResult* result, | 
|  | 864 | const UpdateState& update_state) const { | 
|  | 865 | // Preconditions. These stem from the postconditions and usage contract. | 
|  | 866 | DCHECK(update_state.scatter_wait_period >= kZeroInterval); | 
|  | 867 | DCHECK_GE(update_state.scatter_check_threshold, 0); | 
|  | 868 |  | 
|  | 869 | // Set default result values. | 
|  | 870 | result->is_scattering = false; | 
|  | 871 | result->wait_period = kZeroInterval; | 
|  | 872 | result->check_threshold = 0; | 
|  | 873 |  | 
|  | 874 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); | 
|  | 875 |  | 
|  | 876 | // Ensure that a device policy is loaded. | 
|  | 877 | const bool* device_policy_is_loaded_p = ec->GetValue( | 
|  | 878 | dp_provider->var_device_policy_is_loaded()); | 
|  | 879 | if (!(device_policy_is_loaded_p && *device_policy_is_loaded_p)) | 
|  | 880 | return EvalStatus::kSucceeded; | 
|  | 881 |  | 
|  | 882 | // Is scattering enabled by policy? | 
|  | 883 | const TimeDelta* scatter_factor_p = ec->GetValue( | 
|  | 884 | dp_provider->var_scatter_factor()); | 
|  | 885 | if (!scatter_factor_p || *scatter_factor_p == kZeroInterval) | 
|  | 886 | return EvalStatus::kSucceeded; | 
|  | 887 |  | 
|  | 888 | // Obtain a pseudo-random number generator. | 
|  | 889 | const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed()); | 
|  | 890 | POLICY_CHECK_VALUE_AND_FAIL(seed, error); | 
|  | 891 | PRNG prng(*seed); | 
|  | 892 |  | 
|  | 893 | // Step 1: Maintain the scattering wait period. | 
|  | 894 | // | 
|  | 895 | // If no wait period was previously determined, or it no longer fits in the | 
|  | 896 | // scatter factor, then generate a new one. Otherwise, keep the one we have. | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 897 | TimeDelta wait_period = update_state.scatter_wait_period; | 
|  | 898 | if (wait_period == kZeroInterval || wait_period > *scatter_factor_p) { | 
|  | 899 | wait_period = TimeDelta::FromSeconds( | 
|  | 900 | prng.RandMinMax(1, scatter_factor_p->InSeconds())); | 
|  | 901 | } | 
|  | 902 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 903 | // If we surpassed the wait period or the max scatter period associated with | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 904 | // the update, then no wait is needed. | 
|  | 905 | Time wait_expires = (update_state.first_seen + | 
|  | 906 | min(wait_period, update_state.scatter_wait_period_max)); | 
| Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 907 | if (ec->IsWallclockTimeGreaterThan(wait_expires)) | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 908 | wait_period = kZeroInterval; | 
|  | 909 |  | 
|  | 910 | // Step 2: Maintain the update check threshold count. | 
|  | 911 | // | 
|  | 912 | // If an update check threshold is not specified then generate a new | 
|  | 913 | // one. | 
|  | 914 | int check_threshold = update_state.scatter_check_threshold; | 
|  | 915 | if (check_threshold == 0) { | 
|  | 916 | check_threshold = prng.RandMinMax( | 
|  | 917 | update_state.scatter_check_threshold_min, | 
|  | 918 | update_state.scatter_check_threshold_max); | 
|  | 919 | } | 
|  | 920 |  | 
|  | 921 | // If the update check threshold is not within allowed range then nullify it. | 
|  | 922 | // TODO(garnold) This is compliant with current logic found in | 
|  | 923 | // OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied(). We may want | 
|  | 924 | // to change it so that it behaves similarly to the wait period case, namely | 
|  | 925 | // if the current value exceeds the maximum, we set a new one within range. | 
|  | 926 | if (check_threshold > update_state.scatter_check_threshold_max) | 
|  | 927 | check_threshold = 0; | 
|  | 928 |  | 
|  | 929 | // If the update check threshold is non-zero and satisfied, then nullify it. | 
|  | 930 | if (check_threshold > 0 && update_state.num_checks >= check_threshold) | 
|  | 931 | check_threshold = 0; | 
|  | 932 |  | 
|  | 933 | bool is_scattering = (wait_period != kZeroInterval || check_threshold); | 
|  | 934 | EvalStatus ret = EvalStatus::kSucceeded; | 
|  | 935 | if (is_scattering && wait_period == update_state.scatter_wait_period && | 
|  | 936 | check_threshold == update_state.scatter_check_threshold) | 
|  | 937 | ret = EvalStatus::kAskMeAgainLater; | 
|  | 938 | result->is_scattering = is_scattering; | 
|  | 939 | result->wait_period = wait_period; | 
|  | 940 | result->check_threshold = check_threshold; | 
|  | 941 | return ret; | 
|  | 942 | } | 
|  | 943 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 944 | }  // namespace chromeos_update_manager |