blob: 4ec90d27ce27ba57b4541887be881fadbf980833 [file] [log] [blame]
Josh Gao997cfac2018-07-25 18:15:52 -07001/*
2 * Copyright (C) 2018 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#define TRACE_TAG SERVICES
18
19#include "sysdeps.h"
20
21#include <errno.h>
22#include <netdb.h>
23#include <netinet/in.h>
24#include <stddef.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/ioctl.h>
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -070029#include <sys/socket.h>
30#include <sys/un.h>
Josh Gao997cfac2018-07-25 18:15:52 -070031#include <unistd.h>
32
33#include <thread>
34
35#include <android-base/file.h>
Josh Gao6eb78822018-11-16 15:40:16 -080036#include <android-base/parseint.h>
Josh Gao997cfac2018-07-25 18:15:52 -070037#include <android-base/parsenetaddress.h>
38#include <android-base/properties.h>
39#include <android-base/stringprintf.h>
40#include <android-base/strings.h>
41#include <android-base/unique_fd.h>
Tianjie Xub6d7ea12019-11-06 21:01:57 -080042#include <cutils/android_reboot.h>
Josh Gao997cfac2018-07-25 18:15:52 -070043#include <cutils/sockets.h>
44#include <log/log_properties.h>
45
46#include "adb.h"
47#include "adb_io.h"
48#include "adb_unique_fd.h"
49#include "adb_utils.h"
50#include "services.h"
51#include "socket_spec.h"
52#include "sysdeps.h"
53#include "transport.h"
54
55#include "daemon/file_sync_service.h"
56#include "daemon/framebuffer_service.h"
Josh Gao776c2ec2019-01-22 19:36:15 -080057#include "daemon/restart_service.h"
Josh Gao997cfac2018-07-25 18:15:52 -070058#include "daemon/shell_service.h"
59
Josh Gao997cfac2018-07-25 18:15:52 -070060
61void reconnect_service(unique_fd fd, atransport* t) {
62 WriteFdExactly(fd.get(), "done");
63 kick_transport(t);
64}
65
Josh Gaoe2615412018-12-13 13:06:03 -080066unique_fd reverse_service(std::string_view command, atransport* transport) {
67 // TODO: Switch handle_forward_request to std::string_view.
68 std::string str(command);
69
Josh Gao997cfac2018-07-25 18:15:52 -070070 int s[2];
71 if (adb_socketpair(s)) {
72 PLOG(ERROR) << "cannot create service socket pair.";
73 return unique_fd{};
74 }
75 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
Josh Gaoe2615412018-12-13 13:06:03 -080076 if (!handle_forward_request(str.c_str(), transport, s[1])) {
Josh Gao997cfac2018-07-25 18:15:52 -070077 SendFail(s[1], "not a reverse forwarding command");
78 }
79 adb_close(s[1]);
80 return unique_fd{s[0]};
81}
82
83// Shell service string can look like:
84// shell[,arg1,arg2,...]:[command]
Josh Gaoe2615412018-12-13 13:06:03 -080085unique_fd ShellService(std::string_view args, const atransport* transport) {
Josh Gao997cfac2018-07-25 18:15:52 -070086 size_t delimiter_index = args.find(':');
87 if (delimiter_index == std::string::npos) {
88 LOG(ERROR) << "No ':' found in shell service arguments: " << args;
89 return unique_fd{};
90 }
91
Josh Gaoe2615412018-12-13 13:06:03 -080092 // TODO: android::base::Split(const std::string_view&, ...)
93 std::string service_args(args.substr(0, delimiter_index));
94 std::string command(args.substr(delimiter_index + 1));
Josh Gao997cfac2018-07-25 18:15:52 -070095
96 // Defaults:
97 // PTY for interactive, raw for non-interactive.
98 // No protocol.
99 // $TERM set to "dumb".
100 SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw);
101 SubprocessProtocol protocol = SubprocessProtocol::kNone;
102 std::string terminal_type = "dumb";
103
104 for (const std::string& arg : android::base::Split(service_args, ",")) {
105 if (arg == kShellServiceArgRaw) {
106 type = SubprocessType::kRaw;
107 } else if (arg == kShellServiceArgPty) {
108 type = SubprocessType::kPty;
109 } else if (arg == kShellServiceArgShellProtocol) {
110 protocol = SubprocessProtocol::kShell;
Josh Gaoe2615412018-12-13 13:06:03 -0800111 } else if (arg.starts_with("TERM=")) {
112 terminal_type = arg.substr(strlen("TERM="));
Josh Gao997cfac2018-07-25 18:15:52 -0700113 } else if (!arg.empty()) {
114 // This is not an error to allow for future expansion.
115 LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
116 }
117 }
118
Josh Gaoe2615412018-12-13 13:06:03 -0800119 return StartSubprocess(command, terminal_type.c_str(), type, protocol);
Josh Gao997cfac2018-07-25 18:15:52 -0700120}
121
Josh Gao39c1f4b2018-10-05 17:09:07 -0700122static void spin_service(unique_fd fd) {
123 if (!__android_log_is_debuggable()) {
124 WriteFdExactly(fd.get(), "refusing to spin on non-debuggable build\n");
125 return;
126 }
127
128 // A service that creates an fdevent that's always pending, and then ignores it.
129 unique_fd pipe_read, pipe_write;
130 if (!Pipe(&pipe_read, &pipe_write)) {
131 WriteFdExactly(fd.get(), "failed to create pipe\n");
132 return;
133 }
134
135 fdevent_run_on_main_thread([fd = pipe_read.release()]() {
Josh Gao776c2ec2019-01-22 19:36:15 -0800136 fdevent* fde = fdevent_create(
137 fd, [](int, unsigned, void*) {}, nullptr);
Josh Gao39c1f4b2018-10-05 17:09:07 -0700138 fdevent_add(fde, FDE_READ);
139 });
140
141 WriteFdExactly(fd.get(), "spinning\n");
142}
143
Tianjie Xub6d7ea12019-11-06 21:01:57 -0800144[[maybe_unused]] static unique_fd reboot_device(const std::string& name) {
145#if defined(__ANDROID_RECOVERY__)
146 if (!__android_log_is_debuggable()) {
147 auto reboot_service = [name](unique_fd fd) {
148 std::string reboot_string = android::base::StringPrintf("reboot,%s", name.c_str());
149 if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
150 WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str());
151 return;
152 }
153 while (true) pause();
154 };
155 return create_service_thread("reboot", reboot_service);
156 }
157#endif
158 // Fall through
159 std::string cmd = "/system/bin/reboot ";
160 cmd += name;
161 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
162}
163
Josh Gao6eb78822018-11-16 15:40:16 -0800164struct ServiceSocket : public asocket {
165 ServiceSocket() {
166 install_local_socket(this);
167 this->enqueue = [](asocket* self, apacket::payload_type data) {
168 return static_cast<ServiceSocket*>(self)->Enqueue(std::move(data));
169 };
170 this->ready = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Ready(); };
171 this->close = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Close(); };
172 }
173 virtual ~ServiceSocket() = default;
174
175 virtual int Enqueue(apacket::payload_type data) { return -1; }
176 virtual void Ready() {}
177 virtual void Close() {
178 if (peer) {
179 peer->peer = nullptr;
180 if (peer->shutdown) {
181 peer->shutdown(peer);
182 }
183 peer->close(peer);
184 }
185
186 remove_socket(this);
187 delete this;
188 }
189};
190
191struct SinkSocket : public ServiceSocket {
192 explicit SinkSocket(size_t byte_count) {
193 LOG(INFO) << "Creating new SinkSocket with capacity " << byte_count;
194 bytes_left_ = byte_count;
195 }
196
197 virtual ~SinkSocket() { LOG(INFO) << "SinkSocket destroyed"; }
198
199 virtual int Enqueue(apacket::payload_type data) override final {
200 if (bytes_left_ <= data.size()) {
201 // Done reading.
202 Close();
203 return -1;
204 }
205
206 bytes_left_ -= data.size();
207 return 0;
208 }
209
210 size_t bytes_left_;
211};
212
213struct SourceSocket : public ServiceSocket {
214 explicit SourceSocket(size_t byte_count) {
215 LOG(INFO) << "Creating new SourceSocket with capacity " << byte_count;
216 bytes_left_ = byte_count;
217 }
218
219 virtual ~SourceSocket() { LOG(INFO) << "SourceSocket destroyed"; }
220
221 void Ready() {
222 size_t len = std::min(bytes_left_, get_max_payload());
223 if (len == 0) {
224 Close();
225 return;
226 }
227
228 Block block(len);
229 memset(block.data(), 0, block.size());
230 peer->enqueue(peer, std::move(block));
231 bytes_left_ -= len;
232 }
233
234 int Enqueue(apacket::payload_type data) { return -1; }
235
236 size_t bytes_left_;
237};
238
239asocket* daemon_service_to_socket(std::string_view name) {
240 if (name == "jdwp") {
241 return create_jdwp_service_socket();
242 } else if (name == "track-jdwp") {
243 return create_jdwp_tracker_service_socket();
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700244 } else if (android::base::ConsumePrefix(&name, "sink:")) {
Josh Gao6eb78822018-11-16 15:40:16 -0800245 uint64_t byte_count = 0;
Josh Gao2df76b72019-02-20 20:00:27 -0800246 if (!ParseUint(&byte_count, name)) {
Josh Gao6eb78822018-11-16 15:40:16 -0800247 return nullptr;
248 }
249 return new SinkSocket(byte_count);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700250 } else if (android::base::ConsumePrefix(&name, "source:")) {
Josh Gao6eb78822018-11-16 15:40:16 -0800251 uint64_t byte_count = 0;
Josh Gao2df76b72019-02-20 20:00:27 -0800252 if (!ParseUint(&byte_count, name)) {
Josh Gao6eb78822018-11-16 15:40:16 -0800253 return nullptr;
254 }
255 return new SourceSocket(byte_count);
256 }
257
258 return nullptr;
259}
260
Josh Gaoe2615412018-12-13 13:06:03 -0800261unique_fd daemon_service_to_fd(std::string_view name, atransport* transport) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800262#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
Alex Buynytskyy05626c12019-02-21 14:22:51 -0800263 if (name.starts_with("abb:") || name.starts_with("abb_exec:")) {
264 return execute_abb_command(name);
Alex Buynytskyy640407d2018-12-12 10:48:50 -0800265 }
266#endif
267
Josh Gao776c2ec2019-01-22 19:36:15 -0800268#if defined(__ANDROID__)
269 if (name.starts_with("framebuffer:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700270 return create_service_thread("fb", framebuffer_service);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700271 } else if (android::base::ConsumePrefix(&name, "remount:")) {
Josh Gao8d1d87e2019-10-11 14:41:19 -0700272 std::string cmd = "/system/bin/remount ";
273 cmd += name;
274 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700275 } else if (android::base::ConsumePrefix(&name, "reboot:")) {
Tianjie Xub6d7ea12019-11-06 21:01:57 -0800276 return reboot_device(std::string(name));
Josh Gaoe2615412018-12-13 13:06:03 -0800277 } else if (name.starts_with("root:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700278 return create_service_thread("root", restart_root_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800279 } else if (name.starts_with("unroot:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700280 return create_service_thread("unroot", restart_unroot_service);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700281 } else if (android::base::ConsumePrefix(&name, "backup:")) {
Josh Gaoe2615412018-12-13 13:06:03 -0800282 std::string cmd = "/system/bin/bu backup ";
283 cmd += name;
284 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
285 } else if (name.starts_with("restore:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700286 return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
287 SubprocessProtocol::kNone);
Josh Gao776c2ec2019-01-22 19:36:15 -0800288 } else if (name.starts_with("disable-verity:")) {
Josh Gaof61f4142019-10-22 12:30:30 -0700289 return StartSubprocess("/system/bin/disable-verity", nullptr, SubprocessType::kRaw,
290 SubprocessProtocol::kNone);
Josh Gao776c2ec2019-01-22 19:36:15 -0800291 } else if (name.starts_with("enable-verity:")) {
Josh Gaof61f4142019-10-22 12:30:30 -0700292 return StartSubprocess("/system/bin/enable-verity", nullptr, SubprocessType::kRaw,
293 SubprocessProtocol::kNone);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700294 } else if (android::base::ConsumePrefix(&name, "tcpip:")) {
Josh Gaoe2615412018-12-13 13:06:03 -0800295 std::string str(name);
296
Josh Gao997cfac2018-07-25 18:15:52 -0700297 int port;
Josh Gaoe2615412018-12-13 13:06:03 -0800298 if (sscanf(str.c_str(), "%d", &port) != 1) {
Josh Gao997cfac2018-07-25 18:15:52 -0700299 return unique_fd{};
300 }
301 return create_service_thread("tcp",
302 std::bind(restart_tcp_service, std::placeholders::_1, port));
Josh Gaoe2615412018-12-13 13:06:03 -0800303 } else if (name.starts_with("usb:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700304 return create_service_thread("usb", restart_usb_service);
Josh Gao776c2ec2019-01-22 19:36:15 -0800305 }
306#endif
307
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700308 if (android::base::ConsumePrefix(&name, "dev:")) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800309 return unique_fd{unix_open(name, O_RDWR | O_CLOEXEC)};
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700310 } else if (android::base::ConsumePrefix(&name, "jdwp:")) {
Josh Gao2df76b72019-02-20 20:00:27 -0800311 pid_t pid;
312 if (!ParseUint(&pid, name)) {
313 return unique_fd{};
314 }
315 return create_jdwp_connection_fd(pid);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700316 } else if (android::base::ConsumePrefix(&name, "shell")) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800317 return ShellService(name, transport);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700318 } else if (android::base::ConsumePrefix(&name, "exec:")) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800319 return StartSubprocess(std::string(name), nullptr, SubprocessType::kRaw,
320 SubprocessProtocol::kNone);
321 } else if (name.starts_with("sync:")) {
322 return create_service_thread("sync", file_sync_service);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700323 } else if (android::base::ConsumePrefix(&name, "reverse:")) {
Josh Gaoe2615412018-12-13 13:06:03 -0800324 return reverse_service(name, transport);
Josh Gaoe2615412018-12-13 13:06:03 -0800325 } else if (name == "reconnect") {
Josh Gao997cfac2018-07-25 18:15:52 -0700326 return create_service_thread(
327 "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
Josh Gaoe2615412018-12-13 13:06:03 -0800328 } else if (name == "spin") {
Josh Gao39c1f4b2018-10-05 17:09:07 -0700329 return create_service_thread("spin", spin_service);
Josh Gao997cfac2018-07-25 18:15:52 -0700330 }
Josh Gao39c1f4b2018-10-05 17:09:07 -0700331
Josh Gao997cfac2018-07-25 18:15:52 -0700332 return unique_fd{};
333}