Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 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_check_scheduler.h" |
| 6 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 7 | #include "update_engine/certificate_checker.h" |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 8 | #include "update_engine/http_common.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 9 | #include "update_engine/utils.h" |
| 10 | |
| 11 | namespace chromeos_update_engine { |
| 12 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 13 | // Default update check timeout interval/fuzz values, in seconds. Note that |
| 14 | // actual fuzz is within +/- half of the indicated value. |
| 15 | const int UpdateCheckScheduler::kTimeoutInitialInterval = 7 * 60; |
| 16 | const int UpdateCheckScheduler::kTimeoutPeriodicInterval = 45 * 60; |
| 17 | const int UpdateCheckScheduler::kTimeoutQuickInterval = 1 * 60; |
| 18 | const int UpdateCheckScheduler::kTimeoutMaxBackoffInterval = 4 * 60 * 60; |
| 19 | const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 20 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 21 | UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 22 | GpioHandler* gpio_handler, |
| 23 | SystemState* system_state) |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 24 | : update_attempter_(update_attempter), |
| 25 | enabled_(false), |
| 26 | scheduled_(false), |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 27 | last_interval_(0), |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 28 | poll_interval_(0), |
| 29 | is_test_update_attempted_(false), |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 30 | gpio_handler_(gpio_handler), |
| 31 | system_state_(system_state) {} |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 32 | |
| 33 | UpdateCheckScheduler::~UpdateCheckScheduler() {} |
| 34 | |
| 35 | void UpdateCheckScheduler::Run() { |
| 36 | enabled_ = false; |
| 37 | update_attempter_->set_update_check_scheduler(NULL); |
| 38 | |
| 39 | if (!IsOfficialBuild()) { |
| 40 | LOG(WARNING) << "Non-official build: periodic update checks disabled."; |
| 41 | return; |
| 42 | } |
| 43 | if (IsBootDeviceRemovable()) { |
| 44 | LOG(WARNING) << "Removable device boot: periodic update checks disabled."; |
| 45 | return; |
| 46 | } |
| 47 | enabled_ = true; |
| 48 | |
| 49 | // Registers this scheduler with the update attempter so that scheduler can be |
| 50 | // notified of update status changes. |
| 51 | update_attempter_->set_update_check_scheduler(this); |
| 52 | |
| 53 | // Kicks off periodic update checks. The first check is scheduled |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 54 | // |kTimeoutInitialInterval| seconds from now. Subsequent checks are scheduled |
| 55 | // by ScheduleNextCheck, normally at |kTimeoutPeriodicInterval|-second |
| 56 | // intervals. |
| 57 | ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool UpdateCheckScheduler::IsBootDeviceRemovable() { |
| 61 | return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice())); |
| 62 | } |
| 63 | |
| 64 | bool UpdateCheckScheduler::IsOfficialBuild() { |
| 65 | return utils::IsOfficialBuild(); |
| 66 | } |
| 67 | |
| 68 | guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval, |
| 69 | GSourceFunc function) { |
| 70 | return g_timeout_add_seconds(interval, function, this); |
| 71 | } |
| 72 | |
| 73 | void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) { |
| 74 | if (!CanSchedule()) { |
| 75 | return; |
| 76 | } |
| 77 | last_interval_ = interval; |
| 78 | interval = utils::FuzzInt(interval, fuzz); |
| 79 | if (interval < 0) { |
| 80 | interval = 0; |
| 81 | } |
| 82 | GTimeoutAddSeconds(interval, StaticCheck); |
| 83 | scheduled_ = true; |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 84 | LOG(INFO) << "Next update check in " << utils::FormatSecs(interval); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) { |
| 88 | UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler); |
| 89 | CHECK(me->scheduled_); |
| 90 | me->scheduled_ = false; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 91 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 92 | bool is_test = false; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 93 | if (me->system_state_->IsOOBEComplete() || |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 94 | (is_test = (!me->is_test_update_attempted_ && |
| 95 | me->gpio_handler_ && |
| 96 | me->gpio_handler_->IsTestModeSignaled()))) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 97 | if (is_test) { |
| 98 | LOG(WARNING) |
| 99 | << "test mode signaled, allowing update check prior to OOBE complete"; |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 100 | me->is_test_update_attempted_ = true; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 103 | // Before updating, we flush any previously generated UMA reports. |
| 104 | CertificateChecker::FlushReport(); |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 105 | me->update_attempter_->Update("", "", false, false, is_test, false); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 106 | } else { |
| 107 | // Skips all automatic update checks if the OOBE process is not complete and |
| 108 | // schedules a new check as if it is the first one. |
| 109 | LOG(WARNING) << "Skipping update check because OOBE is not complete."; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 110 | me->ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 111 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 112 | // This check ensures that future update checks will be or are already |
| 113 | // scheduled. The check should never fail. A check failure means that there's |
| 114 | // a bug that will most likely prevent further automatic update checks. It |
| 115 | // seems better to crash in such cases and restart the update_engine daemon |
| 116 | // into, hopefully, a known good state. |
| 117 | CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE || |
| 118 | !me->CanSchedule()); |
| 119 | return FALSE; // Don't run again. |
| 120 | } |
| 121 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 122 | void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(const int forced_interval, |
| 123 | int* next_interval, |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 124 | int* next_fuzz) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 125 | CHECK(next_interval && next_fuzz); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 126 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 127 | int interval = forced_interval; |
| 128 | int fuzz = 0; // Use default fuzz value (see below) |
| 129 | |
| 130 | if (interval == 0) { |
| 131 | int http_response_code; |
| 132 | if (poll_interval_ > 0) { |
| 133 | // Server-dictated poll interval. |
| 134 | interval = poll_interval_; |
| 135 | LOG(WARNING) << "Using server-dictated poll interval: " << interval; |
| 136 | } else if ((http_response_code = update_attempter_->http_response_code()) == |
| 137 | kHttpResponseInternalServerError || |
| 138 | http_response_code == kHttpResponseServiceUnavailable) { |
| 139 | // Implements exponential back off on 500 (Internal Server Error) and 503 |
| 140 | // (Service Unavailable) HTTP response codes. |
| 141 | interval = 2 * last_interval_; |
| 142 | LOG(WARNING) << "Exponential back off due to HTTP response code (" |
| 143 | << http_response_code << ")"; |
| 144 | } |
| 145 | |
| 146 | // Backoff cannot exceed a predetermined maximum period. |
| 147 | if (interval > kTimeoutMaxBackoffInterval) |
| 148 | interval = kTimeoutMaxBackoffInterval; |
| 149 | |
| 150 | // Ensures that under normal conditions the regular update check interval |
| 151 | // and fuzz are used. Also covers the case where back off is required based |
| 152 | // on the initial update check. |
| 153 | if (interval < kTimeoutPeriodicInterval) { |
| 154 | interval = kTimeoutPeriodicInterval; |
| 155 | fuzz = kTimeoutRegularFuzz; |
| 156 | } |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 157 | } |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 158 | |
| 159 | // Set default fuzz to +/- |interval|/2. |
| 160 | if (fuzz == 0) |
| 161 | fuzz = interval; |
| 162 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 163 | *next_interval = interval; |
| 164 | *next_fuzz = fuzz; |
| 165 | } |
| 166 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 167 | void UpdateCheckScheduler::ScheduleNextCheck(bool is_force_quick) { |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 168 | int interval, fuzz; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 169 | ComputeNextIntervalAndFuzz(is_force_quick ? kTimeoutQuickInterval : 0, |
| 170 | &interval, &fuzz); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 171 | ScheduleCheck(interval, fuzz); |
| 172 | } |
| 173 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 174 | void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status, |
| 175 | UpdateNotice notice) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 176 | // We want to schedule the update checks for when we're idle as well as |
| 177 | // after we've successfully applied an update and waiting for the user |
| 178 | // to reboot to ensure our active count is accurate. |
| 179 | if (status == UPDATE_STATUS_IDLE || |
| 180 | status == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 181 | ScheduleNextCheck(notice == kUpdateNoticeTestAddrFailed); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | } // namespace chromeos_update_engine |