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