blob: 3485807debd66268ed207d739bf64293ed63fb71 [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 Deymo2130ee02016-02-02 18:35:50 -080042#include "update_engine/update_status_utils.h"
Alex Deymo5f528112016-01-27 23:32:36 -080043
44using android::binder::Status;
45
46namespace chromeos_update_engine {
47namespace internal {
48
49class UpdateEngineClientAndroid : public brillo::Daemon {
50 public:
51 UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {
52 }
53
54 int ExitWhenIdle(const Status& status);
55 int ExitWhenIdle(int return_code);
56
57 private:
58 class UECallback : public android::os::BnUpdateEngineCallback {
59 public:
60 UECallback(UpdateEngineClientAndroid* client) : client_(client) {
61 }
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 Deymo2130ee02016-02-02 18:35:50 -080073 // Called whenever the UpdateEngine daemon dies.
74 void UpdateEngineServiceDied();
75
Alex Deymo5f528112016-01-27 23:32:36 -080076 // 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
86Status 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
95Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete(
96 int error_code) {
97 ErrorCode code = static_cast<ErrorCode>(error_code);
98 // TODO(deymo): Print the ErrorCode as a string.
99 LOG(INFO) << "onPayloadApplicationComplete(" << error_code << ")";
100 client_->ExitWhenIdle(code == ErrorCode::kSuccess ? EX_OK : 1);
101 return Status::ok();
102}
103
104int UpdateEngineClientAndroid::OnInit() {
105 int ret = Daemon::OnInit();
106 if (ret != EX_OK)
107 return ret;
108
109 DEFINE_bool(update, false, "Start a new update, if no update in progress.");
110 DEFINE_string(payload,
111 "http://127.0.0.1:8080/payload",
112 "The URI to the update payload to use.");
Alex Deymo95b8f242016-01-28 16:06:57 -0800113 DEFINE_int64(offset, 0,
114 "The offset in the payload where the CrAU update starts. "
115 "Used when --update is passed.");
116 DEFINE_int64(size, 0,
117 "The size of the CrAU part of the payload. If 0 is passed, it "
118 "will be autodetected. Used when --update is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800119 DEFINE_string(headers,
120 "",
Alex Deymo95b8f242016-01-28 16:06:57 -0800121 "A list of key-value pairs, one element of the list per line. "
122 "Used when --update is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800123
124 DEFINE_bool(suspend, false, "Suspend an ongoing update and exit.");
125 DEFINE_bool(resume, false, "Resume a suspended update.");
126 DEFINE_bool(cancel, false, "Cancel the ongoing update and exit.");
127 DEFINE_bool(follow,
128 false,
129 "Follow status update changes until a final state is reached. "
130 "Exit status is 0 if the update succeeded, and 1 otherwise.");
131
132 // Boilerplate init commands.
133 base::CommandLine::Init(argc_, argv_);
134 brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
135 if (argc_ == 1) {
136 LOG(ERROR) << "Nothing to do. Run with --help for help.";
137 return 1;
138 }
139
140 // Ensure there are no positional arguments.
141 const std::vector<std::string> positional_args =
142 base::CommandLine::ForCurrentProcess()->GetArgs();
143 if (!positional_args.empty()) {
144 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
145 << "'. If you want to pass a value to a flag, pass it as "
146 "--flag=value.";
147 return 1;
148 }
149
150 bool keep_running = false;
Alex Deymo95224dd2016-01-29 11:18:35 -0800151 brillo::InitLog(brillo::kLogToStderr);
Alex Deymo2130ee02016-02-02 18:35:50 -0800152
153 // Initialize a binder watcher early in the process before any interaction
154 // with the binder driver.
155 binder_watcher_.Init();
156
Alex Deymo5f528112016-01-27 23:32:36 -0800157 android::status_t status = android::getService(
158 android::String16("android.os.UpdateEngineService"), &service_);
159 if (status != android::OK) {
160 LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: "
161 << Status::fromStatusT(status).toString8();
Alex Deymo2130ee02016-02-02 18:35:50 -0800162 return ExitWhenIdle(1);
Alex Deymo5f528112016-01-27 23:32:36 -0800163 }
164
165 if (FLAGS_suspend) {
166 return ExitWhenIdle(service_->suspend());
167 }
168
169 if (FLAGS_resume) {
170 return ExitWhenIdle(service_->resume());
171 }
172
173 if (FLAGS_cancel) {
174 return ExitWhenIdle(service_->cancel());
175 }
176
177 if (FLAGS_follow) {
178 // Register a callback object with the service.
179 callback_ = new UECallback(this);
180 bool bound;
181 if (!service_->bind(callback_, &bound).isOk() || !bound) {
182 LOG(ERROR) << "Failed to bind() the UpdateEngine daemon.";
183 return 1;
184 }
185 keep_running = true;
186 }
187
188 if (FLAGS_update) {
189 std::vector<std::string> headers = base::SplitString(
190 FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
191 std::vector<android::String16> and_headers;
192 for (const auto& header : headers) {
193 and_headers.push_back(android::String16{header.data(), header.size()});
194 }
195 Status status = service_->applyPayload(
196 android::String16{FLAGS_payload.data(), FLAGS_payload.size()},
Alex Deymo95b8f242016-01-28 16:06:57 -0800197 FLAGS_offset,
198 FLAGS_size,
Alex Deymo5f528112016-01-27 23:32:36 -0800199 and_headers);
200 if (!status.isOk())
201 return ExitWhenIdle(status);
202 }
203
204 if (!keep_running)
205 return ExitWhenIdle(EX_OK);
206
Alex Deymo2130ee02016-02-02 18:35:50 -0800207 // When following updates status changes, exit if the update_engine daemon
208 // dies.
209 android::BinderWrapper::Create();
210 android::BinderWrapper::Get()->RegisterForDeathNotifications(
211 android::os::IUpdateEngine::asBinder(service_),
212 base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied,
213 base::Unretained(this)));
214
Alex Deymo5f528112016-01-27 23:32:36 -0800215 return EX_OK;
216}
217
218int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) {
219 if (status.isOk())
220 return ExitWhenIdle(EX_OK);
221 LOG(ERROR) << status.toString8();
222 return ExitWhenIdle(status.exceptionCode());
223}
224
225int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) {
226 auto delayed_exit = base::Bind(
227 &Daemon::QuitWithExitCode, base::Unretained(this), return_code);
228 if (!brillo::MessageLoop::current()->PostTask(delayed_exit))
229 return 1;
230 return EX_OK;
231}
232
Alex Deymo2130ee02016-02-02 18:35:50 -0800233void UpdateEngineClientAndroid::UpdateEngineServiceDied() {
234 LOG(ERROR) << "UpdateEngineService died.";
235 QuitWithExitCode(1);
236}
237
Alex Deymo5f528112016-01-27 23:32:36 -0800238} // namespace internal
239} // namespace chromeos_update_engine
240
241int main(int argc, char** argv) {
242 chromeos_update_engine::internal::UpdateEngineClientAndroid client(
243 argc, argv);
244 return client.Run();
245}