blob: 8417690435c484ca2d5f1e24a51ac30d564b3119 [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>
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
61void 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
75void 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
84void 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
94void 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 Gao6fb94612018-08-01 16:00:05 -070099void reboot_service(unique_fd fd, const std::string& arg) {
Josh Gao997cfac2018-07-25 18:15:52 -0700100 std::string reboot_arg = arg;
Josh Gao997cfac2018-07-25 18:15:52 -0700101 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 Gao6fb94612018-08-01 16:00:05 -0700105
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700106 if (reboot_arg == "fastboot" && access("/dev/socket/recovery", F_OK) == 0) {
107 LOG(INFO) << "Recovery specific reboot fastboot";
108 /*
109 * The socket is created to allow switching between recovery and
110 * fastboot.
111 */
112 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
113 if (sock < 0) {
114 WriteFdFmt(fd, "reboot (%s) create\n", strerror(errno));
115 PLOG(ERROR) << "Creating recovery socket failed";
116 return;
117 }
118
119 sockaddr_un addr = {.sun_family = AF_UNIX};
120 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
121 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) {
122 WriteFdFmt(fd, "reboot (%s) connect\n", strerror(errno));
123 PLOG(ERROR) << "Couldn't connect to recovery socket";
124 return;
125 }
126 const char msg_switch_to_fastboot = 'f';
127 auto ret = adb_write(sock, &msg_switch_to_fastboot, sizeof(msg_switch_to_fastboot));
128 if (ret != sizeof(msg_switch_to_fastboot)) {
129 WriteFdFmt(fd, "reboot (%s) write\n", strerror(errno));
130 PLOG(ERROR) << "Couldn't write message to recovery socket to switch to fastboot";
131 return;
132 }
133 } else {
134 if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
135 WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str());
136 return;
137 }
138 }
Josh Gao997cfac2018-07-25 18:15:52 -0700139 // Don't return early. Give the reboot command time to take effect
140 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
141 while (true) {
142 pause();
143 }
144}
145
146void reconnect_service(unique_fd fd, atransport* t) {
147 WriteFdExactly(fd.get(), "done");
148 kick_transport(t);
149}
150
151unique_fd reverse_service(const char* command, atransport* transport) {
152 int s[2];
153 if (adb_socketpair(s)) {
154 PLOG(ERROR) << "cannot create service socket pair.";
155 return unique_fd{};
156 }
157 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
Josh Gao19062432018-07-30 18:49:03 -0700158 if (!handle_forward_request(command, transport, s[1])) {
Josh Gao997cfac2018-07-25 18:15:52 -0700159 SendFail(s[1], "not a reverse forwarding command");
160 }
161 adb_close(s[1]);
162 return unique_fd{s[0]};
163}
164
165// Shell service string can look like:
166// shell[,arg1,arg2,...]:[command]
167unique_fd ShellService(const std::string& args, const atransport* transport) {
168 size_t delimiter_index = args.find(':');
169 if (delimiter_index == std::string::npos) {
170 LOG(ERROR) << "No ':' found in shell service arguments: " << args;
171 return unique_fd{};
172 }
173
174 const std::string service_args = args.substr(0, delimiter_index);
175 const std::string command = args.substr(delimiter_index + 1);
176
177 // Defaults:
178 // PTY for interactive, raw for non-interactive.
179 // No protocol.
180 // $TERM set to "dumb".
181 SubprocessType type(command.empty() ? SubprocessType::kPty : SubprocessType::kRaw);
182 SubprocessProtocol protocol = SubprocessProtocol::kNone;
183 std::string terminal_type = "dumb";
184
185 for (const std::string& arg : android::base::Split(service_args, ",")) {
186 if (arg == kShellServiceArgRaw) {
187 type = SubprocessType::kRaw;
188 } else if (arg == kShellServiceArgPty) {
189 type = SubprocessType::kPty;
190 } else if (arg == kShellServiceArgShellProtocol) {
191 protocol = SubprocessProtocol::kShell;
192 } else if (android::base::StartsWith(arg, "TERM=")) {
193 terminal_type = arg.substr(5);
194 } else if (!arg.empty()) {
195 // This is not an error to allow for future expansion.
196 LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
197 }
198 }
199
200 return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol);
201}
202
203unique_fd daemon_service_to_fd(const char* name, atransport* transport) {
204 if (!strncmp("dev:", name, 4)) {
205 return unique_fd{unix_open(name + 4, O_RDWR | O_CLOEXEC)};
206 } else if (!strncmp(name, "framebuffer:", 12)) {
207 return create_service_thread("fb", framebuffer_service);
208 } else if (!strncmp(name, "jdwp:", 5)) {
209 return create_jdwp_connection_fd(atoi(name + 5));
210 } else if (!strncmp(name, "shell", 5)) {
211 return ShellService(name + 5, transport);
212 } else if (!strncmp(name, "exec:", 5)) {
213 return StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
214 } else if (!strncmp(name, "sync:", 5)) {
215 return create_service_thread("sync", file_sync_service);
216 } else if (!strncmp(name, "remount:", 8)) {
217 std::string options(name + strlen("remount:"));
218 return create_service_thread("remount",
219 std::bind(remount_service, std::placeholders::_1, options));
220 } else if (!strncmp(name, "reboot:", 7)) {
221 std::string arg(name + strlen("reboot:"));
222 return create_service_thread("reboot",
223 std::bind(reboot_service, std::placeholders::_1, arg));
224 } else if (!strncmp(name, "root:", 5)) {
225 return create_service_thread("root", restart_root_service);
226 } else if (!strncmp(name, "unroot:", 7)) {
227 return create_service_thread("unroot", restart_unroot_service);
228 } else if (!strncmp(name, "backup:", 7)) {
229 return StartSubprocess(
230 android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(),
231 nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
232 } else if (!strncmp(name, "restore:", 8)) {
233 return StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
234 SubprocessProtocol::kNone);
235 } else if (!strncmp(name, "tcpip:", 6)) {
236 int port;
237 if (sscanf(name + 6, "%d", &port) != 1) {
238 return unique_fd{};
239 }
240 return create_service_thread("tcp",
241 std::bind(restart_tcp_service, std::placeholders::_1, port));
242 } else if (!strncmp(name, "usb:", 4)) {
243 return create_service_thread("usb", restart_usb_service);
244 } else if (!strncmp(name, "reverse:", 8)) {
245 return reverse_service(name + 8, transport);
246 } else if (!strncmp(name, "disable-verity:", 15)) {
247 return create_service_thread("verity-on", std::bind(set_verity_enabled_state_service,
248 std::placeholders::_1, false));
249 } else if (!strncmp(name, "enable-verity:", 15)) {
250 return create_service_thread("verity-off", std::bind(set_verity_enabled_state_service,
251 std::placeholders::_1, true));
252 } else if (!strcmp(name, "reconnect")) {
253 return create_service_thread(
254 "reconnect", std::bind(reconnect_service, std::placeholders::_1, transport));
255 }
256 return unique_fd{};
257}