blob: d0fee7a7cbeaab9b6863a33aff8f037a6ad9ff66 [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;
Alex Deymo13e0dd62015-06-30 23:11:44 -070045 // Wait for the UpdateEngine to be available or timeout.
46 proxy_->GetObjectProxy()->WaitForServiceToBeAvailable(
47 base::Bind(&UpdateEngineClient::OnServiceAvailable,
48 base::Unretained(this)));
49 base::MessageLoop::current()->PostDelayedTask(
50 FROM_HERE,
51 base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout,
52 base::Unretained(this)),
53 base::TimeDelta::FromSeconds(10));
Alex Deymo72aa0022015-06-19 21:16:49 -070054 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070055 }
Alex Deymo72aa0022015-06-19 21:16:49 -070056
57 private:
58 bool InitProxy();
59
Alex Deymo13e0dd62015-06-30 23:11:44 -070060 // Callback called when the UpdateEngine service becomes available.
61 void OnServiceAvailable(bool service_is_available);
62
63 // Callback called when the UpdateEngine service doesn't become available
64 // after a timeout.
65 void OnServiceAvailableTimeout();
66
67
Alex Deymo72aa0022015-06-19 21:16:49 -070068 // Callback called when a StatusUpdate signal is received.
69 void OnStatusUpdateSignal(int64_t last_checked_time,
70 double progress,
71 const string& current_operation,
72 const string& new_version,
73 int64_t new_size);
74 // Callback called when the OnStatusUpdateSignal() handler is registered.
75 void OnStatusUpdateSignalRegistration(const string& interface,
76 const string& signal_name,
77 bool success);
78
79 // Registers a callback that prints on stderr the received StatusUpdate
80 // signals.
81 // The daemon should continue running for this to work.
82 void WatchForUpdates();
83
84 void ResetStatus();
85
86 // Show the status of the update engine in stdout.
87 // Blocking call. Exits the program with error 1 in case of an error.
88 void ShowStatus();
89
90 // Return the current operation status, such as UPDATE_STATUS_IDLE.
91 // Blocking call. Exits the program with error 1 in case of an error.
92 string GetCurrentOperation();
93
94 void Rollback(bool rollback);
95 string GetRollbackPartition();
96 string GetKernelDevices();
97 void CheckForUpdates(const string& app_version,
98 const string& omaha_url,
99 bool interactive);
100
101 // Reboot the device if a reboot is needed.
102 // Blocking call. Ignores failures.
103 void RebootIfNeeded();
104
105 // Getter and setter for the target channel. If |get_current_channel| is true,
106 // the current channel instead of the target channel will be returned.
107 // Blocking call. Exits the program with error 1 in case of an error.
108 void SetTargetChannel(const string& target_channel, bool allow_powerwash);
109 string GetChannel(bool get_current_channel);
110
111 // Getter and setter for the updates over cellular connections.
112 // Blocking call. Exits the program with error 1 in case of an error.
113 void SetUpdateOverCellularPermission(bool allowed);
114 bool GetUpdateOverCellularPermission();
115
116 // Getter and setter for the updates from P2P permission.
117 // Blocking call. Exits the program with error 1 in case of an error.
118 void SetP2PUpdatePermission(bool enabled);
119 bool GetP2PUpdatePermission();
120
121 // This is similar to watching for updates but rather than registering
122 // a signal watch, actively poll the daemon just in case it stops
123 // sending notifications.
124 void WaitForUpdateComplete();
125 void OnUpdateCompleteCheck(int64_t last_checked_time,
126 double progress,
127 const string& current_operation,
128 const string& new_version,
129 int64_t new_size);
130
131 // Blocking call. Exits the program with error 1 in case of an error.
132 void ShowPrevVersion();
133
134 // Returns whether the current status is such that a reboot is needed.
135 // Blocking call. Exits the program with error 1 in case of an error.
136 bool GetIsRebootNeeded();
137
138 // Blocks until a reboot is needed. If the reboot is needed, exits the program
139 // with 0. Otherwise it exits the program with 1 if an error occurs before
140 // the reboot is needed.
141 void WaitForRebootNeeded();
142 void OnRebootNeededCheck(int64_t last_checked_time,
143 double progress,
144 const string& current_operation,
145 const string& new_version,
146 int64_t new_size);
147 // Callback called when the OnRebootNeededCheck() handler is registered. This
148 // is useful to check at this point if the reboot is needed, without loosing
149 // any StatusUpdate signals and avoiding the race condition.
150 void OnRebootNeededCheckRegistration(const string& interface,
151 const string& signal_name,
152 bool success);
153
154 // Main method that parses and triggers all the actions based on the passed
155 // flags.
156 int ProcessFlags();
157
158 // DBus Proxy to the update_engine daemon object used for all the calls.
159 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
160
161 // Copy of argc and argv passed to main().
162 int argc_;
163 char** argv_;
164
Alex Deymo13e0dd62015-06-30 23:11:44 -0700165 // Tell whether the UpdateEngine service is available after startup.
166 bool service_is_available_{false};
167
Alex Deymo72aa0022015-06-19 21:16:49 -0700168 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
169};
170
Alex Deymo72aa0022015-06-19 21:16:49 -0700171bool UpdateEngineClient::InitProxy() {
172 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
173
174 if (!proxy_->GetObjectProxy()) {
175 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
176 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800177 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700178 return true;
179}
180
Alex Deymo13e0dd62015-06-30 23:11:44 -0700181void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
182 service_is_available_ = service_is_available;
183 if (!service_is_available) {
184 LOG(ERROR) << "UpdateEngineService not available.";
185 QuitWithExitCode(-1);
186 }
187 int ret = ProcessFlags();
188 if (ret != kContinueRunning)
189 QuitWithExitCode(ret);
190}
191
192void UpdateEngineClient::OnServiceAvailableTimeout() {
193 if (!service_is_available_) {
194 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
195 "daemon running?";
196 QuitWithExitCode(-1);
197 }
198}
199
Alex Deymo72aa0022015-06-19 21:16:49 -0700200void UpdateEngineClient::OnStatusUpdateSignal(
201 int64_t last_checked_time,
202 double progress,
203 const string& current_operation,
204 const string& new_version,
205 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700206 LOG(INFO) << "Got status update:";
207 LOG(INFO) << " last_checked_time: " << last_checked_time;
208 LOG(INFO) << " progress: " << progress;
209 LOG(INFO) << " current_operation: " << current_operation;
210 LOG(INFO) << " new_version: " << new_version;
211 LOG(INFO) << " new_size: " << new_size;
212}
213
Alex Deymo72aa0022015-06-19 21:16:49 -0700214void UpdateEngineClient::OnStatusUpdateSignalRegistration(
215 const string& interface,
216 const string& signal_name,
217 bool success) {
218 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
219 << signal_name << ", " << success << ");";
220 if (!success) {
221 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
222 exit(1);
223 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700224}
225
Alex Deymo72aa0022015-06-19 21:16:49 -0700226void UpdateEngineClient::WatchForUpdates() {
227 proxy_->RegisterStatusUpdateSignalHandler(
228 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
229 base::Unretained(this)),
230 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
231 base::Unretained(this)));
232}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700233
Alex Deymo72aa0022015-06-19 21:16:49 -0700234void UpdateEngineClient::ResetStatus() {
235 bool ret = proxy_->ResetStatus(nullptr);
236 CHECK(ret) << "ResetStatus() failed.";
237}
Andrew de los Reyesada42202010-07-15 22:23:20 -0700238
Alex Deymo72aa0022015-06-19 21:16:49 -0700239void UpdateEngineClient::ShowStatus() {
240 int64_t last_checked_time = 0;
241 double progress = 0.0;
242 string current_op;
243 string new_version;
244 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700245
Alex Deymo72aa0022015-06-19 21:16:49 -0700246 bool ret = proxy_->GetStatus(
247 &last_checked_time, &progress, &current_op, &new_version, &new_size,
248 nullptr);
249 CHECK(ret) << "GetStatus() failed";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700250 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
251 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
252 last_checked_time,
253 progress,
Alex Deymo72aa0022015-06-19 21:16:49 -0700254 current_op.c_str(),
255 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700256 new_size);
Alex Deymo72aa0022015-06-19 21:16:49 -0700257}
258
259string UpdateEngineClient::GetCurrentOperation() {
260 int64_t last_checked_time = 0;
261 double progress = 0.0;
262 string current_op;
263 string new_version;
264 int64_t new_size = 0;
265
266 bool ret = proxy_->GetStatus(
267 &last_checked_time, &progress, &current_op, &new_version, &new_size,
268 nullptr);
269 CHECK(ret) << "GetStatus() failed";
270 return current_op;
271}
272
273void UpdateEngineClient::Rollback(bool rollback) {
274 bool ret = proxy_->AttemptRollback(rollback, nullptr);
275 CHECK(ret) << "Rollback request failed.";
276}
277
278string UpdateEngineClient::GetRollbackPartition() {
279 string rollback_partition;
280 bool ret = proxy_->GetRollbackPartition(&rollback_partition, nullptr);
281 CHECK(ret) << "Error while querying rollback partition availabilty.";
282 return rollback_partition;
283}
284
285string UpdateEngineClient::GetKernelDevices() {
286 string kernel_devices;
287 bool ret = proxy_->GetKernelDevices(&kernel_devices, nullptr);
288 CHECK(ret) << "Error while getting a list of kernel devices";
289 return kernel_devices;
290}
291
292void UpdateEngineClient::CheckForUpdates(const string& app_version,
293 const string& omaha_url,
294 bool interactive) {
295 int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
296 bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
297 nullptr);
298 CHECK(ret) << "Error checking for update.";
299}
300
301void UpdateEngineClient::RebootIfNeeded() {
302 bool ret = proxy_->RebootIfNeeded(nullptr);
303 if (!ret) {
304 // Reboot error code doesn't necessarily mean that a reboot
305 // failed. For example, D-Bus may be shutdown before we receive the
306 // result.
307 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700308 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700309}
310
Alex Deymo72aa0022015-06-19 21:16:49 -0700311void UpdateEngineClient::SetTargetChannel(const string& target_channel,
312 bool allow_powerwash) {
313 bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
314 CHECK(ret) << "Error setting the channel.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700315 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700316}
317
Alex Deymo72aa0022015-06-19 21:16:49 -0700318string UpdateEngineClient::GetChannel(bool get_current_channel) {
319 string channel;
320 bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
321 CHECK(ret) << "Error getting the channel.";
322 return channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900323}
324
Alex Deymo72aa0022015-06-19 21:16:49 -0700325void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
326 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
327 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700328}
329
Alex Deymo72aa0022015-06-19 21:16:49 -0700330bool UpdateEngineClient::GetUpdateOverCellularPermission() {
331 bool allowed;
332 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
333 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700334 return allowed;
335}
336
Alex Deymo72aa0022015-06-19 21:16:49 -0700337void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
338 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
339 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700340}
341
Alex Deymo72aa0022015-06-19 21:16:49 -0700342bool UpdateEngineClient::GetP2PUpdatePermission() {
343 bool enabled;
344 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
345 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700346 return enabled;
347}
348
Alex Deymo72aa0022015-06-19 21:16:49 -0700349void UpdateEngineClient::OnUpdateCompleteCheck(
350 int64_t /* last_checked_time */,
351 double /* progress */,
352 const string& current_operation,
353 const string& /* new_version */,
354 int64_t /* new_size */) {
355 if (current_operation == update_engine::kUpdateStatusIdle) {
356 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700357 exit(1);
358 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700359 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700360 LOG(INFO) << "Update succeeded -- reboot needed.";
361 exit(0);
362 }
Darin Petkov58529db2010-08-13 09:19:47 -0700363}
364
Alex Deymo72aa0022015-06-19 21:16:49 -0700365void UpdateEngineClient::WaitForUpdateComplete() {
366 proxy_->RegisterStatusUpdateSignalHandler(
367 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
368 base::Unretained(this)),
369 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
370 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700371}
372
Alex Deymo72aa0022015-06-19 21:16:49 -0700373void UpdateEngineClient::ShowPrevVersion() {
374 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700375
Alex Deymo72aa0022015-06-19 21:16:49 -0700376 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
377 if (!ret) {
378 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700379 } else {
380 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700381 }
382}
383
Alex Deymo72aa0022015-06-19 21:16:49 -0700384bool UpdateEngineClient::GetIsRebootNeeded() {
385 return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
David Zeuthen9d73a722014-04-04 14:52:46 -0700386}
387
Alex Deymo72aa0022015-06-19 21:16:49 -0700388void UpdateEngineClient::OnRebootNeededCheck(
389 int64_t /* last_checked_time */,
390 double /* progress */,
391 const string& current_operation,
392 const string& /* new_version */,
393 int64_t /* new_size */) {
394 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
395 LOG(INFO) << "Reboot needed.";
396 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700397 }
398}
399
Alex Deymo72aa0022015-06-19 21:16:49 -0700400void UpdateEngineClient::OnRebootNeededCheckRegistration(
401 const string& interface,
402 const string& signal_name,
403 bool success) {
404 if (GetIsRebootNeeded())
405 exit(0);
406 if (!success) {
407 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
408 exit(1);
409 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700410}
411
412// Blocks until a reboot is needed. Returns true if waiting succeeded,
413// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700414void UpdateEngineClient::WaitForRebootNeeded() {
415 proxy_->RegisterStatusUpdateSignalHandler(
416 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
417 base::Unretained(this)),
418 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
419 base::Unretained(this)));
420 if (GetIsRebootNeeded())
421 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700422}
423
Alex Deymo72aa0022015-06-19 21:16:49 -0700424int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700425 DEFINE_string(app_version, "", "Force the current app version.");
426 DEFINE_string(channel, "",
427 "Set the target channel. The device will be powerwashed if the "
428 "target channel is more stable than the current channel unless "
429 "--nopowerwash is specified.");
430 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
431 DEFINE_bool(follow, false, "Wait for any update operations to complete."
432 "Exit status is 0 if the update succeeded, and 1 otherwise.");
433 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
434 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
435 DEFINE_string(p2p_update, "",
436 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
437 " sharing.");
438 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
439 "do a powerwash or allow it respectively.");
440 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
441 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
442 "2 if reboot is not needed or 1 if an error occurred.");
443 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
444 "needed. Returns non-zero exit status if an error occurred.");
445 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800446 DEFINE_bool(rollback, false,
447 "Perform a rollback to the previous partition. The device will "
448 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700449 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
450 "is available.");
451 DEFINE_bool(show_channel, false, "Show the current and target channels.");
452 DEFINE_bool(show_p2p_update, false,
453 "Show the current setting for peer-to-peer update sharing.");
454 DEFINE_bool(show_update_over_cellular, false,
455 "Show the current setting for updates over cellular networks.");
456 DEFINE_bool(status, false, "Print the status to stdout.");
457 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
458 "Implies --follow.");
459 DEFINE_string(update_over_cellular, "",
460 "Enables (\"yes\") or disables (\"no\") the updates over "
461 "cellular networks.");
462 DEFINE_bool(watch_for_updates, false,
463 "Listen for status updates and print them to the screen.");
464 DEFINE_bool(prev_version, false,
465 "Show the previous OS version used before the update reboot.");
466 DEFINE_bool(show_kernels, false, "Show the list of kernel patritions and "
467 "whether each of them is bootable or not");
468
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700469 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700470 base::CommandLine::Init(argc_, argv_);
471 chromeos::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700472
Alex Deymo8ce80d62015-01-27 15:10:43 -0800473 // Ensure there are no positional arguments.
474 const std::vector<string> positional_args =
475 base::CommandLine::ForCurrentProcess()->GetArgs();
476 if (!positional_args.empty()) {
477 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
478 << "'. If you want to pass a value to a flag, pass it as "
479 "--flag=value.";
480 return 1;
481 }
482
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700483 // Update the status if requested.
484 if (FLAGS_reset_status) {
485 LOG(INFO) << "Setting Update Engine status to idle ...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700486 ResetStatus();
Gilad Arnold50c60632013-01-25 10:27:19 -0800487 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n"
488 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} "
489 "| sed 's/^[^0-9]*//')-1)) $D;)";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700490 }
Darin Petkov58529db2010-08-13 09:19:47 -0700491
Alex Deymof4867c42013-06-28 14:41:39 -0700492 // Changes the current update over cellular network setting.
493 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700494 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700495 if (!allowed && FLAGS_update_over_cellular != "no") {
496 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
497 << "\". Please specify \"yes\" or \"no\".";
498 } else {
499 SetUpdateOverCellularPermission(allowed);
500 }
501 }
502
503 // Show the current update over cellular network setting.
504 if (FLAGS_show_update_over_cellular) {
505 bool allowed = GetUpdateOverCellularPermission();
506 LOG(INFO) << "Current update over cellular network setting: "
507 << (allowed ? "ENABLED" : "DISABLED");
508 }
509
Chris Sosacb7fa882013-07-25 17:02:59 -0700510 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700511 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700512 return 1;
513 }
514
Alex Deymo5fdf7762013-07-17 20:01:40 -0700515 // Change the P2P enabled setting.
516 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700517 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700518 if (!enabled && FLAGS_p2p_update != "no") {
519 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
520 << "\". Please specify \"yes\" or \"no\".";
521 } else {
522 SetP2PUpdatePermission(enabled);
523 }
524 }
525
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800526 // Show the rollback availability.
527 if (FLAGS_can_rollback) {
Alex Deymof329b932014-10-30 01:37:48 -0700528 string rollback_partition = GetRollbackPartition();
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700529 bool can_rollback = true;
530 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700531 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700532 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700533 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700534 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700535 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700536
537 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700538 if (!can_rollback) {
539 return 1;
540 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800541 }
542
Alex Deymo5fdf7762013-07-17 20:01:40 -0700543 // Show the current P2P enabled setting.
544 if (FLAGS_show_p2p_update) {
545 bool enabled = GetP2PUpdatePermission();
546 LOG(INFO) << "Current update using P2P setting: "
547 << (enabled ? "ENABLED" : "DISABLED");
548 }
549
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700550 // First, update the target channel if requested.
551 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700552 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700553
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700554 // Show the current and target channels if requested.
555 if (FLAGS_show_channel) {
556 string current_channel = GetChannel(true);
557 LOG(INFO) << "Current Channel: " << current_channel;
558
559 string target_channel = GetChannel(false);
560 if (!target_channel.empty())
561 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900562 }
563
Chris Sosad317e402013-06-12 13:47:09 -0700564 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
565 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700566 if (FLAGS_update)
567 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700568
Chris Sosad317e402013-06-12 13:47:09 -0700569 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700570 LOG(ERROR) << "Incompatible flags specified with rollback."
571 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700572 return 1;
573 }
574
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700575 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700576 LOG(INFO) << "Requesting rollback.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700577 Rollback(FLAGS_powerwash);
Chris Sosad317e402013-06-12 13:47:09 -0700578 }
579
Darin Petkov58529db2010-08-13 09:19:47 -0700580 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700581 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700582 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700583 string app_version = FLAGS_app_version;
584 if (FLAGS_update && app_version.empty()) {
585 app_version = "ForcedUpdate";
586 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700587 }
Darin Petkov58529db2010-08-13 09:19:47 -0700588 LOG(INFO) << "Initiating update check and install.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700589 CheckForUpdates(app_version, FLAGS_omaha_url, FLAGS_interactive);
Chris Sosa192449e2013-10-28 14:16:19 -0700590 }
Darin Petkov58529db2010-08-13 09:19:47 -0700591
Chris Sosa192449e2013-10-28 14:16:19 -0700592 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700593 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
594 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700595 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700596 LOG(ERROR) << "Multiple exclusive options selected. "
597 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700598 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700599 << "or --status.";
600 return 1;
601 }
602
603 if (FLAGS_status) {
604 LOG(INFO) << "Querying Update Engine status...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700605 ShowStatus();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700606 return 0;
607 }
Darin Petkov58529db2010-08-13 09:19:47 -0700608
Chris Sosa192449e2013-10-28 14:16:19 -0700609 if (FLAGS_follow) {
610 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700611 WaitForUpdateComplete();
612 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700613 }
614
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700615 if (FLAGS_watch_for_updates) {
616 LOG(INFO) << "Watching for status updates.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700617 WatchForUpdates();
618 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700619 }
Darin Petkov58529db2010-08-13 09:19:47 -0700620
Darin Petkov296889c2010-07-23 16:20:54 -0700621 if (FLAGS_reboot) {
622 LOG(INFO) << "Requesting a reboot...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700623 RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700624 return 0;
625 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700626
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700627 if (FLAGS_prev_version) {
628 ShowPrevVersion();
629 }
630
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700631 if (FLAGS_show_kernels) {
632 LOG(INFO) << "Kernel partitions:\n"
633 << GetKernelDevices();
634 }
635
David Zeuthen9d73a722014-04-04 14:52:46 -0700636 if (FLAGS_is_reboot_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700637 // In case of error GetIsRebootNeeded() will exit with 1.
638 if (GetIsRebootNeeded()) {
639 return 0;
640 } else {
David Zeuthen9d73a722014-04-04 14:52:46 -0700641 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700642 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700643 }
644
645 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700646 WaitForRebootNeeded();
647 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700648 }
649
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700650 return 0;
651}
Alex Deymo72aa0022015-06-19 21:16:49 -0700652
653} // namespace
654
655int main(int argc, char** argv) {
656 UpdateEngineClient client(argc, argv);
657 return client.Run();
658}