blob: 52c2cd2e2beca7bb0aa42e0f49946263f5af85ce [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.
105 // Blocking call. Exits the program with error 1 in case of an error.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800106 bool ShowStatus();
Alex Deymo72aa0022015-06-19 21:16:49 -0700107
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800108 // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
109 // occurred, 2 if no reboot is needed.
110 int GetNeedReboot();
Alex Deymo72aa0022015-06-19 21:16:49 -0700111
112 void Rollback(bool rollback);
113 string GetRollbackPartition();
Alex Deymo72aa0022015-06-19 21:16:49 -0700114
115 // Reboot the device if a reboot is needed.
116 // Blocking call. Ignores failures.
117 void RebootIfNeeded();
118
Alex Deymo72aa0022015-06-19 21:16:49 -0700119 // Getter and setter for the updates over cellular connections.
120 // Blocking call. Exits the program with error 1 in case of an error.
121 void SetUpdateOverCellularPermission(bool allowed);
122 bool GetUpdateOverCellularPermission();
123
124 // Getter and setter for the updates from P2P permission.
125 // Blocking call. Exits the program with error 1 in case of an error.
126 void SetP2PUpdatePermission(bool enabled);
127 bool GetP2PUpdatePermission();
128
129 // This is similar to watching for updates but rather than registering
130 // a signal watch, actively poll the daemon just in case it stops
131 // sending notifications.
132 void WaitForUpdateComplete();
133 void OnUpdateCompleteCheck(int64_t last_checked_time,
134 double progress,
135 const string& current_operation,
136 const string& new_version,
137 int64_t new_size);
138
139 // Blocking call. Exits the program with error 1 in case of an error.
140 void ShowPrevVersion();
141
Alex Deymo72aa0022015-06-19 21:16:49 -0700142 // Blocks until a reboot is needed. If the reboot is needed, exits the program
143 // with 0. Otherwise it exits the program with 1 if an error occurs before
144 // the reboot is needed.
145 void WaitForRebootNeeded();
146 void OnRebootNeededCheck(int64_t last_checked_time,
147 double progress,
148 const string& current_operation,
149 const string& new_version,
150 int64_t new_size);
151 // Callback called when the OnRebootNeededCheck() handler is registered. This
152 // is useful to check at this point if the reboot is needed, without loosing
153 // any StatusUpdate signals and avoiding the race condition.
154 void OnRebootNeededCheckRegistration(const string& interface,
155 const string& signal_name,
156 bool success);
157
158 // Main method that parses and triggers all the actions based on the passed
159 // flags.
160 int ProcessFlags();
161
162 // DBus Proxy to the update_engine daemon object used for all the calls.
163 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
164
165 // Copy of argc and argv passed to main().
166 int argc_;
167 char** argv_;
168
Casey Dahline844c1a2015-12-16 14:30:58 -0800169 // Library-based client
170 unique_ptr<update_engine::UpdateEngineClient> client_;
171
Alex Deymo13e0dd62015-06-30 23:11:44 -0700172 // Tell whether the UpdateEngine service is available after startup.
173 bool service_is_available_{false};
174
Alex Deymo72aa0022015-06-19 21:16:49 -0700175 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
176};
177
Alex Deymo72aa0022015-06-19 21:16:49 -0700178bool UpdateEngineClient::InitProxy() {
179 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
180
181 if (!proxy_->GetObjectProxy()) {
182 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
183 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800184 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700185 return true;
186}
187
Alex Deymo13e0dd62015-06-30 23:11:44 -0700188void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
189 service_is_available_ = service_is_available;
190 if (!service_is_available) {
191 LOG(ERROR) << "UpdateEngineService not available.";
192 QuitWithExitCode(-1);
193 }
194 int ret = ProcessFlags();
195 if (ret != kContinueRunning)
196 QuitWithExitCode(ret);
197}
198
199void UpdateEngineClient::OnServiceAvailableTimeout() {
200 if (!service_is_available_) {
201 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
202 "daemon running?";
203 QuitWithExitCode(-1);
204 }
205}
206
Alex Deymo72aa0022015-06-19 21:16:49 -0700207void UpdateEngineClient::OnStatusUpdateSignal(
208 int64_t last_checked_time,
209 double progress,
210 const string& current_operation,
211 const string& new_version,
212 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700213 LOG(INFO) << "Got status update:";
214 LOG(INFO) << " last_checked_time: " << last_checked_time;
215 LOG(INFO) << " progress: " << progress;
216 LOG(INFO) << " current_operation: " << current_operation;
217 LOG(INFO) << " new_version: " << new_version;
218 LOG(INFO) << " new_size: " << new_size;
219}
220
Alex Deymo72aa0022015-06-19 21:16:49 -0700221void UpdateEngineClient::OnStatusUpdateSignalRegistration(
222 const string& interface,
223 const string& signal_name,
224 bool success) {
225 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
226 << signal_name << ", " << success << ");";
227 if (!success) {
228 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
229 exit(1);
230 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700231}
232
Alex Deymo72aa0022015-06-19 21:16:49 -0700233void UpdateEngineClient::WatchForUpdates() {
234 proxy_->RegisterStatusUpdateSignalHandler(
235 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
236 base::Unretained(this)),
237 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
238 base::Unretained(this)));
239}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700240
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800241bool UpdateEngineClient::ShowStatus() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700242 int64_t last_checked_time = 0;
243 double progress = 0.0;
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800244 update_engine::UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700245 string new_version;
246 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700247
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800248 if (!client_->GetStatus(
249 &last_checked_time, &progress, &current_op, &new_version, &new_size)) {
250 return false;
251 }
252
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700253 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
254 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
255 last_checked_time,
256 progress,
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800257 UpdateStatusToString(current_op),
Alex Deymo72aa0022015-06-19 21:16:49 -0700258 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700259 new_size);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800260
261 return true;
Alex Deymo72aa0022015-06-19 21:16:49 -0700262}
263
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800264int UpdateEngineClient::GetNeedReboot() {
Alex Deymo72aa0022015-06-19 21:16:49 -0700265 int64_t last_checked_time = 0;
266 double progress = 0.0;
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800267 update_engine::UpdateStatus current_op;
Alex Deymo72aa0022015-06-19 21:16:49 -0700268 string new_version;
269 int64_t new_size = 0;
270
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800271 if (!client_->GetStatus(
272 &last_checked_time, &progress, &current_op, &new_version, &new_size)) {
273 return 1;
274 }
275
276 if (current_op == update_engine::UpdateStatus::UPDATED_NEED_REBOOT) {
277 return 0;
278 }
279
280 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700281}
282
283void UpdateEngineClient::Rollback(bool rollback) {
284 bool ret = proxy_->AttemptRollback(rollback, nullptr);
285 CHECK(ret) << "Rollback request failed.";
286}
287
288string UpdateEngineClient::GetRollbackPartition() {
289 string rollback_partition;
290 bool ret = proxy_->GetRollbackPartition(&rollback_partition, nullptr);
291 CHECK(ret) << "Error while querying rollback partition availabilty.";
292 return rollback_partition;
293}
294
Alex Deymo72aa0022015-06-19 21:16:49 -0700295void UpdateEngineClient::RebootIfNeeded() {
296 bool ret = proxy_->RebootIfNeeded(nullptr);
297 if (!ret) {
298 // Reboot error code doesn't necessarily mean that a reboot
299 // failed. For example, D-Bus may be shutdown before we receive the
300 // result.
301 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700302 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700303}
304
Alex Deymo72aa0022015-06-19 21:16:49 -0700305void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
306 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
307 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700308}
309
Alex Deymo72aa0022015-06-19 21:16:49 -0700310bool UpdateEngineClient::GetUpdateOverCellularPermission() {
311 bool allowed;
312 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
313 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700314 return allowed;
315}
316
Alex Deymo72aa0022015-06-19 21:16:49 -0700317void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
318 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
319 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700320}
321
Alex Deymo72aa0022015-06-19 21:16:49 -0700322bool UpdateEngineClient::GetP2PUpdatePermission() {
323 bool enabled;
324 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
325 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700326 return enabled;
327}
328
Alex Deymo72aa0022015-06-19 21:16:49 -0700329void UpdateEngineClient::OnUpdateCompleteCheck(
330 int64_t /* last_checked_time */,
331 double /* progress */,
332 const string& current_operation,
333 const string& /* new_version */,
334 int64_t /* new_size */) {
335 if (current_operation == update_engine::kUpdateStatusIdle) {
336 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700337 exit(1);
338 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700339 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700340 LOG(INFO) << "Update succeeded -- reboot needed.";
341 exit(0);
342 }
Darin Petkov58529db2010-08-13 09:19:47 -0700343}
344
Alex Deymo72aa0022015-06-19 21:16:49 -0700345void UpdateEngineClient::WaitForUpdateComplete() {
346 proxy_->RegisterStatusUpdateSignalHandler(
347 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
348 base::Unretained(this)),
349 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
350 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700351}
352
Alex Deymo72aa0022015-06-19 21:16:49 -0700353void UpdateEngineClient::ShowPrevVersion() {
354 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700355
Alex Deymo72aa0022015-06-19 21:16:49 -0700356 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
357 if (!ret) {
358 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700359 } else {
360 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700361 }
362}
363
Alex Deymo72aa0022015-06-19 21:16:49 -0700364void UpdateEngineClient::OnRebootNeededCheck(
365 int64_t /* last_checked_time */,
366 double /* progress */,
367 const string& current_operation,
368 const string& /* new_version */,
369 int64_t /* new_size */) {
370 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
371 LOG(INFO) << "Reboot needed.";
372 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700373 }
374}
375
Alex Deymo72aa0022015-06-19 21:16:49 -0700376void UpdateEngineClient::OnRebootNeededCheckRegistration(
377 const string& interface,
378 const string& signal_name,
379 bool success) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800380 int got = GetNeedReboot();
381
382 if (got == 1) {
383 LOG(ERROR) << "Could not query the current operation.";
384 exit(1);
385 }
386
387 if (got == 0) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700388 exit(0);
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800389 }
390
Alex Deymo72aa0022015-06-19 21:16:49 -0700391 if (!success) {
392 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
393 exit(1);
394 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700395}
396
397// Blocks until a reboot is needed. Returns true if waiting succeeded,
398// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700399void UpdateEngineClient::WaitForRebootNeeded() {
400 proxy_->RegisterStatusUpdateSignalHandler(
401 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
402 base::Unretained(this)),
403 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
404 base::Unretained(this)));
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800405 int got = GetNeedReboot();
406
407 if (got == 1) {
408 LOG(ERROR) << "Could not query the current operation.";
409 exit(1);
410 }
411
412 if (got == 0)
Alex Deymo72aa0022015-06-19 21:16:49 -0700413 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700414}
415
Alex Deymo72aa0022015-06-19 21:16:49 -0700416int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700417 DEFINE_string(app_version, "", "Force the current app version.");
418 DEFINE_string(channel, "",
419 "Set the target channel. The device will be powerwashed if the "
420 "target channel is more stable than the current channel unless "
421 "--nopowerwash is specified.");
422 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
423 DEFINE_bool(follow, false, "Wait for any update operations to complete."
424 "Exit status is 0 if the update succeeded, and 1 otherwise.");
425 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
426 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
427 DEFINE_string(p2p_update, "",
428 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
429 " sharing.");
430 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
431 "do a powerwash or allow it respectively.");
432 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
433 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
434 "2 if reboot is not needed or 1 if an error occurred.");
435 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
436 "needed. Returns non-zero exit status if an error occurred.");
437 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800438 DEFINE_bool(rollback, false,
439 "Perform a rollback to the previous partition. The device will "
440 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700441 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
442 "is available.");
443 DEFINE_bool(show_channel, false, "Show the current and target channels.");
444 DEFINE_bool(show_p2p_update, false,
445 "Show the current setting for peer-to-peer update sharing.");
446 DEFINE_bool(show_update_over_cellular, false,
447 "Show the current setting for updates over cellular networks.");
448 DEFINE_bool(status, false, "Print the status to stdout.");
449 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
450 "Implies --follow.");
451 DEFINE_string(update_over_cellular, "",
452 "Enables (\"yes\") or disables (\"no\") the updates over "
453 "cellular networks.");
454 DEFINE_bool(watch_for_updates, false,
455 "Listen for status updates and print them to the screen.");
456 DEFINE_bool(prev_version, false,
457 "Show the previous OS version used before the update reboot.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700458
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700459 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700460 base::CommandLine::Init(argc_, argv_);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700461 brillo::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700462
Alex Deymo8ce80d62015-01-27 15:10:43 -0800463 // Ensure there are no positional arguments.
464 const std::vector<string> positional_args =
465 base::CommandLine::ForCurrentProcess()->GetArgs();
466 if (!positional_args.empty()) {
467 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
468 << "'. If you want to pass a value to a flag, pass it as "
469 "--flag=value.";
470 return 1;
471 }
472
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700473 // Update the status if requested.
474 if (FLAGS_reset_status) {
475 LOG(INFO) << "Setting Update Engine status to idle ...";
Casey Dahline844c1a2015-12-16 14:30:58 -0800476
477 if (client_->ResetStatus()) {
478 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes "
479 "run:\n"
480 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo "
481 "${P#$D} | sed 's/^[^0-9]*//')-1)) $D;)";
482 } else {
483 LOG(ERROR) << "ResetStatus failed";
484 return 1;
485 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700486 }
Darin Petkov58529db2010-08-13 09:19:47 -0700487
Alex Deymof4867c42013-06-28 14:41:39 -0700488 // Changes the current update over cellular network setting.
489 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700490 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700491 if (!allowed && FLAGS_update_over_cellular != "no") {
492 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
493 << "\". Please specify \"yes\" or \"no\".";
494 } else {
495 SetUpdateOverCellularPermission(allowed);
496 }
497 }
498
499 // Show the current update over cellular network setting.
500 if (FLAGS_show_update_over_cellular) {
501 bool allowed = GetUpdateOverCellularPermission();
502 LOG(INFO) << "Current update over cellular network setting: "
503 << (allowed ? "ENABLED" : "DISABLED");
504 }
505
Chris Sosacb7fa882013-07-25 17:02:59 -0700506 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700507 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700508 return 1;
509 }
510
Alex Deymo5fdf7762013-07-17 20:01:40 -0700511 // Change the P2P enabled setting.
512 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700513 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700514 if (!enabled && FLAGS_p2p_update != "no") {
515 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
516 << "\". Please specify \"yes\" or \"no\".";
517 } else {
518 SetP2PUpdatePermission(enabled);
519 }
520 }
521
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800522 // Show the rollback availability.
523 if (FLAGS_can_rollback) {
Alex Deymof329b932014-10-30 01:37:48 -0700524 string rollback_partition = GetRollbackPartition();
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700525 bool can_rollback = true;
526 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700527 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700528 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700529 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700530 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700531 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700532
533 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700534 if (!can_rollback) {
535 return 1;
536 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800537 }
538
Alex Deymo5fdf7762013-07-17 20:01:40 -0700539 // Show the current P2P enabled setting.
540 if (FLAGS_show_p2p_update) {
541 bool enabled = GetP2PUpdatePermission();
542 LOG(INFO) << "Current update using P2P setting: "
543 << (enabled ? "ENABLED" : "DISABLED");
544 }
545
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700546 // First, update the target channel if requested.
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800547 if (!FLAGS_channel.empty()) {
548 if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
549 LOG(ERROR) << "Error setting the channel.";
550 return 1;
551 }
552
553 LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
554 }
Darin Petkov8daa3242010-10-25 13:28:47 -0700555
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700556 // Show the current and target channels if requested.
557 if (FLAGS_show_channel) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800558 string current_channel;
559 string target_channel;
560
561 if (!client_->GetChannel(&current_channel)) {
562 LOG(ERROR) << "Error getting the current channel.";
563 return 1;
564 }
565
566 if (!client_->GetTargetChannel(&target_channel)) {
567 LOG(ERROR) << "Error getting the target channel.";
568 return 1;
569 }
570
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700571 LOG(INFO) << "Current Channel: " << current_channel;
572
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700573 if (!target_channel.empty())
574 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900575 }
576
Chris Sosad317e402013-06-12 13:47:09 -0700577 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
578 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700579 if (FLAGS_update)
580 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700581
Chris Sosad317e402013-06-12 13:47:09 -0700582 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700583 LOG(ERROR) << "Incompatible flags specified with rollback."
584 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700585 return 1;
586 }
587
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700588 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700589 LOG(INFO) << "Requesting rollback.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700590 Rollback(FLAGS_powerwash);
Chris Sosad317e402013-06-12 13:47:09 -0700591 }
592
Darin Petkov58529db2010-08-13 09:19:47 -0700593 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700594 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700595 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700596 string app_version = FLAGS_app_version;
597 if (FLAGS_update && app_version.empty()) {
598 app_version = "ForcedUpdate";
599 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700600 }
Darin Petkov58529db2010-08-13 09:19:47 -0700601 LOG(INFO) << "Initiating update check and install.";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800602 if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
603 FLAGS_interactive)) {
604 LOG(ERROR) << "Error checking for update.";
605 return 1;
606 }
Chris Sosa192449e2013-10-28 14:16:19 -0700607 }
Darin Petkov58529db2010-08-13 09:19:47 -0700608
Chris Sosa192449e2013-10-28 14:16:19 -0700609 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700610 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
611 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700612 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700613 LOG(ERROR) << "Multiple exclusive options selected. "
614 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700615 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700616 << "or --status.";
617 return 1;
618 }
619
620 if (FLAGS_status) {
621 LOG(INFO) << "Querying Update Engine status...";
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800622 if (!ShowStatus()) {
623 LOG(ERROR) << "Failed to query status";
624 return 1;
625 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700626 return 0;
627 }
Darin Petkov58529db2010-08-13 09:19:47 -0700628
Chris Sosa192449e2013-10-28 14:16:19 -0700629 if (FLAGS_follow) {
630 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700631 WaitForUpdateComplete();
632 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700633 }
634
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700635 if (FLAGS_watch_for_updates) {
636 LOG(INFO) << "Watching for status updates.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700637 WatchForUpdates();
638 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700639 }
Darin Petkov58529db2010-08-13 09:19:47 -0700640
Darin Petkov296889c2010-07-23 16:20:54 -0700641 if (FLAGS_reboot) {
642 LOG(INFO) << "Requesting a reboot...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700643 RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700644 return 0;
645 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700646
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700647 if (FLAGS_prev_version) {
648 ShowPrevVersion();
649 }
650
David Zeuthen9d73a722014-04-04 14:52:46 -0700651 if (FLAGS_is_reboot_needed) {
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800652 int ret = GetNeedReboot();
653
654 if (ret == 1) {
655 LOG(ERROR) << "Could not query the current operation.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700656 }
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800657
658 return ret;
David Zeuthen9d73a722014-04-04 14:52:46 -0700659 }
660
661 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700662 WaitForRebootNeeded();
663 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700664 }
665
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700666 return 0;
667}
Alex Deymo72aa0022015-06-19 21:16:49 -0700668
669} // namespace
670
671int main(int argc, char** argv) {
672 UpdateEngineClient client(argc, argv);
673 return client.Run();
674}