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> |
| 16 | #include <glib.h> |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 17 | #include "update_engine/dbus_service.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 18 | #include "update_engine/download_action.h" |
| 19 | #include "update_engine/filesystem_copier_action.h" |
| 20 | #include "update_engine/libcurl_http_fetcher.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 21 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 22 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 23 | #include "update_engine/omaha_response_handler_action.h" |
| 24 | #include "update_engine/postinstall_runner_action.h" |
| 25 | #include "update_engine/set_bootable_flag_action.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | |
| 27 | using std::tr1::shared_ptr; |
| 28 | using std::string; |
| 29 | using std::vector; |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 33 | const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed"; |
| 34 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | // Returns true on success. |
| 37 | bool GetCPUClockTime(struct timespec* out) { |
| 38 | return clock_gettime(CLOCK_REALTIME, out) == 0; |
| 39 | } |
| 40 | // Returns stop - start. |
| 41 | struct timespec CPUClockTimeElapsed(const struct timespec& start, |
| 42 | const struct timespec& stop) { |
| 43 | CHECK(start.tv_sec >= 0); |
| 44 | CHECK(stop.tv_sec >= 0); |
| 45 | CHECK(start.tv_nsec >= 0); |
| 46 | CHECK(stop.tv_nsec >= 0); |
| 47 | |
| 48 | const int64_t kOneBillion = 1000000000L; |
| 49 | const int64_t start64 = start.tv_sec * kOneBillion + start.tv_nsec; |
| 50 | const int64_t stop64 = stop.tv_sec * kOneBillion + stop.tv_nsec; |
| 51 | |
| 52 | const int64_t result64 = stop64 - start64; |
| 53 | |
| 54 | struct timespec ret; |
| 55 | ret.tv_sec = result64 / kOneBillion; |
| 56 | ret.tv_nsec = result64 % kOneBillion; |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) { |
| 61 | if (spec.tv_sec >= 1) |
| 62 | return true; |
| 63 | return (spec.tv_nsec > 500000000); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | const char* UpdateStatusToString(UpdateStatus status) { |
| 68 | switch (status) { |
| 69 | case UPDATE_STATUS_IDLE: |
| 70 | return "UPDATE_STATUS_IDLE"; |
| 71 | case UPDATE_STATUS_CHECKING_FOR_UPDATE: |
| 72 | return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; |
| 73 | case UPDATE_STATUS_UPDATE_AVAILABLE: |
| 74 | return "UPDATE_STATUS_UPDATE_AVAILABLE"; |
| 75 | case UPDATE_STATUS_DOWNLOADING: |
| 76 | return "UPDATE_STATUS_DOWNLOADING"; |
| 77 | case UPDATE_STATUS_VERIFYING: |
| 78 | return "UPDATE_STATUS_VERIFYING"; |
| 79 | case UPDATE_STATUS_FINALIZING: |
| 80 | return "UPDATE_STATUS_FINALIZING"; |
| 81 | case UPDATE_STATUS_UPDATED_NEED_REBOOT: |
| 82 | return "UPDATE_STATUS_UPDATED_NEED_REBOOT"; |
| 83 | default: |
| 84 | return "unknown status"; |
| 85 | } |
| 86 | } |
| 87 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 88 | void UpdateAttempter::Update() { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 89 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 90 | LOG(INFO) << "Not updating b/c we already updated and we're waiting for " |
| 91 | << "reboot"; |
| 92 | return; |
| 93 | } |
| 94 | if (status_ != UPDATE_STATUS_IDLE) { |
| 95 | // Update in progress. Do nothing |
| 96 | return; |
| 97 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 98 | if (!omaha_request_params_.Init()) { |
| 99 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
| 100 | return; |
| 101 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 102 | CHECK(!processor_.IsRunning()); |
| 103 | processor_.set_delegate(this); |
| 104 | |
| 105 | // Actions: |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 106 | shared_ptr<OmahaRequestAction> update_check_action( |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 107 | new OmahaRequestAction(omaha_request_params_, |
| 108 | NULL, |
| 109 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 110 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
| 111 | new OmahaResponseHandlerAction); |
| 112 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 113 | new FilesystemCopierAction(false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 114 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 115 | new FilesystemCopierAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 116 | shared_ptr<DownloadAction> download_action( |
| 117 | new DownloadAction(new LibcurlHttpFetcher)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 118 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit( |
| 119 | new PostinstallRunnerAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 120 | shared_ptr<SetBootableFlagAction> set_bootable_flag_action( |
| 121 | new SetBootableFlagAction); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 122 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit( |
| 123 | new PostinstallRunnerAction(false)); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 124 | shared_ptr<OmahaRequestAction> install_success_action( |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 125 | new OmahaRequestAction(omaha_request_params_, |
| 126 | new OmahaEvent(OmahaEvent::kTypeInstallComplete, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 127 | OmahaEvent::kResultSuccess, |
| 128 | 0), |
| 129 | new LibcurlHttpFetcher)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 130 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 131 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 132 | response_handler_action_ = response_handler_action; |
| 133 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 134 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 135 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 136 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 137 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 138 | kernel_filesystem_copier_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 139 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 140 | actions_.push_back(shared_ptr<AbstractAction>( |
| 141 | postinstall_runner_action_precommit)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 142 | actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 143 | actions_.push_back(shared_ptr<AbstractAction>( |
| 144 | postinstall_runner_action_postcommit)); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 145 | actions_.push_back(shared_ptr<AbstractAction>(install_success_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 146 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 147 | // Enqueue the actions |
| 148 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 149 | it != actions_.end(); ++it) { |
| 150 | processor_.EnqueueAction(it->get()); |
| 151 | } |
| 152 | |
| 153 | // Bond them together. We have to use the leaf-types when calling |
| 154 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 155 | BondActions(update_check_action.get(), |
| 156 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 157 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 158 | filesystem_copier_action.get()); |
| 159 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 160 | kernel_filesystem_copier_action.get()); |
| 161 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 162 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 163 | BondActions(download_action.get(), |
| 164 | postinstall_runner_action_precommit.get()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 165 | BondActions(postinstall_runner_action_precommit.get(), |
| 166 | set_bootable_flag_action.get()); |
| 167 | BondActions(set_bootable_flag_action.get(), |
| 168 | postinstall_runner_action_postcommit.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 169 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 170 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 171 | processor_.StartProcessing(); |
| 172 | } |
| 173 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 174 | void UpdateAttempter::CheckForUpdate() { |
| 175 | if (status_ != UPDATE_STATUS_IDLE) { |
| 176 | LOG(INFO) << "Check for update requested, but status is " |
| 177 | << UpdateStatusToString(status_) << ", so not checking."; |
| 178 | return; |
| 179 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 180 | Update(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 184 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
| 185 | bool success) { |
| 186 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 187 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 188 | actions_.clear(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 189 | if (success) { |
| 190 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 191 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 192 | } else { |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 193 | LOG(INFO) << "Update failed."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 194 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 195 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
| 199 | download_progress_ = 0.0; |
| 200 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 201 | actions_.clear(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Called whenever an action has finished processing, either successfully |
| 205 | // or otherwise. |
| 206 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 207 | AbstractAction* action, |
| 208 | bool success) { |
| 209 | // Reset download progress regardless of whether or not the download action |
| 210 | // succeeded. |
| 211 | const string type = action->Type(); |
| 212 | if (type == DownloadAction::StaticType()) |
| 213 | download_progress_ = 0.0; |
| 214 | if (!success) |
| 215 | return; |
| 216 | // Find out which action completed. |
| 217 | if (type == OmahaResponseHandlerAction::StaticType()) { |
| 218 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 219 | OmahaResponseHandlerAction* omaha_response_handler_action = |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 220 | dynamic_cast<OmahaResponseHandlerAction*>(action); |
| 221 | CHECK(omaha_response_handler_action); |
| 222 | const InstallPlan& plan = omaha_response_handler_action->install_plan(); |
| 223 | last_checked_time_ = time(NULL); |
| 224 | // TODO(adlr): put version in InstallPlan |
| 225 | new_version_ = "0.0.0.0"; |
| 226 | new_size_ = plan.size; |
| 227 | } else if (type == DownloadAction::StaticType()) { |
| 228 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING); |
| 229 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Stop updating. An attempt will be made to record status to the disk |
| 233 | // so that updates can be resumed later. |
| 234 | void UpdateAttempter::Terminate() { |
| 235 | // TODO(adlr): implement this method. |
| 236 | NOTIMPLEMENTED(); |
| 237 | } |
| 238 | |
| 239 | // Try to resume from a previously Terminate()d update. |
| 240 | void UpdateAttempter::ResumeUpdating() { |
| 241 | // TODO(adlr): implement this method. |
| 242 | NOTIMPLEMENTED(); |
| 243 | } |
| 244 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 245 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
| 246 | if (status_ != UPDATE_STATUS_DOWNLOADING) { |
| 247 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 248 | return; |
| 249 | } |
| 250 | download_progress_ = static_cast<double>(bytes_received) / |
| 251 | static_cast<double>(total); |
| 252 | // We self throttle here |
| 253 | timespec now; |
| 254 | now.tv_sec = 0; |
| 255 | now.tv_nsec = 0; |
| 256 | if (GetCPUClockTime(&now) && |
| 257 | CPUClockTimeGreaterThanHalfSecond( |
| 258 | CPUClockTimeElapsed(last_notify_time_, now))) { |
| 259 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 260 | } |
| 261 | } |
| 262 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 263 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 264 | double* progress, |
| 265 | std::string* current_operation, |
| 266 | std::string* new_version, |
| 267 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 268 | *last_checked_time = last_checked_time_; |
| 269 | *progress = download_progress_; |
| 270 | *current_operation = UpdateStatusToString(status_); |
| 271 | *new_version = new_version_; |
| 272 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 276 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { |
| 277 | status_ = status; |
| 278 | if (!dbus_service_) |
| 279 | return; |
| 280 | GetCPUClockTime(&last_notify_time_); |
| 281 | update_engine_service_emit_status_update( |
| 282 | dbus_service_, |
| 283 | last_checked_time_, |
| 284 | download_progress_, |
| 285 | UpdateStatusToString(status_), |
| 286 | new_version_.c_str(), |
| 287 | new_size_); |
| 288 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 289 | |
| 290 | } // namespace chromeos_update_engine |