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> |
Josh Gao | b72b3f8 | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 25 | |
Josh Gao | f0f854b | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 26 | #include <thread> |
| 27 | |
David Pursell | 5f787ed | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 28 | #include <android-base/errors.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/stringprintf.h> |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 32 | |
| 33 | #include "adb.h" |
| 34 | #include "adb_auth.h" |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 35 | #include "adb_client.h" |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 36 | #include "adb_listeners.h" |
Josh Gao | 7d58607 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 37 | #include "adb_utils.h" |
Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 38 | #include "commandline.h" |
Josh Gao | f0f854b | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 39 | #include "sysdeps/chrono.h" |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 40 | #include "transport.h" |
| 41 | |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 42 | const char** __adb_argv; |
| 43 | const char** __adb_envp; |
| 44 | |
Elliott Hughes | 6eadee8 | 2017-06-15 08:35:24 -0700 | [diff] [blame] | 45 | static void setup_daemon_logging() { |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 46 | const std::string log_file_path(GetLogFilePath()); |
Josh Gao | 0f29cbc | 2018-12-12 16:12:28 -0800 | [diff] [blame] | 47 | int fd = unix_open(log_file_path, O_WRONLY | O_CREAT | O_APPEND, 0640); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 48 | if (fd == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 49 | PLOG(FATAL) << "cannot open " << log_file_path; |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 50 | } |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 51 | if (dup2(fd, STDOUT_FILENO) == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 52 | PLOG(FATAL) << "cannot redirect stdout"; |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 53 | } |
| 54 | if (dup2(fd, STDERR_FILENO) == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 55 | PLOG(FATAL) << "cannot redirect stderr"; |
Spencer Low | 155159c | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 56 | } |
Spencer Low | d0f66c3 | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 57 | unix_close(fd); |
| 58 | |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 59 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
Siva Velusamy | 743883b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 60 | LOG(INFO) << adb_version(); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 63 | void adb_server_cleanup() { |
| 64 | // Upon exit, we want to clean up in the following order: |
| 65 | // 1. close_smartsockets, so that we don't get any new clients |
| 66 | // 2. kick_all_transports, to avoid writing only part of a packet to a transport. |
| 67 | // 3. usb_cleanup, to tear down the USB stack. |
| 68 | close_smartsockets(); |
| 69 | kick_all_transports(); |
| 70 | usb_cleanup(); |
| 71 | } |
| 72 | |
Josh Gao | e217611 | 2018-02-27 16:10:29 -0800 | [diff] [blame] | 73 | static void intentionally_leak() { |
| 74 | void* p = ::operator new(1); |
George Burgess IV | 657db00 | 2018-03-01 10:54:21 -0800 | [diff] [blame] | 75 | // The analyzer is upset about this leaking. NOLINTNEXTLINE |
Josh Gao | e217611 | 2018-02-27 16:10:29 -0800 | [diff] [blame] | 76 | LOG(INFO) << "leaking pointer " << p; |
| 77 | } |
| 78 | |
Josh Gao | 9c869b5 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 79 | int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) { |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 80 | #if defined(_WIN32) |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 81 | // 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] | 82 | // anonymous pipes. When the C Runtime sees this, it makes stderr and |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 83 | // stdout buffered, but to improve the chance that error output is seen, |
| 84 | // unbuffer stdout and stderr just like if we were run at the console. |
| 85 | // This also keeps stderr unbuffered when it is redirected to adb.log. |
| 86 | if (is_daemon) { |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 87 | if (setvbuf(stdout, nullptr, _IONBF, 0) == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 88 | PLOG(FATAL) << "cannot make stdout unbuffered"; |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 89 | } |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 90 | if (setvbuf(stderr, nullptr, _IONBF, 0) == -1) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 91 | PLOG(FATAL) << "cannot make stderr unbuffered"; |
Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Spencer Low | 84fc271 | 2018-08-25 23:34:33 -0700 | [diff] [blame] | 95 | // TODO: On Ctrl-C, consider trying to kill a starting up adb server (if we're in |
| 96 | // launch_server) by calling GenerateConsoleCtrlEvent(). |
| 97 | |
| 98 | // On Windows, SIGBREAK is when Ctrl-Break is pressed or the console window is closed. It should |
| 99 | // act like Ctrl-C. |
| 100 | signal(SIGBREAK, [](int) { raise(SIGINT); }); |
| 101 | #endif |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 102 | signal(SIGINT, [](int) { |
Josh Gao | d51c6df | 2018-02-23 14:00:24 -0800 | [diff] [blame] | 103 | fdevent_run_on_main_thread([]() { exit(0); }); |
Josh Gao | 1c70e1b | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 104 | }); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 105 | |
Josh Gao | e217611 | 2018-02-27 16:10:29 -0800 | [diff] [blame] | 106 | char* leak = getenv("ADB_LEAK"); |
| 107 | if (leak && strcmp(leak, "1") == 0) { |
| 108 | intentionally_leak(); |
| 109 | } |
| 110 | |
Josh Gao | 16c5a13 | 2017-05-15 18:26:38 -0700 | [diff] [blame] | 111 | if (is_daemon) { |
| 112 | close_stdin(); |
| 113 | setup_daemon_logging(); |
| 114 | } |
| 115 | |
Josh Gao | d51c6df | 2018-02-23 14:00:24 -0800 | [diff] [blame] | 116 | atexit(adb_server_cleanup); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 117 | |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 118 | init_transport_registration(); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 119 | init_reconnect_handler(); |
Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 120 | |
Josh Gao | 043fbec | 2018-08-10 14:24:25 -0700 | [diff] [blame] | 121 | if (!getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0) { |
| 122 | init_mdns_transport_discovery(); |
| 123 | } |
| 124 | |
| 125 | if (!getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0) { |
| 126 | usb_init(); |
| 127 | } else { |
| 128 | adb_notify_device_scan_complete(); |
| 129 | } |
| 130 | |
| 131 | if (!getenv("ADB_EMU") || strcmp(getenv("ADB_EMU"), "0") != 0) { |
| 132 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| 133 | } |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 134 | |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 135 | std::string error; |
Josh Gao | f0f854b | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 136 | |
| 137 | auto start = std::chrono::steady_clock::now(); |
| 138 | |
| 139 | // If we told a previous adb server to quit because of version mismatch, we can get to this |
| 140 | // point before it's finished exiting. Retry for a while to give it some time. |
| 141 | while (install_listener(socket_spec, "*smartsocket*", nullptr, 0, nullptr, &error) != |
| 142 | INSTALL_STATUS_OK) { |
| 143 | if (std::chrono::steady_clock::now() - start > 0.5s) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 144 | LOG(FATAL) << "could not install *smartsocket* listener: " << error; |
Josh Gao | f0f854b | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | std::this_thread::sleep_for(100ms); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 150 | adb_auth_init(); |
| 151 | |
| 152 | if (is_daemon) { |
Josh Gao | b72b3f8 | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 153 | #if !defined(_WIN32) |
Yabin Cui | 6bf323b | 2016-02-08 22:36:42 -0800 | [diff] [blame] | 154 | // Start a new session for the daemon. Do this here instead of after the fork so |
| 155 | // that a ctrl-c between the "starting server" and "done starting server" messages |
| 156 | // gets a chance to terminate the server. |
Tao Wu | c8fab89 | 2016-09-20 17:58:55 -0700 | [diff] [blame] | 157 | // setsid will fail with EPERM if it's already been a lead process of new session. |
| 158 | // Ignore such error. |
| 159 | if (setsid() == -1 && errno != EPERM) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 160 | PLOG(FATAL) << "setsid() failed"; |
Yabin Cui | 6bf323b | 2016-02-08 22:36:42 -0800 | [diff] [blame] | 161 | } |
Josh Gao | b72b3f8 | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 162 | #endif |
| 163 | |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 164 | // Wait for the USB scan to complete before notifying the parent that we're up. |
| 165 | // We need to perform this in a thread, because we would otherwise block the event loop. |
| 166 | std::thread notify_thread([ack_reply_fd]() { |
| 167 | adb_wait_for_device_initialization(); |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 168 | |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 169 | // Any error output written to stderr now goes to adb.log. We could |
| 170 | // keep around a copy of the stderr fd and use that to write any errors |
| 171 | // encountered by the following code, but that is probably overkill. |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 172 | #if defined(_WIN32) |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 173 | const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); |
| 174 | const CHAR ack[] = "OK\n"; |
| 175 | const DWORD bytes_to_write = arraysize(ack) - 1; |
| 176 | DWORD written = 0; |
| 177 | if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 178 | LOG(FATAL) << "cannot write ACK to handle " << ack_reply_handle |
| 179 | << android::base::SystemErrorCodeToString(GetLastError()); |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 180 | } |
| 181 | if (written != bytes_to_write) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 182 | LOG(FATAL) << "cannot write " << bytes_to_write << " bytes of ACK: only wrote " |
| 183 | << written << " bytes"; |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 184 | } |
| 185 | CloseHandle(ack_reply_handle); |
Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 186 | #else |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 187 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 188 | // "OKAY". |
| 189 | if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 190 | PLOG(FATAL) << "error writing ACK to fd " << ack_reply_fd; |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 191 | } |
| 192 | unix_close(ack_reply_fd); |
Siva Velusamy | 9f2d1a9 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 193 | #endif |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 194 | }); |
| 195 | notify_thread.detach(); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 198 | #if defined(__linux__) |
| 199 | // Write our location to .android/adb.$PORT, so that older clients can exec us. |
| 200 | std::string path; |
| 201 | if (!android::base::Readlink("/proc/self/exe", &path)) { |
| 202 | PLOG(ERROR) << "failed to readlink /proc/self/exe"; |
| 203 | } |
| 204 | |
| 205 | std::optional<std::string> server_executable_path = adb_get_server_executable_path(); |
| 206 | if (server_executable_path) { |
| 207 | if (!android::base::WriteStringToFile(path, *server_executable_path)) { |
| 208 | PLOG(ERROR) << "failed to write server path to " << path; |
| 209 | } |
| 210 | } |
| 211 | #endif |
| 212 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 213 | D("Event loop starting"); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 214 | fdevent_loop(); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Josh Gao | d10b7c4 | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 218 | int main(int argc, char* argv[], char* envp[]) { |
| 219 | __adb_argv = const_cast<const char**>(argv); |
| 220 | __adb_envp = const_cast<const char**>(envp); |
Dan Albert | 9313c0d | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 221 | adb_trace_init(argv); |
Dan Albert | c89e0cc | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 222 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 223 | } |