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