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); |
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) { |
| 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); |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 124 | unix_close(fd); |
| 125 | |
| 126 | #ifdef _WIN32 |
| 127 | // On Windows, stderr is buffered by default, so switch to non-buffered |
| 128 | // to match Linux. |
| 129 | setvbuf(stderr, NULL, _IONBF, 0); |
| 130 | #endif |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 131 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| 132 | } |
| 133 | |
| 134 | int adb_main(int is_daemon, int server_port) { |
| 135 | HOST = 1; |
| 136 | |
| 137 | #if defined(_WIN32) |
| 138 | SetConsoleCtrlHandler(ctrlc_handler, TRUE); |
| 139 | #else |
| 140 | signal(SIGPIPE, SIG_IGN); |
| 141 | #endif |
| 142 | |
| 143 | init_transport_registration(); |
| 144 | |
| 145 | if (kWorkaroundBug6558362 && is_daemon) { |
| 146 | adb_workaround_affinity(); |
| 147 | } |
| 148 | |
| 149 | usb_init(); |
| 150 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 151 | adb_auth_init(); |
| 152 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 153 | std::string error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 154 | std::string local_name = android::base::StringPrintf("tcp:%d", server_port); |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 155 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) { |
| 156 | LOG(FATAL) << "Could not install *smartsocket* listener: " << error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (is_daemon) { |
| 160 | // Inform our parent that we are up and running. |
| 161 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 162 | // "OKAY". |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 163 | // TODO(danalbert): Why do we use stdout for Windows? There is a |
| 164 | // comment in launch_server() that suggests that non-Windows uses |
| 165 | // stderr because it is non-buffered. So perhaps the history is that |
| 166 | // stdout was preferred for all platforms, but it was discovered that |
| 167 | // non-Windows needed a non-buffered fd, so stderr was used there. |
| 168 | // Note that using stderr on unix means that if you do |
| 169 | // `ADB_TRACE=all adb start-server`, it will say "ADB server didn't ACK" |
| 170 | // and "* failed to start daemon *" because the adb server will write |
| 171 | // 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] | 172 | #if defined(_WIN32) |
| 173 | int reply_fd = STDOUT_FILENO; |
Spencer Low | d396dc9 | 2015-05-11 15:23:39 -0700 | [diff] [blame] | 174 | // Change stdout mode to binary so \n => \r\n translation does not |
| 175 | // occur. In a moment stdout will be reopened to the daemon log file |
| 176 | // anyway. |
| 177 | _setmode(reply_fd, _O_BINARY); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 178 | #else |
| 179 | int reply_fd = STDERR_FILENO; |
| 180 | #endif |
| 181 | android::base::WriteStringToFd("OK\n", reply_fd); |
| 182 | close_stdin(); |
| 183 | setup_daemon_logging(); |
| 184 | } |
| 185 | |
| 186 | D("Event loop starting\n"); |
| 187 | fdevent_loop(); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int main(int argc, char** argv) { |
| 193 | adb_sysdeps_init(); |
Dan Albert | 9313c0d | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 194 | adb_trace_init(argv); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 195 | D("Handling commandline()\n"); |
| 196 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 197 | } |