Dan Albert | cf07494 | 2015-05-05 19:25:23 -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 | |
Dan Albert | 3e1cb6d | 2015-05-08 16:08:53 -0700 | [diff] [blame^] | 45 | // We only build the affinity WAR code for Linux. |
Dan Albert | cf07494 | 2015-05-05 19:25:23 -0700 | [diff] [blame] | 46 | #if defined(__linux__) |
Dan Albert | 3e1cb6d | 2015-05-08 16:08:53 -0700 | [diff] [blame^] | 47 | static void adb_set_affinity(void) { |
Dan Albert | cf07494 | 2015-05-05 19:25:23 -0700 | [diff] [blame] | 48 | const char affinity_env[] = "ADB_CPU_AFFINITY_BUG6558362"; |
| 49 | const char* cpunum_str = getenv(affinity_env); |
| 50 | if (cpunum_str == nullptr || *cpunum_str == '\0') { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | char* strtol_res; |
| 55 | int cpu_num = strtol(cpunum_str, &strtol_res, 0); |
| 56 | if (*strtol_res != '\0') { |
| 57 | fatal("bad number (%s) in env var %s. Expecting 0..n.\n", cpunum_str, |
| 58 | affinity_env); |
| 59 | } |
| 60 | |
| 61 | cpu_set_t cpu_set; |
| 62 | sched_getaffinity(0, sizeof(cpu_set), &cpu_set); |
| 63 | D("orig cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]); |
| 64 | |
| 65 | CPU_ZERO(&cpu_set); |
| 66 | CPU_SET(cpu_num, &cpu_set); |
| 67 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); |
| 68 | |
| 69 | sched_getaffinity(0, sizeof(cpu_set), &cpu_set); |
| 70 | D("new cpu_set[0]=0x%08lx\n", cpu_set.__bits[0]); |
Dan Albert | 49513cb | 2015-05-08 11:14:51 -0700 | [diff] [blame] | 71 | } |
Dan Albert | 3e1cb6d | 2015-05-08 16:08:53 -0700 | [diff] [blame^] | 72 | #endif |
Dan Albert | cf07494 | 2015-05-05 19:25:23 -0700 | [diff] [blame] | 73 | |
| 74 | #if defined(_WIN32) |
| 75 | static const char kNullFileName[] = "NUL"; |
| 76 | |
| 77 | static BOOL WINAPI ctrlc_handler(DWORD type) { |
| 78 | exit(STATUS_CONTROL_C_EXIT); |
| 79 | return TRUE; |
| 80 | } |
| 81 | |
| 82 | static std::string GetLogFilePath() { |
| 83 | const char log_name[] = "adb.log"; |
| 84 | char temp_path[MAX_PATH - sizeof(log_name) + 1]; |
| 85 | |
| 86 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx |
| 87 | DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); |
| 88 | CHECK_LE(nchars, sizeof(temp_path)); |
| 89 | if (nchars == 0) { |
| 90 | // TODO(danalbert): Log the error message from FormatError(). |
| 91 | // Windows unfortunately has two errnos, errno and GetLastError(), so |
| 92 | // I'm not sure what to do about PLOG here. Probably better to just |
| 93 | // ignore it and add a simplified version of FormatError() for use in |
| 94 | // log messages. |
| 95 | LOG(ERROR) << "Error creating log file"; |
| 96 | } |
| 97 | |
| 98 | return std::string(temp_path) + log_name; |
| 99 | } |
| 100 | #else |
| 101 | static const char kNullFileName[] = "/dev/null"; |
| 102 | |
| 103 | static std::string GetLogFilePath() { |
| 104 | return std::string("/tmp/adb.log"); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | static void close_stdin() { |
| 109 | int fd = unix_open(kNullFileName, O_RDONLY); |
| 110 | CHECK_NE(fd, -1); |
| 111 | dup2(fd, STDIN_FILENO); |
| 112 | adb_close(fd); |
| 113 | } |
| 114 | |
| 115 | static void setup_daemon_logging(void) { |
| 116 | int fd = unix_open(GetLogFilePath().c_str(), O_WRONLY | O_CREAT | O_APPEND, |
| 117 | 0640); |
| 118 | if (fd == -1) { |
| 119 | fd = unix_open(kNullFileName, O_WRONLY); |
| 120 | } |
| 121 | dup2(fd, STDOUT_FILENO); |
| 122 | dup2(fd, STDERR_FILENO); |
| 123 | adb_close(fd); |
| 124 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| 125 | } |
| 126 | |
| 127 | int adb_main(int is_daemon, int server_port) { |
| 128 | HOST = 1; |
| 129 | |
| 130 | #if defined(_WIN32) |
| 131 | SetConsoleCtrlHandler(ctrlc_handler, TRUE); |
| 132 | #else |
| 133 | signal(SIGPIPE, SIG_IGN); |
| 134 | #endif |
| 135 | |
| 136 | init_transport_registration(); |
| 137 | |
Dan Albert | 3e1cb6d | 2015-05-08 16:08:53 -0700 | [diff] [blame^] | 138 | #if defined(__linux__) |
Dan Albert | cf07494 | 2015-05-05 19:25:23 -0700 | [diff] [blame] | 139 | if (kWorkaroundBug6558362 && is_daemon) { |
Dan Albert | 3e1cb6d | 2015-05-08 16:08:53 -0700 | [diff] [blame^] | 140 | adb_set_affinity(); |
Dan Albert | cf07494 | 2015-05-05 19:25:23 -0700 | [diff] [blame] | 141 | } |
Dan Albert | 3e1cb6d | 2015-05-08 16:08:53 -0700 | [diff] [blame^] | 142 | #endif |
Dan Albert | cf07494 | 2015-05-05 19:25:23 -0700 | [diff] [blame] | 143 | |
| 144 | usb_init(); |
| 145 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 146 | adb_auth_init(); |
| 147 | |
| 148 | std::string local_name = android::base::StringPrintf("tcp:%d", server_port); |
| 149 | if (install_listener(local_name, "*smartsocket*", nullptr, 0)) { |
| 150 | LOG(FATAL) << "Could not install *smartsocket* listener"; |
| 151 | } |
| 152 | |
| 153 | if (is_daemon) { |
| 154 | // Inform our parent that we are up and running. |
| 155 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 156 | // "OKAY". |
| 157 | // TODO(danalbert): Why do we use stdout for Windows? |
| 158 | #if defined(_WIN32) |
| 159 | int reply_fd = STDOUT_FILENO; |
| 160 | #else |
| 161 | int reply_fd = STDERR_FILENO; |
| 162 | #endif |
| 163 | android::base::WriteStringToFd("OK\n", reply_fd); |
| 164 | close_stdin(); |
| 165 | setup_daemon_logging(); |
| 166 | } |
| 167 | |
| 168 | D("Event loop starting\n"); |
| 169 | fdevent_loop(); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int main(int argc, char** argv) { |
| 175 | // adb client/server |
| 176 | adb_sysdeps_init(); |
| 177 | adb_trace_init(); |
| 178 | D("Handling commandline()\n"); |
| 179 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 180 | } |