blob: af3a6a1085f970b67a4f39784437f97704237e1b [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) {
79 exit(STATUS_CONTROL_C_EXIT);
80 return TRUE;
81}
82
83static std::string GetLogFilePath() {
84 const char log_name[] = "adb.log";
Spencer Lowcf4ff642015-05-11 01:08:48 -070085 WCHAR temp_path[MAX_PATH];
Dan Albertc89e0cc2015-05-08 16:13:53 -070086
87 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
Spencer Lowcf4ff642015-05-11 01:08:48 -070088 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
89 if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
90 // If string truncation or some other error.
91 // TODO(danalbert): Log the error message from
92 // FormatMessage(GetLastError()). Pure Windows APIs only touch
93 // GetLastError(), C Runtime APIs touch errno, so maybe there should be
94 // WPLOG or PLOGW (which would read GetLastError() instead of errno),
95 // in addition to PLOG, or maybe better to just ignore it and add a
96 // simplified version of FormatMessage() for use in log messages.
Dan Albertc89e0cc2015-05-08 16:13:53 -070097 LOG(ERROR) << "Error creating log file";
98 }
99
Spencer Lowcf4ff642015-05-11 01:08:48 -0700100 return narrow(temp_path) + log_name;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700101}
102#else
103static const char kNullFileName[] = "/dev/null";
104
105static std::string GetLogFilePath() {
106 return std::string("/tmp/adb.log");
107}
108#endif
109
110static void close_stdin() {
111 int fd = unix_open(kNullFileName, O_RDONLY);
112 CHECK_NE(fd, -1);
113 dup2(fd, STDIN_FILENO);
Spencer Lowd0f66c32015-05-20 23:17:26 -0700114 unix_close(fd);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700115}
116
117static void setup_daemon_logging(void) {
118 int fd = unix_open(GetLogFilePath().c_str(), O_WRONLY | O_CREAT | O_APPEND,
119 0640);
120 if (fd == -1) {
121 fd = unix_open(kNullFileName, O_WRONLY);
122 }
123 dup2(fd, STDOUT_FILENO);
124 dup2(fd, STDERR_FILENO);
Spencer Lowd0f66c32015-05-20 23:17:26 -0700125 unix_close(fd);
126
127#ifdef _WIN32
128 // On Windows, stderr is buffered by default, so switch to non-buffered
129 // to match Linux.
130 setvbuf(stderr, NULL, _IONBF, 0);
131#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700132 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
133}
134
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700135int adb_main(int is_daemon, int server_port, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700136 HOST = 1;
137
138#if defined(_WIN32)
139 SetConsoleCtrlHandler(ctrlc_handler, TRUE);
140#else
141 signal(SIGPIPE, SIG_IGN);
142#endif
143
144 init_transport_registration();
145
146 if (kWorkaroundBug6558362 && is_daemon) {
147 adb_workaround_affinity();
148 }
149
150 usb_init();
151 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
152 adb_auth_init();
153
Spencer Low5200c662015-07-30 23:07:55 -0700154 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700155 std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
Spencer Low5200c662015-07-30 23:07:55 -0700156 if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) {
157 LOG(FATAL) << "Could not install *smartsocket* listener: " << error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700158 }
159
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700160 // Inform our parent that we are up and running.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700161 if (is_daemon) {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700162#if defined(_WIN32)
Spencer Lowd396dc92015-05-11 15:23:39 -0700163 // Change stdout mode to binary so \n => \r\n translation does not
164 // occur. In a moment stdout will be reopened to the daemon log file
165 // anyway.
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700166 _setmode(ack_reply_fd, _O_BINARY);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700167#endif
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700168 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
169 // "OKAY".
170 android::base::WriteStringToFd("OK\n", ack_reply_fd);
171#if !defined(_WIN32)
172 adb_close(ack_reply_fd);
173#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700174 close_stdin();
175 setup_daemon_logging();
176 }
177
178 D("Event loop starting\n");
179 fdevent_loop();
180
181 return 0;
182}
183
Spencer Lowcf4ff642015-05-11 01:08:48 -0700184#ifdef _WIN32
185static bool _argv_is_utf8 = false;
186#endif
187
Dan Albertc89e0cc2015-05-08 16:13:53 -0700188int main(int argc, char** argv) {
Spencer Lowcf4ff642015-05-11 01:08:48 -0700189#ifdef _WIN32
190 if (!_argv_is_utf8) {
191 fatal("_argv_is_utf8 is not set, suggesting that wmain was not "
192 "called. Did you forget to link with -municode?");
193 }
194#endif
195
Dan Albertc89e0cc2015-05-08 16:13:53 -0700196 adb_sysdeps_init();
Dan Albert9313c0d2015-05-21 13:58:50 -0700197 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700198 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
199}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700200
201#ifdef _WIN32
202
203extern "C"
204int wmain(int argc, wchar_t **argv) {
205 // Set diagnostic flag to try to detect if the build system was not
206 // configured to call wmain.
207 _argv_is_utf8 = true;
208
209 // Convert args from UTF-16 to UTF-8 and pass that to main().
210 NarrowArgs narrow_args(argc, argv);
211 return main(argc, narrow_args.data());
212}
213
214#endif