blob: a44c10bb7a0a102f20fb50b2e491de26766eba1a [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>
Josh Gao997cfac2018-07-25 18:15:52 -070042#include <cutils/sockets.h>
43#include <log/log_properties.h>
44
45#include "adb.h"
46#include "adb_io.h"
47#include "adb_unique_fd.h"
48#include "adb_utils.h"
49#include "services.h"
50#include "socket_spec.h"
51#include "sysdeps.h"
52#include "transport.h"
53
54#include "daemon/file_sync_service.h"
55#include "daemon/framebuffer_service.h"
Josh Gao776c2ec2019-01-22 19:36:15 -080056#include "daemon/restart_service.h"
Josh Gao997cfac2018-07-25 18:15:52 -070057#include "daemon/shell_service.h"
58
Josh Gao997cfac2018-07-25 18:15:52 -070059
60void reconnect_service(unique_fd fd, atransport* t) {
61 WriteFdExactly(fd.get(), "done");
62 kick_transport(t);
63}
64
Josh Gaoe2615412018-12-13 13:06:03 -080065unique_fd reverse_service(std::string_view command, atransport* transport) {
66 // TODO: Switch handle_forward_request to std::string_view.
67 std::string str(command);
68
Josh Gao997cfac2018-07-25 18:15:52 -070069 int s[2];
70 if (adb_socketpair(s)) {
71 PLOG(ERROR) << "cannot create service socket pair.";
72 return unique_fd{};
73 }
74 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
Josh Gaoe2615412018-12-13 13:06:03 -080075 if (!handle_forward_request(str.c_str(), transport, s[1])) {
Josh Gao997cfac2018-07-25 18:15:52 -070076 SendFail(s[1], "not a reverse forwarding command");
77 }
78 adb_close(s[1]);
79 return unique_fd{s[0]};
80}
81
82// Shell service string can look like:
83// shell[,arg1,arg2,...]:[command]
Josh Gaoe2615412018-12-13 13:06:03 -080084unique_fd ShellService(std::string_view args, const atransport* transport) {
Josh Gao997cfac2018-07-25 18:15:52 -070085 size_t delimiter_index = args.find(':');
86 if (delimiter_index == std::string::npos) {
87 LOG(ERROR) << "No ':' found in shell service arguments: " << args;
88 return unique_fd{};
89 }
90
Josh Gaoe2615412018-12-13 13:06:03 -080091 // TODO: android::base::Split(const std::string_view&, ...)
92 std::string service_args(args.substr(0, delimiter_index));
93 std::string command(args.substr(delimiter_index + 1));
Josh Gao997cfac2018-07-25 18:15:52 -070094
95 // Defaults:
96 // PTY for interactive, raw for non-interactive.
97 // No protocol.
98 // $TERM set to "dumb".
99 SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw);
100 SubprocessProtocol protocol = SubprocessProtocol::kNone;
101 std::string terminal_type = "dumb";
102
103 for (const std::string& arg : android::base::Split(service_args, ",")) {
104 if (arg == kShellServiceArgRaw) {
105 type = SubprocessType::kRaw;
106 } else if (arg == kShellServiceArgPty) {
107 type = SubprocessType::kPty;
108 } else if (arg == kShellServiceArgShellProtocol) {
109 protocol = SubprocessProtocol::kShell;
Josh Gaoe2615412018-12-13 13:06:03 -0800110 } else if (arg.starts_with("TERM=")) {
111 terminal_type = arg.substr(strlen("TERM="));
Josh Gao997cfac2018-07-25 18:15:52 -0700112 } else if (!arg.empty()) {
113 // This is not an error to allow for future expansion.
114 LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
115 }
116 }
117
Josh Gaoe2615412018-12-13 13:06:03 -0800118 return StartSubprocess(command, terminal_type.c_str(), type, protocol);
Josh Gao997cfac2018-07-25 18:15:52 -0700119}
120
Josh Gao39c1f4b2018-10-05 17:09:07 -0700121static void spin_service(unique_fd fd) {
122 if (!__android_log_is_debuggable()) {
123 WriteFdExactly(fd.get(), "refusing to spin on non-debuggable build\n");
124 return;
125 }
126
127 // A service that creates an fdevent that's always pending, and then ignores it.
128 unique_fd pipe_read, pipe_write;
129 if (!Pipe(&pipe_read, &pipe_write)) {
130 WriteFdExactly(fd.get(), "failed to create pipe\n");
131 return;
132 }
133
134 fdevent_run_on_main_thread([fd = pipe_read.release()]() {
Josh Gao776c2ec2019-01-22 19:36:15 -0800135 fdevent* fde = fdevent_create(
136 fd, [](int, unsigned, void*) {}, nullptr);
Josh Gao39c1f4b2018-10-05 17:09:07 -0700137 fdevent_add(fde, FDE_READ);
138 });
139
140 WriteFdExactly(fd.get(), "spinning\n");
141}
142
Josh Gao6eb78822018-11-16 15:40:16 -0800143struct ServiceSocket : public asocket {
144 ServiceSocket() {
145 install_local_socket(this);
146 this->enqueue = [](asocket* self, apacket::payload_type data) {
147 return static_cast<ServiceSocket*>(self)->Enqueue(std::move(data));
148 };
149 this->ready = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Ready(); };
150 this->close = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Close(); };
151 }
152 virtual ~ServiceSocket() = default;
153
154 virtual int Enqueue(apacket::payload_type data) { return -1; }
155 virtual void Ready() {}
156 virtual void Close() {
157 if (peer) {
158 peer->peer = nullptr;
159 if (peer->shutdown) {
160 peer->shutdown(peer);
161 }
162 peer->close(peer);
163 }
164
165 remove_socket(this);
166 delete this;
167 }
168};
169
170struct SinkSocket : public ServiceSocket {
171 explicit SinkSocket(size_t byte_count) {
172 LOG(INFO) << "Creating new SinkSocket with capacity " << byte_count;
173 bytes_left_ = byte_count;
174 }
175
176 virtual ~SinkSocket() { LOG(INFO) << "SinkSocket destroyed"; }
177
178 virtual int Enqueue(apacket::payload_type data) override final {
179 if (bytes_left_ <= data.size()) {
180 // Done reading.
181 Close();
182 return -1;
183 }
184
185 bytes_left_ -= data.size();
186 return 0;
187 }
188
189 size_t bytes_left_;
190};
191
192struct SourceSocket : public ServiceSocket {
193 explicit SourceSocket(size_t byte_count) {
194 LOG(INFO) << "Creating new SourceSocket with capacity " << byte_count;
195 bytes_left_ = byte_count;
196 }
197
198 virtual ~SourceSocket() { LOG(INFO) << "SourceSocket destroyed"; }
199
200 void Ready() {
201 size_t len = std::min(bytes_left_, get_max_payload());
202 if (len == 0) {
203 Close();
204 return;
205 }
206
207 Block block(len);
208 memset(block.data(), 0, block.size());
209 peer->enqueue(peer, std::move(block));
210 bytes_left_ -= len;
211 }
212
213 int Enqueue(apacket::payload_type data) { return -1; }
214
215 size_t bytes_left_;
216};
217
218asocket* daemon_service_to_socket(std::string_view name) {
219 if (name == "jdwp") {
220 return create_jdwp_service_socket();
221 } else if (name == "track-jdwp") {
222 return create_jdwp_tracker_service_socket();
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700223 } else if (android::base::ConsumePrefix(&name, "sink:")) {
Josh Gao6eb78822018-11-16 15:40:16 -0800224 uint64_t byte_count = 0;
Josh Gao2df76b72019-02-20 20:00:27 -0800225 if (!ParseUint(&byte_count, name)) {
Josh Gao6eb78822018-11-16 15:40:16 -0800226 return nullptr;
227 }
228 return new SinkSocket(byte_count);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700229 } else if (android::base::ConsumePrefix(&name, "source:")) {
Josh Gao6eb78822018-11-16 15:40:16 -0800230 uint64_t byte_count = 0;
Josh Gao2df76b72019-02-20 20:00:27 -0800231 if (!ParseUint(&byte_count, name)) {
Josh Gao6eb78822018-11-16 15:40:16 -0800232 return nullptr;
233 }
234 return new SourceSocket(byte_count);
235 }
236
237 return nullptr;
238}
239
Josh Gaoe2615412018-12-13 13:06:03 -0800240unique_fd daemon_service_to_fd(std::string_view name, atransport* transport) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800241#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
Alex Buynytskyy05626c12019-02-21 14:22:51 -0800242 if (name.starts_with("abb:") || name.starts_with("abb_exec:")) {
243 return execute_abb_command(name);
Alex Buynytskyy640407d2018-12-12 10:48:50 -0800244 }
245#endif
246
Josh Gao776c2ec2019-01-22 19:36:15 -0800247#if defined(__ANDROID__)
248 if (name.starts_with("framebuffer:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700249 return create_service_thread("fb", framebuffer_service);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700250 } else if (android::base::ConsumePrefix(&name, "remount:")) {
Josh Gao8d1d87e2019-10-11 14:41:19 -0700251 std::string cmd = "/system/bin/remount ";
252 cmd += name;
253 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700254 } else if (android::base::ConsumePrefix(&name, "reboot:")) {
Josh Gao2c356bb2019-10-22 12:30:36 -0700255 std::string cmd = "/system/bin/reboot ";
256 cmd += name;
257 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
Josh Gaoe2615412018-12-13 13:06:03 -0800258 } else if (name.starts_with("root:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700259 return create_service_thread("root", restart_root_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800260 } else if (name.starts_with("unroot:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700261 return create_service_thread("unroot", restart_unroot_service);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700262 } else if (android::base::ConsumePrefix(&name, "backup:")) {
Josh Gaoe2615412018-12-13 13:06:03 -0800263 std::string cmd = "/system/bin/bu backup ";
264 cmd += name;
265 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
266 } else if (name.starts_with("restore:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700267 return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
268 SubprocessProtocol::kNone);
Josh Gao776c2ec2019-01-22 19:36:15 -0800269 } else if (name.starts_with("disable-verity:")) {
Josh Gaof61f4142019-10-22 12:30:30 -0700270 return StartSubprocess("/system/bin/disable-verity", nullptr, SubprocessType::kRaw,
271 SubprocessProtocol::kNone);
Josh Gao776c2ec2019-01-22 19:36:15 -0800272 } else if (name.starts_with("enable-verity:")) {
Josh Gaof61f4142019-10-22 12:30:30 -0700273 return StartSubprocess("/system/bin/enable-verity", nullptr, SubprocessType::kRaw,
274 SubprocessProtocol::kNone);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700275 } else if (android::base::ConsumePrefix(&name, "tcpip:")) {
Josh Gaoe2615412018-12-13 13:06:03 -0800276 std::string str(name);
277
Josh Gao997cfac2018-07-25 18:15:52 -0700278 int port;
Josh Gaoe2615412018-12-13 13:06:03 -0800279 if (sscanf(str.c_str(), "%d", &port) != 1) {
Josh Gao997cfac2018-07-25 18:15:52 -0700280 return unique_fd{};
281 }
282 return create_service_thread("tcp",
283 std::bind(restart_tcp_service, std::placeholders::_1, port));
Josh Gaoe2615412018-12-13 13:06:03 -0800284 } else if (name.starts_with("usb:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700285 return create_service_thread("usb", restart_usb_service);
Josh Gao776c2ec2019-01-22 19:36:15 -0800286 }
287#endif
288
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700289 if (android::base::ConsumePrefix(&name, "dev:")) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800290 return unique_fd{unix_open(name, O_RDWR | O_CLOEXEC)};
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700291 } else if (android::base::ConsumePrefix(&name, "jdwp:")) {
Josh Gao2df76b72019-02-20 20:00:27 -0800292 pid_t pid;
293 if (!ParseUint(&pid, name)) {
294 return unique_fd{};
295 }
296 return create_jdwp_connection_fd(pid);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700297 } else if (android::base::ConsumePrefix(&name, "shell")) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800298 return ShellService(name, transport);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700299 } else if (android::base::ConsumePrefix(&name, "exec:")) {
Josh Gao776c2ec2019-01-22 19:36:15 -0800300 return StartSubprocess(std::string(name), nullptr, SubprocessType::kRaw,
301 SubprocessProtocol::kNone);
302 } else if (name.starts_with("sync:")) {
303 return create_service_thread("sync", file_sync_service);
Elliott Hughesb4dc7be2019-05-03 09:02:45 -0700304 } else if (android::base::ConsumePrefix(&name, "reverse:")) {
Josh Gaoe2615412018-12-13 13:06:03 -0800305 return reverse_service(name, transport);
Josh Gaoe2615412018-12-13 13:06:03 -0800306 } else if (name == "reconnect") {
Josh Gao997cfac2018-07-25 18:15:52 -0700307 return create_service_thread(
308 "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
Josh Gaoe2615412018-12-13 13:06:03 -0800309 } else if (name == "spin") {
Josh Gao39c1f4b2018-10-05 17:09:07 -0700310 return create_service_thread("spin", spin_service);
Josh Gao997cfac2018-07-25 18:15:52 -0700311 }
Josh Gao39c1f4b2018-10-05 17:09:07 -0700312
Josh Gao997cfac2018-07-25 18:15:52 -0700313 return unique_fd{};
314}