blob: 42bcfa354155bcc53628ca146789dcf398d65a99 [file] [log] [blame]
Jay Srinivasane73acab2012-07-10 14:34:03 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -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/dbus_service.h"
Darin Petkova07586b2010-10-20 13:41:15 -07006
Alex Deymof4867c42013-06-28 14:41:39 -07007#include <set>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07008#include <string>
Darin Petkova07586b2010-10-20 13:41:15 -07009
10#include <base/logging.h>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070011#include <policy/device_policy.h>
Darin Petkova07586b2010-10-20 13:41:15 -070012
Alex Deymof4867c42013-06-28 14:41:39 -070013#include "update_engine/connection_manager.h"
David Zeuthen75a4c3e2013-09-06 11:36:59 -070014#include "update_engine/dbus_constants.h"
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070015#include "update_engine/hardware_interface.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070016#include "update_engine/marshal.glibmarshal.h"
Darin Petkov49d91322010-10-25 16:34:58 -070017#include "update_engine/omaha_request_params.h"
Alex Deymo5fdf7762013-07-17 20:01:40 -070018#include "update_engine/p2p_manager.h"
Alex Deymof4867c42013-06-28 14:41:39 -070019#include "update_engine/prefs.h"
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070020#include "update_engine/update_attempter.h"
Darin Petkova07586b2010-10-20 13:41:15 -070021#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070022
Alex Deymof4867c42013-06-28 14:41:39 -070023using std::set;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024using std::string;
David Zeuthen75a4c3e2013-09-06 11:36:59 -070025using chromeos_update_engine::AttemptUpdateFlags;
26using chromeos_update_engine::kAttemptUpdateFlagNonInteractive;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027
Darin Petkov820a77b2011-04-27 16:48:58 -070028static const char kAUTestURLRequest[] = "autest";
Jay Srinivasane73acab2012-07-10 14:34:03 -070029// By default autest bypasses scattering. If we want to test scattering,
30// we should use autest-scheduled. The Url used is same in both cases, but
31// different params are passed to CheckForUpdate method.
32static const char kScheduledAUTestURLRequest[] = "autest-scheduled";
33
Darin Petkov820a77b2011-04-27 16:48:58 -070034static const char kAUTestURL[] =
Darin Petkov5445e162011-11-04 10:10:27 +010035 "https://omaha.sandbox.google.com/service/update2";
Darin Petkov820a77b2011-04-27 16:48:58 -070036
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070037G_DEFINE_TYPE(UpdateEngineService, update_engine_service, G_TYPE_OBJECT)
38
39static void update_engine_service_finalize(GObject* object) {
40 G_OBJECT_CLASS(update_engine_service_parent_class)->finalize(object);
41}
42
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070043static guint status_update_signal = 0;
44
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070045static void update_engine_service_class_init(UpdateEngineServiceClass* klass) {
46 GObjectClass *object_class;
47 object_class = G_OBJECT_CLASS(klass);
48 object_class->finalize = update_engine_service_finalize;
Darin Petkov5a7f5652010-07-22 21:40:09 -070049
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070050 status_update_signal = g_signal_new(
51 "status_update",
52 G_OBJECT_CLASS_TYPE(klass),
53 G_SIGNAL_RUN_LAST,
54 0, // 0 == no class method associated
55 NULL, // Accumulator
56 NULL, // Accumulator data
57 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
58 G_TYPE_NONE, // Return type
59 5, // param count:
60 G_TYPE_INT64,
61 G_TYPE_DOUBLE,
62 G_TYPE_STRING,
63 G_TYPE_STRING,
64 G_TYPE_INT64);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070065}
66
67static void update_engine_service_init(UpdateEngineService* object) {
68}
69
70UpdateEngineService* update_engine_service_new(void) {
71 return reinterpret_cast<UpdateEngineService*>(
72 g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
73}
74
Darin Petkov296889c2010-07-23 16:20:54 -070075gboolean update_engine_service_attempt_update(UpdateEngineService* self,
76 gchar* app_version,
77 gchar* omaha_url,
78 GError **error) {
David Zeuthen75a4c3e2013-09-06 11:36:59 -070079 return update_engine_service_attempt_update_with_flags(self,
80 app_version,
81 omaha_url,
82 0, // No flags set.
83 error);
84}
85
86gboolean update_engine_service_attempt_update_with_flags(
87 UpdateEngineService* self,
88 gchar* app_version,
89 gchar* omaha_url,
90 gint flags_as_int,
91 GError **error) {
Darin Petkova07586b2010-10-20 13:41:15 -070092 string update_app_version;
93 string update_omaha_url;
David Zeuthen75a4c3e2013-09-06 11:36:59 -070094 AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>(flags_as_int);
Gilad Arnoldb92f0df2013-01-10 16:32:45 -080095 bool interactive = true;
Jay Srinivasane73acab2012-07-10 14:34:03 -070096
Darin Petkova07586b2010-10-20 13:41:15 -070097 // Only non-official (e.g., dev and test) builds can override the current
Darin Petkov820a77b2011-04-27 16:48:58 -070098 // version and update server URL over D-Bus. However, pointing to the
99 // hardcoded test update server URL is always allowed.
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700100 if (!self->system_state_->hardware()->IsOfficialBuild()) {
Darin Petkova07586b2010-10-20 13:41:15 -0700101 if (app_version) {
102 update_app_version = app_version;
103 }
104 if (omaha_url) {
105 update_omaha_url = omaha_url;
106 }
107 }
Jay Srinivasane73acab2012-07-10 14:34:03 -0700108 if (omaha_url) {
109 if (strcmp(omaha_url, kScheduledAUTestURLRequest) == 0) {
110 update_omaha_url = kAUTestURL;
111 // pretend that it's not user-initiated even though it is,
112 // so as to test scattering logic, etc. which get kicked off
113 // only in scheduled update checks.
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800114 interactive = false;
Jay Srinivasane73acab2012-07-10 14:34:03 -0700115 } else if (strcmp(omaha_url, kAUTestURLRequest) == 0) {
116 update_omaha_url = kAUTestURL;
117 }
Darin Petkov820a77b2011-04-27 16:48:58 -0700118 }
David Zeuthen75a4c3e2013-09-06 11:36:59 -0700119 if (flags & kAttemptUpdateFlagNonInteractive)
120 interactive = false;
Darin Petkov296889c2010-07-23 16:20:54 -0700121 LOG(INFO) << "Attempt update: app_version=\"" << update_app_version << "\" "
Jay Srinivasane73acab2012-07-10 14:34:03 -0700122 << "omaha_url=\"" << update_omaha_url << "\" "
David Zeuthen75a4c3e2013-09-06 11:36:59 -0700123 << "flags=0x" << std::hex << flags << " "
Gilad Arnoldb92f0df2013-01-10 16:32:45 -0800124 << "interactive=" << (interactive? "yes" : "no");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700125 self->system_state_->update_attempter()->CheckForUpdate(update_app_version,
126 update_omaha_url,
127 interactive);
Darin Petkov296889c2010-07-23 16:20:54 -0700128 return TRUE;
129}
130
Chris Sosad317e402013-06-12 13:47:09 -0700131gboolean update_engine_service_attempt_rollback(UpdateEngineService* self,
Alex Deymoad923732013-08-29 16:13:49 -0700132 gboolean powerwash,
Chris Sosad317e402013-06-12 13:47:09 -0700133 GError **error) {
134 LOG(INFO) << "Attempting rollback to non-active partitions.";
Chris Sosa76a29ae2013-07-11 17:59:24 -0700135 return self->system_state_->update_attempter()->Rollback(powerwash, NULL);
Chris Sosad317e402013-06-12 13:47:09 -0700136}
137
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700138gboolean update_engine_service_reset_status(UpdateEngineService* self,
139 GError **error) {
140 *error = NULL;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700141 return self->system_state_->update_attempter()->ResetStatus();
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700142}
143
144
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700145gboolean update_engine_service_get_status(UpdateEngineService* self,
146 int64_t* last_checked_time,
147 double* progress,
148 gchar** current_operation,
149 gchar** new_version,
150 int64_t* new_size,
151 GError **error) {
152 string current_op;
153 string new_version_str;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700154
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700155 CHECK(self->system_state_->update_attempter()->GetStatus(last_checked_time,
156 progress,
157 &current_op,
158 &new_version_str,
159 new_size));
Darin Petkov5a7f5652010-07-22 21:40:09 -0700160
Satoru Takabayashid6982312010-11-29 12:54:12 +0900161 *current_operation = g_strdup(current_op.c_str());
162 *new_version = g_strdup(new_version_str.c_str());
Chris Masonec6c57a52010-09-23 13:06:14 -0700163 if (!(*current_operation && *new_version)) {
164 *error = NULL;
165 return FALSE;
166 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700167 return TRUE;
168}
169
Darin Petkov296889c2010-07-23 16:20:54 -0700170gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700171 GError **error) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700172 if (!self->system_state_->update_attempter()->RebootIfNeeded()) {
Darin Petkov296889c2010-07-23 16:20:54 -0700173 *error = NULL;
174 return FALSE;
175 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700176 return TRUE;
177}
178
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700179gboolean update_engine_service_set_channel(UpdateEngineService* self,
180 gchar* target_channel,
Alex Deymoad923732013-08-29 16:13:49 -0700181 gboolean is_powerwash_allowed,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700182 GError **error) {
183 if (!target_channel)
184 return FALSE;
185
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700186 const policy::DevicePolicy* device_policy =
187 self->system_state_->device_policy();
Chris Sosacb7fa882013-07-25 17:02:59 -0700188
189 // The device_policy is loaded in a lazy way before an update check. Load it
190 // now from the libchromeos cache if it wasn't already loaded.
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700191 if (!device_policy) {
Chris Sosacb7fa882013-07-25 17:02:59 -0700192 chromeos_update_engine::UpdateAttempter* update_attempter =
193 self->system_state_->update_attempter();
194 if (update_attempter) {
195 update_attempter->RefreshDevicePolicy();
196 device_policy = self->system_state_->device_policy();
197 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700198 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700199
200 bool delegated = false;
Chris Sosacb7fa882013-07-25 17:02:59 -0700201 if (device_policy &&
202 device_policy->GetReleaseChannelDelegated(&delegated) && !delegated) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700203 LOG(INFO) << "Cannot set target channel explicitly when channel "
204 "policy/settings is not delegated";
205 return FALSE;
206 }
207
208 LOG(INFO) << "Setting destination channel to: " << target_channel;
209 if (!self->system_state_->request_params()->SetTargetChannel(
210 target_channel, is_powerwash_allowed)) {
211 *error = NULL;
212 return FALSE;
213 }
214
215 return TRUE;
216}
217
218gboolean update_engine_service_get_channel(UpdateEngineService* self,
Alex Deymoad923732013-08-29 16:13:49 -0700219 gboolean get_current_channel,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700220 gchar** channel,
221 GError **error) {
222 chromeos_update_engine::OmahaRequestParams* rp =
223 self->system_state_->request_params();
224
225 string channel_str = get_current_channel ?
226 rp->current_channel() : rp->target_channel();
227
228 *channel = g_strdup(channel_str.c_str());
Darin Petkov8daa3242010-10-25 13:28:47 -0700229 return TRUE;
230}
231
Alex Deymo5fdf7762013-07-17 20:01:40 -0700232gboolean update_engine_service_set_p2p_update_permission(
233 UpdateEngineService* self,
234 gboolean enabled,
235 GError **error) {
236 chromeos_update_engine::PrefsInterface* prefs = self->system_state_->prefs();
237 chromeos_update_engine::P2PManager* p2p_manager =
238 self->system_state_->p2p_manager();
239
240 bool p2p_was_enabled = p2p_manager && p2p_manager->IsP2PEnabled();
241
242 if (!prefs->SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, enabled)) {
243 LOG(ERROR) << "Error setting the update over cellular to "
244 << (enabled ? "true" : "false");
245 *error = NULL;
246 return FALSE;
247 }
248
249 // If P2P is being effectively disabled (IsP2PEnabled() reports the change)
250 // then we need to shutdown the service.
251 if (p2p_was_enabled && !p2p_manager->IsP2PEnabled())
252 p2p_manager->EnsureP2PNotRunning();
253
254 return TRUE;
255}
256
257gboolean update_engine_service_get_p2p_update_permission(
258 UpdateEngineService* self,
259 gboolean* enabled,
260 GError **error) {
261 chromeos_update_engine::PrefsInterface* prefs = self->system_state_->prefs();
262
263 // The default for not present setting is false.
264 if (!prefs->Exists(chromeos_update_engine::kPrefsP2PEnabled)) {
265 *enabled = false;
266 return TRUE;
267 }
268
269 bool p2p_pref = false;
270 if (!prefs->GetBoolean(chromeos_update_engine::kPrefsP2PEnabled, &p2p_pref)) {
271 LOG(ERROR) << "Error getting the P2PEnabled setting.";
272 *error = NULL;
273 return FALSE;
274 }
275
276 *enabled = p2p_pref;
277 return TRUE;
278}
279
Alex Deymof4867c42013-06-28 14:41:39 -0700280gboolean update_engine_service_set_update_over_cellular_permission(
281 UpdateEngineService* self,
Alex Deymoad923732013-08-29 16:13:49 -0700282 gboolean allowed,
Alex Deymof4867c42013-06-28 14:41:39 -0700283 GError **error) {
284 set<string> allowed_types;
285 const policy::DevicePolicy* device_policy =
286 self->system_state_->device_policy();
287
288 // The device_policy is loaded in a lazy way before an update check. Load it
289 // now from the libchromeos cache if it wasn't already loaded.
290 if (!device_policy) {
291 chromeos_update_engine::UpdateAttempter* update_attempter =
292 self->system_state_->update_attempter();
293 if (update_attempter) {
294 update_attempter->RefreshDevicePolicy();
Chris Sosacb7fa882013-07-25 17:02:59 -0700295 device_policy = self->system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -0700296 }
297 }
298
299 // Check if this setting is allowed by the device policy.
300 if (device_policy &&
301 device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
302 LOG(INFO) << "Ignoring the update over cellular setting since there's "
303 "a device policy enforcing this setting.";
304 *error = NULL;
305 return FALSE;
306 }
307
308 // If the policy wasn't loaded yet, then it is still OK to change the local
309 // setting because the policy will be checked again during the update check.
310
311 chromeos_update_engine::PrefsInterface* prefs = self->system_state_->prefs();
312
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700313 if (!prefs->SetBoolean(
Alex Deymof4867c42013-06-28 14:41:39 -0700314 chromeos_update_engine::kPrefsUpdateOverCellularPermission,
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700315 allowed)) {
Alex Deymof4867c42013-06-28 14:41:39 -0700316 LOG(ERROR) << "Error setting the update over cellular to "
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700317 << (allowed ? "true" : "false");
Alex Deymof4867c42013-06-28 14:41:39 -0700318 *error = NULL;
319 return FALSE;
320 }
321
322 return TRUE;
323}
324
325gboolean update_engine_service_get_update_over_cellular_permission(
326 UpdateEngineService* self,
Alex Deymoad923732013-08-29 16:13:49 -0700327 gboolean* allowed,
Alex Deymof4867c42013-06-28 14:41:39 -0700328 GError **/*error*/) {
329 chromeos_update_engine::ConnectionManager* cm =
330 self->system_state_->connection_manager();
331
332 // The device_policy is loaded in a lazy way before an update check and is
333 // used to determine if an update is allowed over cellular. Load the device
334 // policy now from the libchromeos cache if it wasn't already loaded.
335 if (!self->system_state_->device_policy()) {
336 chromeos_update_engine::UpdateAttempter* update_attempter =
337 self->system_state_->update_attempter();
338 if (update_attempter)
339 update_attempter->RefreshDevicePolicy();
340 }
341
342 // Return the current setting based on the same logic used while checking for
343 // updates. A log message could be printed as the result of this test.
344 LOG(INFO) << "Checking if updates over cellular networks are allowed:";
345 *allowed = cm->IsUpdateAllowedOver(chromeos_update_engine::kNetCellular);
346
347 return TRUE;
348}
349
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700350gboolean update_engine_service_emit_status_update(
351 UpdateEngineService* self,
352 gint64 last_checked_time,
353 gdouble progress,
354 const gchar* current_operation,
355 const gchar* new_version,
356 gint64 new_size) {
357 g_signal_emit(self,
358 status_update_signal,
359 0,
360 last_checked_time,
361 progress,
362 current_operation,
363 new_version,
364 new_size);
365 return TRUE;
366}