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