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