Dan Albert | 53a3744 | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | 53a3744 | 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 | fdd946b | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 25 | |
Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 26 | #include <thread> |
| 27 | |
David Pursell | c573d52 | 2016-01-27 08:52:53 -0800 | [diff] [blame] | 28 | #include <android-base/errors.h> |
Elliott Hughes | f55ead9 | 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 | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 32 | |
| 33 | #include "adb.h" |
| 34 | #include "adb_auth.h" |
Josh Gao | 67209db | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 35 | #include "adb_client.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 36 | #include "adb_listeners.h" |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 37 | #include "adb_mdns.h" |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 38 | #include "adb_utils.h" |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 39 | #include "adb_wifi.h" |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 40 | #include "client/usb.h" |
Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 41 | #include "commandline.h" |
Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 42 | #include "sysdeps/chrono.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 43 | #include "transport.h" |
| 44 | |
Josh Gao | 67209db | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 45 | const char** __adb_argv; |
| 46 | const char** __adb_envp; |
| 47 | |
Elliott Hughes | cd61ae3 | 2017-06-15 08:35:24 -0700 | [diff] [blame] | 48 | static void setup_daemon_logging() { |
Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 49 | const std::string log_file_path(GetLogFilePath()); |
Josh Gao | f0c4403 | 2018-12-12 16:12:28 -0800 | [diff] [blame] | 50 | int fd = unix_open(log_file_path, O_WRONLY | O_CREAT | O_APPEND, 0640); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 51 | if (fd == -1) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 52 | PLOG(FATAL) << "cannot open " << log_file_path; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 53 | } |
Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 54 | if (dup2(fd, STDOUT_FILENO) == -1) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 55 | PLOG(FATAL) << "cannot redirect stdout"; |
Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 56 | } |
| 57 | if (dup2(fd, STDERR_FILENO) == -1) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 58 | PLOG(FATAL) << "cannot redirect stderr"; |
Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 59 | } |
Spencer Low | 4911f4b | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 60 | unix_close(fd); |
| 61 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 62 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 63 | LOG(INFO) << adb_version(); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 66 | void adb_server_cleanup() { |
| 67 | // Upon exit, we want to clean up in the following order: |
| 68 | // 1. close_smartsockets, so that we don't get any new clients |
| 69 | // 2. kick_all_transports, to avoid writing only part of a packet to a transport. |
| 70 | // 3. usb_cleanup, to tear down the USB stack. |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 71 | // 4. mdns_cleanup, to tear down mdns stack. |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 72 | close_smartsockets(); |
| 73 | kick_all_transports(); |
| 74 | usb_cleanup(); |
Joshua Duong | f4ba8d7 | 2021-01-13 12:18:15 -0800 | [diff] [blame] | 75 | mdns_cleanup(); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Josh Gao | d64d2ed | 2018-02-27 16:10:29 -0800 | [diff] [blame] | 78 | static void intentionally_leak() { |
| 79 | void* p = ::operator new(1); |
George Burgess IV | a54dbfd | 2018-03-01 10:54:21 -0800 | [diff] [blame] | 80 | // The analyzer is upset about this leaking. NOLINTNEXTLINE |
Josh Gao | d64d2ed | 2018-02-27 16:10:29 -0800 | [diff] [blame] | 81 | LOG(INFO) << "leaking pointer " << p; |
| 82 | } |
| 83 | |
Vince Harron | 5703eb3 | 2021-11-12 12:40:58 -0800 | [diff] [blame] | 84 | // one_device: if null, server owns all devices, else server owns only |
| 85 | // device where atransport::MatchesTarget(one_device) is true. |
| 86 | int adb_server_main(int is_daemon, const std::string& socket_spec, const char* one_device, |
| 87 | int ack_reply_fd) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 88 | #if defined(_WIN32) |
Spencer Low | 2bbb3a9 | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 89 | // adb start-server starts us up with stdout and stderr hooked up to |
Elliott Hughes | 4919c17 | 2015-11-18 18:07:48 -0800 | [diff] [blame] | 90 | // anonymous pipes. When the C Runtime sees this, it makes stderr and |
Spencer Low | 2bbb3a9 | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 91 | // stdout buffered, but to improve the chance that error output is seen, |
| 92 | // unbuffer stdout and stderr just like if we were run at the console. |
| 93 | // This also keeps stderr unbuffered when it is redirected to adb.log. |
| 94 | if (is_daemon) { |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 95 | if (setvbuf(stdout, nullptr, _IONBF, 0) == -1) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 96 | PLOG(FATAL) << "cannot make stdout unbuffered"; |
Spencer Low | 2bbb3a9 | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 97 | } |
Yi Kong | 86e6718 | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 98 | if (setvbuf(stderr, nullptr, _IONBF, 0) == -1) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 99 | PLOG(FATAL) << "cannot make stderr unbuffered"; |
Spencer Low | 2bbb3a9 | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Spencer Low | 0d5b2e9 | 2018-08-25 23:34:33 -0700 | [diff] [blame] | 103 | // TODO: On Ctrl-C, consider trying to kill a starting up adb server (if we're in |
| 104 | // launch_server) by calling GenerateConsoleCtrlEvent(). |
| 105 | |
| 106 | // On Windows, SIGBREAK is when Ctrl-Break is pressed or the console window is closed. It should |
| 107 | // act like Ctrl-C. |
| 108 | signal(SIGBREAK, [](int) { raise(SIGINT); }); |
| 109 | #endif |
Shaju Mathew | 705fa1c | 2022-12-31 19:36:54 -0800 | [diff] [blame] | 110 | signal(SIGINT, [](int) { fdevent_run_on_looper([]() { exit(0); }); }); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 111 | |
Vince Harron | 5703eb3 | 2021-11-12 12:40:58 -0800 | [diff] [blame] | 112 | if (one_device) { |
| 113 | transport_set_one_device(one_device); |
| 114 | } |
| 115 | |
Josh Gao | bea8f3c | 2020-04-06 12:07:56 -0700 | [diff] [blame] | 116 | const char* reject_kill_server = getenv("ADB_REJECT_KILL_SERVER"); |
| 117 | if (reject_kill_server && strcmp(reject_kill_server, "1") == 0) { |
| 118 | adb_set_reject_kill_server(true); |
| 119 | } |
| 120 | |
| 121 | const char* leak = getenv("ADB_LEAK"); |
Josh Gao | d64d2ed | 2018-02-27 16:10:29 -0800 | [diff] [blame] | 122 | if (leak && strcmp(leak, "1") == 0) { |
| 123 | intentionally_leak(); |
| 124 | } |
| 125 | |
Josh Gao | 530b9f1 | 2017-05-15 18:26:38 -0700 | [diff] [blame] | 126 | if (is_daemon) { |
| 127 | close_stdin(); |
| 128 | setup_daemon_logging(); |
| 129 | } |
| 130 | |
Josh Gao | 50576fd | 2018-02-23 14:00:24 -0800 | [diff] [blame] | 131 | atexit(adb_server_cleanup); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 132 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 133 | init_reconnect_handler(); |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 134 | |
Josh Gao | 9aa63fe | 2018-08-10 14:24:25 -0700 | [diff] [blame] | 135 | if (!getenv("ADB_MDNS") || strcmp(getenv("ADB_MDNS"), "0") != 0) { |
| 136 | init_mdns_transport_discovery(); |
| 137 | } |
| 138 | |
| 139 | if (!getenv("ADB_USB") || strcmp(getenv("ADB_USB"), "0") != 0) { |
Fabien Sanglard | 7799b4b | 2024-07-03 13:58:52 -0700 | [diff] [blame] | 140 | if (is_libusb_enabled()) { |
Josh Gao | f2d6af5 | 2021-04-27 20:32:02 -0700 | [diff] [blame] | 141 | libusb::usb_init(); |
| 142 | } else { |
| 143 | usb_init(); |
| 144 | } |
Josh Gao | 9aa63fe | 2018-08-10 14:24:25 -0700 | [diff] [blame] | 145 | } else { |
| 146 | adb_notify_device_scan_complete(); |
| 147 | } |
| 148 | |
| 149 | if (!getenv("ADB_EMU") || strcmp(getenv("ADB_EMU"), "0") != 0) { |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 150 | local_init(android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT)); |
Josh Gao | 9aa63fe | 2018-08-10 14:24:25 -0700 | [diff] [blame] | 151 | } |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 152 | |
Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 153 | std::string error; |
Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 154 | |
| 155 | auto start = std::chrono::steady_clock::now(); |
| 156 | |
| 157 | // If we told a previous adb server to quit because of version mismatch, we can get to this |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 158 | // point before it's finished exiting. Retry for a while to give it some time. Don't actually |
| 159 | // accept any connections until adb_wait_for_device_initialization finishes below. |
Fabien Sanglard | 59aced8 | 2024-01-19 15:30:51 -0800 | [diff] [blame] | 160 | while (install_listener(socket_spec, kSmartSocketConnectTo, nullptr, INSTALL_LISTENER_DISABLED, |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 161 | nullptr, &error) != INSTALL_STATUS_OK) { |
Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 162 | if (std::chrono::steady_clock::now() - start > 0.5s) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 163 | LOG(FATAL) << "could not install *smartsocket* listener: " << error; |
Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | std::this_thread::sleep_for(100ms); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 169 | adb_auth_init(); |
| 170 | |
| 171 | if (is_daemon) { |
Josh Gao | fdd946b | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 172 | #if !defined(_WIN32) |
Yabin Cui | cf91dbe | 2016-02-08 22:36:42 -0800 | [diff] [blame] | 173 | // Start a new session for the daemon. Do this here instead of after the fork so |
| 174 | // that a ctrl-c between the "starting server" and "done starting server" messages |
| 175 | // gets a chance to terminate the server. |
Tao Wu | dc046e6 | 2016-09-20 17:58:55 -0700 | [diff] [blame] | 176 | // setsid will fail with EPERM if it's already been a lead process of new session. |
| 177 | // Ignore such error. |
| 178 | if (setsid() == -1 && errno != EPERM) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 179 | PLOG(FATAL) << "setsid() failed"; |
Yabin Cui | cf91dbe | 2016-02-08 22:36:42 -0800 | [diff] [blame] | 180 | } |
Josh Gao | fdd946b | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 181 | #endif |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 182 | } |
Josh Gao | fdd946b | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 183 | |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 184 | // Wait for the USB scan to complete before notifying the parent that we're up. |
| 185 | // We need to perform this in a thread, because we would otherwise block the event loop. |
| 186 | std::thread notify_thread([ack_reply_fd]() { |
| 187 | adb_wait_for_device_initialization(); |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 188 | |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 189 | if (ack_reply_fd >= 0) { |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 190 | // Any error output written to stderr now goes to adb.log. We could |
| 191 | // keep around a copy of the stderr fd and use that to write any errors |
| 192 | // encountered by the following code, but that is probably overkill. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 193 | #if defined(_WIN32) |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 194 | const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); |
| 195 | const CHAR ack[] = "OK\n"; |
| 196 | const DWORD bytes_to_write = arraysize(ack) - 1; |
| 197 | DWORD written = 0; |
| 198 | if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 199 | LOG(FATAL) << "cannot write ACK to handle " << ack_reply_handle |
| 200 | << android::base::SystemErrorCodeToString(GetLastError()); |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 201 | } |
| 202 | if (written != bytes_to_write) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 203 | LOG(FATAL) << "cannot write " << bytes_to_write << " bytes of ACK: only wrote " |
| 204 | << written << " bytes"; |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 205 | } |
| 206 | CloseHandle(ack_reply_handle); |
Spencer Low | b28812f | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 207 | #else |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 208 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 209 | // "OKAY". |
| 210 | if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 211 | PLOG(FATAL) << "error writing ACK to fd " << ack_reply_fd; |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 212 | } |
| 213 | unix_close(ack_reply_fd); |
Siva Velusamy | a55dbd8 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 214 | #endif |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 215 | } |
| 216 | // We don't accept() client connections until this point: this way, clients |
| 217 | // can't see wonky state early in startup even if they're connecting directly |
| 218 | // to the server instead of going through the adb program. |
Shaju Mathew | 705fa1c | 2022-12-31 19:36:54 -0800 | [diff] [blame] | 219 | fdevent_run_on_looper([] { enable_server_sockets(); }); |
Daniel Colascione | 232c39c | 2019-11-13 17:51:19 -0800 | [diff] [blame] | 220 | }); |
| 221 | notify_thread.detach(); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 222 | |
Josh Gao | 67209db | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 223 | #if defined(__linux__) |
| 224 | // Write our location to .android/adb.$PORT, so that older clients can exec us. |
| 225 | std::string path; |
| 226 | if (!android::base::Readlink("/proc/self/exe", &path)) { |
| 227 | PLOG(ERROR) << "failed to readlink /proc/self/exe"; |
| 228 | } |
| 229 | |
| 230 | std::optional<std::string> server_executable_path = adb_get_server_executable_path(); |
| 231 | if (server_executable_path) { |
| 232 | if (!android::base::WriteStringToFile(path, *server_executable_path)) { |
| 233 | PLOG(ERROR) << "failed to write server path to " << path; |
| 234 | } |
| 235 | } |
| 236 | #endif |
| 237 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 238 | D("Event loop starting"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 239 | fdevent_loop(); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Josh Gao | 67209db | 2019-03-14 15:38:02 -0700 | [diff] [blame] | 243 | int main(int argc, char* argv[], char* envp[]) { |
| 244 | __adb_argv = const_cast<const char**>(argv); |
| 245 | __adb_envp = const_cast<const char**>(envp); |
Dan Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 246 | adb_trace_init(argv); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 247 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 248 | } |