blob: c6d4857af18bb52839618e640ffee814dc21056b [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>
27#include <chromeos/daemons/dbus_daemon.h>
Steve Fung97b6f5a2014-10-07 12:39:51 -070028#include <chromeos/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 Deymo72aa0022015-06-19 21:16:49 -070043class UpdateEngineClient : public chromeos::DBusDaemon {
44 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();
106 string GetKernelDevices();
107 void CheckForUpdates(const string& app_version,
108 const string& omaha_url,
109 bool interactive);
110
111 // Reboot the device if a reboot is needed.
112 // Blocking call. Ignores failures.
113 void RebootIfNeeded();
114
115 // Getter and setter for the target channel. If |get_current_channel| is true,
116 // the current channel instead of the target channel will be returned.
117 // Blocking call. Exits the program with error 1 in case of an error.
118 void SetTargetChannel(const string& target_channel, bool allow_powerwash);
119 string GetChannel(bool get_current_channel);
120
121 // Getter and setter for the updates over cellular connections.
122 // Blocking call. Exits the program with error 1 in case of an error.
123 void SetUpdateOverCellularPermission(bool allowed);
124 bool GetUpdateOverCellularPermission();
125
126 // Getter and setter for the updates from P2P permission.
127 // Blocking call. Exits the program with error 1 in case of an error.
128 void SetP2PUpdatePermission(bool enabled);
129 bool GetP2PUpdatePermission();
130
131 // This is similar to watching for updates but rather than registering
132 // a signal watch, actively poll the daemon just in case it stops
133 // sending notifications.
134 void WaitForUpdateComplete();
135 void OnUpdateCompleteCheck(int64_t last_checked_time,
136 double progress,
137 const string& current_operation,
138 const string& new_version,
139 int64_t new_size);
140
141 // Blocking call. Exits the program with error 1 in case of an error.
142 void ShowPrevVersion();
143
144 // Returns whether the current status is such that a reboot is needed.
145 // Blocking call. Exits the program with error 1 in case of an error.
146 bool GetIsRebootNeeded();
147
148 // Blocks until a reboot is needed. If the reboot is needed, exits the program
149 // with 0. Otherwise it exits the program with 1 if an error occurs before
150 // the reboot is needed.
151 void WaitForRebootNeeded();
152 void OnRebootNeededCheck(int64_t last_checked_time,
153 double progress,
154 const string& current_operation,
155 const string& new_version,
156 int64_t new_size);
157 // Callback called when the OnRebootNeededCheck() handler is registered. This
158 // is useful to check at this point if the reboot is needed, without loosing
159 // any StatusUpdate signals and avoiding the race condition.
160 void OnRebootNeededCheckRegistration(const string& interface,
161 const string& signal_name,
162 bool success);
163
164 // Main method that parses and triggers all the actions based on the passed
165 // flags.
166 int ProcessFlags();
167
168 // DBus Proxy to the update_engine daemon object used for all the calls.
169 std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
170
171 // Copy of argc and argv passed to main().
172 int argc_;
173 char** argv_;
174
Alex Deymo13e0dd62015-06-30 23:11:44 -0700175 // Tell whether the UpdateEngine service is available after startup.
176 bool service_is_available_{false};
177
Alex Deymo72aa0022015-06-19 21:16:49 -0700178 DISALLOW_COPY_AND_ASSIGN(UpdateEngineClient);
179};
180
Alex Deymo72aa0022015-06-19 21:16:49 -0700181bool UpdateEngineClient::InitProxy() {
182 proxy_.reset(new org::chromium::UpdateEngineInterfaceProxy(bus_));
183
184 if (!proxy_->GetObjectProxy()) {
185 LOG(ERROR) << "Error getting dbus proxy for " << kUpdateEngineServiceName;
186 return false;
Richard Barnetted7936062013-01-18 13:38:51 -0800187 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700188 return true;
189}
190
Alex Deymo13e0dd62015-06-30 23:11:44 -0700191void UpdateEngineClient::OnServiceAvailable(bool service_is_available) {
192 service_is_available_ = service_is_available;
193 if (!service_is_available) {
194 LOG(ERROR) << "UpdateEngineService not available.";
195 QuitWithExitCode(-1);
196 }
197 int ret = ProcessFlags();
198 if (ret != kContinueRunning)
199 QuitWithExitCode(ret);
200}
201
202void UpdateEngineClient::OnServiceAvailableTimeout() {
203 if (!service_is_available_) {
204 LOG(ERROR) << "Waiting for UpdateEngineService timeout. Is update_engine "
205 "daemon running?";
206 QuitWithExitCode(-1);
207 }
208}
209
Alex Deymo72aa0022015-06-19 21:16:49 -0700210void UpdateEngineClient::OnStatusUpdateSignal(
211 int64_t last_checked_time,
212 double progress,
213 const string& current_operation,
214 const string& new_version,
215 int64_t new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700216 LOG(INFO) << "Got status update:";
217 LOG(INFO) << " last_checked_time: " << last_checked_time;
218 LOG(INFO) << " progress: " << progress;
219 LOG(INFO) << " current_operation: " << current_operation;
220 LOG(INFO) << " new_version: " << new_version;
221 LOG(INFO) << " new_size: " << new_size;
222}
223
Alex Deymo72aa0022015-06-19 21:16:49 -0700224void UpdateEngineClient::OnStatusUpdateSignalRegistration(
225 const string& interface,
226 const string& signal_name,
227 bool success) {
228 VLOG(1) << "OnStatusUpdateSignalRegistration(" << interface << ", "
229 << signal_name << ", " << success << ");";
230 if (!success) {
231 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
232 exit(1);
233 }
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700234}
235
Alex Deymo72aa0022015-06-19 21:16:49 -0700236void UpdateEngineClient::WatchForUpdates() {
237 proxy_->RegisterStatusUpdateSignalHandler(
238 base::Bind(&UpdateEngineClient::OnStatusUpdateSignal,
239 base::Unretained(this)),
240 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
241 base::Unretained(this)));
242}
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700243
Alex Deymo72aa0022015-06-19 21:16:49 -0700244void UpdateEngineClient::ResetStatus() {
245 bool ret = proxy_->ResetStatus(nullptr);
246 CHECK(ret) << "ResetStatus() failed.";
247}
Andrew de los Reyesada42202010-07-15 22:23:20 -0700248
Alex Deymo72aa0022015-06-19 21:16:49 -0700249void UpdateEngineClient::ShowStatus() {
250 int64_t last_checked_time = 0;
251 double progress = 0.0;
252 string current_op;
253 string new_version;
254 int64_t new_size = 0;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700255
Alex Deymo72aa0022015-06-19 21:16:49 -0700256 bool ret = proxy_->GetStatus(
257 &last_checked_time, &progress, &current_op, &new_version, &new_size,
258 nullptr);
259 CHECK(ret) << "GetStatus() failed";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700260 printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
261 "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
262 last_checked_time,
263 progress,
Alex Deymo72aa0022015-06-19 21:16:49 -0700264 current_op.c_str(),
265 new_version.c_str(),
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700266 new_size);
Alex Deymo72aa0022015-06-19 21:16:49 -0700267}
268
269string UpdateEngineClient::GetCurrentOperation() {
270 int64_t last_checked_time = 0;
271 double progress = 0.0;
272 string current_op;
273 string new_version;
274 int64_t new_size = 0;
275
276 bool ret = proxy_->GetStatus(
277 &last_checked_time, &progress, &current_op, &new_version, &new_size,
278 nullptr);
279 CHECK(ret) << "GetStatus() failed";
280 return current_op;
281}
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
295string UpdateEngineClient::GetKernelDevices() {
296 string kernel_devices;
297 bool ret = proxy_->GetKernelDevices(&kernel_devices, nullptr);
298 CHECK(ret) << "Error while getting a list of kernel devices";
299 return kernel_devices;
300}
301
302void UpdateEngineClient::CheckForUpdates(const string& app_version,
303 const string& omaha_url,
304 bool interactive) {
305 int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
306 bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
307 nullptr);
308 CHECK(ret) << "Error checking for update.";
309}
310
311void UpdateEngineClient::RebootIfNeeded() {
312 bool ret = proxy_->RebootIfNeeded(nullptr);
313 if (!ret) {
314 // Reboot error code doesn't necessarily mean that a reboot
315 // failed. For example, D-Bus may be shutdown before we receive the
316 // result.
317 LOG(INFO) << "RebootIfNeeded() failure ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700318 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700319}
320
Alex Deymo72aa0022015-06-19 21:16:49 -0700321void UpdateEngineClient::SetTargetChannel(const string& target_channel,
322 bool allow_powerwash) {
323 bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
324 CHECK(ret) << "Error setting the channel.";
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700325 LOG(INFO) << "Channel permanently set to: " << target_channel;
Darin Petkov8daa3242010-10-25 13:28:47 -0700326}
327
Alex Deymo72aa0022015-06-19 21:16:49 -0700328string UpdateEngineClient::GetChannel(bool get_current_channel) {
329 string channel;
330 bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
331 CHECK(ret) << "Error getting the channel.";
332 return channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900333}
334
Alex Deymo72aa0022015-06-19 21:16:49 -0700335void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
336 bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
337 CHECK(ret) << "Error setting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700338}
339
Alex Deymo72aa0022015-06-19 21:16:49 -0700340bool UpdateEngineClient::GetUpdateOverCellularPermission() {
341 bool allowed;
342 bool ret = proxy_->GetUpdateOverCellularPermission(&allowed, nullptr);
343 CHECK(ret) << "Error getting the update over cellular setting.";
Alex Deymof4867c42013-06-28 14:41:39 -0700344 return allowed;
345}
346
Alex Deymo72aa0022015-06-19 21:16:49 -0700347void UpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
348 bool ret = proxy_->SetP2PUpdatePermission(enabled, nullptr);
349 CHECK(ret) << "Error setting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700350}
351
Alex Deymo72aa0022015-06-19 21:16:49 -0700352bool UpdateEngineClient::GetP2PUpdatePermission() {
353 bool enabled;
354 bool ret = proxy_->GetP2PUpdatePermission(&enabled, nullptr);
355 CHECK(ret) << "Error getting the peer-to-peer update setting.";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700356 return enabled;
357}
358
Alex Deymo72aa0022015-06-19 21:16:49 -0700359void UpdateEngineClient::OnUpdateCompleteCheck(
360 int64_t /* last_checked_time */,
361 double /* progress */,
362 const string& current_operation,
363 const string& /* new_version */,
364 int64_t /* new_size */) {
365 if (current_operation == update_engine::kUpdateStatusIdle) {
366 LOG(ERROR) << "Update failed, current operations is " << current_operation;
Darin Petkov58529db2010-08-13 09:19:47 -0700367 exit(1);
368 }
Alex Deymo72aa0022015-06-19 21:16:49 -0700369 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
Darin Petkov58529db2010-08-13 09:19:47 -0700370 LOG(INFO) << "Update succeeded -- reboot needed.";
371 exit(0);
372 }
Darin Petkov58529db2010-08-13 09:19:47 -0700373}
374
Alex Deymo72aa0022015-06-19 21:16:49 -0700375void UpdateEngineClient::WaitForUpdateComplete() {
376 proxy_->RegisterStatusUpdateSignalHandler(
377 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
378 base::Unretained(this)),
379 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
380 base::Unretained(this)));
Darin Petkov58529db2010-08-13 09:19:47 -0700381}
382
Alex Deymo72aa0022015-06-19 21:16:49 -0700383void UpdateEngineClient::ShowPrevVersion() {
384 string prev_version = nullptr;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700385
Alex Deymo72aa0022015-06-19 21:16:49 -0700386 bool ret = proxy_->GetPrevVersion(&prev_version, nullptr);;
387 if (!ret) {
388 LOG(ERROR) << "Error getting previous version.";
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700389 } else {
390 LOG(INFO) << "Previous version = " << prev_version;
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700391 }
392}
393
Alex Deymo72aa0022015-06-19 21:16:49 -0700394bool UpdateEngineClient::GetIsRebootNeeded() {
395 return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
David Zeuthen9d73a722014-04-04 14:52:46 -0700396}
397
Alex Deymo72aa0022015-06-19 21:16:49 -0700398void UpdateEngineClient::OnRebootNeededCheck(
399 int64_t /* last_checked_time */,
400 double /* progress */,
401 const string& current_operation,
402 const string& /* new_version */,
403 int64_t /* new_size */) {
404 if (current_operation == update_engine::kUpdateStatusUpdatedNeedReboot) {
405 LOG(INFO) << "Reboot needed.";
406 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700407 }
408}
409
Alex Deymo72aa0022015-06-19 21:16:49 -0700410void UpdateEngineClient::OnRebootNeededCheckRegistration(
411 const string& interface,
412 const string& signal_name,
413 bool success) {
414 if (GetIsRebootNeeded())
415 exit(0);
416 if (!success) {
417 LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
418 exit(1);
419 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700420}
421
422// Blocks until a reboot is needed. Returns true if waiting succeeded,
423// false if an error occurred.
Alex Deymo72aa0022015-06-19 21:16:49 -0700424void UpdateEngineClient::WaitForRebootNeeded() {
425 proxy_->RegisterStatusUpdateSignalHandler(
426 base::Bind(&UpdateEngineClient::OnUpdateCompleteCheck,
427 base::Unretained(this)),
428 base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
429 base::Unretained(this)));
430 if (GetIsRebootNeeded())
431 exit(0);
David Zeuthen9d73a722014-04-04 14:52:46 -0700432}
433
Alex Deymo72aa0022015-06-19 21:16:49 -0700434int UpdateEngineClient::ProcessFlags() {
Steve Fung97b6f5a2014-10-07 12:39:51 -0700435 DEFINE_string(app_version, "", "Force the current app version.");
436 DEFINE_string(channel, "",
437 "Set the target channel. The device will be powerwashed if the "
438 "target channel is more stable than the current channel unless "
439 "--nopowerwash is specified.");
440 DEFINE_bool(check_for_update, false, "Initiate check for updates.");
441 DEFINE_bool(follow, false, "Wait for any update operations to complete."
442 "Exit status is 0 if the update succeeded, and 1 otherwise.");
443 DEFINE_bool(interactive, true, "Mark the update request as interactive.");
444 DEFINE_string(omaha_url, "", "The URL of the Omaha update server.");
445 DEFINE_string(p2p_update, "",
446 "Enables (\"yes\") or disables (\"no\") the peer-to-peer update"
447 " sharing.");
448 DEFINE_bool(powerwash, true, "When performing rollback or channel change, "
449 "do a powerwash or allow it respectively.");
450 DEFINE_bool(reboot, false, "Initiate a reboot if needed.");
451 DEFINE_bool(is_reboot_needed, false, "Exit status 0 if reboot is needed, "
452 "2 if reboot is not needed or 1 if an error occurred.");
453 DEFINE_bool(block_until_reboot_is_needed, false, "Blocks until reboot is "
454 "needed. Returns non-zero exit status if an error occurred.");
455 DEFINE_bool(reset_status, false, "Sets the status in update_engine to idle.");
Alex Deymo1ac8b592015-01-26 13:22:58 -0800456 DEFINE_bool(rollback, false,
457 "Perform a rollback to the previous partition. The device will "
458 "be powerwashed unless --nopowerwash is specified.");
Steve Fung97b6f5a2014-10-07 12:39:51 -0700459 DEFINE_bool(can_rollback, false, "Shows whether rollback partition "
460 "is available.");
461 DEFINE_bool(show_channel, false, "Show the current and target channels.");
462 DEFINE_bool(show_p2p_update, false,
463 "Show the current setting for peer-to-peer update sharing.");
464 DEFINE_bool(show_update_over_cellular, false,
465 "Show the current setting for updates over cellular networks.");
466 DEFINE_bool(status, false, "Print the status to stdout.");
467 DEFINE_bool(update, false, "Forces an update and waits for it to complete. "
468 "Implies --follow.");
469 DEFINE_string(update_over_cellular, "",
470 "Enables (\"yes\") or disables (\"no\") the updates over "
471 "cellular networks.");
472 DEFINE_bool(watch_for_updates, false,
473 "Listen for status updates and print them to the screen.");
474 DEFINE_bool(prev_version, false,
475 "Show the previous OS version used before the update reboot.");
476 DEFINE_bool(show_kernels, false, "Show the list of kernel patritions and "
477 "whether each of them is bootable or not");
478
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700479 // Boilerplate init commands.
Alex Deymo72aa0022015-06-19 21:16:49 -0700480 base::CommandLine::Init(argc_, argv_);
481 chromeos::FlagHelper::Init(argc_, argv_, "Chromium OS Update Engine Client");
Andrew de los Reyesada42202010-07-15 22:23:20 -0700482
Alex Deymo8ce80d62015-01-27 15:10:43 -0800483 // Ensure there are no positional arguments.
484 const std::vector<string> positional_args =
485 base::CommandLine::ForCurrentProcess()->GetArgs();
486 if (!positional_args.empty()) {
487 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
488 << "'. If you want to pass a value to a flag, pass it as "
489 "--flag=value.";
490 return 1;
491 }
492
Jay Srinivasanc1ba09a2012-08-14 14:15:57 -0700493 // Update the status if requested.
494 if (FLAGS_reset_status) {
495 LOG(INFO) << "Setting Update Engine status to idle ...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700496 ResetStatus();
Gilad Arnold50c60632013-01-25 10:27:19 -0800497 LOG(INFO) << "ResetStatus succeeded; to undo partition table changes run:\n"
498 "(D=$(rootdev -d) P=$(rootdev -s); cgpt p -i$(($(echo ${P#$D} "
499 "| sed 's/^[^0-9]*//')-1)) $D;)";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700500 }
Darin Petkov58529db2010-08-13 09:19:47 -0700501
Alex Deymof4867c42013-06-28 14:41:39 -0700502 // Changes the current update over cellular network setting.
503 if (!FLAGS_update_over_cellular.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700504 bool allowed = FLAGS_update_over_cellular == "yes";
Alex Deymof4867c42013-06-28 14:41:39 -0700505 if (!allowed && FLAGS_update_over_cellular != "no") {
506 LOG(ERROR) << "Unknown option: \"" << FLAGS_update_over_cellular
507 << "\". Please specify \"yes\" or \"no\".";
508 } else {
509 SetUpdateOverCellularPermission(allowed);
510 }
511 }
512
513 // Show the current update over cellular network setting.
514 if (FLAGS_show_update_over_cellular) {
515 bool allowed = GetUpdateOverCellularPermission();
516 LOG(INFO) << "Current update over cellular network setting: "
517 << (allowed ? "ENABLED" : "DISABLED");
518 }
519
Chris Sosacb7fa882013-07-25 17:02:59 -0700520 if (!FLAGS_powerwash && !FLAGS_rollback && FLAGS_channel.empty()) {
Chris Sosa192449e2013-10-28 14:16:19 -0700521 LOG(ERROR) << "powerwash flag only makes sense rollback or channel change";
Chris Sosacb7fa882013-07-25 17:02:59 -0700522 return 1;
523 }
524
Alex Deymo5fdf7762013-07-17 20:01:40 -0700525 // Change the P2P enabled setting.
526 if (!FLAGS_p2p_update.empty()) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700527 bool enabled = FLAGS_p2p_update == "yes";
Alex Deymo5fdf7762013-07-17 20:01:40 -0700528 if (!enabled && FLAGS_p2p_update != "no") {
529 LOG(ERROR) << "Unknown option: \"" << FLAGS_p2p_update
530 << "\". Please specify \"yes\" or \"no\".";
531 } else {
532 SetP2PUpdatePermission(enabled);
533 }
534 }
535
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800536 // Show the rollback availability.
537 if (FLAGS_can_rollback) {
Alex Deymof329b932014-10-30 01:37:48 -0700538 string rollback_partition = GetRollbackPartition();
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700539 bool can_rollback = true;
540 if (rollback_partition.empty()) {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700541 rollback_partition = "UNAVAILABLE";
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700542 can_rollback = false;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700543 } else {
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700544 rollback_partition = "AVAILABLE: " + rollback_partition;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700545 }
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700546
547 LOG(INFO) << "Rollback partition: " << rollback_partition;
Chris Sosaf5c0b9c2014-04-17 16:12:03 -0700548 if (!can_rollback) {
549 return 1;
550 }
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800551 }
552
Alex Deymo5fdf7762013-07-17 20:01:40 -0700553 // Show the current P2P enabled setting.
554 if (FLAGS_show_p2p_update) {
555 bool enabled = GetP2PUpdatePermission();
556 LOG(INFO) << "Current update using P2P setting: "
557 << (enabled ? "ENABLED" : "DISABLED");
558 }
559
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700560 // First, update the target channel if requested.
561 if (!FLAGS_channel.empty())
Chris Sosacb7fa882013-07-25 17:02:59 -0700562 SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
Darin Petkov8daa3242010-10-25 13:28:47 -0700563
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700564 // Show the current and target channels if requested.
565 if (FLAGS_show_channel) {
566 string current_channel = GetChannel(true);
567 LOG(INFO) << "Current Channel: " << current_channel;
568
569 string target_channel = GetChannel(false);
570 if (!target_channel.empty())
571 LOG(INFO) << "Target Channel (pending update): " << target_channel;
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900572 }
573
Chris Sosad317e402013-06-12 13:47:09 -0700574 bool do_update_request = FLAGS_check_for_update | FLAGS_update |
575 !FLAGS_app_version.empty() | !FLAGS_omaha_url.empty();
Chris Sosa192449e2013-10-28 14:16:19 -0700576 if (FLAGS_update)
577 FLAGS_follow = true;
Chris Sosad317e402013-06-12 13:47:09 -0700578
Chris Sosad317e402013-06-12 13:47:09 -0700579 if (do_update_request && FLAGS_rollback) {
Chris Sosa192449e2013-10-28 14:16:19 -0700580 LOG(ERROR) << "Incompatible flags specified with rollback."
581 << "Rollback should not include update-related flags.";
Chris Sosad317e402013-06-12 13:47:09 -0700582 return 1;
583 }
584
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700585 if (FLAGS_rollback) {
Chris Sosad317e402013-06-12 13:47:09 -0700586 LOG(INFO) << "Requesting rollback.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700587 Rollback(FLAGS_powerwash);
Chris Sosad317e402013-06-12 13:47:09 -0700588 }
589
Darin Petkov58529db2010-08-13 09:19:47 -0700590 // Initiate an update check, if necessary.
Chris Sosad317e402013-06-12 13:47:09 -0700591 if (do_update_request) {
Darin Petkov296889c2010-07-23 16:20:54 -0700592 LOG_IF(WARNING, FLAGS_reboot) << "-reboot flag ignored.";
Darin Petkov58529db2010-08-13 09:19:47 -0700593 string app_version = FLAGS_app_version;
594 if (FLAGS_update && app_version.empty()) {
595 app_version = "ForcedUpdate";
596 LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700597 }
Darin Petkov58529db2010-08-13 09:19:47 -0700598 LOG(INFO) << "Initiating update check and install.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700599 CheckForUpdates(app_version, FLAGS_omaha_url, FLAGS_interactive);
Chris Sosa192449e2013-10-28 14:16:19 -0700600 }
Darin Petkov58529db2010-08-13 09:19:47 -0700601
Chris Sosa192449e2013-10-28 14:16:19 -0700602 // These final options are all mutually exclusive with one another.
David Zeuthen9d73a722014-04-04 14:52:46 -0700603 if (FLAGS_follow + FLAGS_watch_for_updates + FLAGS_reboot +
604 FLAGS_status + FLAGS_is_reboot_needed +
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700605 FLAGS_block_until_reboot_is_needed > 1) {
Chris Sosa192449e2013-10-28 14:16:19 -0700606 LOG(ERROR) << "Multiple exclusive options selected. "
607 << "Select only one of --follow, --watch_for_updates, --reboot, "
David Zeuthen9d73a722014-04-04 14:52:46 -0700608 << "--is_reboot_needed, --block_until_reboot_is_needed, "
Chris Sosa192449e2013-10-28 14:16:19 -0700609 << "or --status.";
610 return 1;
611 }
612
613 if (FLAGS_status) {
614 LOG(INFO) << "Querying Update Engine status...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700615 ShowStatus();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700616 return 0;
617 }
Darin Petkov58529db2010-08-13 09:19:47 -0700618
Chris Sosa192449e2013-10-28 14:16:19 -0700619 if (FLAGS_follow) {
620 LOG(INFO) << "Waiting for update to complete.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700621 WaitForUpdateComplete();
622 return kContinueRunning;
Chris Sosa192449e2013-10-28 14:16:19 -0700623 }
624
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700625 if (FLAGS_watch_for_updates) {
626 LOG(INFO) << "Watching for status updates.";
Alex Deymo72aa0022015-06-19 21:16:49 -0700627 WatchForUpdates();
628 return kContinueRunning;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700629 }
Darin Petkov58529db2010-08-13 09:19:47 -0700630
Darin Petkov296889c2010-07-23 16:20:54 -0700631 if (FLAGS_reboot) {
632 LOG(INFO) << "Requesting a reboot...";
Alex Deymo72aa0022015-06-19 21:16:49 -0700633 RebootIfNeeded();
Darin Petkov296889c2010-07-23 16:20:54 -0700634 return 0;
635 }
Andrew de los Reyesada42202010-07-15 22:23:20 -0700636
Alex Vakulenkodea2eac2014-03-14 15:56:59 -0700637 if (FLAGS_prev_version) {
638 ShowPrevVersion();
639 }
640
Alex Vakulenko2bddadd2014-03-27 13:23:46 -0700641 if (FLAGS_show_kernels) {
642 LOG(INFO) << "Kernel partitions:\n"
643 << GetKernelDevices();
644 }
645
David Zeuthen9d73a722014-04-04 14:52:46 -0700646 if (FLAGS_is_reboot_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700647 // In case of error GetIsRebootNeeded() will exit with 1.
648 if (GetIsRebootNeeded()) {
649 return 0;
650 } else {
David Zeuthen9d73a722014-04-04 14:52:46 -0700651 return 2;
Alex Deymo72aa0022015-06-19 21:16:49 -0700652 }
David Zeuthen9d73a722014-04-04 14:52:46 -0700653 }
654
655 if (FLAGS_block_until_reboot_is_needed) {
Alex Deymo72aa0022015-06-19 21:16:49 -0700656 WaitForRebootNeeded();
657 return kContinueRunning;
David Zeuthen9d73a722014-04-04 14:52:46 -0700658 }
659
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700660 return 0;
661}
Alex Deymo72aa0022015-06-19 21:16:49 -0700662
663} // namespace
664
665int main(int argc, char** argv) {
666 UpdateEngineClient client(argc, argv);
667 return client.Run();
668}