Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Valsaraju | 137367f | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <thread> |
| 34 | |
| 35 | #include <android-base/file.h> |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 37 | #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 Xu | 18a0beb | 2019-11-06 21:01:57 -0800 | [diff] [blame] | 42 | #include <cutils/android_reboot.h> |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 43 | #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" |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 52 | #include "transport.h" |
| 53 | |
| 54 | #include "daemon/file_sync_service.h" |
| 55 | #include "daemon/framebuffer_service.h" |
Josh Gao | bd77521 | 2020-02-26 16:39:20 -0800 | [diff] [blame] | 56 | #include "daemon/logging.h" |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 57 | #include "daemon/restart_service.h" |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 58 | #include "daemon/shell_service.h" |
| 59 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 60 | void reconnect_service(unique_fd fd, atransport* t) { |
| 61 | WriteFdExactly(fd.get(), "done"); |
| 62 | kick_transport(t); |
| 63 | } |
| 64 | |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 65 | unique_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 Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 69 | 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 75 | if (!handle_forward_request(str.c_str(), transport, s[1])) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 76 | 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 84 | unique_fd ShellService(std::string_view args, const atransport* transport) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 85 | 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 91 | // 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 Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 94 | |
| 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 110 | } else if (arg.starts_with("TERM=")) { |
| 111 | terminal_type = arg.substr(strlen("TERM=")); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 112 | } 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 118 | return StartSubprocess(command, terminal_type.c_str(), type, protocol); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Josh Gao | d727d31 | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 121 | static 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 Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 135 | fdevent* fde = fdevent_create( |
| 136 | fd, [](int, unsigned, void*) {}, nullptr); |
Josh Gao | d727d31 | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 137 | fdevent_add(fde, FDE_READ); |
| 138 | }); |
| 139 | |
| 140 | WriteFdExactly(fd.get(), "spinning\n"); |
| 141 | } |
| 142 | |
Tianjie Xu | 18a0beb | 2019-11-06 21:01:57 -0800 | [diff] [blame] | 143 | [[maybe_unused]] static unique_fd reboot_device(const std::string& name) { |
| 144 | #if defined(__ANDROID_RECOVERY__) |
| 145 | if (!__android_log_is_debuggable()) { |
| 146 | auto reboot_service = [name](unique_fd fd) { |
| 147 | std::string reboot_string = android::base::StringPrintf("reboot,%s", name.c_str()); |
| 148 | if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) { |
| 149 | WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str()); |
| 150 | return; |
| 151 | } |
| 152 | while (true) pause(); |
| 153 | }; |
| 154 | return create_service_thread("reboot", reboot_service); |
| 155 | } |
| 156 | #endif |
| 157 | // Fall through |
| 158 | std::string cmd = "/system/bin/reboot "; |
| 159 | cmd += name; |
| 160 | return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
| 161 | } |
| 162 | |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 163 | struct ServiceSocket : public asocket { |
| 164 | ServiceSocket() { |
| 165 | install_local_socket(this); |
| 166 | this->enqueue = [](asocket* self, apacket::payload_type data) { |
| 167 | return static_cast<ServiceSocket*>(self)->Enqueue(std::move(data)); |
| 168 | }; |
| 169 | this->ready = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Ready(); }; |
| 170 | this->close = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Close(); }; |
| 171 | } |
| 172 | virtual ~ServiceSocket() = default; |
| 173 | |
| 174 | virtual int Enqueue(apacket::payload_type data) { return -1; } |
| 175 | virtual void Ready() {} |
| 176 | virtual void Close() { |
| 177 | if (peer) { |
| 178 | peer->peer = nullptr; |
| 179 | if (peer->shutdown) { |
| 180 | peer->shutdown(peer); |
| 181 | } |
| 182 | peer->close(peer); |
| 183 | } |
| 184 | |
| 185 | remove_socket(this); |
| 186 | delete this; |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | struct SinkSocket : public ServiceSocket { |
| 191 | explicit SinkSocket(size_t byte_count) { |
| 192 | LOG(INFO) << "Creating new SinkSocket with capacity " << byte_count; |
| 193 | bytes_left_ = byte_count; |
| 194 | } |
| 195 | |
| 196 | virtual ~SinkSocket() { LOG(INFO) << "SinkSocket destroyed"; } |
| 197 | |
| 198 | virtual int Enqueue(apacket::payload_type data) override final { |
| 199 | if (bytes_left_ <= data.size()) { |
| 200 | // Done reading. |
| 201 | Close(); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | bytes_left_ -= data.size(); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | size_t bytes_left_; |
| 210 | }; |
| 211 | |
| 212 | struct SourceSocket : public ServiceSocket { |
| 213 | explicit SourceSocket(size_t byte_count) { |
| 214 | LOG(INFO) << "Creating new SourceSocket with capacity " << byte_count; |
| 215 | bytes_left_ = byte_count; |
| 216 | } |
| 217 | |
| 218 | virtual ~SourceSocket() { LOG(INFO) << "SourceSocket destroyed"; } |
| 219 | |
| 220 | void Ready() { |
| 221 | size_t len = std::min(bytes_left_, get_max_payload()); |
| 222 | if (len == 0) { |
| 223 | Close(); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | Block block(len); |
| 228 | memset(block.data(), 0, block.size()); |
| 229 | peer->enqueue(peer, std::move(block)); |
| 230 | bytes_left_ -= len; |
| 231 | } |
| 232 | |
| 233 | int Enqueue(apacket::payload_type data) { return -1; } |
| 234 | |
| 235 | size_t bytes_left_; |
| 236 | }; |
| 237 | |
| 238 | asocket* daemon_service_to_socket(std::string_view name) { |
| 239 | if (name == "jdwp") { |
| 240 | return create_jdwp_service_socket(); |
| 241 | } else if (name == "track-jdwp") { |
| 242 | return create_jdwp_tracker_service_socket(); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 243 | } else if (name == "track-app") { |
| 244 | return create_app_tracker_service_socket(); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 245 | } else if (android::base::ConsumePrefix(&name, "sink:")) { |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 246 | uint64_t byte_count = 0; |
Josh Gao | 10779bd | 2019-02-20 20:00:27 -0800 | [diff] [blame] | 247 | if (!ParseUint(&byte_count, name)) { |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 248 | return nullptr; |
| 249 | } |
| 250 | return new SinkSocket(byte_count); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 251 | } else if (android::base::ConsumePrefix(&name, "source:")) { |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 252 | uint64_t byte_count = 0; |
Josh Gao | 10779bd | 2019-02-20 20:00:27 -0800 | [diff] [blame] | 253 | if (!ParseUint(&byte_count, name)) { |
Josh Gao | 3edf807 | 2018-11-16 15:40:16 -0800 | [diff] [blame] | 254 | return nullptr; |
| 255 | } |
| 256 | return new SourceSocket(byte_count); |
| 257 | } |
| 258 | |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 262 | unique_fd daemon_service_to_fd(std::string_view name, atransport* transport) { |
Josh Gao | bd77521 | 2020-02-26 16:39:20 -0800 | [diff] [blame] | 263 | ADB_LOG(Service) << "transport " << transport->serial_name() << " opening service " << name; |
| 264 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 265 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 266 | if (name.starts_with("abb:") || name.starts_with("abb_exec:")) { |
| 267 | return execute_abb_command(name); |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 268 | } |
| 269 | #endif |
| 270 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 271 | #if defined(__ANDROID__) |
| 272 | if (name.starts_with("framebuffer:")) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 273 | return create_service_thread("fb", framebuffer_service); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 274 | } else if (android::base::ConsumePrefix(&name, "remount:")) { |
Josh Gao | 1540647 | 2019-10-11 14:41:19 -0700 | [diff] [blame] | 275 | std::string cmd = "/system/bin/remount "; |
| 276 | cmd += name; |
| 277 | return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 278 | } else if (android::base::ConsumePrefix(&name, "reboot:")) { |
Tianjie Xu | 18a0beb | 2019-11-06 21:01:57 -0800 | [diff] [blame] | 279 | return reboot_device(std::string(name)); |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 280 | } else if (name.starts_with("root:")) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 281 | return create_service_thread("root", restart_root_service); |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 282 | } else if (name.starts_with("unroot:")) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 283 | return create_service_thread("unroot", restart_unroot_service); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 284 | } else if (android::base::ConsumePrefix(&name, "backup:")) { |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 285 | std::string cmd = "/system/bin/bu backup "; |
| 286 | cmd += name; |
| 287 | return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
| 288 | } else if (name.starts_with("restore:")) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 289 | return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw, |
| 290 | SubprocessProtocol::kNone); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 291 | } else if (name.starts_with("disable-verity:")) { |
Josh Gao | b554f4b | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 292 | return StartSubprocess("/system/bin/disable-verity", nullptr, SubprocessType::kRaw, |
| 293 | SubprocessProtocol::kNone); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 294 | } else if (name.starts_with("enable-verity:")) { |
Josh Gao | b554f4b | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 295 | return StartSubprocess("/system/bin/enable-verity", nullptr, SubprocessType::kRaw, |
| 296 | SubprocessProtocol::kNone); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 297 | } else if (android::base::ConsumePrefix(&name, "tcpip:")) { |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 298 | std::string str(name); |
| 299 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 300 | int port; |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 301 | if (sscanf(str.c_str(), "%d", &port) != 1) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 302 | return unique_fd{}; |
| 303 | } |
| 304 | return create_service_thread("tcp", |
| 305 | std::bind(restart_tcp_service, std::placeholders::_1, port)); |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 306 | } else if (name.starts_with("usb:")) { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 307 | return create_service_thread("usb", restart_usb_service); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 308 | } |
| 309 | #endif |
| 310 | |
Shaju Mathew | 7282f07 | 2022-01-20 22:20:58 +0000 | [diff] [blame] | 311 | if (android::base::ConsumePrefix(&name, "jdwp:")) { |
Josh Gao | 10779bd | 2019-02-20 20:00:27 -0800 | [diff] [blame] | 312 | pid_t pid; |
| 313 | if (!ParseUint(&pid, name)) { |
| 314 | return unique_fd{}; |
| 315 | } |
| 316 | return create_jdwp_connection_fd(pid); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 317 | } else if (android::base::ConsumePrefix(&name, "shell")) { |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 318 | return ShellService(name, transport); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 319 | } else if (android::base::ConsumePrefix(&name, "exec:")) { |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 320 | return StartSubprocess(std::string(name), nullptr, SubprocessType::kRaw, |
| 321 | SubprocessProtocol::kNone); |
| 322 | } else if (name.starts_with("sync:")) { |
| 323 | return create_service_thread("sync", file_sync_service); |
Elliott Hughes | 0d1e8fd | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 324 | } else if (android::base::ConsumePrefix(&name, "reverse:")) { |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 325 | return reverse_service(name, transport); |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 326 | } else if (name == "reconnect") { |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 327 | return create_service_thread( |
| 328 | "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport)); |
Josh Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 329 | } else if (name == "spin") { |
Josh Gao | d727d31 | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 330 | return create_service_thread("spin", spin_service); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 331 | } |
Josh Gao | d727d31 | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 332 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 333 | return unique_fd{}; |
| 334 | } |