blob: 01946e7beef51daf8d8135bbac7ad4ed2b4ff942 [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
Alex Deymo72aa0022015-06-19 21:16:49 -07005#include <inttypes.h>
6#include <sysexits.h>
7#include <unistd.h>
8
Darin Petkov5a7f5652010-07-22 21:40:09 -07009#include <string>
10
Alex Deymo72aa0022015-06-19 21:16:49 -070011#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080012#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070013#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070014#include <base/macros.h>
15#include <chromeos/daemons/dbus_daemon.h>
David Zeuthen9d73a722014-04-04 14:52:46 -070016#include <chromeos/dbus/service_constants.h>
Steve Fung97b6f5a2014-10-07 12:39:51 -070017#include <chromeos/flag_helper.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070018#include <dbus/bus.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070019
20#include "update_engine/dbus_constants.h"
Alex Deymo72aa0022015-06-19 21:16:49 -070021#include "update_engine/dbus_proxies.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070022
David Zeuthen75a4c3e2013-09-06 11:36:59 -070023using chromeos_update_engine::kAttemptUpdateFlagNonInteractive;
Alex Deymo44666f92014-07-22 20:29:24 -070024using chromeos_update_engine::kUpdateEngineServiceName;
Darin Petkov5a7f5652010-07-22 21:40:09 -070025using std::string;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027namespace {
28
Alex Deymo72aa0022015-06-19 21:16:49 -070029// Constant to signal that we need to continue running the daemon after
30// initialization.
31const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032
Alex Deymo72aa0022015-06-19 21:16:49 -070033class UpdateEngineClient : public chromeos::DBusDaemon {
34 public:
35 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
36 ~UpdateEngineClient() override = default;
37
38 protected:
39 int OnInit() override {
40 int ret = DBusDaemon::OnInit();
41 if (ret != EX_OK)
42 return ret;
43 if (!InitProxy())
44 return 1;
45 ret = ProcessFlags();
46 if (ret != kContinueRunning) {
47 base::MessageLoop::current()->PostTask(
48 FROM_HERE,
49 base::Bind(&UpdateEngineClient::QuitWithExitCode,
50 base::Unretained(this), ret));
Darin Petkova0b9e772011-10-06 05:05:56 -070051 }
Alex Deymo72aa0022015-06-19 21:16:49 -070052 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070053 }
Alex Deymo72aa0022015-06-19 21:16:49 -070054
55 private:
56 bool InitProxy();
57
58 // Callback called when a StatusUpdate signal is received.
59 void OnStatusUpdateSignal(int64_t last_checked_time,
60 double progress,
61 const string& current_operation,
62 const string& new_version,
63 int64_t new_size);
64 // Callback called when the OnStatusUpdateSignal() handler is registered.
65 void OnStatusUpdateSignalRegistration(const string& interface,
66 const string& signal_name,
67 bool success);
68
69 // Registers a callback that prints on stderr the received StatusUpdate
70 // signals.
71 // The daemon should continue running for this to work.
72 void WatchForUpdates();
73
74 void ResetStatus();
75
76 // Show the status of the update engine in stdout.
77 // Blocking call. Exits the program with error 1 in case of an error.
78 void ShowStatus();
79
80 // Return the current operation status, such as UPDATE_STATUS_IDLE.
81 // Blocking call. Exits the program with error 1 in case of an error.
82 string GetCurrentOperation();
83
84 void Rollback(bool rollback);
85 string GetRollbackPartition();
86 string GetKernelDevices();
87 void CheckForUpdates(const string& app_version,
88 const string& omaha_url,
89 bool interactive);
90
91 // Reboot the device if a reboot is needed.
92 // Blocking call. Ignores failures.
93 void RebootIfNeeded();
94
95 // Getter and setter for the target channel. If |get_current_channel| is true,
96 // the current channel instead of the target channel will be returned.
97 // Blocking call. Exits the program with error 1 in case of an error.
98 void SetTargetChannel(const string& target_channel, bool allow_powerwash);
99 string GetChannel(bool get_current_channel);
100
101 // Getter and setter for the updates over cellular connections.
102 // Blocking call. Exits the program with error 1 in case of an error.
103 void SetUpdateOverCellularPermission(bool allowed);
104 bool GetUpdateOverCellularPermission();
105
106 // Getter and setter for the updates from P2P permission.
107 // Blocking call. Exits the program with error 1 in case of an error.
108 void SetP2PUpdatePermission(bool enabled);
109 bool GetP2PUpdatePermission();
110
111 // This is similar to watching for updates but rather than registering
112 // a signal watch, actively poll the daemon just in case it stops
113 // sending notifications.
114 void WaitForUpdateComplete();
115 void OnUpdateCompleteCheck(int64_t last_checked_time,
116 double progress,
117 const string& current_operation,
118 const string& new_version,
119 int64_t new_size);
120
121 // Blocking call. Exits the program with error 1 in case of an error.
122 void ShowPrevVersion();
123
124 // Returns whether the current status is such that a reboot is needed.
125 // Blocking call. Exits the program with error 1 in case of an error.
126 bool GetIsRebootNeeded();
127
128 // Blocks until a reboot is needed. If the reboot is needed, exits the program
129 // with 0. Otherwise it exits the program with 1 if an error occurs before
130 // the reboot is needed.
131 void WaitForRebootNeeded();
132 void OnRebootNeededCheck(int64_t last_checked_time,
133 double progress,
134 const string& current_operation,
135 const string& new_version,
136 int64_t new_size);
137 // Callback called when the OnRebootNeededCheck() handler is registered. This
138 // is useful to check at this point if the reboot is needed, without loosing
139 // any StatusUpdate signals and avoiding the race condition.
140 void OnRebootNeededCheckRegistration(const string& interface,
141 const string& signal_name,
142 bool success);
143
144 // Main method that parses and triggers all the actions based on the passed
145 // flags.
146 int ProcessFlags();
147
148 // DBus Proxy to the update_engine daemon object used for all the calls.
149 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
150
151 // Copy of argc and argv passed to main().
152 int argc_;
153 char** argv_;
154
155 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
156};
157
158
159bool UpdateEngineClient::InitProxy() {
160 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
161
162 if (!proxy_->GetObjectProxy()) {
163 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
164 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800165 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700166 return true;
167}
168
Alex Deymo72aa0022015-06-19 21:16:49 -0700169void UpdateEngineClient::OnStatusUpdateSignal(
170 int64_t last_checked_time,
171 double progress,
172 const string& current_operation,
173 const string& new_version,
174 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700175 LOG(INFO) << "Got status update:";
176 LOG(INFO) << " last_checked_time: " << last_checked_time;
177 LOG(INFO) << " progress: " << progress;
178 LOG(INFO) << " current_operation: " << current_operation;
179 LOG(INFO) << " new_version: " << new_version;
180 LOG(INFO) << " new_size: " << new_size;
181}
182
Alex Deymo72aa0022015-06-19 21:16:49 -0700183void UpdateEngineClient::OnStatusUpdateSignalRegistration(
184 const string& interface,
185 const string& signal_name,
186 bool success) {
187 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
188 << signal_name << ", " << success << ");";
189 if (!success) {
190 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
191 exit(1);
192 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700193}
194
Alex Deymo72aa0022015-06-19 21:16:49 -0700195void UpdateEngineClient::WatchForUpdates() {
196 proxy_->RegisterStatusUpdateSignalHandler(
197 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
198 base::Unretained(this)),
199 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
200 base::Unretained(this)));
201}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700202
Alex Deymo72aa0022015-06-19 21:16:49 -0700203void UpdateEngineClient::ResetStatus() {
204 bool ret = proxy_->ResetStatus(nullptr);
205 CHECK(ret) << "ResetStatus() failed.";
206}
Andrew de los Reyesada42202010-07-15 22:23:20 -0700207
Alex Deymo72aa0022015-06-19 21:16:49 -0700208void UpdateEngineClient::ShowStatus() {
209 int64_t last_checked_time = 0;
210 double progress = 0.0;
211 string current_op;
212 string new_version;
213 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700214
Alex Deymo72aa0022015-06-19 21:16:49 -0700215 bool ret = proxy_->GetStatus(
216 &last_checked_time, &progress, &current_op, &new_version, &new_size,
217 nullptr);
218 CHECK(ret) << "GetStatus() failed";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700219 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
220 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
221 last_checked_time,
222 progress,
Alex Deymo72aa0022015-06-19 21:16:49 -0700223 current_op.c_str(),
224 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700225 new_size);
Alex Deymo72aa0022015-06-19 21:16:49 -0700226}
227
228string UpdateEngineClient::GetCurrentOperation() {
229 int64_t last_checked_time = 0;
230 double progress = 0.0;
231 string current_op;
232 string new_version;
233 int64_t new_size = 0;
234
235 bool ret = proxy_->GetStatus(
236 &last_checked_time, &progress, &current_op, &new_version, &new_size,
237 nullptr);
238 CHECK(ret) << "GetStatus() failed";
239 return current_op;
240}
241
242void UpdateEngineClient::Rollback(bool rollback) {
243 bool ret = proxy_->AttemptRollback(rollback, nullptr);
244 CHECK(ret) << "Rollback request failed.";
245}
246
247string UpdateEngineClient::GetRollbackPartition() {
248 string rollback_partition;
249 bool ret = proxy_->GetRollbackPartition(&rollback_partition, nullptr);
250 CHECK(ret) << "Error while querying rollback partition availabilty.";
251 return rollback_partition;
252}
253
254string UpdateEngineClient::GetKernelDevices() {
255 string kernel_devices;
256 bool ret = proxy_->GetKernelDevices(&kernel_devices, nullptr);
257 CHECK(ret) << "Error while getting a list of kernel devices";
258 return kernel_devices;
259}
260
261void UpdateEngineClient::CheckForUpdates(const string& app_version,
262 const string& omaha_url,
263 bool interactive) {
264 int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
265 bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
266 nullptr);
267 CHECK(ret) << "Error checking for update.";
268}
269
270void UpdateEngineClient::RebootIfNeeded() {
271 bool ret = proxy_->RebootIfNeeded(nullptr);
272 if (!ret) {
273 // Reboot error code doesn't necessarily mean that a reboot
274 // failed. For example, D-Bus may be shutdown before we receive the
275 // result.
276 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700277 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700278}
279
Alex Deymo72aa0022015-06-19 21:16:49 -0700280void UpdateEngineClient::SetTargetChannel(const string& target_channel,
281 bool allow_powerwash) {
282 bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
283 CHECK(ret) << "Error setting the channel.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700284 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700285}
286
Alex Deymo72aa0022015-06-19 21:16:49 -0700287string UpdateEngineClient::GetChannel(bool get_current_channel) {
288 string channel;
289 bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
290 CHECK(ret) << "Error getting the channel.";
291 return channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900292}
293
Alex Deymo72aa0022015-06-19 21:16:49 -0700294void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
295 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
296 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700297}
298
Alex Deymo72aa0022015-06-19 21:16:49 -0700299bool UpdateEngineClient::GetUpdateOverCellularPermission() {
300 bool allowed;
301 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
302 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700303 return allowed;
304}
305
Alex Deymo72aa0022015-06-19 21:16:49 -0700306void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
307 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
308 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700309}
310
Alex Deymo72aa0022015-06-19 21:16:49 -0700311bool UpdateEngineClient::GetP2PUpdatePermission() {
312 bool enabled;
313 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
314 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700315 return enabled;
316}
317
Alex Deymo72aa0022015-06-19 21:16:49 -0700318void UpdateEngineClient::OnUpdateCompleteCheck(
319 int64_t /* last_checked_time */,
320 double /* progress */,
321 const string& current_operation,
322 const string& /* new_version */,
323 int64_t /* new_size */) {
324 if (current_operation == update_engine::kUpdateStatusIdle) {
325 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700326 exit(1);
327 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700328 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700329 LOG(INFO) << "Update succeeded -- reboot needed.";
330 exit(0);
331 }
Darin Petkov58529db2010-08-13 09:19:47 -0700332}
333
Alex Deymo72aa0022015-06-19 21:16:49 -0700334void UpdateEngineClient::WaitForUpdateComplete() {
335 proxy_->RegisterStatusUpdateSignalHandler(
336 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
337 base::Unretained(this)),
338 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
339 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700340}
341
Alex Deymo72aa0022015-06-19 21:16:49 -0700342void UpdateEngineClient::ShowPrevVersion() {
343 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700344
Alex Deymo72aa0022015-06-19 21:16:49 -0700345 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
346 if (!ret) {
347 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700348 } else {
349 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700350 }
351}
352
Alex Deymo72aa0022015-06-19 21:16:49 -0700353bool UpdateEngineClient::GetIsRebootNeeded() {
354 return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
David Zeuthen9d73a722014-04-04 14:52:46 -0700355}
356
Alex Deymo72aa0022015-06-19 21:16:49 -0700357void UpdateEngineClient::OnRebootNeededCheck(
358 int64_t /* last_checked_time */,
359 double /* progress */,
360 const string& current_operation,
361 const string& /* new_version */,
362 int64_t /* new_size */) {
363 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
364 LOG(INFO) << "Reboot needed.";
365 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700366 }
367}
368
Alex Deymo72aa0022015-06-19 21:16:49 -0700369void UpdateEngineClient::OnRebootNeededCheckRegistration(
370 const string& interface,
371 const string& signal_name,
372 bool success) {
373 if (GetIsRebootNeeded())
374 exit(0);
375 if (!success) {
376 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
377 exit(1);
378 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700379}
380
381// Blocks until a reboot is needed. Returns true if waiting succeeded,
382// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700383void UpdateEngineClient::WaitForRebootNeeded() {
384 proxy_->RegisterStatusUpdateSignalHandler(
385 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
386 base::Unretained(this)),
387 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
388 base::Unretained(this)));
389 if (GetIsRebootNeeded())
390 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700391}
392
Alex Deymo72aa0022015-06-19 21:16:49 -0700393int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700394 DEFINE_string(app_version, "", "Force the current app version.");
395 DEFINE_string(channel, "",
396 "Set the target channel. The device will be powerwashed if the "
397 "target channel is more stable than the current channel unless "
398 "--nopowerwash is specified.");
399 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
400 DEFINE_bool(follow, false, "Wait for any update operations to complete."
401 "Exit status is 0 if the update succeeded, and 1 otherwise.");
402 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
403 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
404 DEFINE_string(p2p_update, "",
405 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
406 " sharing.");
407 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
408 "do a powerwash or allow it respectively.");
409 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
410 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
411 "2 if reboot is not needed or 1 if an error occurred.");
412 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
413 "needed. Returns non-zero exit status if an error occurred.");
414 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800415 DEFINE_bool(rollback, false,
416 "Perform a rollback to the previous partition. The device will "
417 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700418 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
419 "is available.");
420 DEFINE_bool(show_channel, false, "Show the current and target channels.");
421 DEFINE_bool(show_p2p_update, false,
422 "Show the current setting for peer-to-peer update sharing.");
423 DEFINE_bool(show_update_over_cellular, false,
424 "Show the current setting for updates over cellular networks.");
425 DEFINE_bool(status, false, "Print the status to stdout.");
426 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
427 "Implies --follow.");
428 DEFINE_string(update_over_cellular, "",
429 "Enables (\"yes\") or disables (\"no\") the updates over "
430 "cellular networks.");
431 DEFINE_bool(watch_for_updates, false,
432 "Listen for status updates and print them to the screen.");
433 DEFINE_bool(prev_version, false,
434 "Show the previous OS version used before the update reboot.");
435 DEFINE_bool(show_kernels, false, "Show the list of kernel patritions and "
436 "whether each of them is bootable or not");
437
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700438 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700439 base::CommandLine::Init(argc_, argv_);
440 chromeos::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700441
Alex Deymo8ce80d62015-01-27 15:10:43 -0800442 // Ensure there are no positional arguments.
443 const std::vector<string> positional_args =
444 base::CommandLine::ForCurrentProcess()->GetArgs();
445 if (!positional_args.empty()) {
446 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
447 << "'. If you want to pass a value to a flag, pass it as "
448 "--flag=value.";
449 return 1;
450 }
451
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700452 // Update the status if requested.
453 if (FLAGS_reset_status) {
454 LOG(INFO) << "Setting Update Engine status to idle ...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700455 ResetStatus();
Gilad Arnold50c60632013-01-25 10:27:19 -0800456 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n"
457 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} "
458 "| sed 's/^[^0-9]*//')-1)) $D;)";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700459 }
Darin Petkov58529db2010-08-13 09:19:47 -0700460
Alex Deymof4867c42013-06-28 14:41:39 -0700461 // Changes the current update over cellular network setting.
462 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700463 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700464 if (!allowed && FLAGS_update_over_cellular != "no") {
465 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
466 << "\". Please specify \"yes\" or \"no\".";
467 } else {
468 SetUpdateOverCellularPermission(allowed);
469 }
470 }
471
472 // Show the current update over cellular network setting.
473 if (FLAGS_show_update_over_cellular) {
474 bool allowed = GetUpdateOverCellularPermission();
475 LOG(INFO) << "Current update over cellular network setting: "
476 << (allowed ? "ENABLED" : "DISABLED");
477 }
478
Chris Sosacb7fa882013-07-25 17:02:59 -0700479 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700480 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700481 return 1;
482 }
483
Alex Deymo5fdf7762013-07-17 20:01:40 -0700484 // Change the P2P enabled setting.
485 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700486 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700487 if (!enabled && FLAGS_p2p_update != "no") {
488 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
489 << "\". Please specify \"yes\" or \"no\".";
490 } else {
491 SetP2PUpdatePermission(enabled);
492 }
493 }
494
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800495 // Show the rollback availability.
496 if (FLAGS_can_rollback) {
Alex Deymof329b932014-10-30 01:37:48 -0700497 string rollback_partition = GetRollbackPartition();
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700498 bool can_rollback = true;
499 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700500 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700501 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700502 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700503 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700504 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700505
506 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700507 if (!can_rollback) {
508 return 1;
509 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800510 }
511
Alex Deymo5fdf7762013-07-17 20:01:40 -0700512 // Show the current P2P enabled setting.
513 if (FLAGS_show_p2p_update) {
514 bool enabled = GetP2PUpdatePermission();
515 LOG(INFO) << "Current update using P2P setting: "
516 << (enabled ? "ENABLED" : "DISABLED");
517 }
518
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700519 // First, update the target channel if requested.
520 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700521 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700522
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700523 // Show the current and target channels if requested.
524 if (FLAGS_show_channel) {
525 string current_channel = GetChannel(true);
526 LOG(INFO) << "Current Channel: " << current_channel;
527
528 string target_channel = GetChannel(false);
529 if (!target_channel.empty())
530 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900531 }
532
Chris Sosad317e402013-06-12 13:47:09 -0700533 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
534 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700535 if (FLAGS_update)
536 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700537
Chris Sosad317e402013-06-12 13:47:09 -0700538 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700539 LOG(ERROR) << "Incompatible flags specified with rollback."
540 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700541 return 1;
542 }
543
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700544 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700545 LOG(INFO) << "Requesting rollback.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700546 Rollback(FLAGS_powerwash);
Chris Sosad317e402013-06-12 13:47:09 -0700547 }
548
Darin Petkov58529db2010-08-13 09:19:47 -0700549 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700550 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700551 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700552 string app_version = FLAGS_app_version;
553 if (FLAGS_update && app_version.empty()) {
554 app_version = "ForcedUpdate";
555 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700556 }
Darin Petkov58529db2010-08-13 09:19:47 -0700557 LOG(INFO) << "Initiating update check and install.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700558 CheckForUpdates(app_version, FLAGS_omaha_url, FLAGS_interactive);
Chris Sosa192449e2013-10-28 14:16:19 -0700559 }
Darin Petkov58529db2010-08-13 09:19:47 -0700560
Chris Sosa192449e2013-10-28 14:16:19 -0700561 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700562 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
563 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700564 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700565 LOG(ERROR) << "Multiple exclusive options selected. "
566 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700567 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700568 << "or --status.";
569 return 1;
570 }
571
572 if (FLAGS_status) {
573 LOG(INFO) << "Querying Update Engine status...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700574 ShowStatus();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700575 return 0;
576 }
Darin Petkov58529db2010-08-13 09:19:47 -0700577
Chris Sosa192449e2013-10-28 14:16:19 -0700578 if (FLAGS_follow) {
579 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700580 WaitForUpdateComplete();
581 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700582 }
583
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700584 if (FLAGS_watch_for_updates) {
585 LOG(INFO) << "Watching for status updates.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700586 WatchForUpdates();
587 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700588 }
Darin Petkov58529db2010-08-13 09:19:47 -0700589
Darin Petkov296889c2010-07-23 16:20:54 -0700590 if (FLAGS_reboot) {
591 LOG(INFO) << "Requesting a reboot...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700592 RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700593 return 0;
594 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700595
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700596 if (FLAGS_prev_version) {
597 ShowPrevVersion();
598 }
599
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700600 if (FLAGS_show_kernels) {
601 LOG(INFO) << "Kernel partitions:\n"
602 << GetKernelDevices();
603 }
604
David Zeuthen9d73a722014-04-04 14:52:46 -0700605 if (FLAGS_is_reboot_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700606 // In case of error GetIsRebootNeeded() will exit with 1.
607 if (GetIsRebootNeeded()) {
608 return 0;
609 } else {
David Zeuthen9d73a722014-04-04 14:52:46 -0700610 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700611 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700612 }
613
614 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700615 WaitForRebootNeeded();
616 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700617 }
618
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700619 return 0;
620}
Alex Deymo72aa0022015-06-19 21:16:49 -0700621
622} // namespace
623
624int main(int argc, char** argv) {
625 UpdateEngineClient client(argc, argv);
626 return client.Run();
627}