The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG SERVICES |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <stddef.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #ifndef _WIN32 |
| 28 | #include <netdb.h> |
| 29 | #include <netinet/in.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <unistd.h> |
| 32 | #endif |
| 33 | |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 34 | #include <thread> |
| 35 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> |
David Pursell | a17d272 | 2016-01-21 08:40:59 -0800 | [diff] [blame] | 37 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 40 | #include <cutils/sockets.h> |
Elliott Hughes | e4b6479 | 2015-04-17 20:11:08 -0700 | [diff] [blame] | 41 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 42 | #if !ADB_HOST |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 43 | #include <android-base/properties.h> |
Tao Bao | a8c21dc | 2017-01-05 18:01:01 -0800 | [diff] [blame] | 44 | #include <bootloader_message/bootloader_message.h> |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 45 | #include <cutils/android_reboot.h> |
Steven Moreland | b087d30 | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 46 | #include <log/log_properties.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 47 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #include "adb.h" |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 50 | #include "adb_io.h" |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 51 | #include "adb_unique_fd.h" |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 52 | #include "adb_utils.h" |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 53 | #if !ADB_HOST |
Josh Gao | c7b7459 | 2018-07-25 15:55:25 -0700 | [diff] [blame] | 54 | #include "daemon/file_sync_service.h" |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 55 | #include "daemon/framebuffer_service.h" |
Josh Gao | 977e526 | 2018-07-25 16:07:51 -0700 | [diff] [blame] | 56 | #include "daemon/remount_service.h" |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 57 | #include "daemon/set_verity_enable_state_service.h" |
Josh Gao | 076b5ba | 2018-07-25 16:07:26 -0700 | [diff] [blame] | 58 | #include "daemon/shell_service.h" |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 59 | #endif |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 60 | #include "services.h" |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 61 | #include "socket_spec.h" |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 62 | #include "sysdeps.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 63 | #include "transport.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 65 | namespace { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 67 | void service_bootstrap_func(std::string service_name, std::function<void(unique_fd)> func, |
| 68 | unique_fd fd) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 69 | adb_thread_setname(android::base::StringPrintf("%s svc %d", service_name.c_str(), fd.get())); |
| 70 | func(std::move(fd)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Benoit Goby | 12dc369 | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 73 | #if !ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 75 | void restart_root_service(unique_fd fd) { |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 76 | if (getuid() == 0) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 77 | WriteFdExactly(fd.get(), "adbd is already running as root\n"); |
| 78 | return; |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 79 | } |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 80 | if (!__android_log_is_debuggable()) { |
| 81 | WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n"); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | android::base::SetProperty("service.adb.root", "1"); |
| 86 | WriteFdExactly(fd.get(), "restarting adbd as root\n"); |
The Android Open Source Project | 9c75340 | 2009-03-13 13:04:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 89 | void restart_unroot_service(unique_fd fd) { |
Dan Pasanen | fb787d9 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 90 | if (getuid() != 0) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 91 | WriteFdExactly(fd.get(), "adbd not running as root\n"); |
| 92 | return; |
Dan Pasanen | fb787d9 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 93 | } |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 94 | android::base::SetProperty("service.adb.root", "0"); |
| 95 | WriteFdExactly(fd.get(), "restarting adbd as non root\n"); |
Dan Pasanen | fb787d9 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 98 | void restart_tcp_service(unique_fd fd, int port) { |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 99 | if (port <= 0) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 100 | WriteFdFmt(fd.get(), "invalid port %d\n", port); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 104 | android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port)); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 105 | WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 108 | void restart_usb_service(unique_fd fd) { |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 109 | android::base::SetProperty("service.adb.tcp.port", "0"); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 110 | WriteFdExactly(fd.get(), "restarting in USB mode\n"); |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 113 | bool reboot_service_impl(unique_fd fd, const std::string& arg) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 114 | std::string reboot_arg = arg; |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 115 | bool auto_reboot = false; |
| 116 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 117 | if (reboot_arg == "sideload-auto-reboot") { |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 118 | auto_reboot = true; |
| 119 | reboot_arg = "sideload"; |
| 120 | } |
| 121 | |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 122 | // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot" |
| 123 | // in the command file. |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 124 | if (reboot_arg == "sideload") { |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 125 | if (getuid() != 0) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 126 | WriteFdExactly(fd, "'adb root' is required for 'adb reboot sideload'.\n"); |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
Tao Bao | a8c21dc | 2017-01-05 18:01:01 -0800 | [diff] [blame] | 130 | const std::vector<std::string> options = { |
| 131 | auto_reboot ? "--sideload_auto_reboot" : "--sideload" |
| 132 | }; |
| 133 | std::string err; |
| 134 | if (!write_bootloader_message(options, &err)) { |
| 135 | D("Failed to set bootloader message: %s", err.c_str()); |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | reboot_arg = "recovery"; |
| 140 | } |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 141 | |
| 142 | sync(); |
Mike Lockwood | d49e9eb | 2010-02-24 16:07:23 -0500 | [diff] [blame] | 143 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 144 | if (reboot_arg.empty()) reboot_arg = "adb"; |
| 145 | std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str()); |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 146 | if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) { |
| 147 | WriteFdFmt(fd, "reboot (%s) failed\n", reboot_string.c_str()); |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 148 | return false; |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 149 | } |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 154 | void reboot_service(unique_fd fd, const std::string& arg) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 155 | if (!reboot_service_impl(std::move(fd), arg)) { |
| 156 | return; |
Tao Bao | 0db6764 | 2015-03-29 11:22:34 -0700 | [diff] [blame] | 157 | } |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 158 | // Don't return early. Give the reboot command time to take effect |
| 159 | // to avoid messing up scripts which do "adb reboot && adb wait-for-device" |
| 160 | while (true) { |
| 161 | pause(); |
| 162 | } |
Mike Lockwood | 12a35ea | 2009-08-04 20:37:51 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 165 | void reconnect_service(unique_fd fd, atransport* t) { |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 166 | WriteFdExactly(fd, "done"); |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 167 | kick_transport(t); |
| 168 | } |
| 169 | |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 170 | int reverse_service(const char* command, atransport* transport) { |
Yabin Cui | df85263 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 171 | int s[2]; |
| 172 | if (adb_socketpair(s)) { |
| 173 | PLOG(ERROR) << "cannot create service socket pair."; |
| 174 | return -1; |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 175 | } |
Yabin Cui | df85263 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 176 | VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1]; |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 177 | if (handle_forward_request(command, transport, s[1]) < 0) { |
Yabin Cui | df85263 | 2015-10-30 18:37:26 -0700 | [diff] [blame] | 178 | SendFail(s[1], "not a reverse forwarding command"); |
| 179 | } |
| 180 | adb_close(s[1]); |
| 181 | return s[0]; |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 182 | } |
| 183 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 184 | // Shell service string can look like: |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 185 | // shell[,arg1,arg2,...]:[command] |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 186 | int ShellService(const std::string& args, const atransport* transport) { |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 187 | size_t delimiter_index = args.find(':'); |
| 188 | if (delimiter_index == std::string::npos) { |
| 189 | LOG(ERROR) << "No ':' found in shell service arguments: " << args; |
| 190 | return -1; |
| 191 | } |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 192 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 193 | const std::string service_args = args.substr(0, delimiter_index); |
| 194 | const std::string command = args.substr(delimiter_index + 1); |
| 195 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 196 | // Defaults: |
| 197 | // PTY for interactive, raw for non-interactive. |
| 198 | // No protocol. |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 199 | // $TERM set to "dumb". |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 200 | SubprocessType type(command.empty() ? SubprocessType::kPty |
| 201 | : SubprocessType::kRaw); |
| 202 | SubprocessProtocol protocol = SubprocessProtocol::kNone; |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 203 | std::string terminal_type = "dumb"; |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 204 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 205 | for (const std::string& arg : android::base::Split(service_args, ",")) { |
| 206 | if (arg == kShellServiceArgRaw) { |
| 207 | type = SubprocessType::kRaw; |
| 208 | } else if (arg == kShellServiceArgPty) { |
| 209 | type = SubprocessType::kPty; |
| 210 | } else if (arg == kShellServiceArgShellProtocol) { |
| 211 | protocol = SubprocessProtocol::kShell; |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 212 | } else if (android::base::StartsWith(arg, "TERM=")) { |
| 213 | terminal_type = arg.substr(5); |
| 214 | } else if (!arg.empty()) { |
| 215 | // This is not an error to allow for future expansion. |
| 216 | LOG(WARNING) << "Ignoring unknown shell service argument: " << arg; |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 219 | |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 220 | return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | #endif // !ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 225 | unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | int s[2]; |
Dan Albert | f30d73c | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 227 | if (adb_socketpair(s)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | printf("cannot create service socket pair\n"); |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 229 | return unique_fd(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 231 | D("socketpair: (%d,%d)", s[0], s[1]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 232 | |
Jerry Zhang | f0e239c | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 233 | #if !ADB_HOST |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 234 | if (strcmp(service_name, "sync") == 0) { |
Jerry Zhang | f0e239c | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 235 | // Set file sync service socket to maximum size |
| 236 | int max_buf = LINUX_MAX_SOCKET_SIZE; |
| 237 | adb_setsockopt(s[0], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 238 | adb_setsockopt(s[1], SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 239 | } |
| 240 | #endif // !ADB_HOST |
| 241 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 242 | std::thread(service_bootstrap_func, service_name, func, unique_fd(s[1])).detach(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 244 | D("service thread started, %d:%d",s[0], s[1]); |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 245 | return unique_fd(s[0]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 248 | } // namespace |
| 249 | |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 250 | int service_to_fd(const char* name, atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | int ret = -1; |
| 252 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 253 | if (is_socket_spec(name)) { |
| 254 | std::string error; |
| 255 | ret = socket_spec_connect(name, &error); |
| 256 | if (ret < 0) { |
| 257 | LOG(ERROR) << "failed to connect to socket '" << name << "': " << error; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | } |
Benoit Goby | 12dc369 | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 259 | #if !ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | } else if(!strncmp("dev:", name, 4)) { |
Nick Kralevich | 777523e | 2014-07-18 20:57:35 -0700 | [diff] [blame] | 261 | ret = unix_open(name + 4, O_RDWR | O_CLOEXEC); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 262 | } else if (!strncmp(name, "framebuffer:", 12)) { |
| 263 | ret = create_service_thread("fb", framebuffer_service).release(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | } else if (!strncmp(name, "jdwp:", 5)) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 265 | ret = create_jdwp_connection_fd(atoi(name + 5)); |
| 266 | } else if (!strncmp(name, "shell", 5)) { |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 267 | ret = ShellService(name + 5, transport); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 268 | } else if (!strncmp(name, "exec:", 5)) { |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 269 | ret = StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 270 | } else if (!strncmp(name, "sync:", 5)) { |
| 271 | ret = create_service_thread("sync", file_sync_service).release(); |
| 272 | } else if (!strncmp(name, "remount:", 8)) { |
| 273 | std::string options(name + strlen("remount:")); |
| 274 | ret = create_service_thread("remount", |
| 275 | std::bind(remount_service, std::placeholders::_1, options)) |
| 276 | .release(); |
| 277 | } else if (!strncmp(name, "reboot:", 7)) { |
| 278 | std::string arg(name + strlen("reboot:")); |
| 279 | ret = create_service_thread("reboot", std::bind(reboot_service, std::placeholders::_1, arg)) |
| 280 | .release(); |
| 281 | } else if (!strncmp(name, "root:", 5)) { |
| 282 | ret = create_service_thread("root", restart_root_service).release(); |
| 283 | } else if (!strncmp(name, "unroot:", 7)) { |
| 284 | ret = create_service_thread("unroot", restart_unroot_service).release(); |
| 285 | } else if (!strncmp(name, "backup:", 7)) { |
| 286 | ret = StartSubprocess( |
| 287 | android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(), |
| 288 | nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
| 289 | } else if (!strncmp(name, "restore:", 8)) { |
Elliott Hughes | ff44456 | 2015-11-16 10:55:34 -0800 | [diff] [blame] | 290 | ret = StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw, |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 291 | SubprocessProtocol::kNone); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 292 | } else if (!strncmp(name, "tcpip:", 6)) { |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 293 | int port; |
Spencer Low | 29a029c | 2015-01-25 17:38:36 -0800 | [diff] [blame] | 294 | if (sscanf(name + 6, "%d", &port) != 1) { |
Elliott Hughes | d94e8ba | 2015-07-21 16:13:40 -0700 | [diff] [blame] | 295 | return -1; |
Mike Lockwood | 26b88e3 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 296 | } |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 297 | ret = create_service_thread("tcp", |
| 298 | std::bind(restart_tcp_service, std::placeholders::_1, port)) |
| 299 | .release(); |
| 300 | } else if (!strncmp(name, "usb:", 4)) { |
| 301 | ret = create_service_thread("usb", restart_usb_service).release(); |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 302 | } else if (!strncmp(name, "reverse:", 8)) { |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 303 | ret = reverse_service(name + 8, transport); |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 304 | } else if (!strncmp(name, "disable-verity:", 15)) { |
| 305 | ret = create_service_thread("verity-on", std::bind(set_verity_enabled_state_service, |
| 306 | std::placeholders::_1, false)) |
| 307 | .release(); |
| 308 | } else if (!strncmp(name, "enable-verity:", 15)) { |
| 309 | ret = create_service_thread("verity-off", std::bind(set_verity_enabled_state_service, |
| 310 | std::placeholders::_1, true)) |
| 311 | .release(); |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 312 | } else if (!strcmp(name, "reconnect")) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 313 | ret = create_service_thread("reconnect", |
| 314 | std::bind(reconnect_service, std::placeholders::_1, transport)) |
| 315 | .release(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | #endif |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | } |
| 318 | if (ret >= 0) { |
| 319 | close_on_exec(ret); |
| 320 | } |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | #if ADB_HOST |
| 325 | struct state_info { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 326 | TransportType transport_type; |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 327 | std::string serial; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 328 | TransportId transport_id; |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 329 | ConnectionState state; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | }; |
| 331 | |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 332 | static void wait_for_state(int fd, void* data) { |
| 333 | std::unique_ptr<state_info> sinfo(reinterpret_cast<state_info*>(data)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 334 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 335 | D("wait_for_state %d", sinfo->state); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 337 | while (true) { |
| 338 | bool is_ambiguous = false; |
| 339 | std::string error = "unknown error"; |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 340 | const char* serial = sinfo->serial.length() ? sinfo->serial.c_str() : nullptr; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 341 | atransport* t = acquire_one_transport(sinfo->transport_type, serial, sinfo->transport_id, |
| 342 | &is_ambiguous, &error); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 343 | if (t != nullptr && (sinfo->state == kCsAny || sinfo->state == t->GetConnectionState())) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 344 | SendOkay(fd); |
| 345 | break; |
| 346 | } else if (!is_ambiguous) { |
Josh Gao | c1fab36 | 2016-02-19 10:42:40 -0800 | [diff] [blame] | 347 | adb_pollfd pfd = {.fd = fd, .events = POLLIN }; |
| 348 | int rc = adb_poll(&pfd, 1, 1000); |
| 349 | if (rc < 0) { |
| 350 | SendFail(fd, error); |
| 351 | break; |
| 352 | } else if (rc > 0 && (pfd.revents & POLLHUP) != 0) { |
| 353 | // The other end of the socket is closed, probably because the other side was |
| 354 | // terminated, bail out. |
| 355 | break; |
| 356 | } |
| 357 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 358 | // Try again... |
| 359 | } else { |
| 360 | SendFail(fd, error); |
| 361 | break; |
| 362 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | } |
| 364 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | adb_close(fd); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 366 | D("wait_for_state is done"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 368 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 369 | void connect_emulator(const std::string& port_spec, std::string* response) { |
| 370 | std::vector<std::string> pieces = android::base::Split(port_spec, ","); |
| 371 | if (pieces.size() != 2) { |
| 372 | *response = android::base::StringPrintf("unable to parse '%s' as <console port>,<adb port>", |
| 373 | port_spec.c_str()); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 377 | int console_port = strtol(pieces[0].c_str(), nullptr, 0); |
| 378 | int adb_port = strtol(pieces[1].c_str(), nullptr, 0); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 379 | if (console_port <= 0 || adb_port <= 0) { |
| 380 | *response = android::base::StringPrintf("Invalid port numbers: %s", port_spec.c_str()); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 384 | // Check if the emulator is already known. |
| 385 | // Note: There's a small but harmless race condition here: An emulator not |
| 386 | // present just yet could be registered by another invocation right |
| 387 | // after doing this check here. However, local_connect protects |
| 388 | // against double-registration too. From here, a better error message |
| 389 | // can be produced. In the case of the race condition, the very specific |
| 390 | // error message won't be shown, but the data doesn't get corrupted. |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 391 | atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 392 | if (known_emulator != nullptr) { |
| 393 | *response = android::base::StringPrintf("Emulator already registered on port %d", adb_port); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 397 | // Preconditions met, try to connect to the emulator. |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 398 | std::string error; |
| 399 | if (!local_connect_arbitrary_ports(console_port, adb_port, &error)) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 400 | *response = android::base::StringPrintf("Connected to emulator on ports %d,%d", |
| 401 | console_port, adb_port); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 402 | } else { |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 403 | *response = android::base::StringPrintf("Could not connect to emulator on ports %d,%d: %s", |
| 404 | console_port, adb_port, error.c_str()); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Josh Gao | 4c28dde | 2018-07-25 16:51:59 -0700 | [diff] [blame^] | 408 | static void connect_service(unique_fd fd, std::string host) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 409 | std::string response; |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 410 | if (!strncmp(host.c_str(), "emu:", 4)) { |
| 411 | connect_emulator(host.c_str() + 4, &response); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 412 | } else { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 413 | connect_device(host.c_str(), &response); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Send response for emulator and device |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 417 | SendProtocolString(fd.get(), response); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 418 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | #endif |
| 420 | |
| 421 | #if ADB_HOST |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 422 | asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | if (!strcmp(name,"track-devices")) { |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 424 | return create_device_tracker(false); |
| 425 | } else if (!strcmp(name, "track-devices-l")) { |
| 426 | return create_device_tracker(true); |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 427 | } else if (android::base::StartsWith(name, "wait-for-")) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 428 | name += strlen("wait-for-"); |
| 429 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 430 | std::unique_ptr<state_info> sinfo = std::make_unique<state_info>(); |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 431 | if (sinfo == nullptr) { |
| 432 | fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno)); |
| 433 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 436 | if (serial) sinfo->serial = serial; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 437 | sinfo->transport_id = transport_id; |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 438 | |
| 439 | if (android::base::StartsWith(name, "local")) { |
| 440 | name += strlen("local"); |
| 441 | sinfo->transport_type = kTransportLocal; |
| 442 | } else if (android::base::StartsWith(name, "usb")) { |
| 443 | name += strlen("usb"); |
| 444 | sinfo->transport_type = kTransportUsb; |
| 445 | } else if (android::base::StartsWith(name, "any")) { |
| 446 | name += strlen("any"); |
| 447 | sinfo->transport_type = kTransportAny; |
| 448 | } else { |
| 449 | return nullptr; |
| 450 | } |
| 451 | |
| 452 | if (!strcmp(name, "-device")) { |
| 453 | sinfo->state = kCsDevice; |
| 454 | } else if (!strcmp(name, "-recovery")) { |
| 455 | sinfo->state = kCsRecovery; |
| 456 | } else if (!strcmp(name, "-sideload")) { |
| 457 | sinfo->state = kCsSideload; |
| 458 | } else if (!strcmp(name, "-bootloader")) { |
| 459 | sinfo->state = kCsBootloader; |
Josh Gao | 071328d | 2016-04-13 12:18:58 -0700 | [diff] [blame] | 460 | } else if (!strcmp(name, "-any")) { |
| 461 | sinfo->state = kCsAny; |
Leo Sartre | 6cd9bc3 | 2015-11-27 18:56:48 +0100 | [diff] [blame] | 462 | } else { |
| 463 | return nullptr; |
| 464 | } |
| 465 | |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 466 | int fd = create_service_thread( |
| 467 | "wait", std::bind(wait_for_state, std::placeholders::_1, sinfo.get())) |
| 468 | .release(); |
Ting-Yuan Huang | aa923c1 | 2017-08-15 15:07:21 -0700 | [diff] [blame] | 469 | if (fd != -1) { |
| 470 | sinfo.release(); |
| 471 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | return create_local_socket(fd); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 473 | } else if (!strncmp(name, "connect:", 8)) { |
Luis Hector Chavez | ce7a284 | 2018-07-18 19:40:12 -0700 | [diff] [blame] | 474 | std::string host(name + strlen("connect:")); |
| 475 | int fd = create_service_thread("connect", |
| 476 | std::bind(connect_service, std::placeholders::_1, host)) |
| 477 | .release(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 478 | return create_local_socket(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 | } |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 480 | return nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | } |
| 482 | #endif /* ADB_HOST */ |