blob: 62798cde775a4d0afe4edbd4e1f68148f96fdc26 [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>
Josh Gao1c70e1b2016-09-28 12:32:45 -070031#include <android-base/quick_exit.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080032#include <android-base/stringprintf.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070033
34#include "adb.h"
35#include "adb_auth.h"
36#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080037#include "adb_utils.h"
Felipe Leme698e0652016-07-19 17:07:22 -070038#include "commandline.h"
Josh Gaof0f854b2016-12-12 18:15:33 -080039#include "sysdeps/chrono.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070040#include "transport.h"
41
Dan Albertc89e0cc2015-05-08 16:13:53 -070042static std::string GetLogFilePath() {
Elliott Hughesd89a6c22016-06-07 13:56:52 -070043#if defined(_WIN32)
Dan Albertc89e0cc2015-05-08 16:13:53 -070044 const char log_name[] = "adb.log";
Spencer Lowcf4ff642015-05-11 01:08:48 -070045 WCHAR temp_path[MAX_PATH];
Dan Albertc89e0cc2015-05-08 16:13:53 -070046
47 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
Spencer Lowcf4ff642015-05-11 01:08:48 -070048 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
Elliott Hughesd89a6c22016-06-07 13:56:52 -070049 if (nchars >= arraysize(temp_path) || nchars == 0) {
Spencer Lowcf4ff642015-05-11 01:08:48 -070050 // If string truncation or some other error.
Spencer Low155159c2015-08-11 16:08:43 -070051 fatal("cannot retrieve temporary file path: %s\n",
David Pursell5f787ed2016-01-27 08:52:53 -080052 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -070053 }
54
Spencer Lowd21dc822015-11-12 15:20:15 -080055 std::string temp_path_utf8;
56 if (!android::base::WideToUTF8(temp_path, &temp_path_utf8)) {
57 fatal_errno("cannot convert temporary file path from UTF-16 to UTF-8");
58 }
59
60 return temp_path_utf8 + log_name;
Dan Albertc89e0cc2015-05-08 16:13:53 -070061#else
Elliott Hughesd89a6c22016-06-07 13:56:52 -070062 const char* tmp_dir = getenv("TMPDIR");
63 if (tmp_dir == nullptr) tmp_dir = "/tmp";
64 return android::base::StringPrintf("%s/adb.%u.log", tmp_dir, getuid());
Dan Albertc89e0cc2015-05-08 16:13:53 -070065#endif
Elliott Hughesd89a6c22016-06-07 13:56:52 -070066}
Dan Albertc89e0cc2015-05-08 16:13:53 -070067
Dan Albertc89e0cc2015-05-08 16:13:53 -070068static void setup_daemon_logging(void) {
Spencer Low155159c2015-08-11 16:08:43 -070069 const std::string log_file_path(GetLogFilePath());
Elliott Hughes65433da2015-11-18 18:07:48 -080070 int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640);
Dan Albertc89e0cc2015-05-08 16:13:53 -070071 if (fd == -1) {
Spencer Low155159c2015-08-11 16:08:43 -070072 fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
Dan Albertc89e0cc2015-05-08 16:13:53 -070073 }
Spencer Low155159c2015-08-11 16:08:43 -070074 if (dup2(fd, STDOUT_FILENO) == -1) {
75 fatal("cannot redirect stdout: %s", strerror(errno));
76 }
77 if (dup2(fd, STDERR_FILENO) == -1) {
78 fatal("cannot redirect stderr: %s", strerror(errno));
79 }
Spencer Lowd0f66c32015-05-20 23:17:26 -070080 unix_close(fd);
81
Dan Albertc89e0cc2015-05-08 16:13:53 -070082 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy743883b2015-08-18 17:53:16 -070083 LOG(INFO) << adb_version();
Dan Albertc89e0cc2015-05-08 16:13:53 -070084}
85
Elliott Hughesd89a6c22016-06-07 13:56:52 -070086#if defined(_WIN32)
87static BOOL WINAPI ctrlc_handler(DWORD type) {
88 // TODO: Consider trying to kill a starting up adb server (if we're in
89 // launch_server) by calling GenerateConsoleCtrlEvent().
Josh Gao1c70e1b2016-09-28 12:32:45 -070090 android::base::quick_exit(STATUS_CONTROL_C_EXIT);
Elliott Hughesd89a6c22016-06-07 13:56:52 -070091 return TRUE;
92}
93#endif
94
Josh Gao01b7bc42017-05-09 13:43:35 -070095void adb_server_cleanup() {
96 // Upon exit, we want to clean up in the following order:
97 // 1. close_smartsockets, so that we don't get any new clients
98 // 2. kick_all_transports, to avoid writing only part of a packet to a transport.
99 // 3. usb_cleanup, to tear down the USB stack.
100 close_smartsockets();
101 kick_all_transports();
102 usb_cleanup();
103}
104
Josh Gao9c869b52016-08-25 16:00:22 -0700105int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700106#if defined(_WIN32)
Spencer Low2122c7a2015-08-26 18:46:09 -0700107 // adb start-server starts us up with stdout and stderr hooked up to
Elliott Hughes65433da2015-11-18 18:07:48 -0800108 // anonymous pipes. When the C Runtime sees this, it makes stderr and
Spencer Low2122c7a2015-08-26 18:46:09 -0700109 // stdout buffered, but to improve the chance that error output is seen,
110 // unbuffer stdout and stderr just like if we were run at the console.
111 // This also keeps stderr unbuffered when it is redirected to adb.log.
112 if (is_daemon) {
113 if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
114 fatal("cannot make stdout unbuffered: %s", strerror(errno));
115 }
116 if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
117 fatal("cannot make stderr unbuffered: %s", strerror(errno));
118 }
119 }
120
Dan Albertc89e0cc2015-05-08 16:13:53 -0700121 SetConsoleCtrlHandler(ctrlc_handler, TRUE);
Josh Gao1c70e1b2016-09-28 12:32:45 -0700122#else
123 signal(SIGINT, [](int) {
Josh Gao01b7bc42017-05-09 13:43:35 -0700124 fdevent_run_on_main_thread([]() { android::base::quick_exit(0); });
Josh Gao1c70e1b2016-09-28 12:32:45 -0700125 });
Dan Albertc89e0cc2015-05-08 16:13:53 -0700126#endif
127
Josh Gao16c5a132017-05-15 18:26:38 -0700128 if (is_daemon) {
129 close_stdin();
130 setup_daemon_logging();
131 }
132
Josh Gao01b7bc42017-05-09 13:43:35 -0700133 android::base::at_quick_exit(adb_server_cleanup);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700134
Josh Gao01b7bc42017-05-09 13:43:35 -0700135 init_transport_registration();
Casey Dahlin13a269e2016-06-23 14:19:37 -0700136 init_mdns_transport_discovery();
137
Dan Albertc89e0cc2015-05-08 16:13:53 -0700138 usb_init();
139 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700140
Spencer Low5200c662015-07-30 23:07:55 -0700141 std::string error;
Josh Gaof0f854b2016-12-12 18:15:33 -0800142
143 auto start = std::chrono::steady_clock::now();
144
145 // If we told a previous adb server to quit because of version mismatch, we can get to this
146 // point before it's finished exiting. Retry for a while to give it some time.
147 while (install_listener(socket_spec, "*smartsocket*", nullptr, 0, nullptr, &error) !=
148 INSTALL_STATUS_OK) {
149 if (std::chrono::steady_clock::now() - start > 0.5s) {
150 fatal("could not install *smartsocket* listener: %s", error.c_str());
151 }
152
153 std::this_thread::sleep_for(100ms);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700154 }
155
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700156 adb_auth_init();
157
158 if (is_daemon) {
Josh Gaob72b3f82016-02-04 11:59:12 -0800159#if !defined(_WIN32)
Yabin Cui6bf323b2016-02-08 22:36:42 -0800160 // Start a new session for the daemon. Do this here instead of after the fork so
161 // that a ctrl-c between the "starting server" and "done starting server" messages
162 // gets a chance to terminate the server.
Tao Wuc8fab892016-09-20 17:58:55 -0700163 // setsid will fail with EPERM if it's already been a lead process of new session.
164 // Ignore such error.
165 if (setsid() == -1 && errno != EPERM) {
Yabin Cui6bf323b2016-02-08 22:36:42 -0800166 fatal("setsid() failed: %s", strerror(errno));
167 }
Josh Gaob72b3f82016-02-04 11:59:12 -0800168#endif
169
Josh Gaofd713e52017-05-03 22:37:10 -0700170 // Wait for the USB scan to complete before notifying the parent that we're up.
171 // We need to perform this in a thread, because we would otherwise block the event loop.
172 std::thread notify_thread([ack_reply_fd]() {
173 adb_wait_for_device_initialization();
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700174
Josh Gaofd713e52017-05-03 22:37:10 -0700175 // Any error output written to stderr now goes to adb.log. We could
176 // keep around a copy of the stderr fd and use that to write any errors
177 // encountered by the following code, but that is probably overkill.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700178#if defined(_WIN32)
Josh Gaofd713e52017-05-03 22:37:10 -0700179 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
180 const CHAR ack[] = "OK\n";
181 const DWORD bytes_to_write = arraysize(ack) - 1;
182 DWORD written = 0;
183 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
184 fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
185 android::base::SystemErrorCodeToString(GetLastError()).c_str());
186 }
187 if (written != bytes_to_write) {
188 fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes", bytes_to_write,
189 written);
190 }
191 CloseHandle(ack_reply_handle);
Spencer Low5c398d22015-08-08 15:07:07 -0700192#else
Josh Gaofd713e52017-05-03 22:37:10 -0700193 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
194 // "OKAY".
195 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
196 fatal_errno("error writing ACK to fd %d", ack_reply_fd);
197 }
198 unix_close(ack_reply_fd);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700199#endif
Josh Gaofd713e52017-05-03 22:37:10 -0700200 });
201 notify_thread.detach();
Dan Albertc89e0cc2015-05-08 16:13:53 -0700202 }
203
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700204 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700205 fdevent_loop();
206
207 return 0;
208}
209
210int main(int argc, char** argv) {
Dan Albert9313c0d2015-05-21 13:58:50 -0700211 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700212 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
213}