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. |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame^] | 91 | fatal("cannot retrieve temporary file path: %s\n", |
| 92 | SystemErrorCodeToString(GetLastError()).c_str()); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 95 | return narrow(temp_path) + log_name; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 96 | } |
| 97 | #else |
| 98 | static const char kNullFileName[] = "/dev/null"; |
| 99 | |
| 100 | static std::string GetLogFilePath() { |
| 101 | return std::string("/tmp/adb.log"); |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | static void close_stdin() { |
| 106 | int fd = unix_open(kNullFileName, O_RDONLY); |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame^] | 107 | if (fd == -1) { |
| 108 | fatal("cannot open '%s': %s", kNullFileName, strerror(errno)); |
| 109 | } |
| 110 | if (dup2(fd, STDIN_FILENO) == -1) { |
| 111 | fatal("cannot redirect stdin: %s", strerror(errno)); |
| 112 | } |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 113 | unix_close(fd); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void setup_daemon_logging(void) { |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame^] | 117 | const std::string log_file_path(GetLogFilePath()); |
| 118 | int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 119 | 0640); |
| 120 | if (fd == -1) { |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame^] | 121 | fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno)); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 122 | } |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame^] | 123 | if (dup2(fd, STDOUT_FILENO) == -1) { |
| 124 | fatal("cannot redirect stdout: %s", strerror(errno)); |
| 125 | } |
| 126 | if (dup2(fd, STDERR_FILENO) == -1) { |
| 127 | fatal("cannot redirect stderr: %s", strerror(errno)); |
| 128 | } |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 129 | unix_close(fd); |
| 130 | |
| 131 | #ifdef _WIN32 |
| 132 | // On Windows, stderr is buffered by default, so switch to non-buffered |
| 133 | // to match Linux. |
| 134 | setvbuf(stderr, NULL, _IONBF, 0); |
| 135 | #endif |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 136 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| 137 | } |
| 138 | |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 139 | int adb_main(int is_daemon, int server_port, int ack_reply_fd) { |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 140 | HOST = 1; |
| 141 | |
| 142 | #if defined(_WIN32) |
| 143 | SetConsoleCtrlHandler(ctrlc_handler, TRUE); |
| 144 | #else |
| 145 | signal(SIGPIPE, SIG_IGN); |
| 146 | #endif |
| 147 | |
| 148 | init_transport_registration(); |
| 149 | |
| 150 | if (kWorkaroundBug6558362 && is_daemon) { |
| 151 | adb_workaround_affinity(); |
| 152 | } |
| 153 | |
| 154 | usb_init(); |
| 155 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 156 | adb_auth_init(); |
| 157 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 158 | std::string error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 159 | std::string local_name = android::base::StringPrintf("tcp:%d", server_port); |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 160 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) { |
| 161 | LOG(FATAL) << "Could not install *smartsocket* listener: " << error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 164 | // Inform our parent that we are up and running. |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 165 | if (is_daemon) { |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 166 | #if defined(_WIN32) |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 167 | const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); |
| 168 | const CHAR ack[] = "OK\n"; |
| 169 | const DWORD bytes_to_write = arraysize(ack) - 1; |
| 170 | DWORD written = 0; |
| 171 | if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { |
| 172 | fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle, |
| 173 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 174 | } |
| 175 | if (written != bytes_to_write) { |
| 176 | fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes", |
| 177 | bytes_to_write, written); |
| 178 | } |
| 179 | CloseHandle(ack_reply_handle); |
| 180 | #else |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 181 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 182 | // "OKAY". |
| 183 | android::base::WriteStringToFd("OK\n", ack_reply_fd); |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 184 | unix_close(ack_reply_fd); |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 185 | #endif |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 186 | close_stdin(); |
| 187 | setup_daemon_logging(); |
| 188 | } |
| 189 | |
| 190 | D("Event loop starting\n"); |
| 191 | fdevent_loop(); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 196 | #ifdef _WIN32 |
| 197 | static bool _argv_is_utf8 = false; |
| 198 | #endif |
| 199 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 200 | int main(int argc, char** argv) { |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 201 | #ifdef _WIN32 |
| 202 | if (!_argv_is_utf8) { |
| 203 | fatal("_argv_is_utf8 is not set, suggesting that wmain was not " |
| 204 | "called. Did you forget to link with -municode?"); |
| 205 | } |
| 206 | #endif |
| 207 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 208 | adb_sysdeps_init(); |
Dan Albert | 9313c0d | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 209 | adb_trace_init(argv); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 210 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 211 | } |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 212 | |
| 213 | #ifdef _WIN32 |
| 214 | |
| 215 | extern "C" |
| 216 | int wmain(int argc, wchar_t **argv) { |
| 217 | // Set diagnostic flag to try to detect if the build system was not |
| 218 | // configured to call wmain. |
| 219 | _argv_is_utf8 = true; |
| 220 | |
| 221 | // Convert args from UTF-16 to UTF-8 and pass that to main(). |
| 222 | NarrowArgs narrow_args(argc, argv); |
| 223 | return main(argc, narrow_args.data()); |
| 224 | } |
| 225 | |
| 226 | #endif |