blob: d2ca44e8ae8a2e8be22a12a44ff65c5733102108 [file] [log] [blame]
Dan Albertc89e0cc2015-05-08 16:13:53 -07001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albertc89e0cc2015-05-08 16:13:53 -070018
19#include "sysdeps.h"
20
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
Josh Gaob72b3f82016-02-04 11:59:12 -080024#include <unistd.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070025
David Pursell5f787ed2016-01-27 08:52:53 -080026#include <android-base/errors.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080027#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/stringprintf.h>
Dan Albertc89e0cc2015-05-08 16:13:53 -070030
31#include "adb.h"
32#include "adb_auth.h"
33#include "adb_listeners.h"
Josh Gao7d586072015-11-20 15:37:31 -080034#include "adb_utils.h"
Dan Albertc89e0cc2015-05-08 16:13:53 -070035#include "transport.h"
36
Dan Albertc89e0cc2015-05-08 16:13:53 -070037static std::string GetLogFilePath() {
Elliott Hughesd89a6c22016-06-07 13:56:52 -070038#if defined(_WIN32)
Dan Albertc89e0cc2015-05-08 16:13:53 -070039 const char log_name[] = "adb.log";
Spencer Lowcf4ff642015-05-11 01:08:48 -070040 WCHAR temp_path[MAX_PATH];
Dan Albertc89e0cc2015-05-08 16:13:53 -070041
42 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
Spencer Lowcf4ff642015-05-11 01:08:48 -070043 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
Elliott Hughesd89a6c22016-06-07 13:56:52 -070044 if (nchars >= arraysize(temp_path) || nchars == 0) {
Spencer Lowcf4ff642015-05-11 01:08:48 -070045 // If string truncation or some other error.
Spencer Low155159c2015-08-11 16:08:43 -070046 fatal("cannot retrieve temporary file path: %s\n",
David Pursell5f787ed2016-01-27 08:52:53 -080047 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -070048 }
49
Spencer Lowd21dc822015-11-12 15:20:15 -080050 std::string temp_path_utf8;
51 if (!android::base::WideToUTF8(temp_path, &temp_path_utf8)) {
52 fatal_errno("cannot convert temporary file path from UTF-16 to UTF-8");
53 }
54
55 return temp_path_utf8 + log_name;
Dan Albertc89e0cc2015-05-08 16:13:53 -070056#else
Elliott Hughesd89a6c22016-06-07 13:56:52 -070057 const char* tmp_dir = getenv("TMPDIR");
58 if (tmp_dir == nullptr) tmp_dir = "/tmp";
59 return android::base::StringPrintf("%s/adb.%u.log", tmp_dir, getuid());
Dan Albertc89e0cc2015-05-08 16:13:53 -070060#endif
Elliott Hughesd89a6c22016-06-07 13:56:52 -070061}
Dan Albertc89e0cc2015-05-08 16:13:53 -070062
Dan Albertc89e0cc2015-05-08 16:13:53 -070063static void setup_daemon_logging(void) {
Spencer Low155159c2015-08-11 16:08:43 -070064 const std::string log_file_path(GetLogFilePath());
Elliott Hughes65433da2015-11-18 18:07:48 -080065 int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640);
Dan Albertc89e0cc2015-05-08 16:13:53 -070066 if (fd == -1) {
Spencer Low155159c2015-08-11 16:08:43 -070067 fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
Dan Albertc89e0cc2015-05-08 16:13:53 -070068 }
Spencer Low155159c2015-08-11 16:08:43 -070069 if (dup2(fd, STDOUT_FILENO) == -1) {
70 fatal("cannot redirect stdout: %s", strerror(errno));
71 }
72 if (dup2(fd, STDERR_FILENO) == -1) {
73 fatal("cannot redirect stderr: %s", strerror(errno));
74 }
Spencer Lowd0f66c32015-05-20 23:17:26 -070075 unix_close(fd);
76
Dan Albertc89e0cc2015-05-08 16:13:53 -070077 fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
Siva Velusamy743883b2015-08-18 17:53:16 -070078 LOG(INFO) << adb_version();
Dan Albertc89e0cc2015-05-08 16:13:53 -070079}
80
Elliott Hughesd89a6c22016-06-07 13:56:52 -070081#if defined(_WIN32)
82static BOOL WINAPI ctrlc_handler(DWORD type) {
83 // TODO: Consider trying to kill a starting up adb server (if we're in
84 // launch_server) by calling GenerateConsoleCtrlEvent().
85 exit(STATUS_CONTROL_C_EXIT);
86 return TRUE;
87}
88#endif
89
Elliott Hughes65433da2015-11-18 18:07:48 -080090int adb_server_main(int is_daemon, int server_port, int ack_reply_fd) {
Dan Albertc89e0cc2015-05-08 16:13:53 -070091#if defined(_WIN32)
Spencer Low2122c7a2015-08-26 18:46:09 -070092 // adb start-server starts us up with stdout and stderr hooked up to
Elliott Hughes65433da2015-11-18 18:07:48 -080093 // anonymous pipes. When the C Runtime sees this, it makes stderr and
Spencer Low2122c7a2015-08-26 18:46:09 -070094 // stdout buffered, but to improve the chance that error output is seen,
95 // unbuffer stdout and stderr just like if we were run at the console.
96 // This also keeps stderr unbuffered when it is redirected to adb.log.
97 if (is_daemon) {
98 if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
99 fatal("cannot make stdout unbuffered: %s", strerror(errno));
100 }
101 if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
102 fatal("cannot make stderr unbuffered: %s", strerror(errno));
103 }
104 }
105
Dan Albertc89e0cc2015-05-08 16:13:53 -0700106 SetConsoleCtrlHandler(ctrlc_handler, TRUE);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700107#endif
108
109 init_transport_registration();
110
Dan Albertc89e0cc2015-05-08 16:13:53 -0700111 usb_init();
112 local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
113 adb_auth_init();
114
Spencer Low5200c662015-07-30 23:07:55 -0700115 std::string error;
Dan Albertc89e0cc2015-05-08 16:13:53 -0700116 std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
David Purselleaae97e2016-04-07 11:25:48 -0700117 if (install_listener(local_name, "*smartsocket*", nullptr, 0, nullptr, &error)) {
Spencer Lowbf7c6052015-08-11 16:45:32 -0700118 fatal("could not install *smartsocket* listener: %s", error.c_str());
Dan Albertc89e0cc2015-05-08 16:13:53 -0700119 }
120
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700121 // Inform our parent that we are up and running.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700122 if (is_daemon) {
Spencer Low2122c7a2015-08-26 18:46:09 -0700123 close_stdin();
124 setup_daemon_logging();
125
Josh Gaob72b3f82016-02-04 11:59:12 -0800126#if !defined(_WIN32)
Yabin Cui6bf323b2016-02-08 22:36:42 -0800127 // Start a new session for the daemon. Do this here instead of after the fork so
128 // that a ctrl-c between the "starting server" and "done starting server" messages
129 // gets a chance to terminate the server.
130 if (setsid() == -1) {
131 fatal("setsid() failed: %s", strerror(errno));
132 }
Josh Gaob72b3f82016-02-04 11:59:12 -0800133#endif
134
Spencer Low2122c7a2015-08-26 18:46:09 -0700135 // Any error output written to stderr now goes to adb.log. We could
136 // keep around a copy of the stderr fd and use that to write any errors
137 // encountered by the following code, but that is probably overkill.
Dan Albertc89e0cc2015-05-08 16:13:53 -0700138#if defined(_WIN32)
Spencer Low5c398d22015-08-08 15:07:07 -0700139 const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
140 const CHAR ack[] = "OK\n";
141 const DWORD bytes_to_write = arraysize(ack) - 1;
142 DWORD written = 0;
143 if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
144 fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
David Pursell5f787ed2016-01-27 08:52:53 -0800145 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Spencer Low5c398d22015-08-08 15:07:07 -0700146 }
147 if (written != bytes_to_write) {
148 fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
149 bytes_to_write, written);
150 }
151 CloseHandle(ack_reply_handle);
152#else
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700153 // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
154 // "OKAY".
Spencer Lowf18fc082015-08-11 17:05:02 -0700155 if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
156 fatal_errno("error writing ACK to fd %d", ack_reply_fd);
157 }
Spencer Low5c398d22015-08-08 15:07:07 -0700158 unix_close(ack_reply_fd);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700159#endif
Dan Albertc89e0cc2015-05-08 16:13:53 -0700160 }
161
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700162 D("Event loop starting");
Dan Albertc89e0cc2015-05-08 16:13:53 -0700163 fdevent_loop();
164
165 return 0;
166}
167
168int main(int argc, char** argv) {
169 adb_sysdeps_init();
Dan Albert9313c0d2015-05-21 13:58:50 -0700170 adb_trace_init(argv);
Dan Albertc89e0cc2015-05-08 16:13:53 -0700171 return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
172}