blob: d5835165928d02489dc9b70b25d98c8038036bc4 [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"
35#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080036#include "adb_utils.h"
Felipe Leme698e0652016-07-19 17:07:22 -070037#include "commandline.h"
Josh Gaof0f854b2016-12-12 18:15:33 -080038#include "sysdeps/chrono.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070039#include "transport.h"
40
Dan Albertc89e0cc2015-05-08 16:13:53 -070041static std::string GetLogFilePath() {
Elliott Hughesd89a6c22016-06-07 13:56:52 -070042#if defined(_WIN32)
Dan Albertc89e0cc2015-05-08 16:13:53 -070043 const char log_name[] = "adb.log";
Spencer Lowcf4ff642015-05-11 01:08:48 -070044 WCHAR temp_path[MAX_PATH];
Dan Albertc89e0cc2015-05-08 16:13:53 -070045
46 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
Spencer Lowcf4ff642015-05-11 01:08:48 -070047 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
Elliott Hughesd89a6c22016-06-07 13:56:52 -070048 if (nchars >= arraysize(temp_path) || nchars == 0) {
Spencer Lowcf4ff642015-05-11 01:08:48 -070049 // If string truncation or some other error.
Spencer Low155159c2015-08-11 16:08:43 -070050 fatal("cannot retrieve temporary file path: %s\n",
David Pursell5f787ed2016-01-27 08:52:53 -080051 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -070052 }
53
Spencer Lowd21dc822015-11-12 15:20:15 -080054 std::string temp_path_utf8;
55 if (!android::base::WideToUTF8(temp_path, &temp_path_utf8)) {
56 fatal_errno("cannot convert temporary file path from UTF-16 to UTF-8");
57 }
58
59 return temp_path_utf8 + log_name;
Dan Albertc89e0cc2015-05-08 16:13:53 -070060#else
Elliott Hughesd89a6c22016-06-07 13:56:52 -070061 const char* tmp_dir = getenv("TMPDIR");
62 if (tmp_dir == nullptr) tmp_dir = "/tmp";
63 return android::base::StringPrintf("%s/adb.%u.log", tmp_dir, getuid());
Dan Albertc89e0cc2015-05-08 16:13:53 -070064#endif
Elliott Hughesd89a6c22016-06-07 13:56:52 -070065}
Dan Albertc89e0cc2015-05-08 16:13:53 -070066
Dan Albertc89e0cc2015-05-08 16:13:53 -070067static void setup_daemon_logging(void) {
Spencer Low155159c2015-08-11 16:08:43 -070068 const std::string log_file_path(GetLogFilePath());
Elliott Hughes65433da2015-11-18 18:07:48 -080069 int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640);
Dan Albertc89e0cc2015-05-08 16:13:53 -070070 if (fd == -1) {
Spencer Low155159c2015-08-11 16:08:43 -070071 fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
Dan Albertc89e0cc2015-05-08 16:13:53 -070072 }
Spencer Low155159c2015-08-11 16:08:43 -070073 if (dup2(fd, STDOUT_FILENO) == -1) {
74 fatal("cannot redirect stdout: %s", strerror(errno));
75 }
76 if (dup2(fd, STDERR_FILENO) == -1) {
77 fatal("cannot redirect stderr: %s", strerror(errno));
78 }
Spencer Lowd0f66c32015-05-20 23:17:26 -070079 unix_close(fd);
80
Dan Albertc89e0cc2015-05-08 16:13:53 -070081 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy743883b2015-08-18 17:53:16 -070082 LOG(INFO) << adb_version();
Dan Albertc89e0cc2015-05-08 16:13:53 -070083}
84
Elliott Hughesd89a6c22016-06-07 13:56:52 -070085#if defined(_WIN32)
86static BOOL WINAPI ctrlc_handler(DWORD type) {
87 // TODO: Consider trying to kill a starting up adb server (if we're in
88 // launch_server) by calling GenerateConsoleCtrlEvent().
89 exit(STATUS_CONTROL_C_EXIT);
90 return TRUE;
91}
92#endif
93
Josh Gao9c869b52016-08-25 16:00:22 -070094int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -070095#if defined(_WIN32)
Spencer Low2122c7a2015-08-26 18:46:09 -070096 // adb start-server starts us up with stdout and stderr hooked up to
Elliott Hughes65433da2015-11-18 18:07:48 -080097 // anonymous pipes. When the C Runtime sees this, it makes stderr and
Spencer Low2122c7a2015-08-26 18:46:09 -070098 // stdout buffered, but to improve the chance that error output is seen,
99 // unbuffer stdout and stderr just like if we were run at the console.
100 // This also keeps stderr unbuffered when it is redirected to adb.log.
101 if (is_daemon) {
102 if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
103 fatal("cannot make stdout unbuffered: %s", strerror(errno));
104 }
105 if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
106 fatal("cannot make stderr unbuffered: %s", strerror(errno));
107 }
108 }
109
Dan Albertc89e0cc2015-05-08 16:13:53 -0700110 SetConsoleCtrlHandler(ctrlc_handler, TRUE);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700111#endif
112
113 init_transport_registration();
114
Dan Albertc89e0cc2015-05-08 16:13:53 -0700115 usb_init();
116 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700117
Spencer Low5200c662015-07-30 23:07:55 -0700118 std::string error;
Josh Gaof0f854b2016-12-12 18:15:33 -0800119
120 auto start = std::chrono::steady_clock::now();
121
122 // If we told a previous adb server to quit because of version mismatch, we can get to this
123 // point before it's finished exiting. Retry for a while to give it some time.
124 while (install_listener(socket_spec, "*smartsocket*", nullptr, 0, nullptr, &error) !=
125 INSTALL_STATUS_OK) {
126 if (std::chrono::steady_clock::now() - start > 0.5s) {
127 fatal("could not install *smartsocket* listener: %s", error.c_str());
128 }
129
130 std::this_thread::sleep_for(100ms);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700131 }
132
133 if (is_daemon) {
Spencer Low2122c7a2015-08-26 18:46:09 -0700134 close_stdin();
135 setup_daemon_logging();
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700136 }
Spencer Low2122c7a2015-08-26 18:46:09 -0700137
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700138 adb_auth_init();
139
140 if (is_daemon) {
Josh Gaob72b3f82016-02-04 11:59:12 -0800141#if !defined(_WIN32)
Yabin Cui6bf323b2016-02-08 22:36:42 -0800142 // Start a new session for the daemon. Do this here instead of after the fork so
143 // that a ctrl-c between the "starting server" and "done starting server" messages
144 // gets a chance to terminate the server.
Tao Wuc8fab892016-09-20 17:58:55 -0700145 // setsid will fail with EPERM if it's already been a lead process of new session.
146 // Ignore such error.
147 if (setsid() == -1 && errno != EPERM) {
Yabin Cui6bf323b2016-02-08 22:36:42 -0800148 fatal("setsid() failed: %s", strerror(errno));
149 }
Josh Gaob72b3f82016-02-04 11:59:12 -0800150#endif
151
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700152 // Inform our parent that we are up and running.
153
Spencer Low2122c7a2015-08-26 18:46:09 -0700154 // Any error output written to stderr now goes to adb.log. We could
155 // keep around a copy of the stderr fd and use that to write any errors
156 // encountered by the following code, but that is probably overkill.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700157#if defined(_WIN32)
Spencer Low5c398d22015-08-08 15:07:07 -0700158 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
159 const CHAR ack[] = "OK\n";
160 const DWORD bytes_to_write = arraysize(ack) - 1;
161 DWORD written = 0;
162 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
163 fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
David Pursell5f787ed2016-01-27 08:52:53 -0800164 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Low5c398d22015-08-08 15:07:07 -0700165 }
166 if (written != bytes_to_write) {
167 fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
168 bytes_to_write, written);
169 }
170 CloseHandle(ack_reply_handle);
171#else
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700172 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
173 // "OKAY".
Spencer Lowf18fc082015-08-11 17:05:02 -0700174 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
175 fatal_errno("error writing ACK to fd %d", ack_reply_fd);
176 }
Spencer Low5c398d22015-08-08 15:07:07 -0700177 unix_close(ack_reply_fd);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700178#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700179 }
180
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700181 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700182 fdevent_loop();
183
184 return 0;
185}
186
187int main(int argc, char** argv) {
Dan Albert9313c0d2015-05-21 13:58:50 -0700188 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700189 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
190}