blob: b59f22f8cc71878e59e0ba3823577e9d7dffd588 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070016
Alex Deymo72aa0022015-06-19 21:16:49 -070017#include <inttypes.h>
18#include <sysexits.h>
19#include <unistd.h>
20
Darin Petkov5a7f5652010-07-22 21:40:09 -070021#include <string>
22
Alex Deymo72aa0022015-06-19 21:16:49 -070023#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080024#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070025#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070026#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#include <brillo/daemons/dbus_daemon.h>
28#include <brillo/flag_helper.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070029#include <dbus/bus.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070030#include <update_engine/dbus-constants.h>
31#include <update_engine/dbus-proxies.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032
Darin Petkov5a7f5652010-07-22 21:40:09 -070033using std::string;
Alex Deymod6deb1d2015-08-28 15:54:37 -070034using update_engine::kAttemptUpdateFlagNonInteractive;
35using update_engine::kUpdateEngineServiceName;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070037namespace {
38
Alex Deymo72aa0022015-06-19 21:16:49 -070039// Constant to signal that we need to continue running the daemon after
40// initialization.
41const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070042
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070043class UpdateEngineClient : public brillo::DBusDaemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070044 public:
45 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {}
46 ~UpdateEngineClient() override = default;
47
48 protected:
49 int OnInit() override {
50 int ret = DBusDaemon::OnInit();
51 if (ret != EX_OK)
52 return ret;
53 if (!InitProxy())
54 return 1;
Alex Deymo13e0dd62015-06-30 23:11:44 -070055 // Wait for the UpdateEngine to be available or timeout.
56 proxy_->GetObjectProxy()->WaitForServiceToBeAvailable(
57 base::Bind(&UpdateEngineClient::OnServiceAvailable,
58 base::Unretained(this)));
59 base::MessageLoop::current()->PostDelayedTask(
60 FROM_HERE,
61 base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout,
62 base::Unretained(this)),
63 base::TimeDelta::FromSeconds(10));
Alex Deymo72aa0022015-06-19 21:16:49 -070064 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070065 }
Alex Deymo72aa0022015-06-19 21:16:49 -070066
67 private:
68 bool InitProxy();
69
Alex Deymo13e0dd62015-06-30 23:11:44 -070070 // Callback called when the UpdateEngine service becomes available.
71 void OnServiceAvailable(bool service_is_available);
72
73 // Callback called when the UpdateEngine service doesn't become available
74 // after a timeout.
75 void OnServiceAvailableTimeout();
76
77
Alex Deymo72aa0022015-06-19 21:16:49 -070078 // Callback called when a StatusUpdate signal is received.
79 void OnStatusUpdateSignal(int64_t last_checked_time,
80 double progress,
81 const string& current_operation,
82 const string& new_version,
83 int64_t new_size);
84 // Callback called when the OnStatusUpdateSignal() handler is registered.
85 void OnStatusUpdateSignalRegistration(const string& interface,
86 const string& signal_name,
87 bool success);
88
89 // Registers a callback that prints on stderr the received StatusUpdate
90 // signals.
91 // The daemon should continue running for this to work.
92 void WatchForUpdates();
93
94 void ResetStatus();
95
96 // Show the status of the update engine in stdout.
97 // Blocking call. Exits the program with error 1 in case of an error.
98 void ShowStatus();
99
100 // Return the current operation status, such as UPDATE_STATUS_IDLE.
101 // Blocking call. Exits the program with error 1 in case of an error.
102 string GetCurrentOperation();
103
104 void Rollback(bool rollback);
105 string GetRollbackPartition();
Alex Deymo72aa0022015-06-19 21:16:49 -0700106 void CheckForUpdates(const string& app_version,
107 const string& omaha_url,
108 bool interactive);
109
110 // Reboot the device if a reboot is needed.
111 // Blocking call. Ignores failures.
112 void RebootIfNeeded();
113
114 // Getter and setter for the target channel. If |get_current_channel| is true,
115 // the current channel instead of the target channel will be returned.
116 // Blocking call. Exits the program with error 1 in case of an error.
117 void SetTargetChannel(const string& target_channel, bool allow_powerwash);
118 string GetChannel(bool get_current_channel);
119
120 // Getter and setter for the updates over cellular connections.
121 // Blocking call. Exits the program with error 1 in case of an error.
122 void SetUpdateOverCellularPermission(bool allowed);
123 bool GetUpdateOverCellularPermission();
124
125 // Getter and setter for the updates from P2P permission.
126 // Blocking call. Exits the program with error 1 in case of an error.
127 void SetP2PUpdatePermission(bool enabled);
128 bool GetP2PUpdatePermission();
129
130 // This is similar to watching for updates but rather than registering
131 // a signal watch, actively poll the daemon just in case it stops
132 // sending notifications.
133 void WaitForUpdateComplete();
134 void OnUpdateCompleteCheck(int64_t last_checked_time,
135 double progress,
136 const string& current_operation,
137 const string& new_version,
138 int64_t new_size);
139
140 // Blocking call. Exits the program with error 1 in case of an error.
141 void ShowPrevVersion();
142
143 // Returns whether the current status is such that a reboot is needed.
144 // Blocking call. Exits the program with error 1 in case of an error.
145 bool GetIsRebootNeeded();
146
147 // Blocks until a reboot is needed. If the reboot is needed, exits the program
148 // with 0. Otherwise it exits the program with 1 if an error occurs before
149 // the reboot is needed.
150 void WaitForRebootNeeded();
151 void OnRebootNeededCheck(int64_t last_checked_time,
152 double progress,
153 const string& current_operation,
154 const string& new_version,
155 int64_t new_size);
156 // Callback called when the OnRebootNeededCheck() handler is registered. This
157 // is useful to check at this point if the reboot is needed, without loosing
158 // any StatusUpdate signals and avoiding the race condition.
159 void OnRebootNeededCheckRegistration(const string& interface,
160 const string& signal_name,
161 bool success);
162
163 // Main method that parses and triggers all the actions based on the passed
164 // flags.
165 int ProcessFlags();
166
167 // DBus Proxy to the update_engine daemon object used for all the calls.
168 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
169
170 // Copy of argc and argv passed to main().
171 int argc_;
172 char** argv_;
173
Alex Deymo13e0dd62015-06-30 23:11:44 -0700174 // Tell whether the UpdateEngine service is available after startup.
175 bool service_is_available_{false};
176
Alex Deymo72aa0022015-06-19 21:16:49 -0700177 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
178};
179
Alex Deymo72aa0022015-06-19 21:16:49 -0700180bool UpdateEngineClient::InitProxy() {
181 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
182
183 if (!proxy_->GetObjectProxy()) {
184 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
185 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800186 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700187 return true;
188}
189
Alex Deymo13e0dd62015-06-30 23:11:44 -0700190void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
191 service_is_available_ = service_is_available;
192 if (!service_is_available) {
193 LOG(ERROR) << "UpdateEngineService not available.";
194 QuitWithExitCode(-1);
195 }
196 int ret = ProcessFlags();
197 if (ret != kContinueRunning)
198 QuitWithExitCode(ret);
199}
200
201void UpdateEngineClient::OnServiceAvailableTimeout() {
202 if (!service_is_available_) {
203 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
204 "daemon running?";
205 QuitWithExitCode(-1);
206 }
207}
208
Alex Deymo72aa0022015-06-19 21:16:49 -0700209void UpdateEngineClient::OnStatusUpdateSignal(
210 int64_t last_checked_time,
211 double progress,
212 const string& current_operation,
213 const string& new_version,
214 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700215 LOG(INFO) << "Got status update:";
216 LOG(INFO) << " last_checked_time: " << last_checked_time;
217 LOG(INFO) << " progress: " << progress;
218 LOG(INFO) << " current_operation: " << current_operation;
219 LOG(INFO) << " new_version: " << new_version;
220 LOG(INFO) << " new_size: " << new_size;
221}
222
Alex Deymo72aa0022015-06-19 21:16:49 -0700223void UpdateEngineClient::OnStatusUpdateSignalRegistration(
224 const string& interface,
225 const string& signal_name,
226 bool success) {
227 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
228 << signal_name << ", " << success << ");";
229 if (!success) {
230 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
231 exit(1);
232 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700233}
234
Alex Deymo72aa0022015-06-19 21:16:49 -0700235void UpdateEngineClient::WatchForUpdates() {
236 proxy_->RegisterStatusUpdateSignalHandler(
237 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
238 base::Unretained(this)),
239 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
240 base::Unretained(this)));
241}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700242
Alex Deymo72aa0022015-06-19 21:16:49 -0700243void UpdateEngineClient::ResetStatus() {
244 bool ret = proxy_->ResetStatus(nullptr);
245 CHECK(ret) << "ResetStatus() failed.";
246}
Andrew de los Reyesada42202010-07-15 22:23:20 -0700247
Alex Deymo72aa0022015-06-19 21:16:49 -0700248void UpdateEngineClient::ShowStatus() {
249 int64_t last_checked_time = 0;
250 double progress = 0.0;
251 string current_op;
252 string new_version;
253 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700254
Alex Deymo72aa0022015-06-19 21:16:49 -0700255 bool ret = proxy_->GetStatus(
256 &last_checked_time, &progress, &current_op, &new_version, &new_size,
257 nullptr);
258 CHECK(ret) << "GetStatus() failed";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700259 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
260 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
261 last_checked_time,
262 progress,
Alex Deymo72aa0022015-06-19 21:16:49 -0700263 current_op.c_str(),
264 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700265 new_size);
Alex Deymo72aa0022015-06-19 21:16:49 -0700266}
267
268string UpdateEngineClient::GetCurrentOperation() {
269 int64_t last_checked_time = 0;
270 double progress = 0.0;
271 string current_op;
272 string new_version;
273 int64_t new_size = 0;
274
275 bool ret = proxy_->GetStatus(
276 &last_checked_time, &progress, &current_op, &new_version, &new_size,
277 nullptr);
278 CHECK(ret) << "GetStatus() failed";
279 return current_op;
280}
281
282void UpdateEngineClient::Rollback(bool rollback) {
283 bool ret = proxy_->AttemptRollback(rollback, nullptr);
284 CHECK(ret) << "Rollback request failed.";
285}
286
287string UpdateEngineClient::GetRollbackPartition() {
288 string rollback_partition;
289 bool ret = proxy_->GetRollbackPartition(&rollback_partition, nullptr);
290 CHECK(ret) << "Error while querying rollback partition availabilty.";
291 return rollback_partition;
292}
293
Alex Deymo72aa0022015-06-19 21:16:49 -0700294void UpdateEngineClient::CheckForUpdates(const string& app_version,
295 const string& omaha_url,
296 bool interactive) {
297 int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
298 bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
299 nullptr);
300 CHECK(ret) << "Error checking for update.";
301}
302
303void UpdateEngineClient::RebootIfNeeded() {
304 bool ret = proxy_->RebootIfNeeded(nullptr);
305 if (!ret) {
306 // Reboot error code doesn't necessarily mean that a reboot
307 // failed. For example, D-Bus may be shutdown before we receive the
308 // result.
309 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700310 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700311}
312
Alex Deymo72aa0022015-06-19 21:16:49 -0700313void UpdateEngineClient::SetTargetChannel(const string& target_channel,
314 bool allow_powerwash) {
315 bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
316 CHECK(ret) << "Error setting the channel.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700317 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700318}
319
Alex Deymo72aa0022015-06-19 21:16:49 -0700320string UpdateEngineClient::GetChannel(bool get_current_channel) {
321 string channel;
322 bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
323 CHECK(ret) << "Error getting the channel.";
324 return channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900325}
326
Alex Deymo72aa0022015-06-19 21:16:49 -0700327void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
328 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
329 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700330}
331
Alex Deymo72aa0022015-06-19 21:16:49 -0700332bool UpdateEngineClient::GetUpdateOverCellularPermission() {
333 bool allowed;
334 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
335 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700336 return allowed;
337}
338
Alex Deymo72aa0022015-06-19 21:16:49 -0700339void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
340 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
341 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700342}
343
Alex Deymo72aa0022015-06-19 21:16:49 -0700344bool UpdateEngineClient::GetP2PUpdatePermission() {
345 bool enabled;
346 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
347 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700348 return enabled;
349}
350
Alex Deymo72aa0022015-06-19 21:16:49 -0700351void UpdateEngineClient::OnUpdateCompleteCheck(
352 int64_t /* last_checked_time */,
353 double /* progress */,
354 const string& current_operation,
355 const string& /* new_version */,
356 int64_t /* new_size */) {
357 if (current_operation == update_engine::kUpdateStatusIdle) {
358 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700359 exit(1);
360 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700361 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700362 LOG(INFO) << "Update succeeded -- reboot needed.";
363 exit(0);
364 }
Darin Petkov58529db2010-08-13 09:19:47 -0700365}
366
Alex Deymo72aa0022015-06-19 21:16:49 -0700367void UpdateEngineClient::WaitForUpdateComplete() {
368 proxy_->RegisterStatusUpdateSignalHandler(
369 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
370 base::Unretained(this)),
371 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
372 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700373}
374
Alex Deymo72aa0022015-06-19 21:16:49 -0700375void UpdateEngineClient::ShowPrevVersion() {
376 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700377
Alex Deymo72aa0022015-06-19 21:16:49 -0700378 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
379 if (!ret) {
380 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700381 } else {
382 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700383 }
384}
385
Alex Deymo72aa0022015-06-19 21:16:49 -0700386bool UpdateEngineClient::GetIsRebootNeeded() {
387 return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
David Zeuthen9d73a722014-04-04 14:52:46 -0700388}
389
Alex Deymo72aa0022015-06-19 21:16:49 -0700390void UpdateEngineClient::OnRebootNeededCheck(
391 int64_t /* last_checked_time */,
392 double /* progress */,
393 const string& current_operation,
394 const string& /* new_version */,
395 int64_t /* new_size */) {
396 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
397 LOG(INFO) << "Reboot needed.";
398 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700399 }
400}
401
Alex Deymo72aa0022015-06-19 21:16:49 -0700402void UpdateEngineClient::OnRebootNeededCheckRegistration(
403 const string& interface,
404 const string& signal_name,
405 bool success) {
406 if (GetIsRebootNeeded())
407 exit(0);
408 if (!success) {
409 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
410 exit(1);
411 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700412}
413
414// Blocks until a reboot is needed. Returns true if waiting succeeded,
415// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700416void UpdateEngineClient::WaitForRebootNeeded() {
417 proxy_->RegisterStatusUpdateSignalHandler(
418 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
419 base::Unretained(this)),
420 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
421 base::Unretained(this)));
422 if (GetIsRebootNeeded())
423 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700424}
425
Alex Deymo72aa0022015-06-19 21:16:49 -0700426int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700427 DEFINE_string(app_version, "", "Force the current app version.");
428 DEFINE_string(channel, "",
429 "Set the target channel. The device will be powerwashed if the "
430 "target channel is more stable than the current channel unless "
431 "--nopowerwash is specified.");
432 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
433 DEFINE_bool(follow, false, "Wait for any update operations to complete."
434 "Exit status is 0 if the update succeeded, and 1 otherwise.");
435 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
436 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
437 DEFINE_string(p2p_update, "",
438 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
439 " sharing.");
440 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
441 "do a powerwash or allow it respectively.");
442 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
443 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
444 "2 if reboot is not needed or 1 if an error occurred.");
445 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
446 "needed. Returns non-zero exit status if an error occurred.");
447 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800448 DEFINE_bool(rollback, false,
449 "Perform a rollback to the previous partition. The device will "
450 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700451 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
452 "is available.");
453 DEFINE_bool(show_channel, false, "Show the current and target channels.");
454 DEFINE_bool(show_p2p_update, false,
455 "Show the current setting for peer-to-peer update sharing.");
456 DEFINE_bool(show_update_over_cellular, false,
457 "Show the current setting for updates over cellular networks.");
458 DEFINE_bool(status, false, "Print the status to stdout.");
459 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
460 "Implies --follow.");
461 DEFINE_string(update_over_cellular, "",
462 "Enables (\"yes\") or disables (\"no\") the updates over "
463 "cellular networks.");
464 DEFINE_bool(watch_for_updates, false,
465 "Listen for status updates and print them to the screen.");
466 DEFINE_bool(prev_version, false,
467 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700468
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_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700471 brillo::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
David Zeuthen9d73a722014-04-04 14:52:46 -0700631 if (FLAGS_is_reboot_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700632 // In case of error GetIsRebootNeeded() will exit with 1.
633 if (GetIsRebootNeeded()) {
634 return 0;
635 } else {
David Zeuthen9d73a722014-04-04 14:52:46 -0700636 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700637 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700638 }
639
640 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700641 WaitForRebootNeeded();
642 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700643 }
644
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700645 return 0;
646}
Alex Deymo72aa0022015-06-19 21:16:49 -0700647
648} // namespace
649
650int main(int argc, char** argv) {
651 UpdateEngineClient client(argc, argv);
652 return client.Run();
653}