| Josh Gao | 997cfac | 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> | 
|  | 29 | #include <unistd.h> | 
|  | 30 |  | 
|  | 31 | #include <thread> | 
|  | 32 |  | 
|  | 33 | #include <android-base/file.h> | 
|  | 34 | #include <android-base/parsenetaddress.h> | 
|  | 35 | #include <android-base/properties.h> | 
|  | 36 | #include <android-base/stringprintf.h> | 
|  | 37 | #include <android-base/strings.h> | 
|  | 38 | #include <android-base/unique_fd.h> | 
|  | 39 | #include <bootloader_message/bootloader_message.h> | 
|  | 40 | #include <cutils/android_reboot.h> | 
|  | 41 | #include <cutils/sockets.h> | 
|  | 42 | #include <log/log_properties.h> | 
|  | 43 |  | 
|  | 44 | #include "adb.h" | 
|  | 45 | #include "adb_io.h" | 
|  | 46 | #include "adb_unique_fd.h" | 
|  | 47 | #include "adb_utils.h" | 
|  | 48 | #include "services.h" | 
|  | 49 | #include "socket_spec.h" | 
|  | 50 | #include "sysdeps.h" | 
|  | 51 | #include "transport.h" | 
|  | 52 |  | 
|  | 53 | #include "daemon/file_sync_service.h" | 
|  | 54 | #include "daemon/framebuffer_service.h" | 
|  | 55 | #include "daemon/remount_service.h" | 
|  | 56 | #include "daemon/set_verity_enable_state_service.h" | 
|  | 57 | #include "daemon/shell_service.h" | 
|  | 58 |  | 
|  | 59 | void restart_root_service(unique_fd fd) { | 
|  | 60 | if (getuid() == 0) { | 
|  | 61 | WriteFdExactly(fd.get(), "adbd is already running as root\n"); | 
|  | 62 | return; | 
|  | 63 | } | 
|  | 64 | if (!__android_log_is_debuggable()) { | 
|  | 65 | WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n"); | 
|  | 66 | return; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | android::base::SetProperty("service.adb.root", "1"); | 
|  | 70 | WriteFdExactly(fd.get(), "restarting adbd as root\n"); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | void restart_unroot_service(unique_fd fd) { | 
|  | 74 | if (getuid() != 0) { | 
|  | 75 | WriteFdExactly(fd.get(), "adbd not running as root\n"); | 
|  | 76 | return; | 
|  | 77 | } | 
|  | 78 | android::base::SetProperty("service.adb.root", "0"); | 
|  | 79 | WriteFdExactly(fd.get(), "restarting adbd as non root\n"); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | void restart_tcp_service(unique_fd fd, int port) { | 
|  | 83 | if (port <= 0) { | 
|  | 84 | WriteFdFmt(fd.get(), "invalid port %d\n", port); | 
|  | 85 | return; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port)); | 
|  | 89 | WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | void restart_usb_service(unique_fd fd) { | 
|  | 93 | android::base::SetProperty("service.adb.tcp.port", "0"); | 
|  | 94 | WriteFdExactly(fd.get(), "restarting in USB mode\n"); | 
|  | 95 | } | 
|  | 96 |  | 
| Josh Gao | 6fb9461 | 2018-08-01 16:00:05 -0700 | [diff] [blame] | 97 | void reboot_service(unique_fd fd, const std::string& arg) { | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 98 | std::string reboot_arg = arg; | 
|  | 99 | bool auto_reboot = false; | 
|  | 100 |  | 
|  | 101 | if (reboot_arg == "sideload-auto-reboot") { | 
|  | 102 | auto_reboot = true; | 
|  | 103 | reboot_arg = "sideload"; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot" | 
|  | 107 | // in the command file. | 
|  | 108 | if (reboot_arg == "sideload") { | 
|  | 109 | if (getuid() != 0) { | 
|  | 110 | WriteFdExactly(fd.get(), "'adb root' is required for 'adb reboot sideload'.\n"); | 
| Josh Gao | 6fb9461 | 2018-08-01 16:00:05 -0700 | [diff] [blame] | 111 | return; | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
|  | 114 | const std::vector<std::string> options = {auto_reboot ? "--sideload_auto_reboot" | 
|  | 115 | : "--sideload"}; | 
|  | 116 | std::string err; | 
|  | 117 | if (!write_bootloader_message(options, &err)) { | 
|  | 118 | D("Failed to set bootloader message: %s", err.c_str()); | 
| Josh Gao | 6fb9461 | 2018-08-01 16:00:05 -0700 | [diff] [blame] | 119 | return; | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
|  | 122 | reboot_arg = "recovery"; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | sync(); | 
|  | 126 |  | 
|  | 127 | if (reboot_arg.empty()) reboot_arg = "adb"; | 
|  | 128 | std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str()); | 
|  | 129 | if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) { | 
|  | 130 | WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str()); | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 131 | return; | 
|  | 132 | } | 
| Josh Gao | 6fb9461 | 2018-08-01 16:00:05 -0700 | [diff] [blame] | 133 |  | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 134 | // Don't return early. Give the reboot command time to take effect | 
|  | 135 | // to avoid messing up scripts which do "adb reboot && adb wait-for-device" | 
|  | 136 | while (true) { | 
|  | 137 | pause(); | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | void reconnect_service(unique_fd fd, atransport* t) { | 
|  | 142 | WriteFdExactly(fd.get(), "done"); | 
|  | 143 | kick_transport(t); | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | unique_fd reverse_service(const char* command, atransport* transport) { | 
|  | 147 | int s[2]; | 
|  | 148 | if (adb_socketpair(s)) { | 
|  | 149 | PLOG(ERROR) << "cannot create service socket pair."; | 
|  | 150 | return unique_fd{}; | 
|  | 151 | } | 
|  | 152 | VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1]; | 
| Josh Gao | 1906243 | 2018-07-30 18:49:03 -0700 | [diff] [blame] | 153 | if (!handle_forward_request(command, transport, s[1])) { | 
| Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 154 | SendFail(s[1], "not a reverse forwarding command"); | 
|  | 155 | } | 
|  | 156 | adb_close(s[1]); | 
|  | 157 | return unique_fd{s[0]}; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | // Shell service string can look like: | 
|  | 161 | //   shell[,arg1,arg2,...]:[command] | 
|  | 162 | unique_fd ShellService(const std::string& args, const atransport* transport) { | 
|  | 163 | size_t delimiter_index = args.find(':'); | 
|  | 164 | if (delimiter_index == std::string::npos) { | 
|  | 165 | LOG(ERROR) << "No ':' found in shell service arguments: " << args; | 
|  | 166 | return unique_fd{}; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | const std::string service_args = args.substr(0, delimiter_index); | 
|  | 170 | const std::string command = args.substr(delimiter_index + 1); | 
|  | 171 |  | 
|  | 172 | // Defaults: | 
|  | 173 | //   PTY for interactive, raw for non-interactive. | 
|  | 174 | //   No protocol. | 
|  | 175 | //   $TERM set to "dumb". | 
|  | 176 | SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw); | 
|  | 177 | SubprocessProtocol protocol = SubprocessProtocol::kNone; | 
|  | 178 | std::string terminal_type = "dumb"; | 
|  | 179 |  | 
|  | 180 | for (const std::string& arg : android::base::Split(service_args, ",")) { | 
|  | 181 | if (arg == kShellServiceArgRaw) { | 
|  | 182 | type = SubprocessType::kRaw; | 
|  | 183 | } else if (arg == kShellServiceArgPty) { | 
|  | 184 | type = SubprocessType::kPty; | 
|  | 185 | } else if (arg == kShellServiceArgShellProtocol) { | 
|  | 186 | protocol = SubprocessProtocol::kShell; | 
|  | 187 | } else if (android::base::StartsWith(arg, "TERM=")) { | 
|  | 188 | terminal_type = arg.substr(5); | 
|  | 189 | } else if (!arg.empty()) { | 
|  | 190 | // This is not an error to allow for future expansion. | 
|  | 191 | LOG(WARNING) << "Ignoring unknown shell service argument: " << arg; | 
|  | 192 | } | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | unique_fd daemon_service_to_fd(const char* name, atransport* transport) { | 
|  | 199 | if (!strncmp("dev:", name, 4)) { | 
|  | 200 | return unique_fd{unix_open(name + 4, O_RDWR | O_CLOEXEC)}; | 
|  | 201 | } else if (!strncmp(name, "framebuffer:", 12)) { | 
|  | 202 | return create_service_thread("fb", framebuffer_service); | 
|  | 203 | } else if (!strncmp(name, "jdwp:", 5)) { | 
|  | 204 | return create_jdwp_connection_fd(atoi(name + 5)); | 
|  | 205 | } else if (!strncmp(name, "shell", 5)) { | 
|  | 206 | return ShellService(name + 5, transport); | 
|  | 207 | } else if (!strncmp(name, "exec:", 5)) { | 
|  | 208 | return StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); | 
|  | 209 | } else if (!strncmp(name, "sync:", 5)) { | 
|  | 210 | return create_service_thread("sync", file_sync_service); | 
|  | 211 | } else if (!strncmp(name, "remount:", 8)) { | 
|  | 212 | std::string options(name + strlen("remount:")); | 
|  | 213 | return create_service_thread("remount", | 
|  | 214 | std::bind(remount_service, std::placeholders::_1, options)); | 
|  | 215 | } else if (!strncmp(name, "reboot:", 7)) { | 
|  | 216 | std::string arg(name + strlen("reboot:")); | 
|  | 217 | return create_service_thread("reboot", | 
|  | 218 | std::bind(reboot_service, std::placeholders::_1, arg)); | 
|  | 219 | } else if (!strncmp(name, "root:", 5)) { | 
|  | 220 | return create_service_thread("root", restart_root_service); | 
|  | 221 | } else if (!strncmp(name, "unroot:", 7)) { | 
|  | 222 | return create_service_thread("unroot", restart_unroot_service); | 
|  | 223 | } else if (!strncmp(name, "backup:", 7)) { | 
|  | 224 | return StartSubprocess( | 
|  | 225 | android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(), | 
|  | 226 | nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); | 
|  | 227 | } else if (!strncmp(name, "restore:", 8)) { | 
|  | 228 | return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw, | 
|  | 229 | SubprocessProtocol::kNone); | 
|  | 230 | } else if (!strncmp(name, "tcpip:", 6)) { | 
|  | 231 | int port; | 
|  | 232 | if (sscanf(name + 6, "%d", &port) != 1) { | 
|  | 233 | return unique_fd{}; | 
|  | 234 | } | 
|  | 235 | return create_service_thread("tcp", | 
|  | 236 | std::bind(restart_tcp_service, std::placeholders::_1, port)); | 
|  | 237 | } else if (!strncmp(name, "usb:", 4)) { | 
|  | 238 | return create_service_thread("usb", restart_usb_service); | 
|  | 239 | } else if (!strncmp(name, "reverse:", 8)) { | 
|  | 240 | return reverse_service(name + 8, transport); | 
|  | 241 | } else if (!strncmp(name, "disable-verity:", 15)) { | 
|  | 242 | return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service, | 
|  | 243 | std::placeholders::_1, false)); | 
|  | 244 | } else if (!strncmp(name, "enable-verity:", 15)) { | 
|  | 245 | return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service, | 
|  | 246 | std::placeholders::_1, true)); | 
|  | 247 | } else if (!strcmp(name, "reconnect")) { | 
|  | 248 | return create_service_thread( | 
|  | 249 | "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport)); | 
|  | 250 | } | 
|  | 251 | return unique_fd{}; | 
|  | 252 | } |