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