blob: 25024b05a598a86d33722376c45232667a7ee162 [file] [log] [blame]
Josh Gao997cfac2018-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>
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
59void 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
73void 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
82void 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
92void 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
97bool reboot_service_impl(unique_fd fd, const std::string& arg) {
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");
111 return false;
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());
119 return false;
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());
131 return false;
132 }
133
134 return true;
135}
136
137void reboot_service(unique_fd fd, const std::string& arg) {
138 if (!reboot_service_impl(std::move(fd), arg)) {
139 return;
140 }
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
148void reconnect_service(unique_fd fd, atransport* t) {
149 WriteFdExactly(fd.get(), "done");
150 kick_transport(t);
151}
152
153unique_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];
160 if (handle_forward_request(command, transport, s[1]) < 0) {
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]
169unique_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
205unique_fd daemon_service_to_fd(const char* name, atransport* transport) {
206 if (!strncmp("dev:", name, 4)) {
207 return unique_fd{unix_open(name + 4, O_RDWR | O_CLOEXEC)};
208 } else if (!strncmp(name, "framebuffer:", 12)) {
209 return create_service_thread("fb", framebuffer_service);
210 } else if (!strncmp(name, "jdwp:", 5)) {
211 return create_jdwp_connection_fd(atoi(name + 5));
212 } else if (!strncmp(name, "shell", 5)) {
213 return ShellService(name + 5, transport);
214 } else if (!strncmp(name, "exec:", 5)) {
215 return StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
216 } else if (!strncmp(name, "sync:", 5)) {
217 return create_service_thread("sync", file_sync_service);
218 } else if (!strncmp(name, "remount:", 8)) {
219 std::string options(name + strlen("remount:"));
220 return create_service_thread("remount",
221 std::bind(remount_service, std::placeholders::_1, options));
222 } else if (!strncmp(name, "reboot:", 7)) {
223 std::string arg(name + strlen("reboot:"));
224 return create_service_thread("reboot",
225 std::bind(reboot_service, std::placeholders::_1, arg));
226 } else if (!strncmp(name, "root:", 5)) {
227 return create_service_thread("root", restart_root_service);
228 } else if (!strncmp(name, "unroot:", 7)) {
229 return create_service_thread("unroot", restart_unroot_service);
230 } else if (!strncmp(name, "backup:", 7)) {
231 return StartSubprocess(
232 android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(),
233 nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
234 } else if (!strncmp(name, "restore:", 8)) {
235 return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
236 SubprocessProtocol::kNone);
237 } else if (!strncmp(name, "tcpip:", 6)) {
238 int port;
239 if (sscanf(name + 6, "%d", &port) != 1) {
240 return unique_fd{};
241 }
242 return create_service_thread("tcp",
243 std::bind(restart_tcp_service, std::placeholders::_1, port));
244 } else if (!strncmp(name, "usb:", 4)) {
245 return create_service_thread("usb", restart_usb_service);
246 } else if (!strncmp(name, "reverse:", 8)) {
247 return reverse_service(name + 8, transport);
248 } else if (!strncmp(name, "disable-verity:", 15)) {
249 return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service,
250 std::placeholders::_1, false));
251 } else if (!strncmp(name, "enable-verity:", 15)) {
252 return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service,
253 std::placeholders::_1, true));
254 } else if (!strcmp(name, "reconnect")) {
255 return create_service_thread(
256 "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
257 }
258 return unique_fd{};
259}