blob: 181a8c5f044d2e22743fcc9b2550e03035dc1d68 [file] [log] [blame]
Josh Gao5607e922018-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 Valsaraju137367f2018-08-02 15:02:06 -070029#include <sys/socket.h>
30#include <sys/un.h>
Josh Gao5607e922018-07-25 18:15:52 -070031#include <unistd.h>
32
33#include <thread>
34
35#include <android-base/file.h>
Josh Gao3edf8072018-11-16 15:40:16 -080036#include <android-base/parseint.h>
Josh Gao5607e922018-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 Gao5607e922018-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 Gao0560feb2019-01-22 19:36:15 -080056#include "daemon/reboot_service.h"
Josh Gao0560feb2019-01-22 19:36:15 -080057#include "daemon/restart_service.h"
Josh Gao5607e922018-07-25 18:15:52 -070058#include "daemon/set_verity_enable_state_service.h"
59#include "daemon/shell_service.h"
60
Josh Gao5607e922018-07-25 18:15:52 -070061
62void reconnect_service(unique_fd fd, atransport* t) {
63 WriteFdExactly(fd.get(), "done");
64 kick_transport(t);
65}
66
Josh Gaof0fa1e42018-12-13 13:06:03 -080067unique_fd reverse_service(std::string_view command, atransport* transport) {
68 // TODO: Switch handle_forward_request to std::string_view.
69 std::string str(command);
70
Josh Gao5607e922018-07-25 18:15:52 -070071 int s[2];
72 if (adb_socketpair(s)) {
73 PLOG(ERROR) << "cannot create service socket pair.";
74 return unique_fd{};
75 }
76 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
Josh Gaof0fa1e42018-12-13 13:06:03 -080077 if (!handle_forward_request(str.c_str(), transport, s[1])) {
Josh Gao5607e922018-07-25 18:15:52 -070078 SendFail(s[1], "not a reverse forwarding command");
79 }
80 adb_close(s[1]);
81 return unique_fd{s[0]};
82}
83
84// Shell service string can look like:
85// shell[,arg1,arg2,...]:[command]
Josh Gaof0fa1e42018-12-13 13:06:03 -080086unique_fd ShellService(std::string_view args, const atransport* transport) {
Josh Gao5607e922018-07-25 18:15:52 -070087 size_t delimiter_index = args.find(':');
88 if (delimiter_index == std::string::npos) {
89 LOG(ERROR) << "No ':' found in shell service arguments: " << args;
90 return unique_fd{};
91 }
92
Josh Gaof0fa1e42018-12-13 13:06:03 -080093 // TODO: android::base::Split(const std::string_view&, ...)
94 std::string service_args(args.substr(0, delimiter_index));
95 std::string command(args.substr(delimiter_index + 1));
Josh Gao5607e922018-07-25 18:15:52 -070096
97 // Defaults:
98 // PTY for interactive, raw for non-interactive.
99 // No protocol.
100 // $TERM set to "dumb".
101 SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw);
102 SubprocessProtocol protocol = SubprocessProtocol::kNone;
103 std::string terminal_type = "dumb";
104
105 for (const std::string& arg : android::base::Split(service_args, ",")) {
106 if (arg == kShellServiceArgRaw) {
107 type = SubprocessType::kRaw;
108 } else if (arg == kShellServiceArgPty) {
109 type = SubprocessType::kPty;
110 } else if (arg == kShellServiceArgShellProtocol) {
111 protocol = SubprocessProtocol::kShell;
Josh Gaof0fa1e42018-12-13 13:06:03 -0800112 } else if (arg.starts_with("TERM=")) {
113 terminal_type = arg.substr(strlen("TERM="));
Josh Gao5607e922018-07-25 18:15:52 -0700114 } else if (!arg.empty()) {
115 // This is not an error to allow for future expansion.
116 LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
117 }
118 }
119
Josh Gaof0fa1e42018-12-13 13:06:03 -0800120 return StartSubprocess(command, terminal_type.c_str(), type, protocol);
Josh Gao5607e922018-07-25 18:15:52 -0700121}
122
Josh Gaod727d312018-10-05 17:09:07 -0700123static void spin_service(unique_fd fd) {
124 if (!__android_log_is_debuggable()) {
125 WriteFdExactly(fd.get(), "refusing to spin on non-debuggable build\n");
126 return;
127 }
128
129 // A service that creates an fdevent that's always pending, and then ignores it.
130 unique_fd pipe_read, pipe_write;
131 if (!Pipe(&pipe_read, &pipe_write)) {
132 WriteFdExactly(fd.get(), "failed to create pipe\n");
133 return;
134 }
135
136 fdevent_run_on_main_thread([fd = pipe_read.release()]() {
Josh Gao0560feb2019-01-22 19:36:15 -0800137 fdevent* fde = fdevent_create(
138 fd, [](int, unsigned, void*) {}, nullptr);
Josh Gaod727d312018-10-05 17:09:07 -0700139 fdevent_add(fde, FDE_READ);
140 });
141
142 WriteFdExactly(fd.get(), "spinning\n");
143}
144
Josh Gao3edf8072018-11-16 15:40:16 -0800145struct ServiceSocket : public asocket {
146 ServiceSocket() {
147 install_local_socket(this);
148 this->enqueue = [](asocket* self, apacket::payload_type data) {
149 return static_cast<ServiceSocket*>(self)->Enqueue(std::move(data));
150 };
151 this->ready = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Ready(); };
152 this->close = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Close(); };
153 }
154 virtual ~ServiceSocket() = default;
155
156 virtual int Enqueue(apacket::payload_type data) { return -1; }
157 virtual void Ready() {}
158 virtual void Close() {
159 if (peer) {
160 peer->peer = nullptr;
161 if (peer->shutdown) {
162 peer->shutdown(peer);
163 }
164 peer->close(peer);
165 }
166
167 remove_socket(this);
168 delete this;
169 }
170};
171
172struct SinkSocket : public ServiceSocket {
173 explicit SinkSocket(size_t byte_count) {
174 LOG(INFO) << "Creating new SinkSocket with capacity " << byte_count;
175 bytes_left_ = byte_count;
176 }
177
178 virtual ~SinkSocket() { LOG(INFO) << "SinkSocket destroyed"; }
179
180 virtual int Enqueue(apacket::payload_type data) override final {
181 if (bytes_left_ <= data.size()) {
182 // Done reading.
183 Close();
184 return -1;
185 }
186
187 bytes_left_ -= data.size();
188 return 0;
189 }
190
191 size_t bytes_left_;
192};
193
194struct SourceSocket : public ServiceSocket {
195 explicit SourceSocket(size_t byte_count) {
196 LOG(INFO) << "Creating new SourceSocket with capacity " << byte_count;
197 bytes_left_ = byte_count;
198 }
199
200 virtual ~SourceSocket() { LOG(INFO) << "SourceSocket destroyed"; }
201
202 void Ready() {
203 size_t len = std::min(bytes_left_, get_max_payload());
204 if (len == 0) {
205 Close();
206 return;
207 }
208
209 Block block(len);
210 memset(block.data(), 0, block.size());
211 peer->enqueue(peer, std::move(block));
212 bytes_left_ -= len;
213 }
214
215 int Enqueue(apacket::payload_type data) { return -1; }
216
217 size_t bytes_left_;
218};
219
220asocket* daemon_service_to_socket(std::string_view name) {
221 if (name == "jdwp") {
222 return create_jdwp_service_socket();
223 } else if (name == "track-jdwp") {
224 return create_jdwp_tracker_service_socket();
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700225 } else if (android::base::ConsumePrefix(&name, "sink:")) {
Josh Gao3edf8072018-11-16 15:40:16 -0800226 uint64_t byte_count = 0;
Josh Gao10779bd2019-02-20 20:00:27 -0800227 if (!ParseUint(&byte_count, name)) {
Josh Gao3edf8072018-11-16 15:40:16 -0800228 return nullptr;
229 }
230 return new SinkSocket(byte_count);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700231 } else if (android::base::ConsumePrefix(&name, "source:")) {
Josh Gao3edf8072018-11-16 15:40:16 -0800232 uint64_t byte_count = 0;
Josh Gao10779bd2019-02-20 20:00:27 -0800233 if (!ParseUint(&byte_count, name)) {
Josh Gao3edf8072018-11-16 15:40:16 -0800234 return nullptr;
235 }
236 return new SourceSocket(byte_count);
237 }
238
239 return nullptr;
240}
241
Josh Gaof0fa1e42018-12-13 13:06:03 -0800242unique_fd daemon_service_to_fd(std::string_view name, atransport* transport) {
Josh Gao0560feb2019-01-22 19:36:15 -0800243#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
Alex Buynytskyy4f3fa052019-02-21 14:22:51 -0800244 if (name.starts_with("abb:") || name.starts_with("abb_exec:")) {
245 return execute_abb_command(name);
Alex Buynytskyyef34f012018-12-12 10:48:50 -0800246 }
247#endif
248
Josh Gao0560feb2019-01-22 19:36:15 -0800249#if defined(__ANDROID__)
250 if (name.starts_with("framebuffer:")) {
Josh Gao5607e922018-07-25 18:15:52 -0700251 return create_service_thread("fb", framebuffer_service);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700252 } else if (android::base::ConsumePrefix(&name, "remount:")) {
Josh Gao15406472019-10-11 14:41:19 -0700253 std::string cmd = "/system/bin/remount ";
254 cmd += name;
255 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700256 } else if (android::base::ConsumePrefix(&name, "reboot:")) {
Josh Gao10779bd2019-02-20 20:00:27 -0800257 std::string arg(name);
Josh Gao5607e922018-07-25 18:15:52 -0700258 return create_service_thread("reboot",
259 std::bind(reboot_service, std::placeholders::_1, arg));
Josh Gaof0fa1e42018-12-13 13:06:03 -0800260 } else if (name.starts_with("root:")) {
Josh Gao5607e922018-07-25 18:15:52 -0700261 return create_service_thread("root", restart_root_service);
Josh Gaof0fa1e42018-12-13 13:06:03 -0800262 } else if (name.starts_with("unroot:")) {
Josh Gao5607e922018-07-25 18:15:52 -0700263 return create_service_thread("unroot", restart_unroot_service);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700264 } else if (android::base::ConsumePrefix(&name, "backup:")) {
Josh Gaof0fa1e42018-12-13 13:06:03 -0800265 std::string cmd = "/system/bin/bu backup ";
266 cmd += name;
267 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
268 } else if (name.starts_with("restore:")) {
Josh Gao5607e922018-07-25 18:15:52 -0700269 return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
270 SubprocessProtocol::kNone);
Josh Gao0560feb2019-01-22 19:36:15 -0800271 } else if (name.starts_with("disable-verity:")) {
272 return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service,
273 std::placeholders::_1, false));
274 } else if (name.starts_with("enable-verity:")) {
275 return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service,
276 std::placeholders::_1, true));
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700277 } else if (android::base::ConsumePrefix(&name, "tcpip:")) {
Josh Gaof0fa1e42018-12-13 13:06:03 -0800278 std::string str(name);
279
Josh Gao5607e922018-07-25 18:15:52 -0700280 int port;
Josh Gaof0fa1e42018-12-13 13:06:03 -0800281 if (sscanf(str.c_str(), "%d", &port) != 1) {
Josh Gao5607e922018-07-25 18:15:52 -0700282 return unique_fd{};
283 }
284 return create_service_thread("tcp",
285 std::bind(restart_tcp_service, std::placeholders::_1, port));
Josh Gaof0fa1e42018-12-13 13:06:03 -0800286 } else if (name.starts_with("usb:")) {
Josh Gao5607e922018-07-25 18:15:52 -0700287 return create_service_thread("usb", restart_usb_service);
Josh Gao0560feb2019-01-22 19:36:15 -0800288 }
289#endif
290
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700291 if (android::base::ConsumePrefix(&name, "dev:")) {
Josh Gao0560feb2019-01-22 19:36:15 -0800292 return unique_fd{unix_open(name, O_RDWR | O_CLOEXEC)};
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700293 } else if (android::base::ConsumePrefix(&name, "jdwp:")) {
Josh Gao10779bd2019-02-20 20:00:27 -0800294 pid_t pid;
295 if (!ParseUint(&pid, name)) {
296 return unique_fd{};
297 }
298 return create_jdwp_connection_fd(pid);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700299 } else if (android::base::ConsumePrefix(&name, "shell")) {
Josh Gao0560feb2019-01-22 19:36:15 -0800300 return ShellService(name, transport);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700301 } else if (android::base::ConsumePrefix(&name, "exec:")) {
Josh Gao0560feb2019-01-22 19:36:15 -0800302 return StartSubprocess(std::string(name), nullptr, SubprocessType::kRaw,
303 SubprocessProtocol::kNone);
304 } else if (name.starts_with("sync:")) {
305 return create_service_thread("sync", file_sync_service);
Elliott Hughes0d1e8fd2019-05-03 09:02:45 -0700306 } else if (android::base::ConsumePrefix(&name, "reverse:")) {
Josh Gaof0fa1e42018-12-13 13:06:03 -0800307 return reverse_service(name, transport);
Josh Gaof0fa1e42018-12-13 13:06:03 -0800308 } else if (name == "reconnect") {
Josh Gao5607e922018-07-25 18:15:52 -0700309 return create_service_thread(
310 "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
Josh Gaof0fa1e42018-12-13 13:06:03 -0800311 } else if (name == "spin") {
Josh Gaod727d312018-10-05 17:09:07 -0700312 return create_service_thread("spin", spin_service);
Josh Gao5607e922018-07-25 18:15:52 -0700313 }
Josh Gaod727d312018-10-05 17:09:07 -0700314
Josh Gao5607e922018-07-25 18:15:52 -0700315 return unique_fd{};
316}