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" |
| 21 | #include "update_engine/omaha_request_prep_action.h" |
| 22 | #include "update_engine/omaha_response_handler_action.h" |
| 23 | #include "update_engine/postinstall_runner_action.h" |
| 24 | #include "update_engine/set_bootable_flag_action.h" |
| 25 | #include "update_engine/update_check_action.h" |
| 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 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 88 | void UpdateAttempter::Update(bool force_full_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 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 98 | full_update_ = force_full_update; |
| 99 | CHECK(!processor_.IsRunning()); |
| 100 | processor_.set_delegate(this); |
| 101 | |
| 102 | // Actions: |
| 103 | shared_ptr<OmahaRequestPrepAction> request_prep_action( |
| 104 | new OmahaRequestPrepAction(force_full_update)); |
| 105 | shared_ptr<UpdateCheckAction> update_check_action( |
| 106 | new UpdateCheckAction(new LibcurlHttpFetcher)); |
| 107 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
| 108 | new OmahaResponseHandlerAction); |
| 109 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 110 | new FilesystemCopierAction(false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 111 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 112 | new FilesystemCopierAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 113 | shared_ptr<DownloadAction> download_action( |
| 114 | new DownloadAction(new LibcurlHttpFetcher)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 115 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit( |
| 116 | new PostinstallRunnerAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 117 | shared_ptr<SetBootableFlagAction> set_bootable_flag_action( |
| 118 | new SetBootableFlagAction); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 119 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit( |
| 120 | new PostinstallRunnerAction(false)); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 121 | |
| 122 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 123 | response_handler_action_ = response_handler_action; |
| 124 | |
| 125 | actions_.push_back(shared_ptr<AbstractAction>(request_prep_action)); |
| 126 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 127 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 128 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 129 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 130 | kernel_filesystem_copier_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 131 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 132 | actions_.push_back(shared_ptr<AbstractAction>( |
| 133 | postinstall_runner_action_precommit)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 134 | actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 135 | actions_.push_back(shared_ptr<AbstractAction>( |
| 136 | postinstall_runner_action_postcommit)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 137 | |
| 138 | // Enqueue the actions |
| 139 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 140 | it != actions_.end(); ++it) { |
| 141 | processor_.EnqueueAction(it->get()); |
| 142 | } |
| 143 | |
| 144 | // Bond them together. We have to use the leaf-types when calling |
| 145 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 146 | BondActions(request_prep_action.get(), |
| 147 | update_check_action.get()); |
| 148 | BondActions(update_check_action.get(), |
| 149 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 150 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 151 | filesystem_copier_action.get()); |
| 152 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 153 | kernel_filesystem_copier_action.get()); |
| 154 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 155 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 156 | BondActions(download_action.get(), |
| 157 | postinstall_runner_action_precommit.get()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 158 | BondActions(postinstall_runner_action_precommit.get(), |
| 159 | set_bootable_flag_action.get()); |
| 160 | BondActions(set_bootable_flag_action.get(), |
| 161 | postinstall_runner_action_postcommit.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 162 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 163 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 164 | processor_.StartProcessing(); |
| 165 | } |
| 166 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 167 | void UpdateAttempter::CheckForUpdate() { |
| 168 | if (status_ != UPDATE_STATUS_IDLE) { |
| 169 | LOG(INFO) << "Check for update requested, but status is " |
| 170 | << UpdateStatusToString(status_) << ", so not checking."; |
| 171 | return; |
| 172 | } |
| 173 | Update(false); |
| 174 | } |
| 175 | |
| 176 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 177 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
| 178 | bool success) { |
| 179 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 180 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame^] | 181 | actions_.clear(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 182 | if (success) { |
| 183 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame^] | 184 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 185 | } else { |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 186 | LOG(INFO) << "Update failed."; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 187 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 188 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
| 192 | download_progress_ = 0.0; |
| 193 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame^] | 194 | actions_.clear(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Called whenever an action has finished processing, either successfully |
| 198 | // or otherwise. |
| 199 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 200 | AbstractAction* action, |
| 201 | bool success) { |
| 202 | // Reset download progress regardless of whether or not the download action |
| 203 | // succeeded. |
| 204 | const string type = action->Type(); |
| 205 | if (type == DownloadAction::StaticType()) |
| 206 | download_progress_ = 0.0; |
| 207 | if (!success) |
| 208 | return; |
| 209 | // Find out which action completed. |
| 210 | if (type == OmahaResponseHandlerAction::StaticType()) { |
| 211 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 212 | OmahaResponseHandlerAction* omaha_response_handler_action = |
| 213 | dynamic_cast<OmahaResponseHandlerAction*>(action); |
| 214 | CHECK(omaha_response_handler_action); |
| 215 | const InstallPlan& plan = omaha_response_handler_action->install_plan(); |
| 216 | last_checked_time_ = time(NULL); |
| 217 | // TODO(adlr): put version in InstallPlan |
| 218 | new_version_ = "0.0.0.0"; |
| 219 | new_size_ = plan.size; |
| 220 | } else if (type == DownloadAction::StaticType()) { |
| 221 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING); |
| 222 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Stop updating. An attempt will be made to record status to the disk |
| 226 | // so that updates can be resumed later. |
| 227 | void UpdateAttempter::Terminate() { |
| 228 | // TODO(adlr): implement this method. |
| 229 | NOTIMPLEMENTED(); |
| 230 | } |
| 231 | |
| 232 | // Try to resume from a previously Terminate()d update. |
| 233 | void UpdateAttempter::ResumeUpdating() { |
| 234 | // TODO(adlr): implement this method. |
| 235 | NOTIMPLEMENTED(); |
| 236 | } |
| 237 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 238 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
| 239 | if (status_ != UPDATE_STATUS_DOWNLOADING) { |
| 240 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 241 | return; |
| 242 | } |
| 243 | download_progress_ = static_cast<double>(bytes_received) / |
| 244 | static_cast<double>(total); |
| 245 | // We self throttle here |
| 246 | timespec now; |
| 247 | now.tv_sec = 0; |
| 248 | now.tv_nsec = 0; |
| 249 | if (GetCPUClockTime(&now) && |
| 250 | CPUClockTimeGreaterThanHalfSecond( |
| 251 | CPUClockTimeElapsed(last_notify_time_, now))) { |
| 252 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 253 | } |
| 254 | } |
| 255 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 256 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 257 | double* progress, |
| 258 | std::string* current_operation, |
| 259 | std::string* new_version, |
| 260 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 261 | *last_checked_time = last_checked_time_; |
| 262 | *progress = download_progress_; |
| 263 | *current_operation = UpdateStatusToString(status_); |
| 264 | *new_version = new_version_; |
| 265 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 269 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { |
| 270 | status_ = status; |
| 271 | if (!dbus_service_) |
| 272 | return; |
| 273 | GetCPUClockTime(&last_notify_time_); |
| 274 | update_engine_service_emit_status_update( |
| 275 | dbus_service_, |
| 276 | last_checked_time_, |
| 277 | download_progress_, |
| 278 | UpdateStatusToString(status_), |
| 279 | new_version_.c_str(), |
| 280 | new_size_); |
| 281 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 282 | |
| 283 | } // namespace chromeos_update_engine |