blob: f4b284f899cd750bb1a9343bd4e57802aa51e2e7 [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// 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 Reyes63b96d72010-05-10 13:08:54 -07006
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 Reyes4e9b9f42010-04-26 15:06:43 -070013#include <tr1/memory>
14#include <string>
15#include <vector>
16#include <glib.h>
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070017#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070018#include "update_engine/download_action.h"
19#include "update_engine/filesystem_copier_action.h"
20#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070021#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070023#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 Reyes4e9b9f42010-04-26 15:06:43 -070026
27using std::tr1::shared_ptr;
28using std::string;
29using std::vector;
30
31namespace chromeos_update_engine {
32
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070033const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
34
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070035namespace {
36// Returns true on success.
37bool GetCPUClockTime(struct timespec* out) {
38 return clock_gettime(CLOCK_REALTIME, out) == 0;
39}
40// Returns stop - start.
41struct 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}
60bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) {
61 if (spec.tv_sec >= 1)
62 return true;
63 return (spec.tv_nsec > 500000000);
64}
65}
66
67const 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 Petkova4a8a8c2010-07-15 22:21:12 -070088void UpdateAttempter::Update() {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070089 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 Petkova4a8a8c2010-07-15 22:21:12 -070098 if (!omaha_request_params_.Init()) {
99 LOG(ERROR) << "Unable to initialize Omaha request device params.";
100 return;
101 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700102 CHECK(!processor_.IsRunning());
103 processor_.set_delegate(this);
104
105 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700107 new OmahaRequestAction(omaha_request_params_,
108 NULL,
109 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700110 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
111 new OmahaResponseHandlerAction);
112 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700113 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700114 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700115 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700116 shared_ptr<OmahaRequestAction> download_started_action(
117 new OmahaRequestAction(omaha_request_params_,
118 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700119 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700120 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700121 shared_ptr<DownloadAction> download_action(
122 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700123 shared_ptr<OmahaRequestAction> download_finished_action(
124 new OmahaRequestAction(omaha_request_params_,
125 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700126 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700127 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700128 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
129 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700130 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
131 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700132 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
133 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700134 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700135 new OmahaRequestAction(omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700136 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700137 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700138
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700139 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700140 response_handler_action_ = response_handler_action;
141
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700142 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
143 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
144 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700145 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700146 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700147 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700148 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700149 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700150 actions_.push_back(shared_ptr<AbstractAction>(
151 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700152 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700153 actions_.push_back(shared_ptr<AbstractAction>(
154 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700155 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700156
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700157 // Enqueue the actions
158 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
159 it != actions_.end(); ++it) {
160 processor_.EnqueueAction(it->get());
161 }
162
163 // Bond them together. We have to use the leaf-types when calling
164 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700165 BondActions(update_check_action.get(),
166 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700167 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700168 filesystem_copier_action.get());
169 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700170 kernel_filesystem_copier_action.get());
171 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700172 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700173 BondActions(download_action.get(),
174 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700175 BondActions(postinstall_runner_action_precommit.get(),
176 set_bootable_flag_action.get());
177 BondActions(set_bootable_flag_action.get(),
178 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700179
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700180 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700181 processor_.StartProcessing();
182}
183
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700184void UpdateAttempter::CheckForUpdate() {
185 if (status_ != UPDATE_STATUS_IDLE) {
186 LOG(INFO) << "Check for update requested, but status is "
187 << UpdateStatusToString(status_) << ", so not checking.";
188 return;
189 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700190 Update();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700191}
192
193// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700194void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700195 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700196 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700197 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700198 actions_.clear();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700199 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700200 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700201 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700202 } else {
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700203 LOG(INFO) << "Update failed.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700204 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700205 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700206}
207
208void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
209 download_progress_ = 0.0;
210 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700211 actions_.clear();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700212}
213
214// Called whenever an action has finished processing, either successfully
215// or otherwise.
216void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
217 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700218 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700219 // Reset download progress regardless of whether or not the download action
220 // succeeded.
221 const string type = action->Type();
222 if (type == DownloadAction::StaticType())
223 download_progress_ = 0.0;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700224 if (code != kActionCodeSuccess)
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700225 return;
226 // Find out which action completed.
227 if (type == OmahaResponseHandlerAction::StaticType()) {
228 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700229 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700230 dynamic_cast<OmahaResponseHandlerAction*>(action);
231 CHECK(omaha_response_handler_action);
232 const InstallPlan& plan = omaha_response_handler_action->install_plan();
233 last_checked_time_ = time(NULL);
234 // TODO(adlr): put version in InstallPlan
235 new_version_ = "0.0.0.0";
236 new_size_ = plan.size;
237 } else if (type == DownloadAction::StaticType()) {
238 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
239 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700240}
241
242// Stop updating. An attempt will be made to record status to the disk
243// so that updates can be resumed later.
244void UpdateAttempter::Terminate() {
245 // TODO(adlr): implement this method.
246 NOTIMPLEMENTED();
247}
248
249// Try to resume from a previously Terminate()d update.
250void UpdateAttempter::ResumeUpdating() {
251 // TODO(adlr): implement this method.
252 NOTIMPLEMENTED();
253}
254
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700255void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
256 if (status_ != UPDATE_STATUS_DOWNLOADING) {
257 LOG(ERROR) << "BytesReceived called while not downloading.";
258 return;
259 }
260 download_progress_ = static_cast<double>(bytes_received) /
261 static_cast<double>(total);
262 // We self throttle here
263 timespec now;
264 now.tv_sec = 0;
265 now.tv_nsec = 0;
266 if (GetCPUClockTime(&now) &&
267 CPUClockTimeGreaterThanHalfSecond(
268 CPUClockTimeElapsed(last_notify_time_, now))) {
269 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
270 }
271}
272
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700273bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
274 double* progress,
275 std::string* current_operation,
276 std::string* new_version,
277 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700278 *last_checked_time = last_checked_time_;
279 *progress = download_progress_;
280 *current_operation = UpdateStatusToString(status_);
281 *new_version = new_version_;
282 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700283 return true;
284}
285
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700286void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
287 status_ = status;
288 if (!dbus_service_)
289 return;
290 GetCPUClockTime(&last_notify_time_);
291 update_engine_service_emit_status_update(
292 dbus_service_,
293 last_checked_time_,
294 download_progress_,
295 UpdateStatusToString(status_),
296 new_version_.c_str(),
297 new_size_);
298}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700299
300} // namespace chromeos_update_engine