blob: 9294d366e07c5c88f11131872ef69ae7e5ccbc95 [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
Casey Dahline844c1a2015-12-16 14:30:58 -080021#include <memory>
Darin Petkov5a7f5652010-07-22 21:40:09 -070022#include <string>
23
Alex Deymo72aa0022015-06-19 21:16:49 -070024#include <base/bind.h>
Alex Deymo8ce80d62015-01-27 15:10:43 -080025#include <base/command_line.h>
Alex Deymo44666f92014-07-22 20:29:24 -070026#include <base/logging.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070027#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070028#include <brillo/daemons/dbus_daemon.h>
29#include <brillo/flag_helper.h>
Alex Deymo72aa0022015-06-19 21:16:49 -070030#include <dbus/bus.h>
Casey Dahlin87ab88e2015-12-16 17:58:05 -080031
32#include "update_engine/client.h"
33#include "update_engine/update_status.h"
34#include "update_engine/dbus-constants.h"
35#include "update_engine/dbus-proxies.h"
36#include "update_status_utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070037
Darin Petkov5a7f5652010-07-22 21:40:09 -070038using std::string;
Casey Dahline844c1a2015-12-16 14:30:58 -080039using std::unique_ptr;
Alex Deymod6deb1d2015-08-28 15:54:37 -070040using update_engine::kAttemptUpdateFlagNonInteractive;
41using update_engine::kUpdateEngineServiceName;
Casey Dahlin87ab88e2015-12-16 17:58:05 -080042using chromeos_update_engine::UpdateStatusToString;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070043
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070044namespace {
45
Alex Deymo72aa0022015-06-19 21:16:49 -070046// Constant to signal that we need to continue running the daemon after
47// initialization.
48const int kContinueRunning = -1;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070049
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070050class UpdateEngineClient : public brillo::DBusDaemon {
Alex Deymo72aa0022015-06-19 21:16:49 -070051 public:
Casey Dahline844c1a2015-12-16 14:30:58 -080052 UpdateEngineClient(int argc, char** argv) : argc_(argc), argv_(argv) {
53 client_ = update_engine::UpdateEngineClient::CreateInstance();
54 }
55
Alex Deymo72aa0022015-06-19 21:16:49 -070056 ~UpdateEngineClient() override = default;
57
58 protected:
59 int OnInit() override {
60 int ret = DBusDaemon::OnInit();
61 if (ret != EX_OK)
62 return ret;
63 if (!InitProxy())
64 return 1;
Alex Deymo13e0dd62015-06-30 23:11:44 -070065 // Wait for the UpdateEngine to be available or timeout.
66 proxy_->GetObjectProxy()->WaitForServiceToBeAvailable(
67 base::Bind(&UpdateEngineClient::OnServiceAvailable,
68 base::Unretained(this)));
69 base::MessageLoop::current()->PostDelayedTask(
70 FROM_HERE,
71 base::Bind(&UpdateEngineClient::OnServiceAvailableTimeout,
72 base::Unretained(this)),
73 base::TimeDelta::FromSeconds(10));
Alex Deymo72aa0022015-06-19 21:16:49 -070074 return EX_OK;
Andrew de los Reyes68ab6ed2011-08-09 14:46:39 -070075 }
Alex Deymo72aa0022015-06-19 21:16:49 -070076
77 private:
78 bool InitProxy();
79
Alex Deymo13e0dd62015-06-30 23:11:44 -070080 // Callback called when the UpdateEngine service becomes available.
81 void OnServiceAvailable(bool service_is_available);
82
83 // Callback called when the UpdateEngine service doesn't become available
84 // after a timeout.
85 void OnServiceAvailableTimeout();
86
87
Alex Deymo72aa0022015-06-19 21:16:49 -070088 // Callback called when a StatusUpdate signal is received.
89 void OnStatusUpdateSignal(int64_t last_checked_time,
90 double progress,
91 const string& current_operation,
92 const string& new_version,
93 int64_t new_size);
94 // Callback called when the OnStatusUpdateSignal() handler is registered.
95 void OnStatusUpdateSignalRegistration(const string& interface,
96 const string& signal_name,
97 bool success);
98
99 // Registers a callback that prints on stderr the received StatusUpdate
100 // signals.
101 // The daemon should continue running for this to work.
102 void WatchForUpdates();
103
Alex Deymo72aa0022015-06-19 21:16:49 -0700104 // Show the status of the update engine in stdout.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800105 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -0700106
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800107 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
108 // occurred, 2 if no reboot is needed.
109 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -0700110
Alex Deymo72aa0022015-06-19 21:16:49 -0700111 // 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
Alex Deymo72aa0022015-06-19 21:16:49 -0700121 // Blocks until a reboot is needed. If the reboot is needed, exits the program
122 // with 0. Otherwise it exits the program with 1 if an error occurs before
123 // the reboot is needed.
124 void WaitForRebootNeeded();
125 void OnRebootNeededCheck(int64_t last_checked_time,
126 double progress,
127 const string& current_operation,
128 const string& new_version,
129 int64_t new_size);
130 // Callback called when the OnRebootNeededCheck() handler is registered. This
131 // is useful to check at this point if the reboot is needed, without loosing
132 // any StatusUpdate signals and avoiding the race condition.
133 void OnRebootNeededCheckRegistration(const string& interface,
134 const string& signal_name,
135 bool success);
136
137 // Main method that parses and triggers all the actions based on the passed
138 // flags.
139 int ProcessFlags();
140
141 // DBus Proxy to the update_engine daemon object used for all the calls.
142 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
143
144 // Copy of argc and argv passed to main().
145 int argc_;
146 char** argv_;
147
Casey Dahline844c1a2015-12-16 14:30:58 -0800148 // Library-based client
149 unique_ptr<update_engine::UpdateEngineClient> client_;
150
Alex Deymo13e0dd62015-06-30 23:11:44 -0700151 // Tell whether the UpdateEngine service is available after startup.
152 bool service_is_available_{false};
153
Alex Deymo72aa0022015-06-19 21:16:49 -0700154 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
155};
156
Alex Deymo72aa0022015-06-19 21:16:49 -0700157bool UpdateEngineClient::InitProxy() {
158 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
159
160 if (!proxy_->GetObjectProxy()) {
161 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
162 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800163 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700164 return true;
165}
166
Alex Deymo13e0dd62015-06-30 23:11:44 -0700167void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
168 service_is_available_ = service_is_available;
169 if (!service_is_available) {
170 LOG(ERROR) << "UpdateEngineService not available.";
171 QuitWithExitCode(-1);
172 }
173 int ret = ProcessFlags();
174 if (ret != kContinueRunning)
175 QuitWithExitCode(ret);
176}
177
178void UpdateEngineClient::OnServiceAvailableTimeout() {
179 if (!service_is_available_) {
180 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
181 "daemon running?";
182 QuitWithExitCode(-1);
183 }
184}
185
Alex Deymo72aa0022015-06-19 21:16:49 -0700186void UpdateEngineClient::OnStatusUpdateSignal(
187 int64_t last_checked_time,
188 double progress,
189 const string& current_operation,
190 const string& new_version,
191 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700192 LOG(INFO) << "Got status update:";
193 LOG(INFO) << " last_checked_time: " << last_checked_time;
194 LOG(INFO) << " progress: " << progress;
195 LOG(INFO) << " current_operation: " << current_operation;
196 LOG(INFO) << " new_version: " << new_version;
197 LOG(INFO) << " new_size: " << new_size;
198}
199
Alex Deymo72aa0022015-06-19 21:16:49 -0700200void UpdateEngineClient::OnStatusUpdateSignalRegistration(
201 const string& interface,
202 const string& signal_name,
203 bool success) {
204 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
205 << signal_name << ", " << success << ");";
206 if (!success) {
207 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
208 exit(1);
209 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700210}
211
Alex Deymo72aa0022015-06-19 21:16:49 -0700212void UpdateEngineClient::WatchForUpdates() {
213 proxy_->RegisterStatusUpdateSignalHandler(
214 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
215 base::Unretained(this)),
216 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
217 base::Unretained(this)));
218}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700219
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800220bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700221 int64_t last_checked_time = 0;
222 double progress = 0.0;
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800223 update_engine::UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700224 string new_version;
225 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700226
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800227 if (!client_->GetStatus(
228 &last_checked_time, &progress, &current_op, &new_version, &new_size)) {
229 return false;
230 }
231
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700232 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
233 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
234 last_checked_time,
235 progress,
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800236 UpdateStatusToString(current_op),
Alex Deymo72aa0022015-06-19 21:16:49 -0700237 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700238 new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800239
240 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700241}
242
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800243int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700244 int64_t last_checked_time = 0;
245 double progress = 0.0;
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800246 update_engine::UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700247 string new_version;
248 int64_t new_size = 0;
249
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800250 if (!client_->GetStatus(
251 &last_checked_time, &progress, &current_op, &new_version, &new_size)) {
252 return 1;
253 }
254
255 if (current_op == update_engine::UpdateStatus::UPDATED_NEED_REBOOT) {
256 return 0;
257 }
258
259 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700260}
261
Alex Deymo72aa0022015-06-19 21:16:49 -0700262void UpdateEngineClient::OnUpdateCompleteCheck(
263 int64_t /* last_checked_time */,
264 double /* progress */,
265 const string& current_operation,
266 const string& /* new_version */,
267 int64_t /* new_size */) {
268 if (current_operation == update_engine::kUpdateStatusIdle) {
269 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700270 exit(1);
271 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700272 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700273 LOG(INFO) << "Update succeeded -- reboot needed.";
274 exit(0);
275 }
Darin Petkov58529db2010-08-13 09:19:47 -0700276}
277
Alex Deymo72aa0022015-06-19 21:16:49 -0700278void UpdateEngineClient::WaitForUpdateComplete() {
279 proxy_->RegisterStatusUpdateSignalHandler(
280 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
281 base::Unretained(this)),
282 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
283 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700284}
285
Alex Deymo72aa0022015-06-19 21:16:49 -0700286void UpdateEngineClient::OnRebootNeededCheck(
287 int64_t /* last_checked_time */,
288 double /* progress */,
289 const string& current_operation,
290 const string& /* new_version */,
291 int64_t /* new_size */) {
292 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
293 LOG(INFO) << "Reboot needed.";
294 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700295 }
296}
297
Alex Deymo72aa0022015-06-19 21:16:49 -0700298void UpdateEngineClient::OnRebootNeededCheckRegistration(
299 const string& interface,
300 const string& signal_name,
301 bool success) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800302 int got = GetNeedReboot();
303
304 if (got == 1) {
305 LOG(ERROR) << "Could not query the current operation.";
306 exit(1);
307 }
308
309 if (got == 0) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700310 exit(0);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800311 }
312
Alex Deymo72aa0022015-06-19 21:16:49 -0700313 if (!success) {
314 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
315 exit(1);
316 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700317}
318
319// Blocks until a reboot is needed. Returns true if waiting succeeded,
320// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700321void UpdateEngineClient::WaitForRebootNeeded() {
322 proxy_->RegisterStatusUpdateSignalHandler(
323 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
324 base::Unretained(this)),
325 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
326 base::Unretained(this)));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800327 int got = GetNeedReboot();
328
329 if (got == 1) {
330 LOG(ERROR) << "Could not query the current operation.";
331 exit(1);
332 }
333
334 if (got == 0)
Alex Deymo72aa0022015-06-19 21:16:49 -0700335 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700336}
337
Alex Deymo72aa0022015-06-19 21:16:49 -0700338int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700339 DEFINE_string(app_version, "", "Force the current app version.");
340 DEFINE_string(channel, "",
341 "Set the target channel. The device will be powerwashed if the "
342 "target channel is more stable than the current channel unless "
343 "--nopowerwash is specified.");
344 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
345 DEFINE_bool(follow, false, "Wait for any update operations to complete."
346 "Exit status is 0 if the update succeeded, and 1 otherwise.");
347 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
348 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
349 DEFINE_string(p2p_update, "",
350 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
351 " sharing.");
352 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
353 "do a powerwash or allow it respectively.");
354 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
355 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
356 "2 if reboot is not needed or 1 if an error occurred.");
357 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
358 "needed. Returns non-zero exit status if an error occurred.");
359 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800360 DEFINE_bool(rollback, false,
361 "Perform a rollback to the previous partition. The device will "
362 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700363 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
364 "is available.");
365 DEFINE_bool(show_channel, false, "Show the current and target channels.");
366 DEFINE_bool(show_p2p_update, false,
367 "Show the current setting for peer-to-peer update sharing.");
368 DEFINE_bool(show_update_over_cellular, false,
369 "Show the current setting for updates over cellular networks.");
370 DEFINE_bool(status, false, "Print the status to stdout.");
371 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
372 "Implies --follow.");
373 DEFINE_string(update_over_cellular, "",
374 "Enables (\"yes\") or disables (\"no\") the updates over "
375 "cellular networks.");
376 DEFINE_bool(watch_for_updates, false,
377 "Listen for status updates and print them to the screen.");
378 DEFINE_bool(prev_version, false,
379 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700380
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700381 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700382 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700383 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700384
Alex Deymo8ce80d62015-01-27 15:10:43 -0800385 // Ensure there are no positional arguments.
386 const std::vector<string> positional_args =
387 base::CommandLine::ForCurrentProcess()->GetArgs();
388 if (!positional_args.empty()) {
389 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
390 << "'. If you want to pass a value to a flag, pass it as "
391 "--flag=value.";
392 return 1;
393 }
394
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700395 // Update the status if requested.
396 if (FLAGS_reset_status) {
397 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800398
399 if (client_->ResetStatus()) {
400 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
401 "run:\n"
402 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
403 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
404 } else {
405 LOG(ERROR) << "ResetStatus failed";
406 return 1;
407 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700408 }
Darin Petkov58529db2010-08-13 09:19:47 -0700409
Alex Deymof4867c42013-06-28 14:41:39 -0700410 // Changes the current update over cellular network setting.
411 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700412 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700413 if (!allowed && FLAGS_update_over_cellular != "no") {
414 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
415 << "\". Please specify \"yes\" or \"no\".";
416 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800417 if(!client_->SetUpdateOverCellularPermission(allowed)) {
418 LOG(ERROR) << "Error setting the update over cellular setting.";
419 return 1;
420 }
Alex Deymof4867c42013-06-28 14:41:39 -0700421 }
422 }
423
424 // Show the current update over cellular network setting.
425 if (FLAGS_show_update_over_cellular) {
Casey Dahlinef361132015-12-17 13:02:37 -0800426 bool allowed;
427
428 if (!client_->GetUpdateOverCellularPermission(&allowed)) {
429 LOG(ERROR) << "Error getting the update over cellular setting.";
430 return 1;
431 }
432
Alex Deymof4867c42013-06-28 14:41:39 -0700433 LOG(INFO) << "Current update over cellular network setting: "
434 << (allowed ? "ENABLED" : "DISABLED");
435 }
436
Chris Sosacb7fa882013-07-25 17:02:59 -0700437 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700438 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700439 return 1;
440 }
441
Alex Deymo5fdf7762013-07-17 20:01:40 -0700442 // Change the P2P enabled setting.
443 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700444 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700445 if (!enabled && FLAGS_p2p_update != "no") {
446 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
447 << "\". Please specify \"yes\" or \"no\".";
448 } else {
Casey Dahlinef361132015-12-17 13:02:37 -0800449 if (!client_->SetP2PUpdatePermission(enabled)) {
450 LOG(ERROR) << "Error setting the peer-to-peer update setting.";
451 return 1;
452 }
Alex Deymo5fdf7762013-07-17 20:01:40 -0700453 }
454 }
455
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800456 // Show the rollback availability.
457 if (FLAGS_can_rollback) {
Casey Dahlinef361132015-12-17 13:02:37 -0800458 string rollback_partition;
459
460 if (!client_->GetRollbackPartition(&rollback_partition)) {
461 LOG(ERROR) << "Error while querying rollback partition availabilty.";
462 return 1;
463 }
464
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700465 bool can_rollback = true;
466 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700467 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700468 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700469 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700470 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700471 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700472
473 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700474 if (!can_rollback) {
475 return 1;
476 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800477 }
478
Alex Deymo5fdf7762013-07-17 20:01:40 -0700479 // Show the current P2P enabled setting.
480 if (FLAGS_show_p2p_update) {
Casey Dahlinef361132015-12-17 13:02:37 -0800481 bool enabled;
482
483 if (!client_->GetP2PUpdatePermission(&enabled)) {
484 LOG(ERROR) << "Error getting the peer-to-peer update setting.";
485 return 1;
486 }
487
Alex Deymo5fdf7762013-07-17 20:01:40 -0700488 LOG(INFO) << "Current update using P2P setting: "
489 << (enabled ? "ENABLED" : "DISABLED");
490 }
491
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700492 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800493 if (!FLAGS_channel.empty()) {
494 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
495 LOG(ERROR) << "Error setting the channel.";
496 return 1;
497 }
498
499 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
500 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700501
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700502 // Show the current and target channels if requested.
503 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800504 string current_channel;
505 string target_channel;
506
507 if (!client_->GetChannel(&current_channel)) {
508 LOG(ERROR) << "Error getting the current channel.";
509 return 1;
510 }
511
512 if (!client_->GetTargetChannel(&target_channel)) {
513 LOG(ERROR) << "Error getting the target channel.";
514 return 1;
515 }
516
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700517 LOG(INFO) << "Current Channel: " << current_channel;
518
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700519 if (!target_channel.empty())
520 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900521 }
522
Chris Sosad317e402013-06-12 13:47:09 -0700523 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
524 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700525 if (FLAGS_update)
526 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700527
Chris Sosad317e402013-06-12 13:47:09 -0700528 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700529 LOG(ERROR) << "Incompatible flags specified with rollback."
530 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700531 return 1;
532 }
533
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700534 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700535 LOG(INFO) << "Requesting rollback.";
Casey Dahlinef361132015-12-17 13:02:37 -0800536 if (!client_->Rollback(FLAGS_powerwash)) {
537 LOG(ERROR) << "Rollback request failed.";
538 return 1;
539 }
Chris Sosad317e402013-06-12 13:47:09 -0700540 }
541
Darin Petkov58529db2010-08-13 09:19:47 -0700542 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700543 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700544 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700545 string app_version = FLAGS_app_version;
546 if (FLAGS_update && app_version.empty()) {
547 app_version = "ForcedUpdate";
548 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700549 }
Darin Petkov58529db2010-08-13 09:19:47 -0700550 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800551 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
552 FLAGS_interactive)) {
553 LOG(ERROR) << "Error checking for update.";
554 return 1;
555 }
Chris Sosa192449e2013-10-28 14:16:19 -0700556 }
Darin Petkov58529db2010-08-13 09:19:47 -0700557
Chris Sosa192449e2013-10-28 14:16:19 -0700558 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700559 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
560 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700561 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700562 LOG(ERROR) << "Multiple exclusive options selected. "
563 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700564 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700565 << "or --status.";
566 return 1;
567 }
568
569 if (FLAGS_status) {
570 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800571 if (!ShowStatus()) {
572 LOG(ERROR) << "Failed to query status";
573 return 1;
574 }
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...";
Casey Dahlinef361132015-12-17 13:02:37 -0800592 client_->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) {
Casey Dahlinef361132015-12-17 13:02:37 -0800597 string prev_version;
598
599 if (!client_->GetPrevVersion(&prev_version)) {
600 LOG(ERROR) << "Error getting previous version.";
601 } else {
602 LOG(INFO) << "Previous version = " << prev_version;
603 }
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700604 }
605
David Zeuthen9d73a722014-04-04 14:52:46 -0700606 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800607 int ret = GetNeedReboot();
608
609 if (ret == 1) {
610 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700611 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800612
613 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700614 }
615
616 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700617 WaitForRebootNeeded();
618 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700619 }
620
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700621 return 0;
622}
Alex Deymo72aa0022015-06-19 21:16:49 -0700623
624} // namespace
625
626int main(int argc, char** argv) {
627 UpdateEngineClient client(argc, argv);
628 return client.Run();
629}