| 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> |
| Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 31 | #include <android-base/quick_exit.h> |
| Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 33 | |
| 34 | #include "adb.h" |
| 35 | #include "adb_auth.h" |
| 36 | #include "adb_listeners.h" |
| Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 37 | #include "adb_utils.h" |
| Felipe Leme | 042c351 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 38 | #include "commandline.h" |
| Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 39 | #include "sysdeps/chrono.h" |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 40 | #include "transport.h" |
| 41 | |
| Elliott Hughes | cd61ae3 | 2017-06-15 08:35:24 -0700 | [diff] [blame] | 42 | static void setup_daemon_logging() { |
| Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 43 | const std::string log_file_path(GetLogFilePath()); |
| Elliott Hughes | 4919c17 | 2015-11-18 18:07:48 -0800 | [diff] [blame] | 44 | int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 45 | if (fd == -1) { |
| Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 46 | fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno)); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 47 | } |
| Spencer Low | fe6ab81 | 2015-08-11 16:08:43 -0700 | [diff] [blame] | 48 | if (dup2(fd, STDOUT_FILENO) == -1) { |
| 49 | fatal("cannot redirect stdout: %s", strerror(errno)); |
| 50 | } |
| 51 | if (dup2(fd, STDERR_FILENO) == -1) { |
| 52 | fatal("cannot redirect stderr: %s", strerror(errno)); |
| 53 | } |
| Spencer Low | 4911f4b | 2015-05-20 23:17:26 -0700 | [diff] [blame] | 54 | unix_close(fd); |
| 55 | |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 56 | fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid()); |
| Siva Velusamy | 878e36b | 2015-08-18 17:53:16 -0700 | [diff] [blame] | 57 | LOG(INFO) << adb_version(); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| Elliott Hughes | 38e02a7 | 2016-06-07 13:56:52 -0700 | [diff] [blame] | 60 | #if defined(_WIN32) |
| 61 | static BOOL WINAPI ctrlc_handler(DWORD type) { |
| 62 | // TODO: Consider trying to kill a starting up adb server (if we're in |
| 63 | // launch_server) by calling GenerateConsoleCtrlEvent(). |
| Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 64 | android::base::quick_exit(STATUS_CONTROL_C_EXIT); |
| Elliott Hughes | 38e02a7 | 2016-06-07 13:56:52 -0700 | [diff] [blame] | 65 | return TRUE; |
| 66 | } |
| 67 | #endif |
| 68 | |
| Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 69 | void adb_server_cleanup() { |
| 70 | // Upon exit, we want to clean up in the following order: |
| 71 | // 1. close_smartsockets, so that we don't get any new clients |
| 72 | // 2. kick_all_transports, to avoid writing only part of a packet to a transport. |
| 73 | // 3. usb_cleanup, to tear down the USB stack. |
| 74 | close_smartsockets(); |
| 75 | kick_all_transports(); |
| 76 | usb_cleanup(); |
| 77 | } |
| 78 | |
| Josh Gao | bb4f860 | 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 | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 80 | #if defined(_WIN32) |
| Spencer Low | 2bbb3a9 | 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 | 4919c17 | 2015-11-18 18:07:48 -0800 | [diff] [blame] | 82 | // anonymous pipes. When the C Runtime sees this, it makes stderr and |
| Spencer Low | 2bbb3a9 | 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) { |
| 87 | if (setvbuf(stdout, NULL, _IONBF, 0) == -1) { |
| 88 | fatal("cannot make stdout unbuffered: %s", strerror(errno)); |
| 89 | } |
| 90 | if (setvbuf(stderr, NULL, _IONBF, 0) == -1) { |
| 91 | fatal("cannot make stderr unbuffered: %s", strerror(errno)); |
| 92 | } |
| 93 | } |
| 94 | |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 95 | SetConsoleCtrlHandler(ctrlc_handler, TRUE); |
| Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 96 | #else |
| 97 | signal(SIGINT, [](int) { |
| Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 98 | fdevent_run_on_main_thread([]() { android::base::quick_exit(0); }); |
| Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 99 | }); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
| Josh Gao | 530b9f1 | 2017-05-15 18:26:38 -0700 | [diff] [blame] | 102 | if (is_daemon) { |
| 103 | close_stdin(); |
| 104 | setup_daemon_logging(); |
| 105 | } |
| 106 | |
| Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 107 | android::base::at_quick_exit(adb_server_cleanup); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 108 | |
| Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 109 | init_transport_registration(); |
| Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 110 | init_mdns_transport_discovery(); |
| 111 | |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 112 | usb_init(); |
| 113 | local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 114 | |
| Spencer Low | 753d485 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 115 | std::string error; |
| Josh Gao | e80f47a | 2016-12-12 18:15:33 -0800 | [diff] [blame] | 116 | |
| 117 | auto start = std::chrono::steady_clock::now(); |
| 118 | |
| 119 | // If we told a previous adb server to quit because of version mismatch, we can get to this |
| 120 | // point before it's finished exiting. Retry for a while to give it some time. |
| 121 | while (install_listener(socket_spec, "*smartsocket*", nullptr, 0, nullptr, &error) != |
| 122 | INSTALL_STATUS_OK) { |
| 123 | if (std::chrono::steady_clock::now() - start > 0.5s) { |
| 124 | fatal("could not install *smartsocket* listener: %s", error.c_str()); |
| 125 | } |
| 126 | |
| 127 | std::this_thread::sleep_for(100ms); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 130 | adb_auth_init(); |
| 131 | |
| 132 | if (is_daemon) { |
| Josh Gao | fdd946b | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 133 | #if !defined(_WIN32) |
| Yabin Cui | cf91dbe | 2016-02-08 22:36:42 -0800 | [diff] [blame] | 134 | // Start a new session for the daemon. Do this here instead of after the fork so |
| 135 | // that a ctrl-c between the "starting server" and "done starting server" messages |
| 136 | // gets a chance to terminate the server. |
| Tao Wu | dc046e6 | 2016-09-20 17:58:55 -0700 | [diff] [blame] | 137 | // setsid will fail with EPERM if it's already been a lead process of new session. |
| 138 | // Ignore such error. |
| 139 | if (setsid() == -1 && errno != EPERM) { |
| Yabin Cui | cf91dbe | 2016-02-08 22:36:42 -0800 | [diff] [blame] | 140 | fatal("setsid() failed: %s", strerror(errno)); |
| 141 | } |
| Josh Gao | fdd946b | 2016-02-04 11:59:12 -0800 | [diff] [blame] | 142 | #endif |
| 143 | |
| Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 144 | // Wait for the USB scan to complete before notifying the parent that we're up. |
| 145 | // We need to perform this in a thread, because we would otherwise block the event loop. |
| 146 | std::thread notify_thread([ack_reply_fd]() { |
| 147 | adb_wait_for_device_initialization(); |
| Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 148 | |
| Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 149 | // Any error output written to stderr now goes to adb.log. We could |
| 150 | // keep around a copy of the stderr fd and use that to write any errors |
| 151 | // encountered by the following code, but that is probably overkill. |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 152 | #if defined(_WIN32) |
| Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 153 | const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); |
| 154 | const CHAR ack[] = "OK\n"; |
| 155 | const DWORD bytes_to_write = arraysize(ack) - 1; |
| 156 | DWORD written = 0; |
| 157 | if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { |
| 158 | fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle, |
| 159 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); |
| 160 | } |
| 161 | if (written != bytes_to_write) { |
| 162 | fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes", bytes_to_write, |
| 163 | written); |
| 164 | } |
| 165 | CloseHandle(ack_reply_handle); |
| Spencer Low | b28812f | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 166 | #else |
| Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 167 | // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not |
| 168 | // "OKAY". |
| 169 | if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) { |
| 170 | fatal_errno("error writing ACK to fd %d", ack_reply_fd); |
| 171 | } |
| 172 | unix_close(ack_reply_fd); |
| Siva Velusamy | a55dbd8 | 2015-08-07 10:10:29 -0700 | [diff] [blame] | 173 | #endif |
| Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 174 | }); |
| 175 | notify_thread.detach(); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 178 | D("Event loop starting"); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 179 | fdevent_loop(); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | int main(int argc, char** argv) { |
| Dan Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 185 | adb_trace_init(argv); |
| Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 186 | return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); |
| 187 | } |