blob: 6bd3d9ff8aebbb6403f864d64dc19a8ef0608697 [file] [log] [blame]
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -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
Darin Petkov5a7f5652010-07-22 21:40:09 -07005#include <string>
6
Ben Chan46bf5c82013-06-24 11:17:41 -07007#include <dbus/dbus.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07008#include <gflags/gflags.h>
9#include <glib.h>
10
11#include "update_engine/dbus_constants.h"
12#include "update_engine/subprocess.h"
13#include "update_engine/utils.h"
14
15extern "C" {
16#include "update_engine/update_engine.dbusclient.h"
17}
18
19using chromeos_update_engine::kUpdateEngineServiceName;
20using chromeos_update_engine::kUpdateEngineServicePath;
21using chromeos_update_engine::kUpdateEngineServiceInterface;
David Zeuthen75a4c3e2013-09-06 11:36:59 -070022using chromeos_update_engine::AttemptUpdateFlags;
23using chromeos_update_engine::kAttemptUpdateFlagNonInteractive;
Darin Petkova0b9e772011-10-06 05:05:56 -070024using chromeos_update_engine::utils::GetAndFreeGError;
Darin Petkov5a7f5652010-07-22 21:40:09 -070025using std::string;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026
Darin Petkov296889c2010-07-23 16:20:54 -070027DEFINE_string(app_version, "", "Force the current app version.");
Jay Srinivasanae4697c2013-03-18 17:08:08 -070028DEFINE_string(channel, "",
Chris Sosa192449e2013-10-28 14:16:19 -070029 "Set the target channel. The device will be powerwashed if the "
30 "target channel is more stable than the current channel unless "
31 "--nopowerwash is specified.");
Chris Sosad317e402013-06-12 13:47:09 -070032DEFINE_bool(check_for_update, false, "Initiate check for updates.");
Chris Sosa192449e2013-10-28 14:16:19 -070033DEFINE_bool(follow, false, "Wait for any update operations to complete."
34 "Exit status is 0 if the update succeeded, and 1 otherwise.");
35DEFINE_bool(interactive, true, "Mark the update request as interactive.");
Chris Sosad317e402013-06-12 13:47:09 -070036DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
Chris Sosa192449e2013-10-28 14:16:19 -070037DEFINE_string(p2p_update, "",
38 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update "
39 "sharing.");
40DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
41 "do a powerwash or allow it respectively.");
Chris Sosad317e402013-06-12 13:47:09 -070042DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
43DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
44DEFINE_bool(rollback, false, "Perform a rollback to the previous partition.");
Alex Vakulenko59e253e2014-02-24 10:40:21 -080045DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
46 "is available.");
Chris Sosad317e402013-06-12 13:47:09 -070047DEFINE_bool(show_channel, false, "Show the current and target channels.");
Chris Sosa192449e2013-10-28 14:16:19 -070048DEFINE_bool(show_p2p_update, false,
49 "Show the current setting for peer-to-peer update sharing.");
Alex Deymof4867c42013-06-28 14:41:39 -070050DEFINE_bool(show_update_over_cellular, false,
51 "Show the current setting for updates over cellular networks.");
Chris Sosa192449e2013-10-28 14:16:19 -070052DEFINE_bool(status, false, "Print the status to stdout.");
53DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
54 "Implies --follow.");
Alex Deymof4867c42013-06-28 14:41:39 -070055DEFINE_string(update_over_cellular, "",
56 "Enables (\"yes\") or disables (\"no\") the updates over "
57 "cellular networks.");
Chris Sosa192449e2013-10-28 14:16:19 -070058DEFINE_bool(watch_for_updates, false,
59 "Listen for status updates and print them to the screen.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070060
61namespace {
62
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070063bool GetProxy(DBusGProxy** out_proxy) {
64 DBusGConnection* bus;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070065 DBusGProxy* proxy = NULL;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070066 GError* error = NULL;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070067 const int kTries = 4;
Darin Petkova0b9e772011-10-06 05:05:56 -070068 const int kRetrySeconds = 10;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070069
70 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Richard Barnetted7936062013-01-18 13:38:51 -080071 if (bus == NULL) {
72 LOG(ERROR) << "Failed to get bus: " << GetAndFreeGError(&error);
73 exit(1);
74 }
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070075 for (int i = 0; !proxy && i < kTries; ++i) {
Darin Petkova0b9e772011-10-06 05:05:56 -070076 if (i > 0) {
77 LOG(INFO) << "Retrying to get dbus proxy. Try "
78 << (i + 1) << "/" << kTries;
Gilad Arnold8e3f1262013-01-08 14:59:54 -080079 g_usleep(kRetrySeconds * G_USEC_PER_SEC);
Darin Petkova0b9e772011-10-06 05:05:56 -070080 }
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070081 proxy = dbus_g_proxy_new_for_name_owner(bus,
82 kUpdateEngineServiceName,
83 kUpdateEngineServicePath,
84 kUpdateEngineServiceInterface,
85 &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070086 LOG_IF(WARNING, !proxy) << "Error getting dbus proxy for "
87 << kUpdateEngineServiceName << ": "
88 << GetAndFreeGError(&error);
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070089 }
Richard Barnetted7936062013-01-18 13:38:51 -080090 if (proxy == NULL) {
91 LOG(ERROR) << "Giving up -- unable to get dbus proxy for "
92 << kUpdateEngineServiceName;
93 exit(1);
94 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070095 *out_proxy = proxy;
96 return true;
97}
98
99static void StatusUpdateSignalHandler(DBusGProxy* proxy,
100 int64_t last_checked_time,
101 double progress,
102 gchar* current_operation,
103 gchar* new_version,
104 int64_t new_size,
105 void* user_data) {
106 LOG(INFO) << "Got status update:";
107 LOG(INFO) << " last_checked_time: " << last_checked_time;
108 LOG(INFO) << " progress: " << progress;
109 LOG(INFO) << " current_operation: " << current_operation;
110 LOG(INFO) << " new_version: " << new_version;
111 LOG(INFO) << " new_size: " << new_size;
112}
113
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700114bool ResetStatus() {
115 DBusGProxy* proxy;
116 GError* error = NULL;
117
118 CHECK(GetProxy(&proxy));
119
Alex Deymo36dc2f32013-08-29 15:45:06 -0700120 gboolean rc = update_engine_client_reset_status(proxy, &error);
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700121 return rc;
122}
123
124
Darin Petkov58529db2010-08-13 09:19:47 -0700125// If |op| is non-NULL, sets it to the current operation string or an
126// empty string if unable to obtain the current status.
127bool GetStatus(string* op) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700128 DBusGProxy* proxy;
129 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700130
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700131 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700132
133 gint64 last_checked_time = 0;
134 gdouble progress = 0.0;
135 char* current_op = NULL;
136 char* new_version = NULL;
137 gint64 new_size = 0;
138
Alex Deymo36dc2f32013-08-29 15:45:06 -0700139 gboolean rc = update_engine_client_get_status(proxy,
140 &last_checked_time,
141 &progress,
142 &current_op,
143 &new_version,
144 &new_size,
145 &error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700146 if (rc == FALSE) {
Darin Petkova0b9e772011-10-06 05:05:56 -0700147 LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700148 }
149 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
150 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
151 last_checked_time,
152 progress,
153 current_op,
154 new_version,
155 new_size);
Darin Petkov58529db2010-08-13 09:19:47 -0700156 if (op) {
157 *op = current_op ? current_op : "";
158 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700159 return true;
160}
161
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700162// Should never return.
163void WatchForUpdates() {
164 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700165
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700166 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700167
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700168 // Register marshaller
169 dbus_g_object_register_marshaller(
Alex Deymo3d41c4d2014-02-13 23:26:33 -0800170 NULL,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700171 G_TYPE_NONE,
172 G_TYPE_INT64,
173 G_TYPE_DOUBLE,
174 G_TYPE_STRING,
175 G_TYPE_STRING,
176 G_TYPE_INT64,
177 G_TYPE_INVALID);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700178
179 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700180 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700181 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700182 G_TYPE_INT64,
183 G_TYPE_DOUBLE,
184 G_TYPE_STRING,
185 G_TYPE_STRING,
186 G_TYPE_INT64,
187 G_TYPE_INVALID);
188 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
189 dbus_g_proxy_connect_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700190 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700191 G_CALLBACK(StatusUpdateSignalHandler),
192 NULL,
193 NULL);
194 g_main_loop_run(loop);
195 g_main_loop_unref(loop);
196}
197
Chris Sosad317e402013-06-12 13:47:09 -0700198bool Rollback(bool rollback) {
199 DBusGProxy* proxy;
200 GError* error = NULL;
201
202 CHECK(GetProxy(&proxy));
203
Alex Deymo36dc2f32013-08-29 15:45:06 -0700204 gboolean rc = update_engine_client_attempt_rollback(proxy,
205 rollback,
206 &error);
Chris Sosad317e402013-06-12 13:47:09 -0700207 CHECK_EQ(rc, TRUE) << "Error with rollback request: "
208 << GetAndFreeGError(&error);
209 return true;
210}
211
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800212bool CanRollback() {
213 DBusGProxy* proxy;
214 GError* error = NULL;
215
216 CHECK(GetProxy(&proxy));
217
218 gboolean can_rollback = FALSE;
219 gboolean rc = update_engine_client_can_rollback(proxy,
220 &can_rollback,
221 &error);
222 CHECK_EQ(rc, TRUE) << "Error while querying rollback partition availabilty: "
223 << GetAndFreeGError(&error);
224 return can_rollback;
225}
Darin Petkov58529db2010-08-13 09:19:47 -0700226bool CheckForUpdates(const string& app_version, const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700227 DBusGProxy* proxy;
228 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700229
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700230 CHECK(GetProxy(&proxy));
231
David Zeuthen75a4c3e2013-09-06 11:36:59 -0700232 AttemptUpdateFlags flags = static_cast<AttemptUpdateFlags>(
233 FLAGS_interactive ? 0 : kAttemptUpdateFlagNonInteractive);
234 gboolean rc =
235 update_engine_client_attempt_update_with_flags(proxy,
236 app_version.c_str(),
237 omaha_url.c_str(),
238 static_cast<gint>(flags),
239 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700240 CHECK_EQ(rc, TRUE) << "Error checking for update: "
Darin Petkova0b9e772011-10-06 05:05:56 -0700241 << GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700242 return true;
243}
244
Darin Petkov296889c2010-07-23 16:20:54 -0700245bool RebootIfNeeded() {
246 DBusGProxy* proxy;
247 GError* error = NULL;
248
249 CHECK(GetProxy(&proxy));
250
251 gboolean rc =
Alex Deymo36dc2f32013-08-29 15:45:06 -0700252 update_engine_client_reboot_if_needed(proxy, &error);
Darin Petkov296889c2010-07-23 16:20:54 -0700253 // Reboot error code doesn't necessarily mean that a reboot
254 // failed. For example, D-Bus may be shutdown before we receive the
255 // result.
Darin Petkova0b9e772011-10-06 05:05:56 -0700256 LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error);
Darin Petkov296889c2010-07-23 16:20:54 -0700257 return true;
258}
259
Chris Sosacb7fa882013-07-25 17:02:59 -0700260void SetTargetChannel(const string& target_channel, bool allow_powerwash) {
Darin Petkov8daa3242010-10-25 13:28:47 -0700261 DBusGProxy* proxy;
262 GError* error = NULL;
263
264 CHECK(GetProxy(&proxy));
265
Alex Deymo36dc2f32013-08-29 15:45:06 -0700266 gboolean rc = update_engine_client_set_channel(proxy,
267 target_channel.c_str(),
268 allow_powerwash,
269 &error);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700270 CHECK_EQ(rc, true) << "Error setting the channel: "
Darin Petkova0b9e772011-10-06 05:05:56 -0700271 << GetAndFreeGError(&error);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700272 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700273}
274
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700275string GetChannel(bool get_current_channel) {
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900276 DBusGProxy* proxy;
277 GError* error = NULL;
278
279 CHECK(GetProxy(&proxy));
280
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700281 char* channel = NULL;
Alex Deymo36dc2f32013-08-29 15:45:06 -0700282 gboolean rc = update_engine_client_get_channel(proxy,
283 get_current_channel,
284 &channel,
285 &error);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700286 CHECK_EQ(rc, true) << "Error getting the channel: "
287 << GetAndFreeGError(&error);
288 string output = channel;
289 g_free(channel);
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900290 return output;
291}
292
Alex Deymo5fdf7762013-07-17 20:01:40 -0700293void SetUpdateOverCellularPermission(gboolean allowed) {
Alex Deymof4867c42013-06-28 14:41:39 -0700294 DBusGProxy* proxy;
295 GError* error = NULL;
296
297 CHECK(GetProxy(&proxy));
298
Alex Deymo36dc2f32013-08-29 15:45:06 -0700299 gboolean rc = update_engine_client_set_update_over_cellular_permission(
300 proxy,
301 allowed,
302 &error);
Alex Deymof4867c42013-06-28 14:41:39 -0700303 CHECK_EQ(rc, true) << "Error setting the update over cellular setting: "
304 << GetAndFreeGError(&error);
Alex Deymof4867c42013-06-28 14:41:39 -0700305}
306
307bool GetUpdateOverCellularPermission() {
308 DBusGProxy* proxy;
309 GError* error = NULL;
310
311 CHECK(GetProxy(&proxy));
312
313 gboolean allowed;
Alex Deymo36dc2f32013-08-29 15:45:06 -0700314 gboolean rc = update_engine_client_get_update_over_cellular_permission(
315 proxy,
316 &allowed,
317 &error);
Alex Deymof4867c42013-06-28 14:41:39 -0700318 CHECK_EQ(rc, true) << "Error getting the update over cellular setting: "
319 << GetAndFreeGError(&error);
320 return allowed;
321}
322
Alex Deymo5fdf7762013-07-17 20:01:40 -0700323void SetP2PUpdatePermission(gboolean enabled) {
324 DBusGProxy* proxy;
325 GError* error = NULL;
326
327 CHECK(GetProxy(&proxy));
328
Alex Deymo36dc2f32013-08-29 15:45:06 -0700329 gboolean rc = update_engine_client_set_p2p_update_permission(
330 proxy,
331 enabled,
332 &error);
Alex Deymo5fdf7762013-07-17 20:01:40 -0700333 CHECK_EQ(rc, true) << "Error setting the peer-to-peer update setting: "
334 << GetAndFreeGError(&error);
335}
336
337bool GetP2PUpdatePermission() {
338 DBusGProxy* proxy;
339 GError* error = NULL;
340
341 CHECK(GetProxy(&proxy));
342
343 gboolean enabled;
Alex Deymo36dc2f32013-08-29 15:45:06 -0700344 gboolean rc = update_engine_client_get_p2p_update_permission(
345 proxy,
346 &enabled,
347 &error);
Alex Deymo5fdf7762013-07-17 20:01:40 -0700348 CHECK_EQ(rc, true) << "Error getting the peer-to-peer update setting: "
349 << GetAndFreeGError(&error);
350 return enabled;
351}
352
Darin Petkov58529db2010-08-13 09:19:47 -0700353static gboolean CompleteUpdateSource(gpointer data) {
354 string current_op;
355 if (!GetStatus(&current_op) || current_op == "UPDATE_STATUS_IDLE") {
356 LOG(ERROR) << "Update failed.";
357 exit(1);
358 }
359 if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") {
360 LOG(INFO) << "Update succeeded -- reboot needed.";
361 exit(0);
362 }
363 return TRUE;
364}
365
366// This is similar to watching for updates but rather than registering
367// a signal watch, activelly poll the daemon just in case it stops
368// sending notifications.
369void CompleteUpdate() {
370 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
371 g_timeout_add_seconds(5, CompleteUpdateSource, NULL);
372 g_main_loop_run(loop);
373 g_main_loop_unref(loop);
374}
375
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700376} // namespace {}
377
378int main(int argc, char** argv) {
379 // Boilerplate init commands.
380 g_type_init();
Ben Chan46bf5c82013-06-24 11:17:41 -0700381 dbus_threads_init_default();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700382 chromeos_update_engine::Subprocess::Init();
383 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700384
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700385 // Update the status if requested.
386 if (FLAGS_reset_status) {
387 LOG(INFO) << "Setting Update Engine status to idle ...";
388 if (!ResetStatus()) {
389 LOG(ERROR) << "ResetStatus failed.";
390 return 1;
391 }
Gilad Arnold50c60632013-01-25 10:27:19 -0800392 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n"
393 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} "
394 "| sed 's/^[^0-9]*//')-1)) $D;)";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700395 }
Darin Petkov58529db2010-08-13 09:19:47 -0700396
Alex Deymof4867c42013-06-28 14:41:39 -0700397 // Changes the current update over cellular network setting.
398 if (!FLAGS_update_over_cellular.empty()) {
399 gboolean allowed = FLAGS_update_over_cellular == "yes";
400 if (!allowed && FLAGS_update_over_cellular != "no") {
401 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
402 << "\". Please specify \"yes\" or \"no\".";
403 } else {
404 SetUpdateOverCellularPermission(allowed);
405 }
406 }
407
408 // Show the current update over cellular network setting.
409 if (FLAGS_show_update_over_cellular) {
410 bool allowed = GetUpdateOverCellularPermission();
411 LOG(INFO) << "Current update over cellular network setting: "
412 << (allowed ? "ENABLED" : "DISABLED");
413 }
414
Chris Sosacb7fa882013-07-25 17:02:59 -0700415 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700416 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700417 return 1;
418 }
419
Alex Deymo5fdf7762013-07-17 20:01:40 -0700420 // Change the P2P enabled setting.
421 if (!FLAGS_p2p_update.empty()) {
422 gboolean enabled = FLAGS_p2p_update == "yes";
423 if (!enabled && FLAGS_p2p_update != "no") {
424 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
425 << "\". Please specify \"yes\" or \"no\".";
426 } else {
427 SetP2PUpdatePermission(enabled);
428 }
429 }
430
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800431 // Show the rollback availability.
432 if (FLAGS_can_rollback) {
433 bool can_rollback = CanRollback();
434 LOG(INFO) << "Rollback partition: "
435 << (can_rollback ? "AVAILABLE" : "UNAVAILABLE");
436 }
437
Alex Deymo5fdf7762013-07-17 20:01:40 -0700438 // Show the current P2P enabled setting.
439 if (FLAGS_show_p2p_update) {
440 bool enabled = GetP2PUpdatePermission();
441 LOG(INFO) << "Current update using P2P setting: "
442 << (enabled ? "ENABLED" : "DISABLED");
443 }
444
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700445 // First, update the target channel if requested.
446 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700447 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700448
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700449 // Show the current and target channels if requested.
450 if (FLAGS_show_channel) {
451 string current_channel = GetChannel(true);
452 LOG(INFO) << "Current Channel: " << current_channel;
453
454 string target_channel = GetChannel(false);
455 if (!target_channel.empty())
456 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900457 }
458
Chris Sosad317e402013-06-12 13:47:09 -0700459 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
460 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700461 if (FLAGS_update)
462 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700463
Chris Sosad317e402013-06-12 13:47:09 -0700464 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700465 LOG(ERROR) << "Incompatible flags specified with rollback."
466 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700467 return 1;
468 }
469
470 if(FLAGS_rollback) {
471 LOG(INFO) << "Requesting rollback.";
472 CHECK(Rollback(FLAGS_powerwash)) << "Request for rollback failed.";
Chris Sosad317e402013-06-12 13:47:09 -0700473 }
474
Darin Petkov58529db2010-08-13 09:19:47 -0700475 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700476 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700477 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700478 string app_version = FLAGS_app_version;
479 if (FLAGS_update && app_version.empty()) {
480 app_version = "ForcedUpdate";
481 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700482 }
Darin Petkov58529db2010-08-13 09:19:47 -0700483 LOG(INFO) << "Initiating update check and install.";
484 CHECK(CheckForUpdates(app_version, FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700485 << "Update check/initiate update failed.";
Chris Sosa192449e2013-10-28 14:16:19 -0700486 }
Darin Petkov58529db2010-08-13 09:19:47 -0700487
Chris Sosa192449e2013-10-28 14:16:19 -0700488 // These final options are all mutually exclusive with one another.
489 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot + FLAGS_status > 1)
490 {
491 LOG(ERROR) << "Multiple exclusive options selected. "
492 << "Select only one of --follow, --watch_for_updates, --reboot, "
493 << "or --status.";
494 return 1;
495 }
496
497 if (FLAGS_status) {
498 LOG(INFO) << "Querying Update Engine status...";
499 if (!GetStatus(NULL)) {
500 LOG(ERROR) << "GetStatus failed.";
Darin Petkov58529db2010-08-13 09:19:47 -0700501 return 1;
502 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700503 return 0;
504 }
Darin Petkov58529db2010-08-13 09:19:47 -0700505
Chris Sosa192449e2013-10-28 14:16:19 -0700506 if (FLAGS_follow) {
507 LOG(INFO) << "Waiting for update to complete.";
508 CompleteUpdate(); // Should never return.
509 return 1;
510 }
511
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700512 if (FLAGS_watch_for_updates) {
513 LOG(INFO) << "Watching for status updates.";
514 WatchForUpdates(); // Should never return.
515 return 1;
516 }
Darin Petkov58529db2010-08-13 09:19:47 -0700517
Darin Petkov296889c2010-07-23 16:20:54 -0700518 if (FLAGS_reboot) {
519 LOG(INFO) << "Requesting a reboot...";
520 CHECK(RebootIfNeeded());
521 return 0;
522 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700523
Darin Petkov8daa3242010-10-25 13:28:47 -0700524 LOG(INFO) << "Done.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700525 return 0;
526}