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