2015-05-09 07:13:53 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 06:52:57 +08:00
|
|
|
#define TRACE_TAG ADB
|
2015-05-09 07:13:53 +08:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// We only build the affinity WAR code for Linux.
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "base/file.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/stringprintf.h"
|
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_auth.h"
|
|
|
|
#include "adb_listeners.h"
|
|
|
|
#include "transport.h"
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
static const char kNullFileName[] = "NUL";
|
|
|
|
|
|
|
|
static BOOL WINAPI ctrlc_handler(DWORD type) {
|
2015-08-27 09:46:09 +08:00
|
|
|
// TODO: Consider trying to kill a starting up adb server (if we're in
|
|
|
|
// launch_server) by calling GenerateConsoleCtrlEvent().
|
2015-05-09 07:13:53 +08:00
|
|
|
exit(STATUS_CONTROL_C_EXIT);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetLogFilePath() {
|
|
|
|
const char log_name[] = "adb.log";
|
2015-05-11 16:08:48 +08:00
|
|
|
WCHAR temp_path[MAX_PATH];
|
2015-05-09 07:13:53 +08:00
|
|
|
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
|
2015-05-11 16:08:48 +08:00
|
|
|
DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
|
|
|
|
if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
|
|
|
|
// If string truncation or some other error.
|
2015-08-12 07:08:43 +08:00
|
|
|
fatal("cannot retrieve temporary file path: %s\n",
|
|
|
|
SystemErrorCodeToString(GetLastError()).c_str());
|
2015-05-09 07:13:53 +08:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:08:48 +08:00
|
|
|
return narrow(temp_path) + log_name;
|
2015-05-09 07:13:53 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static const char kNullFileName[] = "/dev/null";
|
|
|
|
|
|
|
|
static std::string GetLogFilePath() {
|
|
|
|
return std::string("/tmp/adb.log");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void close_stdin() {
|
|
|
|
int fd = unix_open(kNullFileName, O_RDONLY);
|
2015-08-12 07:08:43 +08:00
|
|
|
if (fd == -1) {
|
|
|
|
fatal("cannot open '%s': %s", kNullFileName, strerror(errno));
|
|
|
|
}
|
|
|
|
if (dup2(fd, STDIN_FILENO) == -1) {
|
|
|
|
fatal("cannot redirect stdin: %s", strerror(errno));
|
|
|
|
}
|
2015-05-21 14:17:26 +08:00
|
|
|
unix_close(fd);
|
2015-05-09 07:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_daemon_logging(void) {
|
2015-08-12 07:08:43 +08:00
|
|
|
const std::string log_file_path(GetLogFilePath());
|
|
|
|
int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND,
|
2015-05-09 07:13:53 +08:00
|
|
|
0640);
|
|
|
|
if (fd == -1) {
|
2015-08-12 07:08:43 +08:00
|
|
|
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));
|
2015-05-09 07:13:53 +08:00
|
|
|
}
|
2015-05-21 14:17:26 +08:00
|
|
|
unix_close(fd);
|
|
|
|
|
2015-05-09 07:13:53 +08:00
|
|
|
fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
|
2015-08-19 08:53:16 +08:00
|
|
|
LOG(INFO) << adb_version();
|
2015-05-09 07:13:53 +08:00
|
|
|
}
|
|
|
|
|
2015-08-08 01:10:29 +08:00
|
|
|
int adb_main(int is_daemon, int server_port, int ack_reply_fd) {
|
2015-05-09 07:13:53 +08:00
|
|
|
#if defined(_WIN32)
|
2015-08-27 09:46:09 +08:00
|
|
|
// adb start-server starts us up with stdout and stderr hooked up to
|
|
|
|
// anonymous pipes to. 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 07:13:53 +08:00
|
|
|
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
|
|
|
|
#else
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
init_transport_registration();
|
|
|
|
|
|
|
|
usb_init();
|
|
|
|
local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
|
|
|
|
adb_auth_init();
|
|
|
|
|
2015-07-31 14:07:55 +08:00
|
|
|
std::string error;
|
2015-05-09 07:13:53 +08:00
|
|
|
std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
|
2015-07-31 14:07:55 +08:00
|
|
|
if (install_listener(local_name, "*smartsocket*", nullptr, 0, &error)) {
|
2015-08-12 07:45:32 +08:00
|
|
|
fatal("could not install *smartsocket* listener: %s", error.c_str());
|
2015-05-09 07:13:53 +08:00
|
|
|
}
|
|
|
|
|
2015-08-08 01:10:29 +08:00
|
|
|
// Inform our parent that we are up and running.
|
2015-05-09 07:13:53 +08:00
|
|
|
if (is_daemon) {
|
2015-08-27 09:46:09 +08:00
|
|
|
close_stdin();
|
|
|
|
setup_daemon_logging();
|
|
|
|
|
|
|
|
// 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.
|
2015-05-09 07:13:53 +08:00
|
|
|
#if defined(_WIN32)
|
2015-08-09 06:07:07 +08:00
|
|
|
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,
|
|
|
|
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
|
2015-08-08 01:10:29 +08:00
|
|
|
// TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
|
|
|
|
// "OKAY".
|
2015-08-12 08:05:02 +08:00
|
|
|
if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) {
|
|
|
|
fatal_errno("error writing ACK to fd %d", ack_reply_fd);
|
|
|
|
}
|
2015-08-09 06:07:07 +08:00
|
|
|
unix_close(ack_reply_fd);
|
2015-05-09 07:13:53 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-03 08:44:28 +08:00
|
|
|
D("Event loop starting");
|
2015-05-09 07:13:53 +08:00
|
|
|
fdevent_loop();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
adb_sysdeps_init();
|
2015-05-22 04:58:50 +08:00
|
|
|
adb_trace_init(argv);
|
2015-05-09 07:13:53 +08:00
|
|
|
return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
|
|
|
|
}
|