blob: 6863799f6019972ba4fbe488d92060a53cfb7007 [file] [log] [blame]
Alex Deymo5f528112016-01-27 23:32:36 -08001//
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 Deymo2130ee02016-02-02 18:35:50 -080029#include <binderwrapper/binder_wrapper.h>
Alex Deymo5f528112016-01-27 23:32:36 -080030#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 Deymo5f528112016-01-27 23:32:36 -080041#include "update_engine/common/error_code.h"
Alex Deymoe88e9fe2016-02-03 16:38:00 -080042#include "update_engine/common/error_code_utils.h"
Alex Deymo2130ee02016-02-02 18:35:50 -080043#include "update_engine/update_status_utils.h"
Alex Deymo5f528112016-01-27 23:32:36 -080044
45using android::binder::Status;
46
47namespace chromeos_update_engine {
48namespace internal {
49
50class UpdateEngineClientAndroid : public brillo::Daemon {
51 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080052 UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {}
Alex Deymo5f528112016-01-27 23:32:36 -080053
54 int ExitWhenIdle(const Status& status);
55 int ExitWhenIdle(int return_code);
56
57 private:
58 class UECallback : public android::os::BnUpdateEngineCallback {
59 public:
Alex Deymoe88e9fe2016-02-03 16:38:00 -080060 explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {}
Alex Deymo5f528112016-01-27 23:32:36 -080061
62 // android::os::BnUpdateEngineCallback overrides.
63 Status onStatusUpdate(int status_code, float progress) override;
64 Status onPayloadApplicationComplete(int error_code) override;
65
66 private:
67 UpdateEngineClientAndroid* client_;
68 };
69
70 int OnInit() override;
71
Alex Deymo2130ee02016-02-02 18:35:50 -080072 // Called whenever the UpdateEngine daemon dies.
73 void UpdateEngineServiceDied();
74
Alex Deymo5f528112016-01-27 23:32:36 -080075 // Copy of argc and argv passed to main().
76 int argc_;
77 char** argv_;
78
79 android::sp<android::os::IUpdateEngine> service_;
80 android::sp<android::os::BnUpdateEngineCallback> callback_;
81
82 brillo::BinderWatcher binder_watcher_;
83};
84
Amin Hassani7cc8bb02019-01-14 16:29:47 -080085Status UpdateEngineClientAndroid::UECallback::onStatusUpdate(int status_code,
86 float progress) {
Alex Deymo5f528112016-01-27 23:32:36 -080087 update_engine::UpdateStatus status =
88 static_cast<update_engine::UpdateStatus>(status_code);
89 LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " ("
90 << status_code << "), " << progress << ")";
91 return Status::ok();
92}
93
94Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete(
95 int error_code) {
96 ErrorCode code = static_cast<ErrorCode>(error_code);
Alex Deymoe88e9fe2016-02-03 16:38:00 -080097 LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code)
98 << " (" << error_code << "))";
Sen Jiang02c49422017-10-31 15:14:11 -070099 client_->ExitWhenIdle(
100 (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive)
101 ? EX_OK
102 : 1);
Alex Deymo5f528112016-01-27 23:32:36 -0800103 return Status::ok();
104}
105
106int UpdateEngineClientAndroid::OnInit() {
107 int ret = Daemon::OnInit();
108 if (ret != EX_OK)
109 return ret;
110
111 DEFINE_bool(update, false, "Start a new update, if no update in progress.");
112 DEFINE_string(payload,
113 "http://127.0.0.1:8080/payload",
114 "The URI to the update payload to use.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800115 DEFINE_int64(offset,
116 0,
Alex Deymo95b8f242016-01-28 16:06:57 -0800117 "The offset in the payload where the CrAU update starts. "
118 "Used when --update is passed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800119 DEFINE_int64(size,
120 0,
Alex Deymo95b8f242016-01-28 16:06:57 -0800121 "The size of the CrAU part of the payload. If 0 is passed, it "
122 "will be autodetected. Used when --update is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800123 DEFINE_string(headers,
124 "",
Alex Deymo95b8f242016-01-28 16:06:57 -0800125 "A list of key-value pairs, one element of the list per line. "
126 "Used when --update is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800127
Sen Jiang6f67d562018-09-19 14:55:50 -0700128 DEFINE_bool(verify,
129 false,
130 "Given payload metadata, verify if the payload is applicable.");
131 DEFINE_string(metadata,
132 "/data/ota_package/metadata",
133 "The path to the update payload metadata. "
134 "Used when --verify is passed.");
135
Alex Deymo5f528112016-01-27 23:32:36 -0800136 DEFINE_bool(suspend, false, "Suspend an ongoing update and exit.");
137 DEFINE_bool(resume, false, "Resume a suspended update.");
138 DEFINE_bool(cancel, false, "Cancel the ongoing update and exit.");
Alex Deymo3b678db2016-02-09 11:50:06 -0800139 DEFINE_bool(reset_status, false, "Reset an already applied update and exit.");
Alex Deymo5f528112016-01-27 23:32:36 -0800140 DEFINE_bool(follow,
141 false,
142 "Follow status update changes until a final state is reached. "
143 "Exit status is 0 if the update succeeded, and 1 otherwise.");
144
145 // Boilerplate init commands.
146 base::CommandLine::Init(argc_, argv_);
147 brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
148 if (argc_ == 1) {
149 LOG(ERROR) << "Nothing to do. Run with --help for help.";
150 return 1;
151 }
152
153 // Ensure there are no positional arguments.
154 const std::vector<std::string> positional_args =
155 base::CommandLine::ForCurrentProcess()->GetArgs();
156 if (!positional_args.empty()) {
157 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
158 << "'. If you want to pass a value to a flag, pass it as "
159 "--flag=value.";
160 return 1;
161 }
162
163 bool keep_running = false;
Alex Deymo95224dd2016-01-29 11:18:35 -0800164 brillo::InitLog(brillo::kLogToStderr);
Alex Deymo2130ee02016-02-02 18:35:50 -0800165
166 // Initialize a binder watcher early in the process before any interaction
167 // with the binder driver.
168 binder_watcher_.Init();
169
Alex Deymo5f528112016-01-27 23:32:36 -0800170 android::status_t status = android::getService(
171 android::String16("android.os.UpdateEngineService"), &service_);
172 if (status != android::OK) {
173 LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: "
174 << Status::fromStatusT(status).toString8();
Alex Deymo2130ee02016-02-02 18:35:50 -0800175 return ExitWhenIdle(1);
Alex Deymo5f528112016-01-27 23:32:36 -0800176 }
177
178 if (FLAGS_suspend) {
179 return ExitWhenIdle(service_->suspend());
180 }
181
182 if (FLAGS_resume) {
183 return ExitWhenIdle(service_->resume());
184 }
185
186 if (FLAGS_cancel) {
187 return ExitWhenIdle(service_->cancel());
188 }
189
Alex Deymo3b678db2016-02-09 11:50:06 -0800190 if (FLAGS_reset_status) {
191 return ExitWhenIdle(service_->resetStatus());
192 }
193
Sen Jiang6f67d562018-09-19 14:55:50 -0700194 if (FLAGS_verify) {
195 bool applicable = false;
196 Status status = service_->verifyPayloadApplicable(
197 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
198 &applicable);
199 LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable.";
200 return ExitWhenIdle(status);
201 }
202
Alex Deymo5f528112016-01-27 23:32:36 -0800203 if (FLAGS_follow) {
204 // Register a callback object with the service.
205 callback_ = new UECallback(this);
206 bool bound;
207 if (!service_->bind(callback_, &bound).isOk() || !bound) {
208 LOG(ERROR) << "Failed to bind() the UpdateEngine daemon.";
209 return 1;
210 }
211 keep_running = true;
212 }
213
214 if (FLAGS_update) {
215 std::vector<std::string> headers = base::SplitString(
216 FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
217 std::vector<android::String16> and_headers;
218 for (const auto& header : headers) {
219 and_headers.push_back(android::String16{header.data(), header.size()});
220 }
221 Status status = service_->applyPayload(
222 android::String16{FLAGS_payload.data(), FLAGS_payload.size()},
Alex Deymo95b8f242016-01-28 16:06:57 -0800223 FLAGS_offset,
224 FLAGS_size,
Alex Deymo5f528112016-01-27 23:32:36 -0800225 and_headers);
226 if (!status.isOk())
227 return ExitWhenIdle(status);
228 }
229
230 if (!keep_running)
231 return ExitWhenIdle(EX_OK);
232
Alex Deymo2130ee02016-02-02 18:35:50 -0800233 // When following updates status changes, exit if the update_engine daemon
234 // dies.
235 android::BinderWrapper::Create();
236 android::BinderWrapper::Get()->RegisterForDeathNotifications(
237 android::os::IUpdateEngine::asBinder(service_),
238 base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied,
239 base::Unretained(this)));
240
Alex Deymo5f528112016-01-27 23:32:36 -0800241 return EX_OK;
242}
243
244int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) {
245 if (status.isOk())
246 return ExitWhenIdle(EX_OK);
247 LOG(ERROR) << status.toString8();
248 return ExitWhenIdle(status.exceptionCode());
249}
250
251int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) {
252 auto delayed_exit = base::Bind(
253 &Daemon::QuitWithExitCode, base::Unretained(this), return_code);
254 if (!brillo::MessageLoop::current()->PostTask(delayed_exit))
255 return 1;
256 return EX_OK;
257}
258
Alex Deymo2130ee02016-02-02 18:35:50 -0800259void UpdateEngineClientAndroid::UpdateEngineServiceDied() {
260 LOG(ERROR) << "UpdateEngineService died.";
261 QuitWithExitCode(1);
262}
263
Alex Deymo5f528112016-01-27 23:32:36 -0800264} // namespace internal
265} // namespace chromeos_update_engine
266
267int main(int argc, char** argv) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800268 chromeos_update_engine::internal::UpdateEngineClientAndroid client(argc,
269 argv);
Alex Deymo5f528112016-01-27 23:32:36 -0800270 return client.Run();
271}