Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 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 | // |
| 16 | |
| 17 | #include <sysexits.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <base/bind.h> |
| 24 | #include <base/callback.h> |
| 25 | #include <base/command_line.h> |
| 26 | #include <base/logging.h> |
| 27 | #include <base/strings/string_split.h> |
| 28 | #include <binder/IServiceManager.h> |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 29 | #include <binderwrapper/binder_wrapper.h> |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 30 | #include <brillo/binder_watcher.h> |
| 31 | #include <brillo/daemons/daemon.h> |
| 32 | #include <brillo/flag_helper.h> |
| 33 | #include <brillo/message_loops/message_loop.h> |
| 34 | #include <brillo/syslog_logging.h> |
| 35 | #include <utils/String16.h> |
| 36 | #include <utils/StrongPointer.h> |
| 37 | |
| 38 | #include "android/os/BnUpdateEngineCallback.h" |
| 39 | #include "android/os/IUpdateEngine.h" |
| 40 | #include "update_engine/client_library/include/update_engine/update_status.h" |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 41 | #include "update_engine/common/error_code.h" |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 42 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 43 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 44 | |
| 45 | using android::binder::Status; |
| 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | namespace internal { |
| 49 | |
| 50 | class UpdateEngineClientAndroid : public brillo::Daemon { |
| 51 | public: |
| 52 | UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) { |
| 53 | } |
| 54 | |
| 55 | int ExitWhenIdle(const Status& status); |
| 56 | int ExitWhenIdle(int return_code); |
| 57 | |
| 58 | private: |
| 59 | class UECallback : public android::os::BnUpdateEngineCallback { |
| 60 | public: |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 61 | explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {} |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 62 | |
| 63 | // android::os::BnUpdateEngineCallback overrides. |
| 64 | Status onStatusUpdate(int status_code, float progress) override; |
| 65 | Status onPayloadApplicationComplete(int error_code) override; |
| 66 | |
| 67 | private: |
| 68 | UpdateEngineClientAndroid* client_; |
| 69 | }; |
| 70 | |
| 71 | int OnInit() override; |
| 72 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 73 | // Called whenever the UpdateEngine daemon dies. |
| 74 | void UpdateEngineServiceDied(); |
| 75 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 76 | // Copy of argc and argv passed to main(). |
| 77 | int argc_; |
| 78 | char** argv_; |
| 79 | |
| 80 | android::sp<android::os::IUpdateEngine> service_; |
| 81 | android::sp<android::os::BnUpdateEngineCallback> callback_; |
| 82 | |
| 83 | brillo::BinderWatcher binder_watcher_; |
| 84 | }; |
| 85 | |
| 86 | Status UpdateEngineClientAndroid::UECallback::onStatusUpdate( |
| 87 | int status_code, float progress) { |
| 88 | update_engine::UpdateStatus status = |
| 89 | static_cast<update_engine::UpdateStatus>(status_code); |
| 90 | LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " (" |
| 91 | << status_code << "), " << progress << ")"; |
| 92 | return Status::ok(); |
| 93 | } |
| 94 | |
| 95 | Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete( |
| 96 | int error_code) { |
| 97 | ErrorCode code = static_cast<ErrorCode>(error_code); |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 98 | LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code) |
| 99 | << " (" << error_code << "))"; |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 100 | client_->ExitWhenIdle( |
| 101 | (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive) |
| 102 | ? EX_OK |
| 103 | : 1); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 104 | return Status::ok(); |
| 105 | } |
| 106 | |
| 107 | int UpdateEngineClientAndroid::OnInit() { |
| 108 | int ret = Daemon::OnInit(); |
| 109 | if (ret != EX_OK) |
| 110 | return ret; |
| 111 | |
| 112 | DEFINE_bool(update, false, "Start a new update, if no update in progress."); |
| 113 | DEFINE_string(payload, |
| 114 | "http://127.0.0.1:8080/payload", |
| 115 | "The URI to the update payload to use."); |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 116 | DEFINE_int64(offset, 0, |
| 117 | "The offset in the payload where the CrAU update starts. " |
| 118 | "Used when --update is passed."); |
| 119 | DEFINE_int64(size, 0, |
| 120 | "The size of the CrAU part of the payload. If 0 is passed, it " |
| 121 | "will be autodetected. Used when --update is passed."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 122 | DEFINE_string(headers, |
| 123 | "", |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 124 | "A list of key-value pairs, one element of the list per line. " |
| 125 | "Used when --update is passed."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 126 | |
Sen Jiang | 6f67d56 | 2018-09-19 14:55:50 -0700 | [diff] [blame] | 127 | DEFINE_bool(verify, |
| 128 | false, |
| 129 | "Given payload metadata, verify if the payload is applicable."); |
| 130 | DEFINE_string(metadata, |
| 131 | "/data/ota_package/metadata", |
| 132 | "The path to the update payload metadata. " |
| 133 | "Used when --verify is passed."); |
| 134 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 135 | DEFINE_bool(suspend, false, "Suspend an ongoing update and exit."); |
| 136 | DEFINE_bool(resume, false, "Resume a suspended update."); |
| 137 | DEFINE_bool(cancel, false, "Cancel the ongoing update and exit."); |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 138 | DEFINE_bool(reset_status, false, "Reset an already applied update and exit."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 139 | DEFINE_bool(follow, |
| 140 | false, |
| 141 | "Follow status update changes until a final state is reached. " |
| 142 | "Exit status is 0 if the update succeeded, and 1 otherwise."); |
| 143 | |
| 144 | // Boilerplate init commands. |
| 145 | base::CommandLine::Init(argc_, argv_); |
| 146 | brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client"); |
| 147 | if (argc_ == 1) { |
| 148 | LOG(ERROR) << "Nothing to do. Run with --help for help."; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | // Ensure there are no positional arguments. |
| 153 | const std::vector<std::string> positional_args = |
| 154 | base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 155 | if (!positional_args.empty()) { |
| 156 | LOG(ERROR) << "Found a positional argument '" << positional_args.front() |
| 157 | << "'. If you want to pass a value to a flag, pass it as " |
| 158 | "--flag=value."; |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | bool keep_running = false; |
Alex Deymo | 95224dd | 2016-01-29 11:18:35 -0800 | [diff] [blame] | 163 | brillo::InitLog(brillo::kLogToStderr); |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 164 | |
| 165 | // Initialize a binder watcher early in the process before any interaction |
| 166 | // with the binder driver. |
| 167 | binder_watcher_.Init(); |
| 168 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 169 | android::status_t status = android::getService( |
| 170 | android::String16("android.os.UpdateEngineService"), &service_); |
| 171 | if (status != android::OK) { |
| 172 | LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: " |
| 173 | << Status::fromStatusT(status).toString8(); |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 174 | return ExitWhenIdle(1); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (FLAGS_suspend) { |
| 178 | return ExitWhenIdle(service_->suspend()); |
| 179 | } |
| 180 | |
| 181 | if (FLAGS_resume) { |
| 182 | return ExitWhenIdle(service_->resume()); |
| 183 | } |
| 184 | |
| 185 | if (FLAGS_cancel) { |
| 186 | return ExitWhenIdle(service_->cancel()); |
| 187 | } |
| 188 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 189 | if (FLAGS_reset_status) { |
| 190 | return ExitWhenIdle(service_->resetStatus()); |
| 191 | } |
| 192 | |
Sen Jiang | 6f67d56 | 2018-09-19 14:55:50 -0700 | [diff] [blame] | 193 | if (FLAGS_verify) { |
| 194 | bool applicable = false; |
| 195 | Status status = service_->verifyPayloadApplicable( |
| 196 | android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()}, |
| 197 | &applicable); |
| 198 | LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable."; |
| 199 | return ExitWhenIdle(status); |
| 200 | } |
| 201 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 202 | if (FLAGS_follow) { |
| 203 | // Register a callback object with the service. |
| 204 | callback_ = new UECallback(this); |
| 205 | bool bound; |
| 206 | if (!service_->bind(callback_, &bound).isOk() || !bound) { |
| 207 | LOG(ERROR) << "Failed to bind() the UpdateEngine daemon."; |
| 208 | return 1; |
| 209 | } |
| 210 | keep_running = true; |
| 211 | } |
| 212 | |
| 213 | if (FLAGS_update) { |
| 214 | std::vector<std::string> headers = base::SplitString( |
| 215 | FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 216 | std::vector<android::String16> and_headers; |
| 217 | for (const auto& header : headers) { |
| 218 | and_headers.push_back(android::String16{header.data(), header.size()}); |
| 219 | } |
| 220 | Status status = service_->applyPayload( |
| 221 | android::String16{FLAGS_payload.data(), FLAGS_payload.size()}, |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 222 | FLAGS_offset, |
| 223 | FLAGS_size, |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 224 | and_headers); |
| 225 | if (!status.isOk()) |
| 226 | return ExitWhenIdle(status); |
| 227 | } |
| 228 | |
| 229 | if (!keep_running) |
| 230 | return ExitWhenIdle(EX_OK); |
| 231 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 232 | // When following updates status changes, exit if the update_engine daemon |
| 233 | // dies. |
| 234 | android::BinderWrapper::Create(); |
| 235 | android::BinderWrapper::Get()->RegisterForDeathNotifications( |
| 236 | android::os::IUpdateEngine::asBinder(service_), |
| 237 | base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied, |
| 238 | base::Unretained(this))); |
| 239 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 240 | return EX_OK; |
| 241 | } |
| 242 | |
| 243 | int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) { |
| 244 | if (status.isOk()) |
| 245 | return ExitWhenIdle(EX_OK); |
| 246 | LOG(ERROR) << status.toString8(); |
| 247 | return ExitWhenIdle(status.exceptionCode()); |
| 248 | } |
| 249 | |
| 250 | int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) { |
| 251 | auto delayed_exit = base::Bind( |
| 252 | &Daemon::QuitWithExitCode, base::Unretained(this), return_code); |
| 253 | if (!brillo::MessageLoop::current()->PostTask(delayed_exit)) |
| 254 | return 1; |
| 255 | return EX_OK; |
| 256 | } |
| 257 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 258 | void UpdateEngineClientAndroid::UpdateEngineServiceDied() { |
| 259 | LOG(ERROR) << "UpdateEngineService died."; |
| 260 | QuitWithExitCode(1); |
| 261 | } |
| 262 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 263 | } // namespace internal |
| 264 | } // namespace chromeos_update_engine |
| 265 | |
| 266 | int main(int argc, char** argv) { |
| 267 | chromeos_update_engine::internal::UpdateEngineClientAndroid client( |
| 268 | argc, argv); |
| 269 | return client.Run(); |
| 270 | } |