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> |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <thread> |
| 34 | |
| 35 | #include <android-base/file.h> |
| 36 | #include <android-base/parsenetaddress.h> |
| 37 | #include <android-base/properties.h> |
| 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
| 40 | #include <android-base/unique_fd.h> |
| 41 | #include <bootloader_message/bootloader_message.h> |
| 42 | #include <cutils/android_reboot.h> |
| 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" |
| 52 | #include "sysdeps.h" |
| 53 | #include "transport.h" |
| 54 | |
| 55 | #include "daemon/file_sync_service.h" |
| 56 | #include "daemon/framebuffer_service.h" |
| 57 | #include "daemon/remount_service.h" |
| 58 | #include "daemon/set_verity_enable_state_service.h" |
| 59 | #include "daemon/shell_service.h" |
| 60 | |
| 61 | void restart_root_service(unique_fd fd) { |
| 62 | if (getuid() == 0) { |
| 63 | WriteFdExactly(fd.get(), "adbd is already running as root\n"); |
| 64 | return; |
| 65 | } |
| 66 | if (!__android_log_is_debuggable()) { |
| 67 | WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | android::base::SetProperty("service.adb.root", "1"); |
| 72 | WriteFdExactly(fd.get(), "restarting adbd as root\n"); |
| 73 | } |
| 74 | |
| 75 | void restart_unroot_service(unique_fd fd) { |
| 76 | if (getuid() != 0) { |
| 77 | WriteFdExactly(fd.get(), "adbd not running as root\n"); |
| 78 | return; |
| 79 | } |
| 80 | android::base::SetProperty("service.adb.root", "0"); |
| 81 | WriteFdExactly(fd.get(), "restarting adbd as non root\n"); |
| 82 | } |
| 83 | |
| 84 | void restart_tcp_service(unique_fd fd, int port) { |
| 85 | if (port <= 0) { |
| 86 | WriteFdFmt(fd.get(), "invalid port %d\n", port); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port)); |
| 91 | WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port); |
| 92 | } |
| 93 | |
| 94 | void restart_usb_service(unique_fd fd) { |
| 95 | android::base::SetProperty("service.adb.tcp.port", "0"); |
| 96 | WriteFdExactly(fd.get(), "restarting in USB mode\n"); |
| 97 | } |
| 98 | |
Josh Gao | 6fb9461 | 2018-08-01 16:00:05 -0700 | [diff] [blame] | 99 | void reboot_service(unique_fd fd, const std::string& arg) { |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 100 | std::string reboot_arg = arg; |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 101 | sync(); |
| 102 | |
| 103 | if (reboot_arg.empty()) reboot_arg = "adb"; |
| 104 | std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str()); |
Josh Gao | 6fb9461 | 2018-08-01 16:00:05 -0700 | [diff] [blame] | 105 | |
Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 106 | if (reboot_arg == "fastboot" && |
| 107 | android::base::GetBoolProperty("ro.boot.logical_partitions", false) && |
| 108 | access("/dev/socket/recovery", F_OK) == 0) { |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 109 | LOG(INFO) << "Recovery specific reboot fastboot"; |
| 110 | /* |
| 111 | * The socket is created to allow switching between recovery and |
| 112 | * fastboot. |
| 113 | */ |
| 114 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 115 | if (sock < 0) { |
| 116 | WriteFdFmt(fd, "reboot (%s) create\n", strerror(errno)); |
| 117 | PLOG(ERROR) << "Creating recovery socket failed"; |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 122 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); |
| 123 | if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) { |
| 124 | WriteFdFmt(fd, "reboot (%s) connect\n", strerror(errno)); |
| 125 | PLOG(ERROR) << "Couldn't connect to recovery socket"; |
| 126 | return; |
| 127 | } |
| 128 | const char msg_switch_to_fastboot = 'f'; |
| 129 | auto ret = adb_write(sock, &msg_switch_to_fastboot, sizeof(msg_switch_to_fastboot)); |
| 130 | if (ret != sizeof(msg_switch_to_fastboot)) { |
| 131 | WriteFdFmt(fd, "reboot (%s) write\n", strerror(errno)); |
| 132 | PLOG(ERROR) << "Couldn't write message to recovery socket to switch to fastboot"; |
| 133 | return; |
| 134 | } |
| 135 | } else { |
| 136 | if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) { |
| 137 | WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str()); |
| 138 | return; |
| 139 | } |
| 140 | } |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 141 | // Don't return early. Give the reboot command time to take effect |
| 142 | // to avoid messing up scripts which do "adb reboot && adb wait-for-device" |
| 143 | while (true) { |
| 144 | pause(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void reconnect_service(unique_fd fd, atransport* t) { |
| 149 | WriteFdExactly(fd.get(), "done"); |
| 150 | kick_transport(t); |
| 151 | } |
| 152 | |
| 153 | unique_fd reverse_service(const char* command, atransport* transport) { |
| 154 | int s[2]; |
| 155 | if (adb_socketpair(s)) { |
| 156 | PLOG(ERROR) << "cannot create service socket pair."; |
| 157 | return unique_fd{}; |
| 158 | } |
| 159 | VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1]; |
Josh Gao | 1906243 | 2018-07-30 18:49:03 -0700 | [diff] [blame] | 160 | if (!handle_forward_request(command, transport, s[1])) { |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 161 | SendFail(s[1], "not a reverse forwarding command"); |
| 162 | } |
| 163 | adb_close(s[1]); |
| 164 | return unique_fd{s[0]}; |
| 165 | } |
| 166 | |
| 167 | // Shell service string can look like: |
| 168 | // shell[,arg1,arg2,...]:[command] |
| 169 | unique_fd ShellService(const std::string& args, const atransport* transport) { |
| 170 | size_t delimiter_index = args.find(':'); |
| 171 | if (delimiter_index == std::string::npos) { |
| 172 | LOG(ERROR) << "No ':' found in shell service arguments: " << args; |
| 173 | return unique_fd{}; |
| 174 | } |
| 175 | |
| 176 | const std::string service_args = args.substr(0, delimiter_index); |
| 177 | const std::string command = args.substr(delimiter_index + 1); |
| 178 | |
| 179 | // Defaults: |
| 180 | // PTY for interactive, raw for non-interactive. |
| 181 | // No protocol. |
| 182 | // $TERM set to "dumb". |
| 183 | SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw); |
| 184 | SubprocessProtocol protocol = SubprocessProtocol::kNone; |
| 185 | std::string terminal_type = "dumb"; |
| 186 | |
| 187 | for (const std::string& arg : android::base::Split(service_args, ",")) { |
| 188 | if (arg == kShellServiceArgRaw) { |
| 189 | type = SubprocessType::kRaw; |
| 190 | } else if (arg == kShellServiceArgPty) { |
| 191 | type = SubprocessType::kPty; |
| 192 | } else if (arg == kShellServiceArgShellProtocol) { |
| 193 | protocol = SubprocessProtocol::kShell; |
| 194 | } else if (android::base::StartsWith(arg, "TERM=")) { |
| 195 | terminal_type = arg.substr(5); |
| 196 | } else if (!arg.empty()) { |
| 197 | // This is not an error to allow for future expansion. |
| 198 | LOG(WARNING) << "Ignoring unknown shell service argument: " << arg; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol); |
| 203 | } |
| 204 | |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 205 | static void spin_service(unique_fd fd) { |
| 206 | if (!__android_log_is_debuggable()) { |
| 207 | WriteFdExactly(fd.get(), "refusing to spin on non-debuggable build\n"); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // A service that creates an fdevent that's always pending, and then ignores it. |
| 212 | unique_fd pipe_read, pipe_write; |
| 213 | if (!Pipe(&pipe_read, &pipe_write)) { |
| 214 | WriteFdExactly(fd.get(), "failed to create pipe\n"); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | fdevent_run_on_main_thread([fd = pipe_read.release()]() { |
| 219 | fdevent* fde = fdevent_create(fd, [](int, unsigned, void*) {}, nullptr); |
| 220 | fdevent_add(fde, FDE_READ); |
| 221 | }); |
| 222 | |
| 223 | WriteFdExactly(fd.get(), "spinning\n"); |
| 224 | } |
| 225 | |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 226 | unique_fd daemon_service_to_fd(const char* name, atransport* transport) { |
| 227 | if (!strncmp("dev:", name, 4)) { |
| 228 | return unique_fd{unix_open(name + 4, O_RDWR | O_CLOEXEC)}; |
| 229 | } else if (!strncmp(name, "framebuffer:", 12)) { |
| 230 | return create_service_thread("fb", framebuffer_service); |
| 231 | } else if (!strncmp(name, "jdwp:", 5)) { |
| 232 | return create_jdwp_connection_fd(atoi(name + 5)); |
| 233 | } else if (!strncmp(name, "shell", 5)) { |
| 234 | return ShellService(name + 5, transport); |
| 235 | } else if (!strncmp(name, "exec:", 5)) { |
| 236 | return StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
| 237 | } else if (!strncmp(name, "sync:", 5)) { |
| 238 | return create_service_thread("sync", file_sync_service); |
| 239 | } else if (!strncmp(name, "remount:", 8)) { |
| 240 | std::string options(name + strlen("remount:")); |
| 241 | return create_service_thread("remount", |
| 242 | std::bind(remount_service, std::placeholders::_1, options)); |
| 243 | } else if (!strncmp(name, "reboot:", 7)) { |
| 244 | std::string arg(name + strlen("reboot:")); |
| 245 | return create_service_thread("reboot", |
| 246 | std::bind(reboot_service, std::placeholders::_1, arg)); |
| 247 | } else if (!strncmp(name, "root:", 5)) { |
| 248 | return create_service_thread("root", restart_root_service); |
| 249 | } else if (!strncmp(name, "unroot:", 7)) { |
| 250 | return create_service_thread("unroot", restart_unroot_service); |
| 251 | } else if (!strncmp(name, "backup:", 7)) { |
| 252 | return StartSubprocess( |
| 253 | android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(), |
| 254 | nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); |
| 255 | } else if (!strncmp(name, "restore:", 8)) { |
| 256 | return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw, |
| 257 | SubprocessProtocol::kNone); |
| 258 | } else if (!strncmp(name, "tcpip:", 6)) { |
| 259 | int port; |
| 260 | if (sscanf(name + 6, "%d", &port) != 1) { |
| 261 | return unique_fd{}; |
| 262 | } |
| 263 | return create_service_thread("tcp", |
| 264 | std::bind(restart_tcp_service, std::placeholders::_1, port)); |
| 265 | } else if (!strncmp(name, "usb:", 4)) { |
| 266 | return create_service_thread("usb", restart_usb_service); |
| 267 | } else if (!strncmp(name, "reverse:", 8)) { |
| 268 | return reverse_service(name + 8, transport); |
| 269 | } else if (!strncmp(name, "disable-verity:", 15)) { |
| 270 | return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service, |
| 271 | std::placeholders::_1, false)); |
| 272 | } else if (!strncmp(name, "enable-verity:", 15)) { |
| 273 | return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service, |
| 274 | std::placeholders::_1, true)); |
| 275 | } else if (!strcmp(name, "reconnect")) { |
| 276 | return create_service_thread( |
| 277 | "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport)); |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 278 | } else if (!strcmp(name, "spin")) { |
| 279 | return create_service_thread("spin", spin_service); |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 280 | } |
Josh Gao | 39c1f4b | 2018-10-05 17:09:07 -0700 | [diff] [blame] | 281 | |
Josh Gao | 997cfac | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 282 | return unique_fd{}; |
| 283 | } |