blob: f6ddeb42619e99d523e62b77efcaeb20c596900d [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
17#define TRACE_TAG TRACE_ADB
18
19#include "sysdeps.h"
20
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
24
25// We only build the affinity WAR code for Linux.
26#if defined(__linux__)
27#include <sched.h>
28#endif
29
30#include "base/file.h"
31#include "base/logging.h"
32#include "base/stringprintf.h"
33
34#include "adb.h"
35#include "adb_auth.h"
36#include "adb_listeners.h"
37#include "transport.h"
38
39#if defined(WORKAROUND_BUG6558362) && defined(__linux__)
40static const bool kWorkaroundBug6558362 = true;
41#else
42static const bool kWorkaroundBug6558362 = false;
43#endif
44
45static void adb_workaround_affinity(void) {
46#if defined(__linux__)
47 const char affinity_env[] = "ADB_CPU_AFFINITY_BUG6558362";
48 const char* cpunum_str = getenv(affinity_env);
49 if (cpunum_str == nullptr || *cpunum_str == '\0') {
50 return;
51 }
52
53 char* strtol_res;
54 int cpu_num = strtol(cpunum_str, &strtol_res, 0);
55 if (*strtol_res != '\0') {
56 fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str,
57 affinity_env);
58 }
59
60 cpu_set_t cpu_set;
61 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
62 D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
63
64 CPU_ZERO(&cpu_set);
65 CPU_SET(cpu_num, &cpu_set);
66 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
67
68 sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
69 D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]);
70#else
71 // No workaround was ever implemented for the other platforms.
72#endif
73}
74
75#if defined(_WIN32)
76static const char kNullFileName[] = "NUL";
77
78static BOOL WINAPI ctrlc_handler(DWORD type) {
Spencer Low2122c7a2015-08-26 18:46:09 -070079 // TODO: Consider trying to kill a starting up adb server (if we're in
80 // launch_server) by calling GenerateConsoleCtrlEvent().
Dan Albertc89e0cc2015-05-08 16:13:53 -070081 exit(STATUS_CONTROL_C_EXIT);
82 return TRUE;
83}
84
85static std::string GetLogFilePath() {
86 const char log_name[] = "adb.log";
Spencer Lowcf4ff642015-05-11 01:08:48 -070087 WCHAR temp_path[MAX_PATH];
Dan Albertc89e0cc2015-05-08 16:13:53 -070088
89 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
Spencer Lowcf4ff642015-05-11 01:08:48 -070090 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
91 if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
92 // If string truncation or some other error.
Spencer Low155159c2015-08-11 16:08:43 -070093 fatal("cannot retrieve temporary file path: %s\n",
94 SystemErrorCodeToString(GetLastError()).c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -070095 }
96
Spencer Lowcf4ff642015-05-11 01:08:48 -070097 return narrow(temp_path) + log_name;
Dan Albertc89e0cc2015-05-08 16:13:53 -070098}
99#else
100static const char kNullFileName[] = "/dev/null";
101
102static std::string GetLogFilePath() {
103 return std::string("/tmp/adb.log");
104}
105#endif
106
107static void close_stdin() {
108 int fd = unix_open(kNullFileName, O_RDONLY);
Spencer Low155159c2015-08-11 16:08:43 -0700109 if (fd == -1) {
110 fatal("cannot open '%s': %s", kNullFileName, strerror(errno));
111 }
112 if (dup2(fd, STDIN_FILENO) == -1) {
113 fatal("cannot redirect stdin: %s", strerror(errno));
114 }
Spencer Lowd0f66c32015-05-20 23:17:26 -0700115 unix_close(fd);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700116}
117
118static void setup_daemon_logging(void) {
Spencer Low155159c2015-08-11 16:08:43 -0700119 const std::string log_file_path(GetLogFilePath());
120 int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND,
Dan Albertc89e0cc2015-05-08 16:13:53 -0700121 0640);
122 if (fd == -1) {
Spencer Low155159c2015-08-11 16:08:43 -0700123 fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
Dan Albertc89e0cc2015-05-08 16:13:53 -0700124 }
Spencer Low155159c2015-08-11 16:08:43 -0700125 if (dup2(fd, STDOUT_FILENO) == -1) {
126 fatal("cannot redirect stdout: %s", strerror(errno));
127 }
128 if (dup2(fd, STDERR_FILENO) == -1) {
129 fatal("cannot redirect stderr: %s", strerror(errno));
130 }
Spencer Lowd0f66c32015-05-20 23:17:26 -0700131 unix_close(fd);
132
Dan Albertc89e0cc2015-05-08 16:13:53 -0700133 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy743883b2015-08-18 17:53:16 -0700134 LOG(INFO) << adb_version();
Dan Albertc89e0cc2015-05-08 16:13:53 -0700135}
136
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700137int adb_main(int is_daemon, int server_port, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700138#if defined(_WIN32)
Spencer Low2122c7a2015-08-26 18:46:09 -0700139 // adb start-server starts us up with stdout and stderr hooked up to
140 // anonymous pipes to. When the C Runtime sees this, it makes stderr and
141 // stdout buffered, but to improve the chance that error output is seen,
142 // unbuffer stdout and stderr just like if we were run at the console.
143 // This also keeps stderr unbuffered when it is redirected to adb.log.
144 if (is_daemon) {
145 if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
146 fatal("cannot make stdout unbuffered: %s", strerror(errno));
147 }
148 if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
149 fatal("cannot make stderr unbuffered: %s", strerror(errno));
150 }
151 }
152
Dan Albertc89e0cc2015-05-08 16:13:53 -0700153 SetConsoleCtrlHandler(ctrlc_handler, TRUE);
154#else
155 signal(SIGPIPE, SIG_IGN);
156#endif
157
158 init_transport_registration();
159
160 if (kWorkaroundBug6558362 && is_daemon) {
161 adb_workaround_affinity();
162 }
163
164 usb_init();
165 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
166 adb_auth_init();
167
Spencer Low5200c662015-07-30 23:07:55 -0700168 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700169 std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
Spencer Low5200c662015-07-30 23:07:55 -0700170 if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700171 fatal("could not install *smartsocket* listener: %s", error.c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700172 }
173
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700174 // Inform our parent that we are up and running.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700175 if (is_daemon) {
Spencer Low2122c7a2015-08-26 18:46:09 -0700176 close_stdin();
177 setup_daemon_logging();
178
179 // Any error output written to stderr now goes to adb.log. We could
180 // keep around a copy of the stderr fd and use that to write any errors
181 // encountered by the following code, but that is probably overkill.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700182#if defined(_WIN32)
Spencer Low5c398d22015-08-08 15:07:07 -0700183 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
184 const CHAR ack[] = "OK\n";
185 const DWORD bytes_to_write = arraysize(ack) - 1;
186 DWORD written = 0;
187 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
188 fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
189 SystemErrorCodeToString(GetLastError()).c_str());
190 }
191 if (written != bytes_to_write) {
192 fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
193 bytes_to_write, written);
194 }
195 CloseHandle(ack_reply_handle);
196#else
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700197 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
198 // "OKAY".
Spencer Lowf18fc082015-08-11 17:05:02 -0700199 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
200 fatal_errno("error writing ACK to fd %d", ack_reply_fd);
201 }
Spencer Low5c398d22015-08-08 15:07:07 -0700202 unix_close(ack_reply_fd);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700203#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700204 }
205
206 D("Event loop starting\n");
207 fdevent_loop();
208
209 return 0;
210}
211
Spencer Lowcf4ff642015-05-11 01:08:48 -0700212#ifdef _WIN32
213static bool _argv_is_utf8 = false;
214#endif
215
Dan Albertc89e0cc2015-05-08 16:13:53 -0700216int main(int argc, char** argv) {
Spencer Lowcf4ff642015-05-11 01:08:48 -0700217#ifdef _WIN32
218 if (!_argv_is_utf8) {
219 fatal("_argv_is_utf8 is not set, suggesting that wmain was not "
220 "called. Did you forget to link with -municode?");
221 }
222#endif
223
Dan Albertc89e0cc2015-05-08 16:13:53 -0700224 adb_sysdeps_init();
Dan Albert9313c0d2015-05-21 13:58:50 -0700225 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700226 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
227}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700228
229#ifdef _WIN32
230
231extern "C"
232int wmain(int argc, wchar_t **argv) {
233 // Set diagnostic flag to try to detect if the build system was not
234 // configured to call wmain.
235 _argv_is_utf8 = true;
236
237 // Convert args from UTF-16 to UTF-8 and pass that to main().
238 NarrowArgs narrow_args(argc, argv);
239 return main(argc, narrow_args.data());
240}
241
242#endif