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"; |
| 85 | char temp_path[MAX_PATH - sizeof(log_name) + 1]; |
| 86 | |
| 87 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx |
| 88 | DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); |
| 89 | CHECK_LE(nchars, sizeof(temp_path)); |
| 90 | if (nchars == 0) { |
| 91 | // TODO(danalbert): Log the error message from FormatError(). |
| 92 | // Windows unfortunately has two errnos, errno and GetLastError(), so |
| 93 | // I'm not sure what to do about PLOG here. Probably better to just |
| 94 | // ignore it and add a simplified version of FormatError() for use in |
| 95 | // log messages. |
| 96 | LOG(ERROR) << "Error creating log file"; |
| 97 | } |
| 98 | |
| 99 | return std::string(temp_path) + log_name; |
| 100 | } |
| 101 | #else |
| 102 | static const char kNullFileName[] = "/dev/null"; |
| 103 | |
| 104 | static std::string GetLogFilePath() { |
| 105 | return std::string("/tmp/adb.log"); |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | static void close_stdin() { |
| 110 | int fd = unix_open(kNullFileName, O_RDONLY); |
| 111 | CHECK_NE(fd, -1); |
| 112 | dup2(fd, STDIN_FILENO); |
| 113 | adb_close(fd); |
| 114 | } |
| 115 | |
| 116 | static void setup_daemon_logging(void) { |
| 117 | int fd = unix_open(GetLogFilePath().c_str(), O_WRONLY | O_CREAT | O_APPEND, |
| 118 | 0640); |
| 119 | if (fd == -1) { |
| 120 | fd = unix_open(kNullFileName, O_WRONLY); |
| 121 | } |
| 122 | dup2(fd, STDOUT_FILENO); |
| 123 | dup2(fd, STDERR_FILENO); |
| 124 | adb_close(fd); |
| 125 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| 126 | } |
| 127 | |
| 128 | int adb_main(int is_daemon, int server_port) { |
| 129 | HOST = 1; |
| 130 | |
| 131 | #if defined(_WIN32) |
| 132 | SetConsoleCtrlHandler(ctrlc_handler, TRUE); |
| 133 | #else |
| 134 | signal(SIGPIPE, SIG_IGN); |
| 135 | #endif |
| 136 | |
| 137 | init_transport_registration(); |
| 138 | |
| 139 | if (kWorkaroundBug6558362 && is_daemon) { |
| 140 | adb_workaround_affinity(); |
| 141 | } |
| 142 | |
| 143 | usb_init(); |
| 144 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 145 | adb_auth_init(); |
| 146 | |
| 147 | std::string local_name = android::base::StringPrintf("tcp:%d", server_port); |
| 148 | if (install_listener(local_name, "*smartsocket*", nullptr, 0)) { |
| 149 | LOG(FATAL) << "Could not install *smartsocket* listener"; |
| 150 | } |
| 151 | |
| 152 | if (is_daemon) { |
| 153 | // Inform our parent that we are up and running. |
| 154 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 155 | // "OKAY". |
| 156 | // TODO(danalbert): Why do we use stdout for Windows? |
| 157 | #if defined(_WIN32) |
| 158 | int reply_fd = STDOUT_FILENO; |
| 159 | #else |
| 160 | int reply_fd = STDERR_FILENO; |
| 161 | #endif |
| 162 | android::base::WriteStringToFd("OK\n", reply_fd); |
| 163 | close_stdin(); |
| 164 | setup_daemon_logging(); |
| 165 | } |
| 166 | |
| 167 | D("Event loop starting\n"); |
| 168 | fdevent_loop(); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int main(int argc, char** argv) { |
| 174 | adb_sysdeps_init(); |
| 175 | |
| 176 | android::base::InitLogging(argv); |
| 177 | adb_trace_init(); |
| 178 | D("Handling commandline()\n"); |
| 179 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 180 | } |