blob: 5ae210f7c6dbd19ca53390aefdbbd3622ee70539 [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>
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -070029#include <sys/socket.h>
30#include <sys/un.h>
Josh Gao997cfac2018-07-25 18:15:52 -070031#include <unistd.h>
32
33#include <thread>
34
35#include <android-base/file.h>
Josh Gao6eb78822018-11-16 15:40:16 -080036#include <android-base/parseint.h>
Josh Gao997cfac2018-07-25 18:15:52 -070037#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>
42#include <bootloader_message/bootloader_message.h>
43#include <cutils/android_reboot.h>
44#include <cutils/sockets.h>
45#include <log/log_properties.h>
46
47#include "adb.h"
48#include "adb_io.h"
49#include "adb_unique_fd.h"
50#include "adb_utils.h"
51#include "services.h"
52#include "socket_spec.h"
53#include "sysdeps.h"
54#include "transport.h"
55
56#include "daemon/file_sync_service.h"
57#include "daemon/framebuffer_service.h"
58#include "daemon/remount_service.h"
59#include "daemon/set_verity_enable_state_service.h"
60#include "daemon/shell_service.h"
61
62void restart_root_service(unique_fd fd) {
63 if (getuid() == 0) {
64 WriteFdExactly(fd.get(), "adbd is already running as root\n");
65 return;
66 }
67 if (!__android_log_is_debuggable()) {
68 WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n");
69 return;
70 }
71
72 android::base::SetProperty("service.adb.root", "1");
73 WriteFdExactly(fd.get(), "restarting adbd as root\n");
74}
75
76void restart_unroot_service(unique_fd fd) {
77 if (getuid() != 0) {
78 WriteFdExactly(fd.get(), "adbd not running as root\n");
79 return;
80 }
81 android::base::SetProperty("service.adb.root", "0");
82 WriteFdExactly(fd.get(), "restarting adbd as non root\n");
83}
84
85void restart_tcp_service(unique_fd fd, int port) {
86 if (port <= 0) {
87 WriteFdFmt(fd.get(), "invalid port %d\n", port);
88 return;
89 }
90
91 android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port));
92 WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port);
93}
94
95void restart_usb_service(unique_fd fd) {
96 android::base::SetProperty("service.adb.tcp.port", "0");
97 WriteFdExactly(fd.get(), "restarting in USB mode\n");
98}
99
Josh Gao6fb94612018-08-01 16:00:05 -0700100void reboot_service(unique_fd fd, const std::string& arg) {
Josh Gao997cfac2018-07-25 18:15:52 -0700101 std::string reboot_arg = arg;
Josh Gao997cfac2018-07-25 18:15:52 -0700102 sync();
103
104 if (reboot_arg.empty()) reboot_arg = "adb";
105 std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str());
Josh Gao6fb94612018-08-01 16:00:05 -0700106
Hridya Valsaraju54258262018-09-19 21:14:18 -0700107 if (reboot_arg == "fastboot" &&
Yifan Hong0e0f8182018-11-16 12:49:06 -0800108 android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
Hridya Valsaraju54258262018-09-19 21:14:18 -0700109 access("/dev/socket/recovery", F_OK) == 0) {
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700110 LOG(INFO) << "Recovery specific reboot fastboot";
111 /*
112 * The socket is created to allow switching between recovery and
113 * fastboot.
114 */
115 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
116 if (sock < 0) {
117 WriteFdFmt(fd, "reboot (%s) create\n", strerror(errno));
118 PLOG(ERROR) << "Creating recovery socket failed";
119 return;
120 }
121
122 sockaddr_un addr = {.sun_family = AF_UNIX};
123 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
124 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) {
125 WriteFdFmt(fd, "reboot (%s) connect\n", strerror(errno));
126 PLOG(ERROR) << "Couldn't connect to recovery socket";
127 return;
128 }
129 const char msg_switch_to_fastboot = 'f';
130 auto ret = adb_write(sock, &msg_switch_to_fastboot, sizeof(msg_switch_to_fastboot));
131 if (ret != sizeof(msg_switch_to_fastboot)) {
132 WriteFdFmt(fd, "reboot (%s) write\n", strerror(errno));
133 PLOG(ERROR) << "Couldn't write message to recovery socket to switch to fastboot";
134 return;
135 }
136 } else {
137 if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
138 WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str());
139 return;
140 }
141 }
Josh Gao997cfac2018-07-25 18:15:52 -0700142 // Don't return early. Give the reboot command time to take effect
143 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
144 while (true) {
145 pause();
146 }
147}
148
149void reconnect_service(unique_fd fd, atransport* t) {
150 WriteFdExactly(fd.get(), "done");
151 kick_transport(t);
152}
153
Josh Gaoe2615412018-12-13 13:06:03 -0800154unique_fd reverse_service(std::string_view command, atransport* transport) {
155 // TODO: Switch handle_forward_request to std::string_view.
156 std::string str(command);
157
Josh Gao997cfac2018-07-25 18:15:52 -0700158 int s[2];
159 if (adb_socketpair(s)) {
160 PLOG(ERROR) << "cannot create service socket pair.";
161 return unique_fd{};
162 }
163 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
Josh Gaoe2615412018-12-13 13:06:03 -0800164 if (!handle_forward_request(str.c_str(), transport, s[1])) {
Josh Gao997cfac2018-07-25 18:15:52 -0700165 SendFail(s[1], "not a reverse forwarding command");
166 }
167 adb_close(s[1]);
168 return unique_fd{s[0]};
169}
170
171// Shell service string can look like:
172// shell[,arg1,arg2,...]:[command]
Josh Gaoe2615412018-12-13 13:06:03 -0800173unique_fd ShellService(std::string_view args, const atransport* transport) {
Josh Gao997cfac2018-07-25 18:15:52 -0700174 size_t delimiter_index = args.find(':');
175 if (delimiter_index == std::string::npos) {
176 LOG(ERROR) << "No ':' found in shell service arguments: " << args;
177 return unique_fd{};
178 }
179
Josh Gaoe2615412018-12-13 13:06:03 -0800180 // TODO: android::base::Split(const std::string_view&, ...)
181 std::string service_args(args.substr(0, delimiter_index));
182 std::string command(args.substr(delimiter_index + 1));
Josh Gao997cfac2018-07-25 18:15:52 -0700183
184 // Defaults:
185 // PTY for interactive, raw for non-interactive.
186 // No protocol.
187 // $TERM set to "dumb".
188 SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw);
189 SubprocessProtocol protocol = SubprocessProtocol::kNone;
190 std::string terminal_type = "dumb";
191
192 for (const std::string& arg : android::base::Split(service_args, ",")) {
193 if (arg == kShellServiceArgRaw) {
194 type = SubprocessType::kRaw;
195 } else if (arg == kShellServiceArgPty) {
196 type = SubprocessType::kPty;
197 } else if (arg == kShellServiceArgShellProtocol) {
198 protocol = SubprocessProtocol::kShell;
Josh Gaoe2615412018-12-13 13:06:03 -0800199 } else if (arg.starts_with("TERM=")) {
200 terminal_type = arg.substr(strlen("TERM="));
Josh Gao997cfac2018-07-25 18:15:52 -0700201 } else if (!arg.empty()) {
202 // This is not an error to allow for future expansion.
203 LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
204 }
205 }
206
Josh Gaoe2615412018-12-13 13:06:03 -0800207 return StartSubprocess(command, terminal_type.c_str(), type, protocol);
Josh Gao997cfac2018-07-25 18:15:52 -0700208}
209
Josh Gao39c1f4b2018-10-05 17:09:07 -0700210static void spin_service(unique_fd fd) {
211 if (!__android_log_is_debuggable()) {
212 WriteFdExactly(fd.get(), "refusing to spin on non-debuggable build\n");
213 return;
214 }
215
216 // A service that creates an fdevent that's always pending, and then ignores it.
217 unique_fd pipe_read, pipe_write;
218 if (!Pipe(&pipe_read, &pipe_write)) {
219 WriteFdExactly(fd.get(), "failed to create pipe\n");
220 return;
221 }
222
223 fdevent_run_on_main_thread([fd = pipe_read.release()]() {
224 fdevent* fde = fdevent_create(fd, [](int, unsigned, void*) {}, nullptr);
225 fdevent_add(fde, FDE_READ);
226 });
227
228 WriteFdExactly(fd.get(), "spinning\n");
229}
230
Josh Gao6eb78822018-11-16 15:40:16 -0800231struct ServiceSocket : public asocket {
232 ServiceSocket() {
233 install_local_socket(this);
234 this->enqueue = [](asocket* self, apacket::payload_type data) {
235 return static_cast<ServiceSocket*>(self)->Enqueue(std::move(data));
236 };
237 this->ready = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Ready(); };
238 this->close = [](asocket* self) { return static_cast<ServiceSocket*>(self)->Close(); };
239 }
240 virtual ~ServiceSocket() = default;
241
242 virtual int Enqueue(apacket::payload_type data) { return -1; }
243 virtual void Ready() {}
244 virtual void Close() {
245 if (peer) {
246 peer->peer = nullptr;
247 if (peer->shutdown) {
248 peer->shutdown(peer);
249 }
250 peer->close(peer);
251 }
252
253 remove_socket(this);
254 delete this;
255 }
256};
257
258struct SinkSocket : public ServiceSocket {
259 explicit SinkSocket(size_t byte_count) {
260 LOG(INFO) << "Creating new SinkSocket with capacity " << byte_count;
261 bytes_left_ = byte_count;
262 }
263
264 virtual ~SinkSocket() { LOG(INFO) << "SinkSocket destroyed"; }
265
266 virtual int Enqueue(apacket::payload_type data) override final {
267 if (bytes_left_ <= data.size()) {
268 // Done reading.
269 Close();
270 return -1;
271 }
272
273 bytes_left_ -= data.size();
274 return 0;
275 }
276
277 size_t bytes_left_;
278};
279
280struct SourceSocket : public ServiceSocket {
281 explicit SourceSocket(size_t byte_count) {
282 LOG(INFO) << "Creating new SourceSocket with capacity " << byte_count;
283 bytes_left_ = byte_count;
284 }
285
286 virtual ~SourceSocket() { LOG(INFO) << "SourceSocket destroyed"; }
287
288 void Ready() {
289 size_t len = std::min(bytes_left_, get_max_payload());
290 if (len == 0) {
291 Close();
292 return;
293 }
294
295 Block block(len);
296 memset(block.data(), 0, block.size());
297 peer->enqueue(peer, std::move(block));
298 bytes_left_ -= len;
299 }
300
301 int Enqueue(apacket::payload_type data) { return -1; }
302
303 size_t bytes_left_;
304};
305
306asocket* daemon_service_to_socket(std::string_view name) {
307 if (name == "jdwp") {
308 return create_jdwp_service_socket();
309 } else if (name == "track-jdwp") {
310 return create_jdwp_tracker_service_socket();
311 } else if (name.starts_with("sink:")) {
312 name.remove_prefix(strlen("sink:"));
313 uint64_t byte_count = 0;
314 if (!android::base::ParseUint(name.data(), &byte_count)) {
315 return nullptr;
316 }
317 return new SinkSocket(byte_count);
318 } else if (name.starts_with("source:")) {
319 name.remove_prefix(strlen("source:"));
320 uint64_t byte_count = 0;
321 if (!android::base::ParseUint(name.data(), &byte_count)) {
322 return nullptr;
323 }
324 return new SourceSocket(byte_count);
325 }
326
327 return nullptr;
328}
329
Josh Gaoe2615412018-12-13 13:06:03 -0800330unique_fd daemon_service_to_fd(std::string_view name, atransport* transport) {
Alex Buynytskyy640407d2018-12-12 10:48:50 -0800331#ifndef __ANDROID_RECOVERY__
332 if (name.starts_with("abb:")) {
333 name.remove_prefix(strlen("abb:"));
334 return execute_binder_command(name);
335 }
336#endif
337
Josh Gaoe2615412018-12-13 13:06:03 -0800338 if (name.starts_with("dev:")) {
339 name.remove_prefix(strlen("dev:"));
340 return unique_fd{unix_open(name, O_RDWR | O_CLOEXEC)};
341 } else if (name.starts_with("framebuffer:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700342 return create_service_thread("fb", framebuffer_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800343 } else if (name.starts_with("jdwp:")) {
344 name.remove_prefix(strlen("jdwp:"));
345 std::string str(name);
346 return create_jdwp_connection_fd(atoi(str.c_str()));
347 } else if (name.starts_with("shell")) {
348 name.remove_prefix(strlen("shell"));
349 return ShellService(name, transport);
350 } else if (name.starts_with("exec:")) {
351 name.remove_prefix(strlen("exec:"));
352 return StartSubprocess(std::string(name), nullptr, SubprocessType::kRaw,
353 SubprocessProtocol::kNone);
354 } else if (name.starts_with("sync:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700355 return create_service_thread("sync", file_sync_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800356 } else if (name.starts_with("remount:")) {
357 std::string arg(name.begin() + strlen("remount:"), name.end());
Josh Gao997cfac2018-07-25 18:15:52 -0700358 return create_service_thread("remount",
Josh Gaoe2615412018-12-13 13:06:03 -0800359 std::bind(remount_service, std::placeholders::_1, arg));
360 } else if (name.starts_with("reboot:")) {
361 std::string arg(name.begin() + strlen("reboot:"), name.end());
Josh Gao997cfac2018-07-25 18:15:52 -0700362 return create_service_thread("reboot",
363 std::bind(reboot_service, std::placeholders::_1, arg));
Josh Gaoe2615412018-12-13 13:06:03 -0800364 } else if (name.starts_with("root:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700365 return create_service_thread("root", restart_root_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800366 } else if (name.starts_with("unroot:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700367 return create_service_thread("unroot", restart_unroot_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800368 } else if (name.starts_with("backup:")) {
369 name.remove_prefix(strlen("backup:"));
370 std::string cmd = "/system/bin/bu backup ";
371 cmd += name;
372 return StartSubprocess(cmd, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
373 } else if (name.starts_with("restore:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700374 return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
375 SubprocessProtocol::kNone);
Josh Gaoe2615412018-12-13 13:06:03 -0800376 } else if (name.starts_with("tcpip:")) {
377 name.remove_prefix(strlen("tcpip:"));
378 std::string str(name);
379
Josh Gao997cfac2018-07-25 18:15:52 -0700380 int port;
Josh Gaoe2615412018-12-13 13:06:03 -0800381 if (sscanf(str.c_str(), "%d", &port) != 1) {
Josh Gao997cfac2018-07-25 18:15:52 -0700382 return unique_fd{};
383 }
384 return create_service_thread("tcp",
385 std::bind(restart_tcp_service, std::placeholders::_1, port));
Josh Gaoe2615412018-12-13 13:06:03 -0800386 } else if (name.starts_with("usb:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700387 return create_service_thread("usb", restart_usb_service);
Josh Gaoe2615412018-12-13 13:06:03 -0800388 } else if (name.starts_with("reverse:")) {
389 name.remove_prefix(strlen("reverse:"));
390 return reverse_service(name, transport);
391 } else if (name.starts_with("disable-verity:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700392 return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service,
393 std::placeholders::_1, false));
Josh Gaoe2615412018-12-13 13:06:03 -0800394 } else if (name.starts_with("enable-verity:")) {
Josh Gao997cfac2018-07-25 18:15:52 -0700395 return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service,
396 std::placeholders::_1, true));
Josh Gaoe2615412018-12-13 13:06:03 -0800397 } else if (name == "reconnect") {
Josh Gao997cfac2018-07-25 18:15:52 -0700398 return create_service_thread(
399 "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
Josh Gaoe2615412018-12-13 13:06:03 -0800400 } else if (name == "spin") {
Josh Gao39c1f4b2018-10-05 17:09:07 -0700401 return create_service_thread("spin", spin_service);
Josh Gao997cfac2018-07-25 18:15:52 -0700402 }
Josh Gao39c1f4b2018-10-05 17:09:07 -0700403
Josh Gao997cfac2018-07-25 18:15:52 -0700404 return unique_fd{};
405}