Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/update_attempter.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 6 | |
| 7 | // From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L |
| 8 | #ifndef _POSIX_C_SOURCE |
| 9 | #define _POSIX_C_SOURCE 199309L |
| 10 | #endif // _POSIX_C_SOURCE |
| 11 | #include <time.h> |
| 12 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 13 | #include <tr1/memory> |
| 14 | #include <string> |
| 15 | #include <vector> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 16 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 17 | #include <glib.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 18 | #include <metrics/metrics_library.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 19 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 20 | #include "update_engine/dbus_service.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | #include "update_engine/download_action.h" |
| 22 | #include "update_engine/filesystem_copier_action.h" |
| 23 | #include "update_engine/libcurl_http_fetcher.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 24 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 25 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | #include "update_engine/omaha_response_handler_action.h" |
| 27 | #include "update_engine/postinstall_runner_action.h" |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 28 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 29 | #include "update_engine/set_bootable_flag_action.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 30 | #include "update_engine/update_check_scheduler.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 31 | |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 32 | using base::TimeDelta; |
| 33 | using base::TimeTicks; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 34 | using std::tr1::shared_ptr; |
| 35 | using std::string; |
| 36 | using std::vector; |
| 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 40 | const int UpdateAttempter::kMaxDeltaUpdateFailures = 3; |
| 41 | |
Darin Petkov | cd1666f | 2010-09-23 09:53:44 -0700 | [diff] [blame] | 42 | const char* kUpdateCompletedMarker = |
| 43 | "/var/run/update_engine_autoupdate_completed"; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 44 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 45 | const char* UpdateStatusToString(UpdateStatus status) { |
| 46 | switch (status) { |
| 47 | case UPDATE_STATUS_IDLE: |
| 48 | return "UPDATE_STATUS_IDLE"; |
| 49 | case UPDATE_STATUS_CHECKING_FOR_UPDATE: |
| 50 | return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; |
| 51 | case UPDATE_STATUS_UPDATE_AVAILABLE: |
| 52 | return "UPDATE_STATUS_UPDATE_AVAILABLE"; |
| 53 | case UPDATE_STATUS_DOWNLOADING: |
| 54 | return "UPDATE_STATUS_DOWNLOADING"; |
| 55 | case UPDATE_STATUS_VERIFYING: |
| 56 | return "UPDATE_STATUS_VERIFYING"; |
| 57 | case UPDATE_STATUS_FINALIZING: |
| 58 | return "UPDATE_STATUS_FINALIZING"; |
| 59 | case UPDATE_STATUS_UPDATED_NEED_REBOOT: |
| 60 | return "UPDATE_STATUS_UPDATED_NEED_REBOOT"; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 61 | case UPDATE_STATUS_REPORTING_ERROR_EVENT: |
| 62 | return "UPDATE_STATUS_REPORTING_ERROR_EVENT"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 63 | default: |
| 64 | return "unknown status"; |
| 65 | } |
| 66 | } |
| 67 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 68 | // Turns a generic kActionCodeError to a generic error code specific |
| 69 | // to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is |
| 70 | // not kActionCodeError, or the action is not matched, returns |code| |
| 71 | // unchanged. |
| 72 | ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 73 | ActionExitCode code) { |
| 74 | if (code != kActionCodeError) |
| 75 | return code; |
| 76 | |
| 77 | const string type = action->Type(); |
| 78 | if (type == OmahaRequestAction::StaticType()) |
| 79 | return kActionCodeOmahaRequestError; |
| 80 | if (type == OmahaResponseHandlerAction::StaticType()) |
| 81 | return kActionCodeOmahaResponseHandlerError; |
| 82 | if (type == FilesystemCopierAction::StaticType()) |
| 83 | return kActionCodeFilesystemCopierError; |
| 84 | if (type == PostinstallRunnerAction::StaticType()) |
| 85 | return kActionCodePostinstallRunnerError; |
| 86 | if (type == SetBootableFlagAction::StaticType()) |
| 87 | return kActionCodeSetBootableFlagError; |
| 88 | |
| 89 | return code; |
| 90 | } |
| 91 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 92 | UpdateAttempter::UpdateAttempter(PrefsInterface* prefs, |
| 93 | MetricsLibraryInterface* metrics_lib) |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 94 | : processor_(new ActionProcessor()), |
| 95 | dbus_service_(NULL), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 96 | prefs_(prefs), |
| 97 | metrics_lib_(metrics_lib), |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 98 | update_check_scheduler_(NULL), |
| 99 | http_response_code_(0), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 100 | priority_(utils::kProcessPriorityNormal), |
| 101 | manage_priority_source_(NULL), |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 102 | download_active_(false), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 103 | status_(UPDATE_STATUS_IDLE), |
| 104 | download_progress_(0.0), |
| 105 | last_checked_time_(0), |
| 106 | new_version_("0.0.0.0"), |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 107 | new_size_(0), |
| 108 | is_full_update_(false) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 109 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 110 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 111 | } |
| 112 | |
| 113 | UpdateAttempter::~UpdateAttempter() { |
| 114 | CleanupPriorityManagement(); |
| 115 | } |
| 116 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 117 | void UpdateAttempter::Update(const std::string& app_version, |
| 118 | const std::string& omaha_url) { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 119 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 120 | LOG(INFO) << "Not updating b/c we already updated and we're waiting for " |
| 121 | << "reboot"; |
| 122 | return; |
| 123 | } |
| 124 | if (status_ != UPDATE_STATUS_IDLE) { |
| 125 | // Update in progress. Do nothing |
| 126 | return; |
| 127 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 128 | http_response_code_ = 0; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 129 | if (!omaha_request_params_.Init(app_version, omaha_url)) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 130 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
| 131 | return; |
| 132 | } |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 133 | DisableDeltaUpdateIfNeeded(); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 134 | CHECK(!processor_->IsRunning()); |
| 135 | processor_->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 136 | |
| 137 | // Actions: |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 138 | shared_ptr<OmahaRequestAction> update_check_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 139 | new OmahaRequestAction(prefs_, |
| 140 | omaha_request_params_, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 141 | NULL, |
| 142 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 143 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
| 144 | new OmahaResponseHandlerAction); |
| 145 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 146 | new FilesystemCopierAction(false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 147 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 148 | new FilesystemCopierAction(true)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 149 | shared_ptr<OmahaRequestAction> download_started_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 150 | new OmahaRequestAction(prefs_, |
| 151 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 152 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 153 | OmahaEvent::kTypeUpdateDownloadStarted), |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 154 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 155 | shared_ptr<DownloadAction> download_action( |
| 156 | new DownloadAction(new LibcurlHttpFetcher)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 157 | shared_ptr<OmahaRequestAction> download_finished_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 158 | new OmahaRequestAction(prefs_, |
| 159 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 160 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 161 | OmahaEvent::kTypeUpdateDownloadFinished), |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 162 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 163 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit( |
| 164 | new PostinstallRunnerAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 165 | shared_ptr<SetBootableFlagAction> set_bootable_flag_action( |
| 166 | new SetBootableFlagAction); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 167 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit( |
| 168 | new PostinstallRunnerAction(false)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 169 | shared_ptr<OmahaRequestAction> update_complete_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 170 | new OmahaRequestAction(prefs_, |
| 171 | omaha_request_params_, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 172 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 173 | new LibcurlHttpFetcher)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 174 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 175 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 176 | response_handler_action_ = response_handler_action; |
| 177 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 178 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 179 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 180 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 181 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 182 | kernel_filesystem_copier_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 183 | actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 184 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 185 | actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 186 | actions_.push_back(shared_ptr<AbstractAction>( |
| 187 | postinstall_runner_action_precommit)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 188 | actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 189 | actions_.push_back(shared_ptr<AbstractAction>( |
| 190 | postinstall_runner_action_postcommit)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 191 | actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 192 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 193 | // Enqueue the actions |
| 194 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 195 | it != actions_.end(); ++it) { |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 196 | processor_->EnqueueAction(it->get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Bond them together. We have to use the leaf-types when calling |
| 200 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 201 | BondActions(update_check_action.get(), |
| 202 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 203 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 204 | filesystem_copier_action.get()); |
| 205 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 206 | kernel_filesystem_copier_action.get()); |
| 207 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 208 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 209 | BondActions(download_action.get(), |
| 210 | postinstall_runner_action_precommit.get()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 211 | BondActions(postinstall_runner_action_precommit.get(), |
| 212 | set_bootable_flag_action.get()); |
| 213 | BondActions(set_bootable_flag_action.get(), |
| 214 | postinstall_runner_action_postcommit.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 215 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 216 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 217 | processor_->StartProcessing(); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 220 | void UpdateAttempter::CheckForUpdate(const std::string& app_version, |
| 221 | const std::string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 222 | if (status_ != UPDATE_STATUS_IDLE) { |
| 223 | LOG(INFO) << "Check for update requested, but status is " |
| 224 | << UpdateStatusToString(status_) << ", so not checking."; |
| 225 | return; |
| 226 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 227 | Update(app_version, omaha_url); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 230 | bool UpdateAttempter::RebootIfNeeded() { |
| 231 | if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 232 | LOG(INFO) << "Reboot requested, but status is " |
| 233 | << UpdateStatusToString(status_) << ", so not rebooting."; |
| 234 | return false; |
| 235 | } |
| 236 | TEST_AND_RETURN_FALSE(utils::Reboot()); |
| 237 | return true; |
| 238 | } |
| 239 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 240 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 241 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 242 | ActionExitCode code) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 243 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 244 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 245 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 246 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 247 | // Reset process priority back to normal. |
| 248 | CleanupPriorityManagement(); |
| 249 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 250 | if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 251 | LOG(INFO) << "Error event sent."; |
| 252 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
| 253 | return; |
| 254 | } |
| 255 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 256 | if (code == kActionCodeSuccess) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 257 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 258 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 259 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0); |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 260 | |
| 261 | // Report the time it took to update the system. |
| 262 | int64_t update_time = time(NULL) - last_checked_time_; |
| 263 | metrics_lib_->SendToUMA("Installer.UpdateTime", |
| 264 | static_cast<int>(update_time), // sample |
| 265 | 1, // min = 1 second |
| 266 | 20 * 60, // max = 20 minutes |
| 267 | 50); // buckets |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 268 | return; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 269 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 270 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 271 | if (ScheduleErrorEventAction()) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 272 | return; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 273 | } |
| 274 | LOG(INFO) << "No update."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 275 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 279 | // Reset process priority back to normal. |
| 280 | CleanupPriorityManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 281 | download_progress_ = 0.0; |
| 282 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 283 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 284 | error_event_.reset(NULL); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Called whenever an action has finished processing, either successfully |
| 288 | // or otherwise. |
| 289 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 290 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 291 | ActionExitCode code) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 292 | // Reset download progress regardless of whether or not the download |
| 293 | // action succeeded. Also, get the response code from HTTP request |
| 294 | // actions (update download as well as the initial update check |
| 295 | // actions). |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 296 | const string type = action->Type(); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 297 | if (type == DownloadAction::StaticType()) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 298 | download_progress_ = 0.0; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 299 | DownloadAction* download_action = dynamic_cast<DownloadAction*>(action); |
| 300 | http_response_code_ = download_action->GetHTTPResponseCode(); |
| 301 | } else if (type == OmahaRequestAction::StaticType()) { |
| 302 | OmahaRequestAction* omaha_request_action = |
| 303 | dynamic_cast<OmahaRequestAction*>(action); |
| 304 | // If the request is not an event, then it's the update-check. |
| 305 | if (!omaha_request_action->IsEvent()) { |
| 306 | http_response_code_ = omaha_request_action->GetHTTPResponseCode(); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 307 | // Forward the server-dictated poll interval to the update check |
| 308 | // scheduler, if any. |
| 309 | if (update_check_scheduler_) { |
| 310 | update_check_scheduler_->set_poll_interval( |
| 311 | omaha_request_action->GetOutputObject().poll_interval); |
| 312 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 315 | if (code != kActionCodeSuccess) { |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 316 | // If this was a delta update attempt and the current state is at or past |
| 317 | // the download phase, count the failure in case a switch to full update |
| 318 | // becomes necessary. Ignore network transfer timeouts and failures. |
| 319 | if (status_ >= UPDATE_STATUS_DOWNLOADING && |
| 320 | !is_full_update_ && |
| 321 | code != kActionCodeDownloadTransferError) { |
| 322 | MarkDeltaUpdateFailure(); |
| 323 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 324 | // On failure, schedule an error event to be sent to Omaha. |
| 325 | CreatePendingErrorEvent(action, code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 326 | return; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 327 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 328 | // Find out which action completed. |
| 329 | if (type == OmahaResponseHandlerAction::StaticType()) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 330 | // Note that the status will be updated to DOWNLOADING when some |
| 331 | // bytes get actually downloaded from the server and the |
| 332 | // BytesReceived callback is invoked. This avoids notifying the |
| 333 | // user that a download has started in cases when the server and |
| 334 | // the client are unable to initiate the download. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 335 | OmahaResponseHandlerAction* omaha_response_handler_action = |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 336 | dynamic_cast<OmahaResponseHandlerAction*>(action); |
| 337 | CHECK(omaha_response_handler_action); |
| 338 | const InstallPlan& plan = omaha_response_handler_action->install_plan(); |
| 339 | last_checked_time_ = time(NULL); |
| 340 | // TODO(adlr): put version in InstallPlan |
| 341 | new_version_ = "0.0.0.0"; |
| 342 | new_size_ = plan.size; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 343 | is_full_update_ = plan.is_full_update; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 344 | SetupPriorityManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 345 | } else if (type == DownloadAction::StaticType()) { |
| 346 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING); |
| 347 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Stop updating. An attempt will be made to record status to the disk |
| 351 | // so that updates can be resumed later. |
| 352 | void UpdateAttempter::Terminate() { |
| 353 | // TODO(adlr): implement this method. |
| 354 | NOTIMPLEMENTED(); |
| 355 | } |
| 356 | |
| 357 | // Try to resume from a previously Terminate()d update. |
| 358 | void UpdateAttempter::ResumeUpdating() { |
| 359 | // TODO(adlr): implement this method. |
| 360 | NOTIMPLEMENTED(); |
| 361 | } |
| 362 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 363 | void UpdateAttempter::SetDownloadStatus(bool active) { |
| 364 | download_active_ = active; |
| 365 | LOG(INFO) << "Download status: " << (active ? "active" : "inactive"); |
| 366 | } |
| 367 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 368 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 369 | if (!download_active_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 370 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 371 | return; |
| 372 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 373 | double progress = static_cast<double>(bytes_received) / |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 374 | static_cast<double>(total); |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 375 | // Self throttle based on progress. Also send notifications if |
| 376 | // progress is too slow. |
| 377 | const double kDeltaPercent = 0.01; // 1% |
| 378 | if (status_ != UPDATE_STATUS_DOWNLOADING || |
| 379 | bytes_received == total || |
| 380 | progress - download_progress_ >= kDeltaPercent || |
| 381 | TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) { |
| 382 | download_progress_ = progress; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 383 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 384 | } |
| 385 | } |
| 386 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 387 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 388 | double* progress, |
| 389 | std::string* current_operation, |
| 390 | std::string* new_version, |
| 391 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 392 | *last_checked_time = last_checked_time_; |
| 393 | *progress = download_progress_; |
| 394 | *current_operation = UpdateStatusToString(status_); |
| 395 | *new_version = new_version_; |
| 396 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 400 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { |
| 401 | status_ = status; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 402 | if (update_check_scheduler_) { |
| 403 | update_check_scheduler_->SetUpdateStatus(status_); |
| 404 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 405 | if (!dbus_service_) |
| 406 | return; |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 407 | last_notify_time_ = TimeTicks::Now(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 408 | update_engine_service_emit_status_update( |
| 409 | dbus_service_, |
| 410 | last_checked_time_, |
| 411 | download_progress_, |
| 412 | UpdateStatusToString(status_), |
| 413 | new_version_.c_str(), |
| 414 | new_size_); |
| 415 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 416 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 417 | void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, |
| 418 | ActionExitCode code) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 419 | if (error_event_.get()) { |
| 420 | // This shouldn't really happen. |
| 421 | LOG(WARNING) << "There's already an existing pending error event."; |
| 422 | return; |
| 423 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 424 | |
| 425 | // For now assume that Omaha response action failure means that |
| 426 | // there's no update so don't send an event. Also, double check that |
| 427 | // the failure has not occurred while sending an error event -- in |
| 428 | // which case don't schedule another. This shouldn't really happen |
| 429 | // but just in case... |
| 430 | if (action->Type() == OmahaResponseHandlerAction::StaticType() || |
| 431 | status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | code = GetErrorCodeForAction(action, code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 436 | error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 437 | OmahaEvent::kResultError, |
| 438 | code)); |
| 439 | } |
| 440 | |
| 441 | bool UpdateAttempter::ScheduleErrorEventAction() { |
| 442 | if (error_event_.get() == NULL) |
| 443 | return false; |
| 444 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 445 | LOG(INFO) << "Update failed -- reporting the error event."; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 446 | shared_ptr<OmahaRequestAction> error_event_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 447 | new OmahaRequestAction(prefs_, |
| 448 | omaha_request_params_, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 449 | error_event_.release(), // Pass ownership. |
| 450 | new LibcurlHttpFetcher)); |
| 451 | actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 452 | processor_->EnqueueAction(error_event_action.get()); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 453 | SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 454 | processor_->StartProcessing(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 458 | void UpdateAttempter::SetPriority(utils::ProcessPriority priority) { |
| 459 | if (priority_ == priority) { |
| 460 | return; |
| 461 | } |
| 462 | if (utils::SetProcessPriority(priority)) { |
| 463 | priority_ = priority; |
| 464 | LOG(INFO) << "Process priority = " << priority_; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void UpdateAttempter::SetupPriorityManagement() { |
| 469 | if (manage_priority_source_) { |
| 470 | LOG(ERROR) << "Process priority timeout source hasn't been destroyed."; |
| 471 | CleanupPriorityManagement(); |
| 472 | } |
| 473 | const int kPriorityTimeout = 10 * 60; // 10 minutes |
| 474 | manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout); |
| 475 | g_source_set_callback(manage_priority_source_, |
| 476 | StaticManagePriorityCallback, |
| 477 | this, |
| 478 | NULL); |
| 479 | g_source_attach(manage_priority_source_, NULL); |
| 480 | SetPriority(utils::kProcessPriorityLow); |
| 481 | } |
| 482 | |
| 483 | void UpdateAttempter::CleanupPriorityManagement() { |
| 484 | if (manage_priority_source_) { |
| 485 | g_source_destroy(manage_priority_source_); |
| 486 | manage_priority_source_ = NULL; |
| 487 | } |
| 488 | SetPriority(utils::kProcessPriorityNormal); |
| 489 | } |
| 490 | |
| 491 | gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) { |
| 492 | return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback(); |
| 493 | } |
| 494 | |
| 495 | bool UpdateAttempter::ManagePriorityCallback() { |
| 496 | // If the current process priority is below normal, set it to normal |
| 497 | // and let GLib invoke this callback again. |
| 498 | if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) { |
| 499 | SetPriority(utils::kProcessPriorityNormal); |
| 500 | return true; |
| 501 | } |
| 502 | // Set the priority to high and let GLib destroy the timeout source. |
| 503 | SetPriority(utils::kProcessPriorityHigh); |
| 504 | manage_priority_source_ = NULL; |
| 505 | return false; |
| 506 | } |
| 507 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame^] | 508 | void UpdateAttempter::DisableDeltaUpdateIfNeeded() { |
| 509 | int64_t delta_failures; |
| 510 | if (omaha_request_params_.delta_okay && |
| 511 | prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) && |
| 512 | delta_failures >= kMaxDeltaUpdateFailures) { |
| 513 | LOG(WARNING) << "Too many delta update failures, forcing full update."; |
| 514 | omaha_request_params_.delta_okay = false; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | void UpdateAttempter::MarkDeltaUpdateFailure() { |
| 519 | CHECK(!is_full_update_); |
| 520 | int64_t delta_failures; |
| 521 | if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) || |
| 522 | delta_failures < 0) { |
| 523 | delta_failures = 0; |
| 524 | } |
| 525 | prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures); |
| 526 | } |
| 527 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 528 | } // namespace chromeos_update_engine |