Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 1 | /* |
| 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__) |
| 40 | static const bool kWorkaroundBug6558362 = true; |
| 41 | #else |
| 42 | static const bool kWorkaroundBug6558362 = false; |
| 43 | #endif |
| 44 | |
| 45 | static 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) |
| 76 | static const char kNullFileName[] = "NUL"; |
| 77 | |
| 78 | static BOOL WINAPI ctrlc_handler(DWORD type) { |
| 79 | exit(STATUS_CONTROL_C_EXIT); |
| 80 | return TRUE; |
| 81 | } |
| 82 | |
| 83 | static std::string GetLogFilePath() { |
| 84 | const char log_name[] = "adb.log"; |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame^] | 85 | WCHAR temp_path[MAX_PATH]; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 86 | |
| 87 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame^] | 88 | 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 Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 97 | LOG(ERROR) << "Error creating log file"; |
| 98 | } |
| 99 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame^] | 100 | return narrow(temp_path) + log_name; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 101 | } |
| 102 | #else |
| 103 | static const char kNullFileName[] = "/dev/null"; |
| 104 | |
| 105 | static std::string GetLogFilePath() { |
| 106 | return std::string("/tmp/adb.log"); |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | static void close_stdin() { |
| 111 | int fd = unix_open(kNullFileName, O_RDONLY); |
| 112 | CHECK_NE(fd, -1); |
| 113 | dup2(fd, STDIN_FILENO); |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 114 | unix_close(fd); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static 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 Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 125 | 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 Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 132 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| 133 | } |
| 134 | |
| 135 | int adb_main(int is_daemon, int server_port) { |
| 136 | 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 Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 154 | std::string error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 155 | std::string local_name = android::base::StringPrintf("tcp:%d", server_port); |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 156 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) { |
| 157 | LOG(FATAL) << "Could not install *smartsocket* listener: " << error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (is_daemon) { |
| 161 | // Inform our parent that we are up and running. |
| 162 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 163 | // "OKAY". |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 164 | // TODO(danalbert): Why do we use stdout for Windows? There is a |
| 165 | // comment in launch_server() that suggests that non-Windows uses |
| 166 | // stderr because it is non-buffered. So perhaps the history is that |
| 167 | // stdout was preferred for all platforms, but it was discovered that |
| 168 | // non-Windows needed a non-buffered fd, so stderr was used there. |
| 169 | // Note that using stderr on unix means that if you do |
| 170 | // `ADB_TRACE=all adb start-server`, it will say "ADB server didn't ACK" |
| 171 | // and "* failed to start daemon *" because the adb server will write |
| 172 | // logging to stderr, obscuring the OK\n output that is sent to stderr. |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 173 | #if defined(_WIN32) |
| 174 | int reply_fd = STDOUT_FILENO; |
Spencer Low | d396dc9 | 2015-05-11 15:23:39 -0700 | [diff] [blame] | 175 | // Change stdout mode to binary so \n => \r\n translation does not |
| 176 | // occur. In a moment stdout will be reopened to the daemon log file |
| 177 | // anyway. |
| 178 | _setmode(reply_fd, _O_BINARY); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 179 | #else |
| 180 | int reply_fd = STDERR_FILENO; |
| 181 | #endif |
| 182 | android::base::WriteStringToFd("OK\n", reply_fd); |
| 183 | close_stdin(); |
| 184 | setup_daemon_logging(); |
| 185 | } |
| 186 | |
| 187 | D("Event loop starting\n"); |
| 188 | fdevent_loop(); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame^] | 193 | #ifdef _WIN32 |
| 194 | static bool _argv_is_utf8 = false; |
| 195 | #endif |
| 196 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 197 | int main(int argc, char** argv) { |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame^] | 198 | #ifdef _WIN32 |
| 199 | if (!_argv_is_utf8) { |
| 200 | fatal("_argv_is_utf8 is not set, suggesting that wmain was not " |
| 201 | "called. Did you forget to link with -municode?"); |
| 202 | } |
| 203 | #endif |
| 204 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 205 | adb_sysdeps_init(); |
Dan Albert | 9313c0d | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 206 | adb_trace_init(argv); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 207 | D("Handling commandline()\n"); |
| 208 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 209 | } |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame^] | 210 | |
| 211 | #ifdef _WIN32 |
| 212 | |
| 213 | extern "C" |
| 214 | int wmain(int argc, wchar_t **argv) { |
| 215 | // Set diagnostic flag to try to detect if the build system was not |
| 216 | // configured to call wmain. |
| 217 | _argv_is_utf8 = true; |
| 218 | |
| 219 | // Convert args from UTF-16 to UTF-8 and pass that to main(). |
| 220 | NarrowArgs narrow_args(argc, argv); |
| 221 | return main(argc, narrow_args.data()); |
| 222 | } |
| 223 | |
| 224 | #endif |