blob: 6e3e4f33bbf5ab37c9b60461470a62d064241275 [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
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070011#include "update_engine/marshal.glibmarshal.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070012#include "update_engine/dbus_constants.h"
13#include "update_engine/subprocess.h"
14#include "update_engine/utils.h"
15
16extern "C" {
17#include "update_engine/update_engine.dbusclient.h"
18}
19
20using chromeos_update_engine::kUpdateEngineServiceName;
21using chromeos_update_engine::kUpdateEngineServicePath;
22using chromeos_update_engine::kUpdateEngineServiceInterface;
Darin Petkova0b9e772011-10-06 05:05:56 -070023using chromeos_update_engine::utils::GetAndFreeGError;
Darin Petkov5a7f5652010-07-22 21:40:09 -070024using std::string;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070025
Darin Petkov296889c2010-07-23 16:20:54 -070026DEFINE_string(app_version, "", "Force the current app version.");
Jay Srinivasanae4697c2013-03-18 17:08:08 -070027DEFINE_string(channel, "",
28 "Set the target channel. The device will be powerwashed if the target "
29 "channel is more stable than the current channel.");
Chris Sosad317e402013-06-12 13:47:09 -070030DEFINE_bool(check_for_update, false, "Initiate check for updates.");
31DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
32DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
33DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
34DEFINE_bool(rollback, false, "Perform a rollback to the previous partition.");
35DEFINE_bool(show_channel, false, "Show the current and target channels.");
Chris Sosacb7fa882013-07-25 17:02:59 -070036DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
37 "do a powerwash or allow it respectively.");
Chris Sosad317e402013-06-12 13:47:09 -070038DEFINE_bool(status, false, "Print the status to stdout.");
Darin Petkov58529db2010-08-13 09:19:47 -070039DEFINE_bool(update, false, "Forces an update and waits for its completion. "
40 "Exit status is 0 if the update succeeded, and 1 otherwise.");
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070041DEFINE_bool(watch_for_updates, false,
42 "Listen for status updates and print them to the screen.");
Alex Deymof4867c42013-06-28 14:41:39 -070043DEFINE_bool(show_update_over_cellular, false,
44 "Show the current setting for updates over cellular networks.");
45DEFINE_string(update_over_cellular, "",
46 "Enables (\"yes\") or disables (\"no\") the updates over "
47 "cellular networks.");
Alex Deymo5fdf7762013-07-17 20:01:40 -070048DEFINE_bool(show_p2p_update, false,
49 "Show the current setting for peer-to-peer update sharing.");
50DEFINE_string(p2p_update, "",
51 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update "
52 "sharing.");
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070053
54namespace {
55
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070056bool GetProxy(DBusGProxy** out_proxy) {
57 DBusGConnection* bus;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070058 DBusGProxy* proxy = NULL;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070059 GError* error = NULL;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070060 const int kTries = 4;
Darin Petkova0b9e772011-10-06 05:05:56 -070061 const int kRetrySeconds = 10;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070062
63 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Richard Barnetted7936062013-01-18 13:38:51 -080064 if (bus == NULL) {
65 LOG(ERROR) << "Failed to get bus: " << GetAndFreeGError(&error);
66 exit(1);
67 }
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070068 for (int i = 0; !proxy && i < kTries; ++i) {
Darin Petkova0b9e772011-10-06 05:05:56 -070069 if (i > 0) {
70 LOG(INFO) << "Retrying to get dbus proxy. Try "
71 << (i + 1) << "/" << kTries;
Gilad Arnold8e3f1262013-01-08 14:59:54 -080072 g_usleep(kRetrySeconds * G_USEC_PER_SEC);
Darin Petkova0b9e772011-10-06 05:05:56 -070073 }
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070074 proxy = dbus_g_proxy_new_for_name_owner(bus,
75 kUpdateEngineServiceName,
76 kUpdateEngineServicePath,
77 kUpdateEngineServiceInterface,
78 &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070079 LOG_IF(WARNING, !proxy) << "Error getting dbus proxy for "
80 << kUpdateEngineServiceName << ": "
81 << GetAndFreeGError(&error);
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070082 }
Richard Barnetted7936062013-01-18 13:38:51 -080083 if (proxy == NULL) {
84 LOG(ERROR) << "Giving up -- unable to get dbus proxy for "
85 << kUpdateEngineServiceName;
86 exit(1);
87 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070088 *out_proxy = proxy;
89 return true;
90}
91
92static void StatusUpdateSignalHandler(DBusGProxy* proxy,
93 int64_t last_checked_time,
94 double progress,
95 gchar* current_operation,
96 gchar* new_version,
97 int64_t new_size,
98 void* user_data) {
99 LOG(INFO) << "Got status update:";
100 LOG(INFO) << " last_checked_time: " << last_checked_time;
101 LOG(INFO) << " progress: " << progress;
102 LOG(INFO) << " current_operation: " << current_operation;
103 LOG(INFO) << " new_version: " << new_version;
104 LOG(INFO) << " new_size: " << new_size;
105}
106
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700107bool ResetStatus() {
108 DBusGProxy* proxy;
109 GError* error = NULL;
110
111 CHECK(GetProxy(&proxy));
112
Alex Deymo36dc2f32013-08-29 15:45:06 -0700113 gboolean rc = update_engine_client_reset_status(proxy, &error);
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700114 return rc;
115}
116
117
Darin Petkov58529db2010-08-13 09:19:47 -0700118// If |op| is non-NULL, sets it to the current operation string or an
119// empty string if unable to obtain the current status.
120bool GetStatus(string* op) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700121 DBusGProxy* proxy;
122 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700123
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700124 CHECK(GetProxy(&proxy));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700125
126 gint64 last_checked_time = 0;
127 gdouble progress = 0.0;
128 char* current_op = NULL;
129 char* new_version = NULL;
130 gint64 new_size = 0;
131
Alex Deymo36dc2f32013-08-29 15:45:06 -0700132 gboolean rc = update_engine_client_get_status(proxy,
133 &last_checked_time,
134 &progress,
135 &current_op,
136 &new_version,
137 &new_size,
138 &error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700139 if (rc == FALSE) {
Darin Petkova0b9e772011-10-06 05:05:56 -0700140 LOG(INFO) << "Error getting status: " << GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700141 }
142 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
143 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
144 last_checked_time,
145 progress,
146 current_op,
147 new_version,
148 new_size);
Darin Petkov58529db2010-08-13 09:19:47 -0700149 if (op) {
150 *op = current_op ? current_op : "";
151 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700152 return true;
153}
154
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700155// Should never return.
156void WatchForUpdates() {
157 DBusGProxy* proxy;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700158
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700159 CHECK(GetProxy(&proxy));
Andrew de los Reyesada42202010-07-15 22:23:20 -0700160
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700161 // Register marshaller
162 dbus_g_object_register_marshaller(
163 update_engine_VOID__INT64_DOUBLE_STRING_STRING_INT64,
164 G_TYPE_NONE,
165 G_TYPE_INT64,
166 G_TYPE_DOUBLE,
167 G_TYPE_STRING,
168 G_TYPE_STRING,
169 G_TYPE_INT64,
170 G_TYPE_INVALID);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700171
172 static const char kStatusUpdate[] = "StatusUpdate";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700173 dbus_g_proxy_add_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700174 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700175 G_TYPE_INT64,
176 G_TYPE_DOUBLE,
177 G_TYPE_STRING,
178 G_TYPE_STRING,
179 G_TYPE_INT64,
180 G_TYPE_INVALID);
181 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
182 dbus_g_proxy_connect_signal(proxy,
Andrew de los Reyesada42202010-07-15 22:23:20 -0700183 kStatusUpdate,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700184 G_CALLBACK(StatusUpdateSignalHandler),
185 NULL,
186 NULL);
187 g_main_loop_run(loop);
188 g_main_loop_unref(loop);
189}
190
Chris Sosad317e402013-06-12 13:47:09 -0700191bool Rollback(bool rollback) {
192 DBusGProxy* proxy;
193 GError* error = NULL;
194
195 CHECK(GetProxy(&proxy));
196
Alex Deymo36dc2f32013-08-29 15:45:06 -0700197 gboolean rc = update_engine_client_attempt_rollback(proxy,
198 rollback,
199 &error);
Chris Sosad317e402013-06-12 13:47:09 -0700200 CHECK_EQ(rc, TRUE) << "Error with rollback request: "
201 << GetAndFreeGError(&error);
202 return true;
203}
204
Darin Petkov58529db2010-08-13 09:19:47 -0700205bool CheckForUpdates(const string& app_version, const string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700206 DBusGProxy* proxy;
207 GError* error = NULL;
Andrew de los Reyesada42202010-07-15 22:23:20 -0700208
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700209 CHECK(GetProxy(&proxy));
210
Alex Deymo36dc2f32013-08-29 15:45:06 -0700211 gboolean rc = update_engine_client_attempt_update(proxy,
212 app_version.c_str(),
213 omaha_url.c_str(),
214 &error);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700215 CHECK_EQ(rc, TRUE) << "Error checking for update: "
Darin Petkova0b9e772011-10-06 05:05:56 -0700216 << GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700217 return true;
218}
219
Darin Petkov296889c2010-07-23 16:20:54 -0700220bool RebootIfNeeded() {
221 DBusGProxy* proxy;
222 GError* error = NULL;
223
224 CHECK(GetProxy(&proxy));
225
226 gboolean rc =
Alex Deymo36dc2f32013-08-29 15:45:06 -0700227 update_engine_client_reboot_if_needed(proxy, &error);
Darin Petkov296889c2010-07-23 16:20:54 -0700228 // Reboot error code doesn't necessarily mean that a reboot
229 // failed. For example, D-Bus may be shutdown before we receive the
230 // result.
Darin Petkova0b9e772011-10-06 05:05:56 -0700231 LOG_IF(INFO, !rc) << "Reboot error message: " << GetAndFreeGError(&error);
Darin Petkov296889c2010-07-23 16:20:54 -0700232 return true;
233}
234
Chris Sosacb7fa882013-07-25 17:02:59 -0700235void SetTargetChannel(const string& target_channel, bool allow_powerwash) {
Darin Petkov8daa3242010-10-25 13:28:47 -0700236 DBusGProxy* proxy;
237 GError* error = NULL;
238
239 CHECK(GetProxy(&proxy));
240
Alex Deymo36dc2f32013-08-29 15:45:06 -0700241 gboolean rc = update_engine_client_set_channel(proxy,
242 target_channel.c_str(),
243 allow_powerwash,
244 &error);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700245 CHECK_EQ(rc, true) << "Error setting the channel: "
Darin Petkova0b9e772011-10-06 05:05:56 -0700246 << GetAndFreeGError(&error);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700247 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700248}
249
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700250string GetChannel(bool get_current_channel) {
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900251 DBusGProxy* proxy;
252 GError* error = NULL;
253
254 CHECK(GetProxy(&proxy));
255
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700256 char* channel = NULL;
Alex Deymo36dc2f32013-08-29 15:45:06 -0700257 gboolean rc = update_engine_client_get_channel(proxy,
258 get_current_channel,
259 &channel,
260 &error);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700261 CHECK_EQ(rc, true) << "Error getting the channel: "
262 << GetAndFreeGError(&error);
263 string output = channel;
264 g_free(channel);
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900265 return output;
266}
267
Alex Deymo5fdf7762013-07-17 20:01:40 -0700268void SetUpdateOverCellularPermission(gboolean allowed) {
Alex Deymof4867c42013-06-28 14:41:39 -0700269 DBusGProxy* proxy;
270 GError* error = NULL;
271
272 CHECK(GetProxy(&proxy));
273
Alex Deymo36dc2f32013-08-29 15:45:06 -0700274 gboolean rc = update_engine_client_set_update_over_cellular_permission(
275 proxy,
276 allowed,
277 &error);
Alex Deymof4867c42013-06-28 14:41:39 -0700278 CHECK_EQ(rc, true) << "Error setting the update over cellular setting: "
279 << GetAndFreeGError(&error);
Alex Deymof4867c42013-06-28 14:41:39 -0700280}
281
282bool GetUpdateOverCellularPermission() {
283 DBusGProxy* proxy;
284 GError* error = NULL;
285
286 CHECK(GetProxy(&proxy));
287
288 gboolean allowed;
Alex Deymo36dc2f32013-08-29 15:45:06 -0700289 gboolean rc = update_engine_client_get_update_over_cellular_permission(
290 proxy,
291 &allowed,
292 &error);
Alex Deymof4867c42013-06-28 14:41:39 -0700293 CHECK_EQ(rc, true) << "Error getting the update over cellular setting: "
294 << GetAndFreeGError(&error);
295 return allowed;
296}
297
Alex Deymo5fdf7762013-07-17 20:01:40 -0700298void SetP2PUpdatePermission(gboolean enabled) {
299 DBusGProxy* proxy;
300 GError* error = NULL;
301
302 CHECK(GetProxy(&proxy));
303
Alex Deymo36dc2f32013-08-29 15:45:06 -0700304 gboolean rc = update_engine_client_set_p2p_update_permission(
305 proxy,
306 enabled,
307 &error);
Alex Deymo5fdf7762013-07-17 20:01:40 -0700308 CHECK_EQ(rc, true) << "Error setting the peer-to-peer update setting: "
309 << GetAndFreeGError(&error);
310}
311
312bool GetP2PUpdatePermission() {
313 DBusGProxy* proxy;
314 GError* error = NULL;
315
316 CHECK(GetProxy(&proxy));
317
318 gboolean enabled;
Alex Deymo36dc2f32013-08-29 15:45:06 -0700319 gboolean rc = update_engine_client_get_p2p_update_permission(
320 proxy,
321 &enabled,
322 &error);
Alex Deymo5fdf7762013-07-17 20:01:40 -0700323 CHECK_EQ(rc, true) << "Error getting the peer-to-peer update setting: "
324 << GetAndFreeGError(&error);
325 return enabled;
326}
327
Darin Petkov58529db2010-08-13 09:19:47 -0700328static gboolean CompleteUpdateSource(gpointer data) {
329 string current_op;
330 if (!GetStatus(&current_op) || current_op == "UPDATE_STATUS_IDLE") {
331 LOG(ERROR) << "Update failed.";
332 exit(1);
333 }
334 if (current_op == "UPDATE_STATUS_UPDATED_NEED_REBOOT") {
335 LOG(INFO) << "Update succeeded -- reboot needed.";
336 exit(0);
337 }
338 return TRUE;
339}
340
341// This is similar to watching for updates but rather than registering
342// a signal watch, activelly poll the daemon just in case it stops
343// sending notifications.
344void CompleteUpdate() {
345 GMainLoop* loop = g_main_loop_new (NULL, TRUE);
346 g_timeout_add_seconds(5, CompleteUpdateSource, NULL);
347 g_main_loop_run(loop);
348 g_main_loop_unref(loop);
349}
350
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700351} // namespace {}
352
353int main(int argc, char** argv) {
354 // Boilerplate init commands.
355 g_type_init();
Ben Chan46bf5c82013-06-24 11:17:41 -0700356 dbus_threads_init_default();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700357 chromeos_update_engine::Subprocess::Init();
358 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyesada42202010-07-15 22:23:20 -0700359
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700360 // Update the status if requested.
361 if (FLAGS_reset_status) {
362 LOG(INFO) << "Setting Update Engine status to idle ...";
363 if (!ResetStatus()) {
364 LOG(ERROR) << "ResetStatus failed.";
365 return 1;
366 }
367
Gilad Arnold50c60632013-01-25 10:27:19 -0800368 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n"
369 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} "
370 "| sed 's/^[^0-9]*//')-1)) $D;)";
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700371 return 0;
372 }
373
374
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700375 if (FLAGS_status) {
376 LOG(INFO) << "Querying Update Engine status...";
Darin Petkov58529db2010-08-13 09:19:47 -0700377 if (!GetStatus(NULL)) {
378 LOG(FATAL) << "GetStatus failed.";
379 return 1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700380 }
381 return 0;
382 }
Darin Petkov58529db2010-08-13 09:19:47 -0700383
Alex Deymof4867c42013-06-28 14:41:39 -0700384 // Changes the current update over cellular network setting.
385 if (!FLAGS_update_over_cellular.empty()) {
386 gboolean allowed = FLAGS_update_over_cellular == "yes";
387 if (!allowed && FLAGS_update_over_cellular != "no") {
388 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
389 << "\". Please specify \"yes\" or \"no\".";
390 } else {
391 SetUpdateOverCellularPermission(allowed);
392 }
393 }
394
395 // Show the current update over cellular network setting.
396 if (FLAGS_show_update_over_cellular) {
397 bool allowed = GetUpdateOverCellularPermission();
398 LOG(INFO) << "Current update over cellular network setting: "
399 << (allowed ? "ENABLED" : "DISABLED");
400 }
401
Chris Sosacb7fa882013-07-25 17:02:59 -0700402 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
403 LOG(FATAL) << "powerwash flag only makes sense rollback or channel change";
404 return 1;
405 }
406
Alex Deymo5fdf7762013-07-17 20:01:40 -0700407 // Change the P2P enabled setting.
408 if (!FLAGS_p2p_update.empty()) {
409 gboolean enabled = FLAGS_p2p_update == "yes";
410 if (!enabled && FLAGS_p2p_update != "no") {
411 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
412 << "\". Please specify \"yes\" or \"no\".";
413 } else {
414 SetP2PUpdatePermission(enabled);
415 }
416 }
417
418 // Show the current P2P enabled setting.
419 if (FLAGS_show_p2p_update) {
420 bool enabled = GetP2PUpdatePermission();
421 LOG(INFO) << "Current update using P2P setting: "
422 << (enabled ? "ENABLED" : "DISABLED");
423 }
424
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700425 // First, update the target channel if requested.
426 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700427 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700428
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700429 // Show the current and target channels if requested.
430 if (FLAGS_show_channel) {
431 string current_channel = GetChannel(true);
432 LOG(INFO) << "Current Channel: " << current_channel;
433
434 string target_channel = GetChannel(false);
435 if (!target_channel.empty())
436 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900437 }
438
Chris Sosad317e402013-06-12 13:47:09 -0700439 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
440 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
441
Chris Sosad317e402013-06-12 13:47:09 -0700442 if (do_update_request && FLAGS_rollback) {
443 LOG(FATAL) << "Update should not be requested with rollback!";
444 return 1;
445 }
446
447 if(FLAGS_rollback) {
448 LOG(INFO) << "Requesting rollback.";
449 CHECK(Rollback(FLAGS_powerwash)) << "Request for rollback failed.";
450 return 0;
451 }
452
Darin Petkov58529db2010-08-13 09:19:47 -0700453 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700454 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700455 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700456 string app_version = FLAGS_app_version;
457 if (FLAGS_update && app_version.empty()) {
458 app_version = "ForcedUpdate";
459 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700460 }
Darin Petkov58529db2010-08-13 09:19:47 -0700461 LOG(INFO) << "Initiating update check and install.";
462 CHECK(CheckForUpdates(app_version, FLAGS_omaha_url))
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700463 << "Update check/initiate update failed.";
Darin Petkov58529db2010-08-13 09:19:47 -0700464
465 // Wait for an update to complete.
466 if (FLAGS_update) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700467 LOG(INFO) << "Waiting for update to complete.";
Darin Petkov58529db2010-08-13 09:19:47 -0700468 CompleteUpdate(); // Should never return.
469 return 1;
470 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700471 return 0;
472 }
Darin Petkov58529db2010-08-13 09:19:47 -0700473
474 // Start watching for updates.
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700475 if (FLAGS_watch_for_updates) {
Darin Petkov296889c2010-07-23 16:20:54 -0700476 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700477 LOG(INFO) << "Watching for status updates.";
478 WatchForUpdates(); // Should never return.
479 return 1;
480 }
Darin Petkov58529db2010-08-13 09:19:47 -0700481
Darin Petkov296889c2010-07-23 16:20:54 -0700482 if (FLAGS_reboot) {
483 LOG(INFO) << "Requesting a reboot...";
484 CHECK(RebootIfNeeded());
485 return 0;
486 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700487
Darin Petkov8daa3242010-10-25 13:28:47 -0700488 LOG(INFO) << "Done.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700489 return 0;
490}