blob: 4fa58fafc1c62101da6380ae9b8c6dcf96495b25 [file] [log] [blame]
Dan Albert53a37442015-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert53a37442015-05-08 16:13:53 -070018
19#include "sysdeps.h"
20
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
Josh Gaofdd946b2016-02-04 11:59:12 -080024#include <unistd.h>
Dan Albert53a37442015-05-08 16:13:53 -070025
Josh Gaoe80f47a2016-12-12 18:15:33 -080026#include <thread>
27
David Pursellc573d522016-01-27 08:52:53 -080028#include <android-base/errors.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080029#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/stringprintf.h>
Dan Albert53a37442015-05-08 16:13:53 -070032
33#include "adb.h"
34#include "adb_auth.h"
Josh Gao67209db2019-03-14 15:38:02 -070035#include "adb_client.h"
Dan Albert53a37442015-05-08 16:13:53 -070036#include "adb_listeners.h"
Joshua Duongf4ba8d72021-01-13 12:18:15 -080037#include "adb_mdns.h"
Josh Gao1eef4782015-11-20 15:37:31 -080038#include "adb_utils.h"
Joshua Duong290ccb52019-11-20 14:18:43 -080039#include "adb_wifi.h"
Josh Gao6b55e752020-03-27 18:09:56 -070040#include "client/usb.h"
Felipe Leme042c3512016-07-19 17:07:22 -070041#include "commandline.h"
Josh Gaoe80f47a2016-12-12 18:15:33 -080042#include "sysdeps/chrono.h"
Dan Albert53a37442015-05-08 16:13:53 -070043#include "transport.h"
44
Josh Gao67209db2019-03-14 15:38:02 -070045const char** __adb_argv;
46const char** __adb_envp;
47
Elliott Hughescd61ae32017-06-15 08:35:24 -070048static void setup_daemon_logging() {
Spencer Lowfe6ab812015-08-11 16:08:43 -070049 const std::string log_file_path(GetLogFilePath());
Josh Gaof0c44032018-12-12 16:12:28 -080050 int fd = unix_open(log_file_path, O_WRONLY | O_CREAT | O_APPEND, 0640);
Dan Albert53a37442015-05-08 16:13:53 -070051 if (fd == -1) {
Elliott Hughese64126b2018-10-19 13:59:44 -070052 PLOG(FATAL) << "cannot open " << log_file_path;
Dan Albert53a37442015-05-08 16:13:53 -070053 }
Spencer Lowfe6ab812015-08-11 16:08:43 -070054 if (dup2(fd, STDOUT_FILENO) == -1) {
Elliott Hughese64126b2018-10-19 13:59:44 -070055 PLOG(FATAL) << "cannot redirect stdout";
Spencer Lowfe6ab812015-08-11 16:08:43 -070056 }
57 if (dup2(fd, STDERR_FILENO) == -1) {
Elliott Hughese64126b2018-10-19 13:59:44 -070058 PLOG(FATAL) << "cannot redirect stderr";
Spencer Lowfe6ab812015-08-11 16:08:43 -070059 }
Spencer Low4911f4b2015-05-20 23:17:26 -070060 unix_close(fd);
61
Dan Albert53a37442015-05-08 16:13:53 -070062 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy878e36b2015-08-18 17:53:16 -070063 LOG(INFO) << adb_version();
Dan Albert53a37442015-05-08 16:13:53 -070064}
65
Josh Gao165460f2017-05-09 13:43:35 -070066void adb_server_cleanup() {
67 // Upon exit, we want to clean up in the following order:
68 // 1. close_smartsockets, so that we don't get any new clients
69 // 2. kick_all_transports, to avoid writing only part of a packet to a transport.
70 // 3. usb_cleanup, to tear down the USB stack.
Joshua Duongf4ba8d72021-01-13 12:18:15 -080071 // 4. mdns_cleanup, to tear down mdns stack.
Josh Gao165460f2017-05-09 13:43:35 -070072 close_smartsockets();
73 kick_all_transports();
74 usb_cleanup();
Joshua Duongf4ba8d72021-01-13 12:18:15 -080075 mdns_cleanup();
Josh Gao165460f2017-05-09 13:43:35 -070076}
77
Josh Gaod64d2ed2018-02-27 16:10:29 -080078static void intentionally_leak() {
79 void* p = ::operator new(1);
George Burgess IVa54dbfd2018-03-01 10:54:21 -080080 // The analyzer is upset about this leaking. NOLINTNEXTLINE
Josh Gaod64d2ed2018-02-27 16:10:29 -080081 LOG(INFO) << "leaking pointer " << p;
82}
83
Josh Gaobb4f8602016-08-25 16:00:22 -070084int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) {
Dan Albert53a37442015-05-08 16:13:53 -070085#if defined(_WIN32)
Spencer Low2bbb3a92015-08-26 18:46:09 -070086 // adb start-server starts us up with stdout and stderr hooked up to
Elliott Hughes4919c172015-11-18 18:07:48 -080087 // anonymous pipes. When the C Runtime sees this, it makes stderr and
Spencer Low2bbb3a92015-08-26 18:46:09 -070088 // stdout buffered, but to improve the chance that error output is seen,
89 // unbuffer stdout and stderr just like if we were run at the console.
90 // This also keeps stderr unbuffered when it is redirected to adb.log.
91 if (is_daemon) {
Yi Kong86e67182018-07-13 18:15:16 -070092 if (setvbuf(stdout, nullptr, _IONBF, 0) == -1) {
Elliott Hughese64126b2018-10-19 13:59:44 -070093 PLOG(FATAL) << "cannot make stdout unbuffered";
Spencer Low2bbb3a92015-08-26 18:46:09 -070094 }
Yi Kong86e67182018-07-13 18:15:16 -070095 if (setvbuf(stderr, nullptr, _IONBF, 0) == -1) {
Elliott Hughese64126b2018-10-19 13:59:44 -070096 PLOG(FATAL) << "cannot make stderr unbuffered";
Spencer Low2bbb3a92015-08-26 18:46:09 -070097 }
98 }
99
Spencer Low0d5b2e92018-08-25 23:34:33 -0700100 // TODO: On Ctrl-C, consider trying to kill a starting up adb server (if we're in
101 // launch_server) by calling GenerateConsoleCtrlEvent().
102
103 // On Windows, SIGBREAK is when Ctrl-Break is pressed or the console window is closed. It should
104 // act like Ctrl-C.
105 signal(SIGBREAK, [](int) { raise(SIGINT); });
106#endif
Josh Gaob7366922016-09-28 12:32:45 -0700107 signal(SIGINT, [](int) {
Josh Gao50576fd2018-02-23 14:00:24 -0800108 fdevent_run_on_main_thread([]() { exit(0); });
Josh Gaob7366922016-09-28 12:32:45 -0700109 });
Dan Albert53a37442015-05-08 16:13:53 -0700110
Josh Gaobea8f3c2020-04-06 12:07:56 -0700111 const char* reject_kill_server = getenv("ADB_REJECT_KILL_SERVER");
112 if (reject_kill_server && strcmp(reject_kill_server, "1") == 0) {
113 adb_set_reject_kill_server(true);
114 }
115
116 const char* leak = getenv("ADB_LEAK");
Josh Gaod64d2ed2018-02-27 16:10:29 -0800117 if (leak && strcmp(leak, "1") == 0) {
118 intentionally_leak();
119 }
120
Josh Gao530b9f12017-05-15 18:26:38 -0700121 if (is_daemon) {
122 close_stdin();
123 setup_daemon_logging();
124 }
125
Josh Gao50576fd2018-02-23 14:00:24 -0800126 atexit(adb_server_cleanup);
Dan Albert53a37442015-05-08 16:13:53 -0700127
Josh Gao165460f2017-05-09 13:43:35 -0700128 init_transport_registration();
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700129 init_reconnect_handler();
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700130
Joshua Duong290ccb52019-11-20 14:18:43 -0800131 adb_wifi_init();
Josh Gao9aa63fe2018-08-10 14:24:25 -0700132 if (!getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0) {
133 init_mdns_transport_discovery();
134 }
135
136 if (!getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0) {
Josh Gaof2d6af52021-04-27 20:32:02 -0700137 if (should_use_libusb()) {
138 libusb::usb_init();
139 } else {
140 usb_init();
141 }
Josh Gao9aa63fe2018-08-10 14:24:25 -0700142 } else {
143 adb_notify_device_scan_complete();
144 }
145
146 if (!getenv("ADB_EMU") || strcmp(getenv("ADB_EMU"), "0") != 0) {
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900147 local_init(android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
Josh Gao9aa63fe2018-08-10 14:24:25 -0700148 }
Dan Albert53a37442015-05-08 16:13:53 -0700149
Spencer Low753d4852015-07-30 23:07:55 -0700150 std::string error;
Josh Gaoe80f47a2016-12-12 18:15:33 -0800151
152 auto start = std::chrono::steady_clock::now();
153
154 // If we told a previous adb server to quit because of version mismatch, we can get to this
Daniel Colascione232c39c2019-11-13 17:51:19 -0800155 // point before it's finished exiting. Retry for a while to give it some time. Don't actually
156 // accept any connections until adb_wait_for_device_initialization finishes below.
157 while (install_listener(socket_spec, "*smartsocket*", nullptr, INSTALL_LISTENER_DISABLED,
158 nullptr, &error) != INSTALL_STATUS_OK) {
Josh Gaoe80f47a2016-12-12 18:15:33 -0800159 if (std::chrono::steady_clock::now() - start > 0.5s) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700160 LOG(FATAL) << "could not install *smartsocket* listener: " << error;
Josh Gaoe80f47a2016-12-12 18:15:33 -0800161 }
162
163 std::this_thread::sleep_for(100ms);
Dan Albert53a37442015-05-08 16:13:53 -0700164 }
165
Elliott Hughes801066a2016-06-29 17:42:01 -0700166 adb_auth_init();
167
168 if (is_daemon) {
Josh Gaofdd946b2016-02-04 11:59:12 -0800169#if !defined(_WIN32)
Yabin Cuicf91dbe2016-02-08 22:36:42 -0800170 // Start a new session for the daemon. Do this here instead of after the fork so
171 // that a ctrl-c between the "starting server" and "done starting server" messages
172 // gets a chance to terminate the server.
Tao Wudc046e62016-09-20 17:58:55 -0700173 // setsid will fail with EPERM if it's already been a lead process of new session.
174 // Ignore such error.
175 if (setsid() == -1 && errno != EPERM) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700176 PLOG(FATAL) << "setsid() failed";
Yabin Cuicf91dbe2016-02-08 22:36:42 -0800177 }
Josh Gaofdd946b2016-02-04 11:59:12 -0800178#endif
Daniel Colascione232c39c2019-11-13 17:51:19 -0800179 }
Josh Gaofdd946b2016-02-04 11:59:12 -0800180
Daniel Colascione232c39c2019-11-13 17:51:19 -0800181 // Wait for the USB scan to complete before notifying the parent that we're up.
182 // We need to perform this in a thread, because we would otherwise block the event loop.
183 std::thread notify_thread([ack_reply_fd]() {
184 adb_wait_for_device_initialization();
Elliott Hughes801066a2016-06-29 17:42:01 -0700185
Daniel Colascione232c39c2019-11-13 17:51:19 -0800186 if (ack_reply_fd >= 0) {
Josh Gao1e3bf732017-05-03 22:37:10 -0700187 // Any error output written to stderr now goes to adb.log. We could
188 // keep around a copy of the stderr fd and use that to write any errors
189 // encountered by the following code, but that is probably overkill.
Dan Albert53a37442015-05-08 16:13:53 -0700190#if defined(_WIN32)
Josh Gao1e3bf732017-05-03 22:37:10 -0700191 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
192 const CHAR ack[] = "OK\n";
193 const DWORD bytes_to_write = arraysize(ack) - 1;
194 DWORD written = 0;
195 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700196 LOG(FATAL) << "cannot write ACK to handle " << ack_reply_handle
197 << android::base::SystemErrorCodeToString(GetLastError());
Josh Gao1e3bf732017-05-03 22:37:10 -0700198 }
199 if (written != bytes_to_write) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700200 LOG(FATAL) << "cannot write " << bytes_to_write << " bytes of ACK: only wrote "
201 << written << " bytes";
Josh Gao1e3bf732017-05-03 22:37:10 -0700202 }
203 CloseHandle(ack_reply_handle);
Spencer Lowb28812f2015-08-08 15:07:07 -0700204#else
Josh Gao1e3bf732017-05-03 22:37:10 -0700205 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
206 // "OKAY".
207 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700208 PLOG(FATAL) << "error writing ACK to fd " << ack_reply_fd;
Josh Gao1e3bf732017-05-03 22:37:10 -0700209 }
210 unix_close(ack_reply_fd);
Siva Velusamya55dbd82015-08-07 10:10:29 -0700211#endif
Daniel Colascione232c39c2019-11-13 17:51:19 -0800212 }
213 // We don't accept() client connections until this point: this way, clients
214 // can't see wonky state early in startup even if they're connecting directly
215 // to the server instead of going through the adb program.
Josh Gao65d18e22020-04-22 20:57:26 -0700216 fdevent_run_on_main_thread([] { enable_server_sockets(); });
Daniel Colascione232c39c2019-11-13 17:51:19 -0800217 });
218 notify_thread.detach();
Dan Albert53a37442015-05-08 16:13:53 -0700219
Josh Gao67209db2019-03-14 15:38:02 -0700220#if defined(__linux__)
221 // Write our location to .android/adb.$PORT, so that older clients can exec us.
222 std::string path;
223 if (!android::base::Readlink("/proc/self/exe", &path)) {
224 PLOG(ERROR) << "failed to readlink /proc/self/exe";
225 }
226
227 std::optional<std::string> server_executable_path = adb_get_server_executable_path();
228 if (server_executable_path) {
229 if (!android::base::WriteStringToFile(path, *server_executable_path)) {
230 PLOG(ERROR) << "failed to write server path to " << path;
231 }
232 }
233#endif
234
Yabin Cui815ad882015-09-02 17:44:28 -0700235 D("Event loop starting");
Dan Albert53a37442015-05-08 16:13:53 -0700236 fdevent_loop();
Dan Albert53a37442015-05-08 16:13:53 -0700237 return 0;
238}
239
Josh Gao67209db2019-03-14 15:38:02 -0700240int main(int argc, char* argv[], char* envp[]) {
241 __adb_argv = const_cast<const char**>(argv);
242 __adb_envp = const_cast<const char**>(envp);
Dan Albert08d552b2015-05-21 13:58:50 -0700243 adb_trace_init(argv);
Dan Albert53a37442015-05-08 16:13:53 -0700244 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
245}