platform_system_core/adb/client/main.cpp

178 lines
5.9 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define TRACE_TAG ADB
#include "sysdeps.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <android-base/errors.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include "adb.h"
#include "adb_auth.h"
#include "adb_listeners.h"
#include "adb_utils.h"
#include "commandline.h"
#include "transport.h"
static std::string GetLogFilePath() {
#if defined(_WIN32)
const char log_name[] = "adb.log";
WCHAR temp_path[MAX_PATH];
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
if (nchars >= arraysize(temp_path) || nchars == 0) {
// If string truncation or some other error.
fatal("cannot retrieve temporary file path: %s\n",
android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
std::string temp_path_utf8;
if (!android::base::WideToUTF8(temp_path, &temp_path_utf8)) {
fatal_errno("cannot convert temporary file path from UTF-16 to UTF-8");
}
return temp_path_utf8 + log_name;
#else
const char* tmp_dir = getenv("TMPDIR");
if (tmp_dir == nullptr) tmp_dir = "/tmp";
return android::base::StringPrintf("%s/adb.%u.log", tmp_dir, getuid());
#endif
}
static void setup_daemon_logging(void) {
const std::string log_file_path(GetLogFilePath());
int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640);
if (fd == -1) {
fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
}
if (dup2(fd, STDOUT_FILENO) == -1) {
fatal("cannot redirect stdout: %s", strerror(errno));
}
if (dup2(fd, STDERR_FILENO) == -1) {
fatal("cannot redirect stderr: %s", strerror(errno));
}
unix_close(fd);
fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
LOG(INFO) << adb_version();
}
#if defined(_WIN32)
static BOOL WINAPI ctrlc_handler(DWORD type) {
// TODO: Consider trying to kill a starting up adb server (if we're in
// launch_server) by calling GenerateConsoleCtrlEvent().
exit(STATUS_CONTROL_C_EXIT);
return TRUE;
}
#endif
int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd) {
#if defined(_WIN32)
// adb start-server starts us up with stdout and stderr hooked up to
// anonymous pipes. When the C Runtime sees this, it makes stderr and
// stdout buffered, but to improve the chance that error output is seen,
// unbuffer stdout and stderr just like if we were run at the console.
// This also keeps stderr unbuffered when it is redirected to adb.log.
if (is_daemon) {
if (setvbuf(stdout, NULL, _IONBF, 0) == -1) {
fatal("cannot make stdout unbuffered: %s", strerror(errno));
}
if (setvbuf(stderr, NULL, _IONBF, 0) == -1) {
fatal("cannot make stderr unbuffered: %s", strerror(errno));
}
}
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
#endif
init_transport_registration();
usb_init();
local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
adb: win32: initial IPv6 support and improved Winsock error reporting Call getaddrinfo() for connecting to IPv6 destinations. Winsock APIs do not set errno. WSAGetLastError() returns Winsock errors that are more numerous than BSD sockets, so it really doesn't make sense to map those to BSD socket errors. Plus, even if we did that, the Windows C Runtime (that mingw binaries use) has a strerror() that does not recognize BSD socket error codes. The solution is to wrap the various libcutils socket_* APIs with sysdeps.h network_* APIs. For POSIX, the network_* APIs just call strerror(). For Windows, they call SystemErrorCodeToString() (adapted from Chromium). Also in this change: - Various other code was modified to return errors in a std::string* argument, to be able to surface the error string to the end-user. - Improved error checking and use of D() to log Winsock errors for improved debuggability. - For sysdeps_win32.cpp, added unique_fh class that works like std::unique_ptr, for calling _fh_close(). - Fix win32 adb_socketpair() setting of errno in error case. - Improve _socket_set_errno() D() logging to reduce confusion. Map a few extra error codes. - Move adb_shutdown() lower in sysdeps_win32.cpp so it can call _socket_set_errno(). - Move network_connect() from adb_utils.cpp to sysdeps.h. - Merge socket_loopback_server() and socket_inaddr_any_server() into _network_server() since most of the code was identical. Change-Id: I945f36870f320578b3a11ba093852ba6f7b93400 Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-07-31 14:07:55 +08:00
std::string error;
if (install_listener(socket_spec, "*smartsocket*", nullptr, 0, nullptr, &error)) {
fatal("could not install *smartsocket* listener: %s", error.c_str());
}
if (is_daemon) {
close_stdin();
setup_daemon_logging();
}
adb_auth_init();
if (is_daemon) {
#if !defined(_WIN32)
// Start a new session for the daemon. Do this here instead of after the fork so
// that a ctrl-c between the "starting server" and "done starting server" messages
// gets a chance to terminate the server.
// setsid will fail with EPERM if it's already been a lead process of new session.
// Ignore such error.
if (setsid() == -1 && errno != EPERM) {
fatal("setsid() failed: %s", strerror(errno));
}
#endif
// Inform our parent that we are up and running.
// Any error output written to stderr now goes to adb.log. We could
// keep around a copy of the stderr fd and use that to write any errors
// encountered by the following code, but that is probably overkill.
#if defined(_WIN32)
const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd);
const CHAR ack[] = "OK\n";
const DWORD bytes_to_write = arraysize(ack) - 1;
DWORD written = 0;
if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
if (written != bytes_to_write) {
fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
bytes_to_write, written);
}
CloseHandle(ack_reply_handle);
#else
// TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
// "OKAY".
if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
fatal_errno("error writing ACK to fd %d", ack_reply_fd);
}
unix_close(ack_reply_fd);
#endif
}
D("Event loop starting");
fdevent_loop();
return 0;
}
int main(int argc, char** argv) {
adb_trace_init(argv);
return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
}