blob: a85a18c4eeda8a7e91da3806313ff9a2a399cac8 [file] [log] [blame]
Dan Albertc89e0cc2015-05-08 16:13:53 -07001/*
2 * Copyright (C) 2015 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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albertc89e0cc2015-05-08 16:13:53 -070018
19#include "sysdeps.h"
20
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
Josh Gaob72b3f82016-02-04 11:59:12 -080024#include <unistd.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070025
Josh Gaof0f854b2016-12-12 18:15:33 -080026#include <thread>
27
David Pursell5f787ed2016-01-27 08:52:53 -080028#include <android-base/errors.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080029#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/stringprintf.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070032
33#include "adb.h"
34#include "adb_auth.h"
Josh Gaod10b7c42019-03-14 15:38:02 -070035#include "adb_client.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070036#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080037#include "adb_utils.h"
Joshua Duongd85f5c02019-11-20 14:18:43 -080038#include "adb_wifi.h"
Felipe Leme698e0652016-07-19 17:07:22 -070039#include "commandline.h"
Josh Gaof0f854b2016-12-12 18:15:33 -080040#include "sysdeps/chrono.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070041#include "transport.h"
42
Josh Gaod10b7c42019-03-14 15:38:02 -070043const char** __adb_argv;
44const char** __adb_envp;
45
Elliott Hughes6eadee82017-06-15 08:35:24 -070046static void setup_daemon_logging() {
Spencer Low155159c2015-08-11 16:08:43 -070047 const std::string log_file_path(GetLogFilePath());
Josh Gao0f29cbc2018-12-12 16:12:28 -080048 int fd = unix_open(log_file_path, O_WRONLY | O_CREAT | O_APPEND, 0640);
Dan Albertc89e0cc2015-05-08 16:13:53 -070049 if (fd == -1) {
Elliott Hughes4679a392018-10-19 13:59:44 -070050 PLOG(FATAL) << "cannot open " << log_file_path;
Dan Albertc89e0cc2015-05-08 16:13:53 -070051 }
Spencer Low155159c2015-08-11 16:08:43 -070052 if (dup2(fd, STDOUT_FILENO) == -1) {
Elliott Hughes4679a392018-10-19 13:59:44 -070053 PLOG(FATAL) << "cannot redirect stdout";
Spencer Low155159c2015-08-11 16:08:43 -070054 }
55 if (dup2(fd, STDERR_FILENO) == -1) {
Elliott Hughes4679a392018-10-19 13:59:44 -070056 PLOG(FATAL) << "cannot redirect stderr";
Spencer Low155159c2015-08-11 16:08:43 -070057 }
Spencer Lowd0f66c32015-05-20 23:17:26 -070058 unix_close(fd);
59
Dan Albertc89e0cc2015-05-08 16:13:53 -070060 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy743883b2015-08-18 17:53:16 -070061 LOG(INFO) << adb_version();
Dan Albertc89e0cc2015-05-08 16:13:53 -070062}
63
Josh Gao01b7bc42017-05-09 13:43:35 -070064void adb_server_cleanup() {
65 // Upon exit, we want to clean up in the following order:
66 // 1. close_smartsockets, so that we don't get any new clients
67 // 2. kick_all_transports, to avoid writing only part of a packet to a transport.
68 // 3. usb_cleanup, to tear down the USB stack.
69 close_smartsockets();
70 kick_all_transports();
71 usb_cleanup();
72}
73
Josh Gaoe2176112018-02-27 16:10:29 -080074static void intentionally_leak() {
75 void* p = ::operator new(1);
George Burgess IV657db002018-03-01 10:54:21 -080076 // The analyzer is upset about this leaking. NOLINTNEXTLINE
Josh Gaoe2176112018-02-27 16:10:29 -080077 LOG(INFO) << "leaking pointer " << p;
78}
79
Josh Gao9c869b52016-08-25 16:00:22 -070080int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -070081#if defined(_WIN32)
Spencer Low2122c7a2015-08-26 18:46:09 -070082 // adb start-server starts us up with stdout and stderr hooked up to
Elliott Hughes65433da2015-11-18 18:07:48 -080083 // anonymous pipes. When the C Runtime sees this, it makes stderr and
Spencer Low2122c7a2015-08-26 18:46:09 -070084 // stdout buffered, but to improve the chance that error output is seen,
85 // unbuffer stdout and stderr just like if we were run at the console.
86 // This also keeps stderr unbuffered when it is redirected to adb.log.
87 if (is_daemon) {
Yi Kongaed415c2018-07-13 18:15:16 -070088 if (setvbuf(stdout, nullptr, _IONBF, 0) == -1) {
Elliott Hughes4679a392018-10-19 13:59:44 -070089 PLOG(FATAL) << "cannot make stdout unbuffered";
Spencer Low2122c7a2015-08-26 18:46:09 -070090 }
Yi Kongaed415c2018-07-13 18:15:16 -070091 if (setvbuf(stderr, nullptr, _IONBF, 0) == -1) {
Elliott Hughes4679a392018-10-19 13:59:44 -070092 PLOG(FATAL) << "cannot make stderr unbuffered";
Spencer Low2122c7a2015-08-26 18:46:09 -070093 }
94 }
95
Spencer Low84fc2712018-08-25 23:34:33 -070096 // TODO: On Ctrl-C, consider trying to kill a starting up adb server (if we're in
97 // launch_server) by calling GenerateConsoleCtrlEvent().
98
99 // On Windows, SIGBREAK is when Ctrl-Break is pressed or the console window is closed. It should
100 // act like Ctrl-C.
101 signal(SIGBREAK, [](int) { raise(SIGINT); });
102#endif
Josh Gao1c70e1b2016-09-28 12:32:45 -0700103 signal(SIGINT, [](int) {
Josh Gaod51c6df2018-02-23 14:00:24 -0800104 fdevent_run_on_main_thread([]() { exit(0); });
Josh Gao1c70e1b2016-09-28 12:32:45 -0700105 });
Dan Albertc89e0cc2015-05-08 16:13:53 -0700106
Josh Gaoe2176112018-02-27 16:10:29 -0800107 char* leak = getenv("ADB_LEAK");
108 if (leak && strcmp(leak, "1") == 0) {
109 intentionally_leak();
110 }
111
Josh Gao16c5a132017-05-15 18:26:38 -0700112 if (is_daemon) {
113 close_stdin();
114 setup_daemon_logging();
115 }
116
Josh Gaod51c6df2018-02-23 14:00:24 -0800117 atexit(adb_server_cleanup);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700118
Josh Gao01b7bc42017-05-09 13:43:35 -0700119 init_transport_registration();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700120 init_reconnect_handler();
Casey Dahlin13a269e2016-06-23 14:19:37 -0700121
Joshua Duongd85f5c02019-11-20 14:18:43 -0800122 adb_wifi_init();
Josh Gao043fbec2018-08-10 14:24:25 -0700123 if (!getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0) {
124 init_mdns_transport_discovery();
125 }
126
127 if (!getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0) {
128 usb_init();
129 } else {
130 adb_notify_device_scan_complete();
131 }
132
133 if (!getenv("ADB_EMU") || strcmp(getenv("ADB_EMU"), "0") != 0) {
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900134 local_init(android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
Josh Gao043fbec2018-08-10 14:24:25 -0700135 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700136
Spencer Low5200c662015-07-30 23:07:55 -0700137 std::string error;
Josh Gaof0f854b2016-12-12 18:15:33 -0800138
139 auto start = std::chrono::steady_clock::now();
140
141 // If we told a previous adb server to quit because of version mismatch, we can get to this
Raman Tennetib71b5af2019-11-12 18:57:51 +0000142 // point before it's finished exiting. Retry for a while to give it some time.
143 while (install_listener(socket_spec, "*smartsocket*", nullptr, 0, nullptr, &error) !=
144 INSTALL_STATUS_OK) {
Josh Gaof0f854b2016-12-12 18:15:33 -0800145 if (std::chrono::steady_clock::now() - start > 0.5s) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700146 LOG(FATAL) << "could not install *smartsocket* listener: " << error;
Josh Gaof0f854b2016-12-12 18:15:33 -0800147 }
148
149 std::this_thread::sleep_for(100ms);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700150 }
151
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700152 adb_auth_init();
153
154 if (is_daemon) {
Josh Gaob72b3f82016-02-04 11:59:12 -0800155#if !defined(_WIN32)
Yabin Cui6bf323b2016-02-08 22:36:42 -0800156 // Start a new session for the daemon. Do this here instead of after the fork so
157 // that a ctrl-c between the "starting server" and "done starting server" messages
158 // gets a chance to terminate the server.
Tao Wuc8fab892016-09-20 17:58:55 -0700159 // setsid will fail with EPERM if it's already been a lead process of new session.
160 // Ignore such error.
161 if (setsid() == -1 && errno != EPERM) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700162 PLOG(FATAL) << "setsid() failed";
Yabin Cui6bf323b2016-02-08 22:36:42 -0800163 }
Josh Gaob72b3f82016-02-04 11:59:12 -0800164#endif
165
Raman Tennetib71b5af2019-11-12 18:57:51 +0000166 // Wait for the USB scan to complete before notifying the parent that we're up.
167 // We need to perform this in a thread, because we would otherwise block the event loop.
168 std::thread notify_thread([ack_reply_fd]() {
169 adb_wait_for_device_initialization();
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700170
Josh Gaofd713e52017-05-03 22:37:10 -0700171 // Any error output written to stderr now goes to adb.log. We could
172 // keep around a copy of the stderr fd and use that to write any errors
173 // encountered by the following code, but that is probably overkill.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700174#if defined(_WIN32)
Josh Gaofd713e52017-05-03 22:37:10 -0700175 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
176 const CHAR ack[] = "OK\n";
177 const DWORD bytes_to_write = arraysize(ack) - 1;
178 DWORD written = 0;
179 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700180 LOG(FATAL) << "cannot write ACK to handle " << ack_reply_handle
181 << android::base::SystemErrorCodeToString(GetLastError());
Josh Gaofd713e52017-05-03 22:37:10 -0700182 }
183 if (written != bytes_to_write) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700184 LOG(FATAL) << "cannot write " << bytes_to_write << " bytes of ACK: only wrote "
185 << written << " bytes";
Josh Gaofd713e52017-05-03 22:37:10 -0700186 }
187 CloseHandle(ack_reply_handle);
Spencer Low5c398d22015-08-08 15:07:07 -0700188#else
Josh Gaofd713e52017-05-03 22:37:10 -0700189 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
190 // "OKAY".
191 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700192 PLOG(FATAL) << "error writing ACK to fd " << ack_reply_fd;
Josh Gaofd713e52017-05-03 22:37:10 -0700193 }
194 unix_close(ack_reply_fd);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700195#endif
Raman Tennetib71b5af2019-11-12 18:57:51 +0000196 });
197 notify_thread.detach();
198 }
Dan Albertc89e0cc2015-05-08 16:13:53 -0700199
Josh Gaod10b7c42019-03-14 15:38:02 -0700200#if defined(__linux__)
201 // Write our location to .android/adb.$PORT, so that older clients can exec us.
202 std::string path;
203 if (!android::base::Readlink("/proc/self/exe", &path)) {
204 PLOG(ERROR) << "failed to readlink /proc/self/exe";
205 }
206
207 std::optional<std::string> server_executable_path = adb_get_server_executable_path();
208 if (server_executable_path) {
209 if (!android::base::WriteStringToFile(path, *server_executable_path)) {
210 PLOG(ERROR) << "failed to write server path to " << path;
211 }
212 }
213#endif
214
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700215 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700216 fdevent_loop();
Dan Albertc89e0cc2015-05-08 16:13:53 -0700217 return 0;
218}
219
Josh Gaod10b7c42019-03-14 15:38:02 -0700220int main(int argc, char* argv[], char* envp[]) {
221 __adb_argv = const_cast<const char**>(argv);
222 __adb_envp = const_cast<const char**>(envp);
Dan Albert9313c0d2015-05-21 13:58:50 -0700223 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700224 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
225}