blob: 85e734f7e14b898130e1a59324b74007b63014d7 [file] [log] [blame]
Gilad Arnold1ebd8132012-03-05 10:19:29 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov1023a602010-08-30 13:47:51 -07002// 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 Rocha7f9aea22011-09-12 14:31:24 -07007#include "update_engine/certificate_checker.h"
Gilad Arnold1ebd8132012-03-05 10:19:29 -08008#include "update_engine/http_common.h"
Jay Srinivasan43488792012-06-19 00:25:31 -07009#include "update_engine/system_state.h"
Darin Petkov1023a602010-08-30 13:47:51 -070010#include "update_engine/utils.h"
11
12namespace chromeos_update_engine {
13
Gilad Arnold1ebd8132012-03-05 10:19:29 -080014// Default update check timeout interval/fuzz values, in seconds. Note that
15// actual fuzz is within +/- half of the indicated value.
16const int UpdateCheckScheduler::kTimeoutInitialInterval = 7 * 60;
17const int UpdateCheckScheduler::kTimeoutPeriodicInterval = 45 * 60;
18const int UpdateCheckScheduler::kTimeoutQuickInterval = 1 * 60;
19const int UpdateCheckScheduler::kTimeoutMaxBackoffInterval = 4 * 60 * 60;
20const int UpdateCheckScheduler::kTimeoutRegularFuzz = 10 * 60;
Darin Petkov1023a602010-08-30 13:47:51 -070021
Gilad Arnold4d740eb2012-05-15 08:48:13 -070022UpdateCheckScheduler::UpdateCheckScheduler(UpdateAttempter* update_attempter,
Jay Srinivasan08fce042012-06-07 16:31:01 -070023 SystemState* system_state)
Darin Petkov1023a602010-08-30 13:47:51 -070024 : update_attempter_(update_attempter),
25 enabled_(false),
26 scheduled_(false),
Darin Petkov85ced132010-09-01 10:20:56 -070027 last_interval_(0),
Gilad Arnold4d740eb2012-05-15 08:48:13 -070028 poll_interval_(0),
29 is_test_update_attempted_(false),
Jay Srinivasan08fce042012-06-07 16:31:01 -070030 system_state_(system_state) {}
Darin Petkov1023a602010-08-30 13:47:51 -070031
32UpdateCheckScheduler::~UpdateCheckScheduler() {}
33
34void UpdateCheckScheduler::Run() {
35 enabled_ = false;
36 update_attempter_->set_update_check_scheduler(NULL);
37
38 if (!IsOfficialBuild()) {
39 LOG(WARNING) << "Non-official build: periodic update checks disabled.";
40 return;
41 }
42 if (IsBootDeviceRemovable()) {
43 LOG(WARNING) << "Removable device boot: periodic update checks disabled.";
44 return;
45 }
46 enabled_ = true;
47
48 // Registers this scheduler with the update attempter so that scheduler can be
49 // notified of update status changes.
50 update_attempter_->set_update_check_scheduler(this);
51
52 // Kicks off periodic update checks. The first check is scheduled
Gilad Arnold1ebd8132012-03-05 10:19:29 -080053 // |kTimeoutInitialInterval| seconds from now. Subsequent checks are scheduled
54 // by ScheduleNextCheck, normally at |kTimeoutPeriodicInterval|-second
55 // intervals.
56 ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz);
Darin Petkov1023a602010-08-30 13:47:51 -070057}
58
59bool UpdateCheckScheduler::IsBootDeviceRemovable() {
60 return utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()));
61}
62
63bool UpdateCheckScheduler::IsOfficialBuild() {
64 return utils::IsOfficialBuild();
65}
66
67guint UpdateCheckScheduler::GTimeoutAddSeconds(guint interval,
68 GSourceFunc function) {
69 return g_timeout_add_seconds(interval, function, this);
70}
71
72void UpdateCheckScheduler::ScheduleCheck(int interval, int fuzz) {
73 if (!CanSchedule()) {
74 return;
75 }
76 last_interval_ = interval;
77 interval = utils::FuzzInt(interval, fuzz);
78 if (interval < 0) {
79 interval = 0;
80 }
81 GTimeoutAddSeconds(interval, StaticCheck);
82 scheduled_ = true;
Gilad Arnoldd7b513d2012-05-10 14:25:27 -070083 LOG(INFO) << "Next update check in " << utils::FormatSecs(interval);
Darin Petkov1023a602010-08-30 13:47:51 -070084}
85
86gboolean UpdateCheckScheduler::StaticCheck(void* scheduler) {
87 UpdateCheckScheduler* me = reinterpret_cast<UpdateCheckScheduler*>(scheduler);
88 CHECK(me->scheduled_);
89 me->scheduled_ = false;
Gilad Arnold1ebd8132012-03-05 10:19:29 -080090
Gilad Arnold7c04e762012-05-23 10:54:02 -070091 bool is_test_mode = false;
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080092 GpioHandler* gpio_handler = me->system_state_->gpio_handler();
Jay Srinivasan08fce042012-06-07 16:31:01 -070093 if (me->system_state_->IsOOBEComplete() ||
Gilad Arnold7c04e762012-05-23 10:54:02 -070094 (is_test_mode = (!me->is_test_update_attempted_ &&
Gilad Arnoldbf7919b2013-01-08 13:07:37 -080095 gpio_handler->IsTestModeSignaled()))) {
Gilad Arnold7c04e762012-05-23 10:54:02 -070096 if (is_test_mode) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -080097 LOG(WARNING)
98 << "test mode signaled, allowing update check prior to OOBE complete";
Gilad Arnold4d740eb2012-05-15 08:48:13 -070099 me->is_test_update_attempted_ = true;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800100 }
101
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700102 // Before updating, we flush any previously generated UMA reports.
103 CertificateChecker::FlushReport();
Gilad Arnold7c04e762012-05-23 10:54:02 -0700104 me->update_attempter_->Update("", "", false, false, is_test_mode, false);
Darin Petkov2a0e6332010-09-24 14:43:41 -0700105 } else {
106 // Skips all automatic update checks if the OOBE process is not complete and
107 // schedules a new check as if it is the first one.
108 LOG(WARNING) << "Skipping update check because OOBE is not complete.";
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800109 me->ScheduleCheck(kTimeoutInitialInterval, kTimeoutRegularFuzz);
Darin Petkov2a0e6332010-09-24 14:43:41 -0700110 }
Darin Petkov1023a602010-08-30 13:47:51 -0700111 // This check ensures that future update checks will be or are already
112 // scheduled. The check should never fail. A check failure means that there's
113 // a bug that will most likely prevent further automatic update checks. It
114 // seems better to crash in such cases and restart the update_engine daemon
115 // into, hopefully, a known good state.
116 CHECK(me->update_attempter_->status() != UPDATE_STATUS_IDLE ||
117 !me->CanSchedule());
118 return FALSE; // Don't run again.
119}
120
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800121void UpdateCheckScheduler::ComputeNextIntervalAndFuzz(const int forced_interval,
122 int* next_interval,
Darin Petkov1023a602010-08-30 13:47:51 -0700123 int* next_fuzz) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800124 CHECK(next_interval && next_fuzz);
Darin Petkov85ced132010-09-01 10:20:56 -0700125
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800126 int interval = forced_interval;
127 int fuzz = 0; // Use default fuzz value (see below)
128
129 if (interval == 0) {
130 int http_response_code;
131 if (poll_interval_ > 0) {
132 // Server-dictated poll interval.
133 interval = poll_interval_;
134 LOG(WARNING) << "Using server-dictated poll interval: " << interval;
135 } else if ((http_response_code = update_attempter_->http_response_code()) ==
136 kHttpResponseInternalServerError ||
137 http_response_code == kHttpResponseServiceUnavailable) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800138 // Implements exponential backoff on 500 (Internal Server Error) and 503
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800139 // (Service Unavailable) HTTP response codes.
140 interval = 2 * last_interval_;
Jay Srinivasan08262882012-12-28 19:29:43 -0800141 LOG(WARNING) << "Exponential backoff due to HTTP response code ("
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800142 << http_response_code << ")";
143 }
144
145 // Backoff cannot exceed a predetermined maximum period.
146 if (interval > kTimeoutMaxBackoffInterval)
147 interval = kTimeoutMaxBackoffInterval;
148
149 // Ensures that under normal conditions the regular update check interval
Jay Srinivasan08262882012-12-28 19:29:43 -0800150 // and fuzz are used. Also covers the case where backoff is required based
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800151 // on the initial update check.
152 if (interval < kTimeoutPeriodicInterval) {
153 interval = kTimeoutPeriodicInterval;
154 fuzz = kTimeoutRegularFuzz;
155 }
Darin Petkov1023a602010-08-30 13:47:51 -0700156 }
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800157
158 // Set default fuzz to +/- |interval|/2.
159 if (fuzz == 0)
160 fuzz = interval;
161
Darin Petkov1023a602010-08-30 13:47:51 -0700162 *next_interval = interval;
163 *next_fuzz = fuzz;
164}
165
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800166void UpdateCheckScheduler::ScheduleNextCheck(bool is_force_quick) {
Darin Petkov1023a602010-08-30 13:47:51 -0700167 int interval, fuzz;
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800168 ComputeNextIntervalAndFuzz(is_force_quick ? kTimeoutQuickInterval : 0,
169 &interval, &fuzz);
Darin Petkov1023a602010-08-30 13:47:51 -0700170 ScheduleCheck(interval, fuzz);
171}
172
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800173void UpdateCheckScheduler::SetUpdateStatus(UpdateStatus status,
174 UpdateNotice notice) {
Thieu Le116fda32011-04-19 11:01:54 -0700175 // We want to schedule the update checks for when we're idle as well as
176 // after we've successfully applied an update and waiting for the user
177 // to reboot to ensure our active count is accurate.
178 if (status == UPDATE_STATUS_IDLE ||
179 status == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800180 ScheduleNextCheck(notice == kUpdateNoticeTestAddrFailed);
Darin Petkov1023a602010-08-30 13:47:51 -0700181 }
182}
183
184} // namespace chromeos_update_engine