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 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 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 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 39 | #if defined(_WIN32) |
| 40 | static const char kNullFileName[] = "NUL"; |
| 41 | |
| 42 | static BOOL WINAPI ctrlc_handler(DWORD type) { |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 43 | // TODO: Consider trying to kill a starting up adb server (if we're in |
| 44 | // launch_server) by calling GenerateConsoleCtrlEvent(). |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 45 | exit(STATUS_CONTROL_C_EXIT); |
| 46 | return TRUE; |
| 47 | } |
| 48 | |
| 49 | static std::string GetLogFilePath() { |
| 50 | const char log_name[] = "adb.log"; |
Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 51 | WCHAR temp_path[MAX_PATH]; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 52 | |
| 53 | // 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] | 54 | DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path); |
| 55 | if ((nchars >= arraysize(temp_path)) || (nchars == 0)) { |
| 56 | // If string truncation or some other error. |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 57 | fatal("cannot retrieve temporary file path: %s\n", |
| 58 | SystemErrorCodeToString(GetLastError()).c_str()); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Spencer Low | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 61 | std::string temp_path_utf8; |
| 62 | if (!android::base::WideToUTF8(temp_path, &temp_path_utf8)) { |
| 63 | fatal_errno("cannot convert temporary file path from UTF-16 to UTF-8"); |
| 64 | } |
| 65 | |
| 66 | return temp_path_utf8 + log_name; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 67 | } |
| 68 | #else |
| 69 | static const char kNullFileName[] = "/dev/null"; |
| 70 | |
| 71 | static std::string GetLogFilePath() { |
| 72 | return std::string("/tmp/adb.log"); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | static void close_stdin() { |
| 77 | int fd = unix_open(kNullFileName, O_RDONLY); |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 78 | if (fd == -1) { |
| 79 | fatal("cannot open '%s': %s", kNullFileName, strerror(errno)); |
| 80 | } |
| 81 | if (dup2(fd, STDIN_FILENO) == -1) { |
| 82 | fatal("cannot redirect stdin: %s", strerror(errno)); |
| 83 | } |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 84 | unix_close(fd); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void setup_daemon_logging(void) { |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 88 | const std::string log_file_path(GetLogFilePath()); |
Elliott Hughes | 65433da | 2015-11-18 18:07:48 -0800 | [diff] [blame^] | 89 | int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 90 | if (fd == -1) { |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 91 | fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno)); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 92 | } |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 93 | if (dup2(fd, STDOUT_FILENO) == -1) { |
| 94 | fatal("cannot redirect stdout: %s", strerror(errno)); |
| 95 | } |
| 96 | if (dup2(fd, STDERR_FILENO) == -1) { |
| 97 | fatal("cannot redirect stderr: %s", strerror(errno)); |
| 98 | } |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 99 | unix_close(fd); |
| 100 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 101 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
Siva Velusamy | 743883b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 102 | LOG(INFO) << adb_version(); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Elliott Hughes | 65433da | 2015-11-18 18:07:48 -0800 | [diff] [blame^] | 105 | int adb_server_main(int is_daemon, int server_port, int ack_reply_fd) { |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 106 | #if defined(_WIN32) |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 107 | // adb start-server starts us up with stdout and stderr hooked up to |
Elliott Hughes | 65433da | 2015-11-18 18:07:48 -0800 | [diff] [blame^] | 108 | // anonymous pipes. When the C Runtime sees this, it makes stderr and |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 109 | // stdout buffered, but to improve the chance that error output is seen, |
| 110 | // unbuffer stdout and stderr just like if we were run at the console. |
| 111 | // This also keeps stderr unbuffered when it is redirected to adb.log. |
| 112 | if (is_daemon) { |
| 113 | if (setvbuf(stdout, NULL, _IONBF, 0) == -1) { |
| 114 | fatal("cannot make stdout unbuffered: %s", strerror(errno)); |
| 115 | } |
| 116 | if (setvbuf(stderr, NULL, _IONBF, 0) == -1) { |
| 117 | fatal("cannot make stderr unbuffered: %s", strerror(errno)); |
| 118 | } |
| 119 | } |
| 120 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 121 | SetConsoleCtrlHandler(ctrlc_handler, TRUE); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | init_transport_registration(); |
| 125 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 126 | usb_init(); |
| 127 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 128 | adb_auth_init(); |
| 129 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 130 | std::string error; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 131 | std::string local_name = android::base::StringPrintf("tcp:%d", server_port); |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 132 | if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) { |
Spencer Low | bf7c605 | 2015-08-11 16:45:32 -0700 | [diff] [blame] | 133 | fatal("could not install *smartsocket* listener: %s", error.c_str()); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 136 | // Inform our parent that we are up and running. |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 137 | if (is_daemon) { |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 138 | close_stdin(); |
| 139 | setup_daemon_logging(); |
| 140 | |
| 141 | // Any error output written to stderr now goes to adb.log. We could |
| 142 | // keep around a copy of the stderr fd and use that to write any errors |
| 143 | // encountered by the following code, but that is probably overkill. |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 144 | #if defined(_WIN32) |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 145 | const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); |
| 146 | const CHAR ack[] = "OK\n"; |
| 147 | const DWORD bytes_to_write = arraysize(ack) - 1; |
| 148 | DWORD written = 0; |
| 149 | if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { |
| 150 | fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle, |
| 151 | SystemErrorCodeToString(GetLastError()).c_str()); |
| 152 | } |
| 153 | if (written != bytes_to_write) { |
| 154 | fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes", |
| 155 | bytes_to_write, written); |
| 156 | } |
| 157 | CloseHandle(ack_reply_handle); |
| 158 | #else |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 159 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 160 | // "OKAY". |
Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 161 | if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) { |
| 162 | fatal_errno("error writing ACK to fd %d", ack_reply_fd); |
| 163 | } |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 164 | unix_close(ack_reply_fd); |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 165 | #endif |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 168 | D("Event loop starting"); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 169 | fdevent_loop(); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int main(int argc, char** argv) { |
| 175 | adb_sysdeps_init(); |
Dan Albert | 9313c0d | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 176 | adb_trace_init(argv); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 177 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 178 | } |