Merge "adb: win32: handle incomplete UTF-8 in console output, other fixes"
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 3005652..35e5945 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -33,6 +33,7 @@
#include <string>
#include <vector>
+#include <android-base/errors.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/parsenetaddress.h>
@@ -473,7 +474,7 @@
// Show the handle value to give us a clue in case we have problems
// with pseudo-handle values.
fprintf(stderr, "Cannot make handle 0x%p non-inheritable: %s\n",
- h, SystemErrorCodeToString(GetLastError()).c_str());
+ h, android::base::SystemErrorCodeToString(GetLastError()).c_str());
return false;
}
@@ -489,7 +490,7 @@
HANDLE pipe_write_raw = NULL;
if (!CreatePipe(&pipe_read_raw, &pipe_write_raw, sa, 0)) {
fprintf(stderr, "Cannot create pipe: %s\n",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return false;
}
@@ -528,7 +529,7 @@
return EXIT_SUCCESS;
} else {
fprintf(stderr, "Failed to read from %s: %s\n", output_name,
- SystemErrorCodeToString(err).c_str());
+ android::base::SystemErrorCodeToString(err).c_str());
return EXIT_FAILURE;
}
}
@@ -540,7 +541,7 @@
if (!WriteFile(write_handle, buf, bytes_read, &bytes_written,
NULL)) {
fprintf(stderr, "Failed to write to %s: %s\n", output_name,
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return EXIT_FAILURE;
}
@@ -591,7 +592,7 @@
FILE_ATTRIBUTE_NORMAL, NULL));
if (nul_read.get() == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Cannot open 'nul': %s\n",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return -1;
}
@@ -661,7 +662,7 @@
if ((module_result >= arraysize(program_path)) || (module_result == 0)) {
// String truncation or some other error.
fprintf(stderr, "Cannot get executable path: %s\n",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return -1;
}
@@ -687,7 +688,7 @@
&startup, /* startup info, i.e. std handles */
&pinfo )) {
fprintf(stderr, "Cannot create process: %s\n",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return -1;
}
@@ -753,7 +754,7 @@
fprintf(stderr, "could not read ok from ADB Server%s\n",
err == ERROR_BROKEN_PIPE ? "" :
android::base::StringPrintf(": %s",
- SystemErrorCodeToString(err).c_str()).c_str());
+ android::base::SystemErrorCodeToString(err).c_str()).c_str());
}
}
@@ -784,7 +785,7 @@
if (wait_result != WAIT_OBJECT_0) {
fprintf(stderr, "Unexpected result waiting for threads: %lu: %s\n",
- wait_result, SystemErrorCodeToString(GetLastError()).c_str());
+ wait_result, android::base::SystemErrorCodeToString(GetLastError()).c_str());
return -1;
}
diff --git a/adb/adb_auth_host.cpp b/adb/adb_auth_host.cpp
index facacef..8f154fd 100644
--- a/adb/adb_auth_host.cpp
+++ b/adb/adb_auth_host.cpp
@@ -43,6 +43,7 @@
#include "mincrypt/rsa.h"
#undef RSA_verify
+#include <android-base/errors.h>
#include <android-base/strings.h>
#include <cutils/list.h>
@@ -307,8 +308,7 @@
WCHAR path[MAX_PATH];
const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
if (FAILED(hr)) {
- D("SHGetFolderPathW failed: %s",
- SystemErrorCodeToString(hr).c_str());
+ D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str());
return -1;
}
if (!android::base::WideToUTF8(path, &home_str)) {
diff --git a/adb/adb_client.cpp b/adb/adb_client.cpp
index bbc4dc7..db9b710 100644
--- a/adb/adb_client.cpp
+++ b/adb/adb_client.cpp
@@ -295,3 +295,27 @@
adb_close(fd);
return true;
}
+
+std::string format_host_command(const char* command, TransportType type, const char* serial) {
+ if (serial) {
+ return android::base::StringPrintf("host-serial:%s:%s", serial, command);
+ }
+
+ const char* prefix = "host";
+ if (type == kTransportUsb) {
+ prefix = "host-usb";
+ } else if (type == kTransportLocal) {
+ prefix = "host-local";
+ }
+ return android::base::StringPrintf("%s:%s", prefix, command);
+}
+
+bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) {
+ std::string result;
+ if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) {
+ *feature_set = StringToFeatureSet(result);
+ return true;
+ }
+ feature_set->clear();
+ return false;
+}
diff --git a/adb/adb_client.h b/adb/adb_client.h
index 5de0638..a9df4d7 100644
--- a/adb/adb_client.h
+++ b/adb/adb_client.h
@@ -18,13 +18,14 @@
#define _ADB_CLIENT_H_
#include "adb.h"
+#include "transport.h"
#include <string>
// Connect to adb, connect to the named service, and return a valid fd for
// interacting with that service upon success or a negative number on failure.
-int adb_connect(const std::string& service, std::string* error);
-int _adb_connect(const std::string& service, std::string* error);
+int adb_connect(const std::string& service, std::string* _Nonnull error);
+int _adb_connect(const std::string& service, std::string* _Nonnull error);
// Connect to adb, connect to the named service, returns true if the connection
// succeeded AND the service returned OKAY. Outputs any returned error otherwise.
@@ -32,24 +33,33 @@
// Connects to the named adb service and fills 'result' with the response.
// Returns true on success; returns false and fills 'error' on failure.
-bool adb_query(const std::string& service, std::string* result, std::string* error);
+bool adb_query(const std::string& service, std::string* _Nonnull result,
+ std::string* _Nonnull error);
// Set the preferred transport to connect to.
-void adb_set_transport(TransportType type, const char* serial);
+void adb_set_transport(TransportType type, const char* _Nullable serial);
// Set TCP specifics of the transport to use.
void adb_set_tcp_specifics(int server_port);
// Set TCP Hostname of the transport to use.
-void adb_set_tcp_name(const char* hostname);
+void adb_set_tcp_name(const char* _Nullable hostname);
// Send commands to the current emulator instance. Will fail if there is not
// exactly one emulator connected (or if you use -s <serial> with a <serial>
// that does not designate an emulator).
-int adb_send_emulator_command(int argc, const char** argv, const char* serial);
+int adb_send_emulator_command(int argc, const char* _Nonnull* _Nonnull argv,
+ const char* _Nullable serial);
// Reads a standard adb status response (OKAY|FAIL) and returns true in the
// event of OKAY, false in the event of FAIL or protocol error.
-bool adb_status(int fd, std::string* error);
+bool adb_status(int fd, std::string* _Nonnull error);
+
+// Create a host command corresponding to selected transport type/serial.
+std::string format_host_command(const char* _Nonnull command, TransportType type,
+ const char* _Nullable serial);
+
+// Get the feature set of the current preferred transport.
+bool adb_get_feature_set(FeatureSet* _Nonnull feature_set, std::string* _Nonnull error);
#endif
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index b37d04d..b7b30c5 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -27,6 +27,7 @@
#include <sched.h>
#endif
+#include <android-base/errors.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
@@ -54,7 +55,7 @@
if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
// If string truncation or some other error.
fatal("cannot retrieve temporary file path: %s\n",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
std::string temp_path_utf8;
@@ -134,7 +135,7 @@
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());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
if (written != bytes_to_write) {
fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index f886698..85ab4d1 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -428,62 +428,47 @@
free(buf);
}
-static std::string format_host_command(const char* command,
- TransportType type, const char* serial) {
- if (serial) {
- return android::base::StringPrintf("host-serial:%s:%s", serial, command);
- }
-
- const char* prefix = "host";
- if (type == kTransportUsb) {
- prefix = "host-usb";
- } else if (type == kTransportLocal) {
- prefix = "host-local";
- }
- return android::base::StringPrintf("%s:%s", prefix, command);
-}
-
-namespace {
-
-enum class ErrorAction {
- kPrintToStderr,
- kDoNotPrint
-};
-
-} // namespace
-
-// Fills |feature_set| using the target indicated by |transport_type| and |serial|. Returns false
-// and clears |feature_set| on failure. |error_action| selects whether to also print error messages
-// on failure.
-static bool GetFeatureSet(TransportType transport_type, const char* serial, FeatureSet* feature_set,
- ErrorAction error_action) {
- std::string result, error;
-
- if (adb_query(format_host_command("features", transport_type, serial), &result, &error)) {
- *feature_set = StringToFeatureSet(result);
- return true;
- }
-
- if (error_action == ErrorAction::kPrintToStderr) {
- fprintf(stderr, "error: %s\n", error.c_str());
- }
- feature_set->clear();
- return false;
-}
-
static void send_window_size_change(int fd, std::unique_ptr<ShellProtocol>& shell) {
-#if !defined(_WIN32)
// Old devices can't handle window size changes.
if (shell == nullptr) return;
+#if defined(_WIN32)
+ struct winsize {
+ unsigned short ws_row;
+ unsigned short ws_col;
+ unsigned short ws_xpixel;
+ unsigned short ws_ypixel;
+ };
+#endif
+
winsize ws;
+
+#if defined(_WIN32)
+ // If stdout is redirected to a non-console, we won't be able to get the
+ // console size, but that makes sense.
+ const intptr_t intptr_handle = _get_osfhandle(STDOUT_FILENO);
+ if (intptr_handle == -1) return;
+
+ const HANDLE handle = reinterpret_cast<const HANDLE>(intptr_handle);
+
+ CONSOLE_SCREEN_BUFFER_INFO info;
+ memset(&info, 0, sizeof(info));
+ if (!GetConsoleScreenBufferInfo(handle, &info)) return;
+
+ memset(&ws, 0, sizeof(ws));
+ // The number of visible rows, excluding offscreen scroll-back rows which are in info.dwSize.Y.
+ ws.ws_row = info.srWindow.Bottom - info.srWindow.Top + 1;
+ // If the user has disabled "Wrap text output on resize", they can make the screen buffer wider
+ // than the window, in which case we should use the width of the buffer.
+ ws.ws_col = info.dwSize.X;
+#else
if (ioctl(fd, TIOCGWINSZ, &ws) == -1) return;
+#endif
// Send the new window size as human-readable ASCII for debugging convenience.
size_t l = snprintf(shell->data(), shell->data_capacity(), "%dx%d,%dx%d",
ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel);
shell->Write(ShellProtocol::kIdWindowSizeChange, l + 1);
-#endif
}
// Used to pass multiple values to the stdin read thread.
@@ -508,7 +493,10 @@
pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
#endif
-#if !defined(_WIN32)
+#if defined(_WIN32)
+ // _get_interesting_input_record_uncached() causes unix_read_interruptible()
+ // to return -1 with errno == EINTR if the window size changes.
+#else
// Unblock SIGWINCH for this thread, so our read(2) below will be
// interrupted if the window size changes.
sigset_t mask;
@@ -537,20 +525,15 @@
EscapeState state = kStartOfLine;
while (true) {
- // Use unix_read() rather than adb_read() for stdin.
- D("stdin_read_thread_loop(): pre unix_read(fdi=%d,...)", args->stdin_fd);
-#if !defined(_WIN32)
-#undef read
- int r = read(args->stdin_fd, buffer_ptr, buffer_size);
+ // Use unix_read_interruptible() rather than adb_read() for stdin.
+ D("stdin_read_thread_loop(): pre unix_read_interruptible(fdi=%d,...)", args->stdin_fd);
+ int r = unix_read_interruptible(args->stdin_fd, buffer_ptr,
+ buffer_size);
if (r == -1 && errno == EINTR) {
send_window_size_change(args->stdin_fd, args->protocol);
continue;
}
-#define read ___xxx_read
-#else
- int r = unix_read(args->stdin_fd, buffer_ptr, buffer_size);
-#endif
- D("stdin_read_thread_loop(): post unix_read(fdi=%d,...)", args->stdin_fd);
+ D("stdin_read_thread_loop(): post unix_read_interruptible(fdi=%d,...)", args->stdin_fd);
if (r <= 0) {
// Only devices using the shell protocol know to close subprocess
// stdin. For older devices we want to just leave the connection
@@ -712,10 +695,12 @@
return exit_code;
}
-static int adb_shell(int argc, const char** argv,
- TransportType transport_type, const char* serial) {
+static int adb_shell(int argc, const char** argv) {
FeatureSet features;
- if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) {
+ std::string error;
+
+ if (!adb_get_feature_set(&features, &error)) {
+ fprintf(stderr, "error: %s\n", error.c_str());
return 1;
}
@@ -742,10 +727,6 @@
argc -= 2;
argv += 2;
} else if (!strcmp(argv[0], "-T") || !strcmp(argv[0], "-t")) {
- if (!CanUseFeature(features, kFeatureShell2)) {
- fprintf(stderr, "error: target doesn't support PTY args -Tt\n");
- return 1;
- }
// Like ssh, -t arguments are cumulative so that multiple -t's
// are needed to force a PTY.
if (argv[0][1] == 't') {
@@ -769,6 +750,17 @@
}
}
+ // Legacy shell protocol requires a remote PTY to close the subprocess properly which creates
+ // some weird interactions with -tT.
+ if (!use_shell_protocol && t_arg_count != 0) {
+ if (!CanUseFeature(features, kFeatureShell2)) {
+ fprintf(stderr, "error: target doesn't support PTY args -Tt\n");
+ } else {
+ fprintf(stderr, "error: PTY args -Tt cannot be used with -x\n");
+ }
+ return 1;
+ }
+
std::string shell_type_arg;
if (CanUseFeature(features, kFeatureShell2)) {
if (t_arg_count < 0) {
@@ -1096,7 +1088,8 @@
// Use shell protocol if it's supported and the caller doesn't explicitly disable it.
if (!disable_shell_protocol) {
FeatureSet features;
- if (GetFeatureSet(transport_type, serial, &features, ErrorAction::kDoNotPrint)) {
+ std::string error;
+ if (adb_get_feature_set(&features, &error)) {
use_shell_protocol = CanUseFeature(features, kFeatureShell2);
} else {
// Device was unreachable.
@@ -1575,7 +1568,7 @@
return adb_send_emulator_command(argc, argv, serial);
}
else if (!strcmp(argv[0], "shell")) {
- return adb_shell(argc, argv, transport_type, serial);
+ return adb_shell(argc, argv);
}
else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
int exec_in = !strcmp(argv[0], "exec-in");
@@ -1731,7 +1724,9 @@
else if (!strcmp(argv[0], "install")) {
if (argc < 2) return usage();
FeatureSet features;
- if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) {
+ std::string error;
+ if (!adb_get_feature_set(&features, &error)) {
+ fprintf(stderr, "error: %s\n", error.c_str());
return 1;
}
@@ -1747,7 +1742,9 @@
else if (!strcmp(argv[0], "uninstall")) {
if (argc < 2) return usage();
FeatureSet features;
- if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) {
+ std::string error;
+ if (!adb_get_feature_set(&features, &error)) {
+ fprintf(stderr, "error: %s\n", error.c_str());
return 1;
}
@@ -1850,7 +1847,9 @@
else if (!strcmp(argv[0], "features")) {
// Only list the features common to both the adb client and the device.
FeatureSet features;
- if (!GetFeatureSet(transport_type, serial, &features, ErrorAction::kPrintToStderr)) {
+ std::string error;
+ if (!adb_get_feature_set(&features, &error)) {
+ fprintf(stderr, "error: %s\n", error.c_str());
return 1;
}
diff --git a/adb/shell_service.cpp b/adb/shell_service.cpp
index c3b9044..491bb68 100644
--- a/adb/shell_service.cpp
+++ b/adb/shell_service.cpp
@@ -212,6 +212,7 @@
const std::string command_;
const std::string terminal_type_;
+ bool make_pty_raw_ = false;
SubprocessType type_;
SubprocessProtocol protocol_;
pid_t pid_ = -1;
@@ -231,6 +232,18 @@
terminal_type_(terminal_type ? terminal_type : ""),
type_(type),
protocol_(protocol) {
+ // If we aren't using the shell protocol we must allocate a PTY to properly close the
+ // subprocess. PTYs automatically send SIGHUP to the slave-side process when the master side
+ // of the PTY closes, which we rely on. If we use a raw pipe, processes that don't read/write,
+ // e.g. screenrecord, will never notice the broken pipe and terminate.
+ // The shell protocol doesn't require a PTY because it's always monitoring the local socket FD
+ // with select() and will send SIGHUP manually to the child process.
+ if (protocol_ == SubprocessProtocol::kNone && type_ == SubprocessType::kRaw) {
+ // Disable PTY input/output processing since the client is expecting raw data.
+ D("Can't create raw subprocess without shell protocol, using PTY in raw mode instead");
+ type_ = SubprocessType::kPty;
+ make_pty_raw_ = true;
+ }
}
Subprocess::~Subprocess() {
@@ -416,6 +429,24 @@
exit(-1);
}
+ if (make_pty_raw_) {
+ termios tattr;
+ if (tcgetattr(child_fd, &tattr) == -1) {
+ int saved_errno = errno;
+ WriteFdExactly(error_sfd->fd(), "tcgetattr failed: ");
+ WriteFdExactly(error_sfd->fd(), strerror(saved_errno));
+ exit(-1);
+ }
+
+ cfmakeraw(&tattr);
+ if (tcsetattr(child_fd, TCSADRAIN, &tattr) == -1) {
+ int saved_errno = errno;
+ WriteFdExactly(error_sfd->fd(), "tcsetattr failed: ");
+ WriteFdExactly(error_sfd->fd(), strerror(saved_errno));
+ exit(-1);
+ }
+ }
+
return child_fd;
}
diff --git a/adb/shell_service_test.cpp b/adb/shell_service_test.cpp
index c85232b..839284e 100644
--- a/adb/shell_service_test.cpp
+++ b/adb/shell_service_test.cpp
@@ -175,8 +175,9 @@
"echo foo; echo bar >&2; [ -t 0 ]; echo $?",
SubprocessType::kRaw, SubprocessProtocol::kNone));
- // [ -t 0 ] == 1 means no terminal (raw).
- ExpectLinesEqual(ReadRaw(subprocess_fd_), {"foo", "bar", "1"});
+ // [ -t 0 ] == 0 means we have a terminal (PTY). Even when requesting a raw subprocess, without
+ // the shell protocol we should always force a PTY to ensure proper cleanup.
+ ExpectLinesEqual(ReadRaw(subprocess_fd_), {"foo", "bar", "0"});
}
// Tests a PTY subprocess with no protocol.
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index 797d7bd..16796cd 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -62,6 +62,10 @@
#ifdef _WIN32
+// Clang-only nullability specifiers
+#define _Nonnull
+#define _Nullable
+
#include <ctype.h>
#include <direct.h>
#include <dirent.h>
@@ -165,8 +169,13 @@
#undef close
#define close ____xxx_close
+// Like unix_read(), but may return EINTR.
+extern int unix_read_interruptible(int fd, void* buf, size_t len);
+
// See the comments for the !defined(_WIN32) version of unix_read().
-extern int unix_read(int fd, void* buf, size_t len);
+static __inline__ int unix_read(int fd, void* buf, size_t len) {
+ return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
+}
#undef read
#define read ___xxx_read
@@ -249,9 +258,6 @@
return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
}
-// Like strerror(), but for Win32 error codes.
-std::string SystemErrorCodeToString(DWORD error_code);
-
// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
// struct stat s;
// stat(filename, &s);
@@ -535,6 +541,11 @@
return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
}
+// Like unix_read(), but does not handle EINTR.
+static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
+ return read(fd, buf, len);
+}
+
#undef read
#define read ___xxx_read
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index 1f6ea3f..0b08981 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -32,6 +32,7 @@
#include <cutils/sockets.h>
+#include <android-base/errors.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
@@ -93,36 +94,6 @@
if (!(cond)) fatal("assertion failed '%s' on %s:%d\n", #cond, __FILE__, __LINE__); \
} while (0)
-std::string SystemErrorCodeToString(const DWORD error_code) {
- const int kErrorMessageBufferSize = 256;
- WCHAR msgbuf[kErrorMessageBufferSize];
- DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
- DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
- arraysize(msgbuf), nullptr);
- if (len == 0) {
- return android::base::StringPrintf(
- "Error (%lu) while retrieving error. (%lu)", GetLastError(),
- error_code);
- }
-
- // Convert UTF-16 to UTF-8.
- std::string msg;
- if (!android::base::WideToUTF8(msgbuf, &msg)) {
- return android::base::StringPrintf(
- "Error (%d) converting from UTF-16 to UTF-8 while retrieving error. (%lu)", errno,
- error_code);
- }
-
- // Messages returned by the system end with line breaks.
- msg = android::base::Trim(msg);
- // There are many Windows error messages compared to POSIX, so include the
- // numeric error code for easier, quicker, accurate identification. Use
- // decimal instead of hex because there are decimal ranges like 10000-11999
- // for Winsock.
- android::base::StringAppendF(&msg, " (%lu)", error_code);
- return msg;
-}
-
void handle_deleter::operator()(HANDLE h) {
// CreateFile() is documented to return INVALID_HANDLE_FILE on error,
// implying that NULL is a valid handle, but this is probably impossible.
@@ -134,7 +105,7 @@
if (h != INVALID_HANDLE_VALUE) {
if (!CloseHandle(h)) {
D("CloseHandle(%p) failed: %s", h,
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
}
}
@@ -470,8 +441,7 @@
return -1;
default:
- D( "unknown error: %s",
- SystemErrorCodeToString( err ).c_str() );
+ D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str());
errno = ENOENT;
return -1;
}
@@ -517,8 +487,7 @@
return -1;
default:
- D( "unknown error: %s",
- SystemErrorCodeToString( err ).c_str() );
+ D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str());
errno = ENOENT;
return -1;
}
@@ -708,7 +677,7 @@
f->event = WSACreateEvent();
if (f->event == WSA_INVALID_EVENT) {
D("WSACreateEvent failed: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
// _event_socket_start assumes that this field is INVALID_HANDLE_VALUE
// on failure, instead of NULL which is what Windows really returns on
@@ -727,19 +696,19 @@
// minimize logging spam, so don't log these errors for now.
#if 0
D("socket shutdown failed: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
#endif
}
if (closesocket(f->fh_socket) == SOCKET_ERROR) {
D("closesocket failed: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
}
f->fh_socket = INVALID_SOCKET;
}
if (f->event != NULL) {
if (!CloseHandle(f->event)) {
D("CloseHandle failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
f->event = NULL;
}
@@ -760,7 +729,7 @@
// that to reduce spam and confusion.
if (err != WSAEWOULDBLOCK) {
D("recv fd %d failed: %s", _fh_to_int(f),
- SystemErrorCodeToString(err).c_str());
+ android::base::SystemErrorCodeToString(err).c_str());
}
_socket_set_errno(err);
result = -1;
@@ -776,7 +745,7 @@
// that to reduce spam and confusion.
if (err != WSAEWOULDBLOCK) {
D("send fd %d failed: %s", _fh_to_int(f),
- SystemErrorCodeToString(err).c_str());
+ android::base::SystemErrorCodeToString(err).c_str());
}
_socket_set_errno(err);
result = -1;
@@ -811,8 +780,8 @@
WSADATA wsaData;
int rc = WSAStartup( MAKEWORD(2,2), &wsaData);
if (rc != 0) {
- fatal( "adb: could not initialize Winsock: %s",
- SystemErrorCodeToString( rc ).c_str());
+ fatal("adb: could not initialize Winsock: %s",
+ android::base::SystemErrorCodeToString(rc).c_str());
}
_winsock_init = 1;
@@ -870,7 +839,7 @@
s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type));
if(s == INVALID_SOCKET) {
*error = android::base::StringPrintf("cannot create socket: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("%s", error->c_str());
return -1;
}
@@ -881,7 +850,7 @@
const DWORD err = WSAGetLastError();
*error = android::base::StringPrintf("cannot connect to %s:%u: %s",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
- SystemErrorCodeToString(err).c_str());
+ android::base::SystemErrorCodeToString(err).c_str());
D("could not connect to %s:%d: %s",
type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
return -1;
@@ -924,7 +893,7 @@
s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type));
if (s == INVALID_SOCKET) {
*error = android::base::StringPrintf("cannot create socket: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("%s", error->c_str());
return -1;
}
@@ -938,7 +907,7 @@
sizeof(n)) == SOCKET_ERROR) {
*error = android::base::StringPrintf(
"cannot set socket option SO_EXCLUSIVEADDRUSE: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("%s", error->c_str());
return -1;
}
@@ -948,7 +917,7 @@
const DWORD err = WSAGetLastError();
*error = android::base::StringPrintf("cannot bind to %s:%u: %s",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
- SystemErrorCodeToString(err).c_str());
+ android::base::SystemErrorCodeToString(err).c_str());
D("could not bind to %s:%d: %s",
type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
return -1;
@@ -956,7 +925,7 @@
if (type == SOCK_STREAM) {
if (listen(s, LISTEN_BACKLOG) == SOCKET_ERROR) {
*error = android::base::StringPrintf("cannot listen on socket: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("could not listen on %s:%d: %s",
type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
return -1;
@@ -1010,7 +979,7 @@
if (getaddrinfo(host.c_str(), port_str, &hints, &addrinfo_ptr) != 0) {
*error = android::base::StringPrintf(
"cannot resolve host '%s' and port %s: %s", host.c_str(),
- port_str, SystemErrorCodeToString(WSAGetLastError()).c_str());
+ port_str, android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("%s", error->c_str());
return -1;
}
@@ -1025,7 +994,7 @@
addrinfo->ai_protocol);
if(s == INVALID_SOCKET) {
*error = android::base::StringPrintf("cannot create socket: %s",
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("%s", error->c_str());
return -1;
}
@@ -1037,7 +1006,7 @@
// TODO: Use WSAAddressToString or inet_ntop on address.
*error = android::base::StringPrintf("cannot connect to %s:%s: %s",
host.c_str(), port_str,
- SystemErrorCodeToString(WSAGetLastError()).c_str());
+ android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
D("could not connect to %s:%s:%s: %s",
type != SOCK_STREAM ? "udp" : "tcp", host.c_str(), port_str,
error->c_str());
@@ -1075,7 +1044,7 @@
if (fh->fh_socket == INVALID_SOCKET) {
const DWORD err = WSAGetLastError();
LOG(ERROR) << "adb_socket_accept: accept on fd " << serverfd <<
- " failed: " + SystemErrorCodeToString(err);
+ " failed: " + android::base::SystemErrorCodeToString(err);
_socket_set_errno( err );
return -1;
}
@@ -1106,9 +1075,8 @@
reinterpret_cast<const char*>(optval), optlen );
if ( result == SOCKET_ERROR ) {
const DWORD err = WSAGetLastError();
- D( "adb_setsockopt: setsockopt on fd %d level %d optname %d "
- "failed: %s\n", fd, level, optname,
- SystemErrorCodeToString(err).c_str() );
+ D("adb_setsockopt: setsockopt on fd %d level %d optname %d failed: %s\n",
+ fd, level, optname, android::base::SystemErrorCodeToString(err).c_str());
_socket_set_errno( err );
result = -1;
}
@@ -1130,7 +1098,7 @@
if (shutdown(f->fh_socket, SD_BOTH) == SOCKET_ERROR) {
const DWORD err = WSAGetLastError();
D("socket shutdown fd %d failed: %s", fd,
- SystemErrorCodeToString(err).c_str());
+ android::base::SystemErrorCodeToString(err).c_str());
_socket_set_errno(err);
return -1;
}
@@ -2580,7 +2548,7 @@
memset(input_record, 0, sizeof(*input_record));
if (!ReadConsoleInputA(console, input_record, 1, &read_count)) {
D("_get_key_event_record: ReadConsoleInputA() failed: %s\n",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
errno = EIO;
return false;
}
@@ -2593,6 +2561,18 @@
fatal("ReadConsoleInputA did not return one input record");
}
+ // If the console window is resized, emulate SIGWINCH by breaking out
+ // of read() with errno == EINTR. Note that there is no event on
+ // vertical resize because we don't give the console our own custom
+ // screen buffer (with CreateConsoleScreenBuffer() +
+ // SetConsoleActiveScreenBuffer()). Instead, we use the default which
+ // supports scrollback, but doesn't seem to raise an event for vertical
+ // window resize.
+ if (input_record->EventType == WINDOW_BUFFER_SIZE_EVENT) {
+ errno = EINTR;
+ return false;
+ }
+
if ((input_record->EventType == KEY_EVENT) &&
(input_record->Event.KeyEvent.bKeyDown)) {
if (input_record->Event.KeyEvent.wRepeatCount == 0) {
@@ -3331,12 +3311,16 @@
// Disable ENABLE_LINE_INPUT so that input is immediately sent.
// Disable ENABLE_ECHO_INPUT to disable local echo. Disabling this
// flag also seems necessary to have proper line-ending processing.
- if (!SetConsoleMode(in, _old_console_mode & ~(ENABLE_PROCESSED_INPUT |
- ENABLE_LINE_INPUT |
- ENABLE_ECHO_INPUT))) {
+ DWORD new_console_mode = _old_console_mode & ~(ENABLE_PROCESSED_INPUT |
+ ENABLE_LINE_INPUT |
+ ENABLE_ECHO_INPUT);
+ // Enable ENABLE_WINDOW_INPUT to get window resizes.
+ new_console_mode |= ENABLE_WINDOW_INPUT;
+
+ if (!SetConsoleMode(in, new_console_mode)) {
// This really should not fail.
D("stdin_raw_init: SetConsoleMode() failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
// Once this is set, it means that stdin has been configured for
@@ -3356,13 +3340,13 @@
if (!SetConsoleMode(in, _old_console_mode)) {
// This really should not fail.
D("stdin_raw_restore: SetConsoleMode() failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
}
}
-// Called by 'adb shell' and 'adb exec-in' to read from stdin.
-int unix_read(int fd, void* buf, size_t len) {
+// Called by 'adb shell' and 'adb exec-in' (via unix_read()) to read from stdin.
+int unix_read_interruptible(int fd, void* buf, size_t len) {
if ((fd == STDIN_FILENO) && (_console_handle != NULL)) {
// If it is a request to read from stdin, and stdin_raw_init() has been
// called, and it successfully configured the console, then read from
diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp
index 8d3501e..e79008f 100644
--- a/adb/usb_windows.cpp
+++ b/adb/usb_windows.cpp
@@ -27,6 +27,8 @@
#include <windows.h>
#include <winerror.h>
+#include <android-base/errors.h>
+
#include "adb.h"
#include "transport.h"
@@ -221,7 +223,7 @@
if (!instance) {
// This is such a common API call that this should never fail.
fatal("GetModuleHandleW failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
WNDCLASSEXW wndclass;
@@ -232,14 +234,14 @@
wndclass.lpszClassName = kPowerNotificationWindowClassName;
if (!RegisterClassExW(&wndclass)) {
fatal("RegisterClassExW failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
if (!CreateWindowExW(WS_EX_NOACTIVATE, kPowerNotificationWindowClassName,
L"ADB Power Notification Window", WS_POPUP, 0, 0, 0, 0,
NULL, NULL, instance, NULL)) {
fatal("CreateWindowExW failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
MSG msg;
@@ -285,7 +287,7 @@
ret->adb_interface = AdbCreateInterfaceByName(interface_name);
if (NULL == ret->adb_interface) {
D("AdbCreateInterfaceByName failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
}
@@ -296,7 +298,7 @@
AdbOpenSharingModeReadWrite);
if (NULL == ret->adb_read_pipe) {
D("AdbOpenDefaultBulkReadEndpoint failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
}
@@ -307,7 +309,7 @@
AdbOpenSharingModeReadWrite);
if (NULL == ret->adb_write_pipe) {
D("AdbOpenDefaultBulkWriteEndpoint failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
}
@@ -319,7 +321,7 @@
false);
if (0 == name_len) {
D("AdbGetInterfaceName returned name length of zero: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
}
@@ -335,7 +337,7 @@
&name_len,
false)) {
D("AdbGetInterfaceName failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
goto fail;
}
@@ -370,7 +372,7 @@
&written,
time_out)) {
D("AdbWriteEndpointSync failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
err = EIO;
goto fail;
}
@@ -394,7 +396,7 @@
&written,
time_out)) {
D("AdbWriteEndpointSync of zero length packet failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
err = EIO;
goto fail;
}
@@ -431,7 +433,7 @@
if (!AdbReadEndpointSync(handle->adb_read_pipe, data, len, &read,
time_out)) {
D("AdbReadEndpointSync failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
err = EIO;
goto fail;
}
@@ -460,7 +462,7 @@
static void _adb_close_handle(ADBAPIHANDLE adb_handle) {
if (!AdbCloseHandle(adb_handle)) {
D("AdbCloseHandle(%p) failed: %s", adb_handle,
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
}
@@ -539,7 +541,7 @@
if (!AdbGetUsbDeviceDescriptor(handle->adb_interface,
&device_desc)) {
D("AdbGetUsbDeviceDescriptor failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return 0;
}
@@ -549,7 +551,7 @@
if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface,
&interf_desc)) {
D("AdbGetUsbInterfaceDescriptor failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return 0;
}
@@ -569,7 +571,7 @@
D("device zero_mask: 0x%x", handle->zero_mask);
} else {
D("AdbGetEndpointInformation failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
}
@@ -591,7 +593,7 @@
if (NULL == enum_handle) {
D("AdbEnumInterfaces failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
return;
}
@@ -627,7 +629,7 @@
}
} else {
D("cannot get serial number: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
usb_cleanup_handle(handle);
free(handle);
}
@@ -644,7 +646,7 @@
if (GetLastError() != ERROR_NO_MORE_ITEMS) {
// Only ERROR_NO_MORE_ITEMS is expected at the end of enumeration.
D("AdbNextInterface failed: %s",
- SystemErrorCodeToString(GetLastError()).c_str());
+ android::base::SystemErrorCodeToString(GetLastError()).c_str());
}
_adb_close_handle(enum_handle);
diff --git a/base/Android.mk b/base/Android.mk
index 18f8686..d20a81f 100644
--- a/base/Android.mk
+++ b/base/Android.mk
@@ -24,10 +24,18 @@
strings.cpp \
test_utils.cpp \
+libbase_linux_src_files := \
+ errors_unix.cpp \
+
+libbase_darwin_src_files := \
+ errors_unix.cpp \
+
libbase_windows_src_files := \
+ errors_windows.cpp \
utf8.cpp \
libbase_test_src_files := \
+ errors_test.cpp \
file_test.cpp \
logging_test.cpp \
parseint_test.cpp \
@@ -55,10 +63,7 @@
include $(CLEAR_VARS)
LOCAL_MODULE := libbase
LOCAL_CLANG := true
-LOCAL_SRC_FILES := $(libbase_src_files)
-LOCAL_SRC_FILES_darwin := $(libbase_darwin_src_files)
-LOCAL_SRC_FILES_linux := $(libbase_linux_src_files)
-LOCAL_SRC_FILES_windows := $(libbase_windows_src_files)
+LOCAL_SRC_FILES := $(libbase_src_files) $(libbase_linux_src_files)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_CPPFLAGS := $(libbase_cppflags) $(libbase_linux_cppflags)
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
diff --git a/base/errors_test.cpp b/base/errors_test.cpp
new file mode 100644
index 0000000..8e7cdd1
--- /dev/null
+++ b/base/errors_test.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "android-base/errors.h"
+
+#include <gtest/gtest.h>
+
+namespace android {
+namespace base {
+
+// Error strings aren't consistent enough across systems to test the output,
+// just make sure we can compile correctly and nothing crashes even if we send
+// it possibly bogus error codes.
+TEST(ErrorsTest, TestSystemErrorString) {
+ SystemErrorCodeToString(-1);
+ SystemErrorCodeToString(0);
+ SystemErrorCodeToString(1);
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/errors_unix.cpp b/base/errors_unix.cpp
new file mode 100644
index 0000000..296995e
--- /dev/null
+++ b/base/errors_unix.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "android-base/errors.h"
+
+#include <errno.h>
+
+namespace android {
+namespace base {
+
+std::string SystemErrorCodeToString(int error_code) {
+ return strerror(error_code);
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/errors_windows.cpp b/base/errors_windows.cpp
new file mode 100644
index 0000000..a5ff511
--- /dev/null
+++ b/base/errors_windows.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "android-base/errors.h"
+
+#include <windows.h>
+
+#include "android-base/stringprintf.h"
+#include "android-base/strings.h"
+#include "android-base/utf8.h"
+
+// A Windows error code is a DWORD. It's simpler to use an int error code for
+// both Unix and Windows if possible, but if this fails we'll need a different
+// function signature for each.
+static_assert(sizeof(int) >= sizeof(DWORD),
+ "Windows system error codes are too large to fit in an int.");
+
+namespace android {
+namespace base {
+
+static constexpr DWORD kErrorMessageBufferSize = 256;
+
+std::string SystemErrorCodeToString(int int_error_code) {
+ WCHAR msgbuf[kErrorMessageBufferSize];
+ DWORD error_code = int_error_code;
+ DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
+ DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
+ kErrorMessageBufferSize, nullptr);
+ if (len == 0) {
+ return android::base::StringPrintf(
+ "Error %lu while retrieving message for error %lu", GetLastError(),
+ error_code);
+ }
+
+ // Convert UTF-16 to UTF-8.
+ std::string msg;
+ if (!android::base::WideToUTF8(msgbuf, &msg)) {
+ return android::base::StringPrintf(
+ "Error %lu while converting message for error %lu from UTF-16 to UTF-8",
+ GetLastError(), error_code);
+ }
+
+ // Messages returned by the system end with line breaks.
+ msg = android::base::Trim(msg);
+
+ // There are many Windows error messages compared to POSIX, so include the
+ // numeric error code for easier, quicker, accurate identification. Use
+ // decimal instead of hex because there are decimal ranges like 10000-11999
+ // for Winsock.
+ android::base::StringAppendF(&msg, " (%lu)", error_code);
+ return msg;
+}
+
+} // namespace base
+} // namespace android
diff --git a/base/file.cpp b/base/file.cpp
index f444c0c..bcdfc5e 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -149,5 +149,32 @@
return true;
}
+bool RemoveFileIfExists(const std::string& path, std::string* err) {
+ struct stat st;
+#if defined(_WIN32)
+ //TODO: Windows version can't handle symbol link correctly.
+ int result = stat(path.c_str(), &st);
+ bool file_type_removable = (result == 0 && S_ISREG(st.st_mode));
+#else
+ int result = lstat(path.c_str(), &st);
+ bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)));
+#endif
+ if (result == 0) {
+ if (!file_type_removable) {
+ if (err != nullptr) {
+ *err = "is not a regular or symbol link file";
+ }
+ return false;
+ }
+ if (unlink(path.c_str()) == -1) {
+ if (err != nullptr) {
+ *err = strerror(errno);
+ }
+ return false;
+ }
+ }
+ return true;
+}
+
} // namespace base
} // namespace android
diff --git a/base/file_test.cpp b/base/file_test.cpp
index 1bf83a4..17755bf 100644
--- a/base/file_test.cpp
+++ b/base/file_test.cpp
@@ -96,3 +96,17 @@
s.resize(1024);
ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
}
+
+TEST(file, RemoveFileIfExist) {
+ TemporaryFile tf;
+ ASSERT_TRUE(tf.fd != -1);
+ close(tf.fd);
+ tf.fd = -1;
+ std::string err;
+ ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
+ ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
+ TemporaryDir td;
+ ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
+ ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
+ ASSERT_EQ("is not a regular or symbol link file", err);
+}
diff --git a/base/include/android-base/errors.h b/base/include/android-base/errors.h
new file mode 100644
index 0000000..ca621fa
--- /dev/null
+++ b/base/include/android-base/errors.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+// Portable error handling functions. This is only necessary for host-side
+// code that needs to be cross-platform; code that is only run on Unix should
+// just use errno and strerror() for simplicity.
+//
+// There is some complexity since Windows has (at least) three different error
+// numbers, not all of which share the same type:
+// * errno: for C runtime errors.
+// * GetLastError(): Windows non-socket errors.
+// * WSAGetLastError(): Windows socket errors.
+// errno can be passed to strerror() on all platforms, but the other two require
+// special handling to get the error string. Refer to Microsoft documentation
+// to determine which error code to check for each function.
+
+#ifndef BASE_ERRORS_H
+#define BASE_ERRORS_H
+
+#include <string>
+
+namespace android {
+namespace base {
+
+// Returns a string describing the given system error code. |error_code| must
+// be errno on Unix or GetLastError()/WSAGetLastError() on Windows. Passing
+// errno on Windows has undefined behavior.
+std::string SystemErrorCodeToString(int error_code);
+
+} // namespace base
+} // namespace android
+
+#endif // BASE_ERRORS_H
diff --git a/base/include/android-base/file.h b/base/include/android-base/file.h
index acd29b3..486befc 100644
--- a/base/include/android-base/file.h
+++ b/base/include/android-base/file.h
@@ -37,6 +37,8 @@
bool ReadFully(int fd, void* data, size_t byte_count);
bool WriteFully(int fd, const void* data, size_t byte_count);
+bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
+
} // namespace base
} // namespace android
diff --git a/metricsd/Android.mk b/metricsd/Android.mk
index 250c657..e140c62 100644
--- a/metricsd/Android.mk
+++ b/metricsd/Android.mk
@@ -72,10 +72,7 @@
libmetrics_shared_libraries := libchrome libbinder libbrillo libutils
metrics_collector_shared_libraries := $(libmetrics_shared_libraries) \
libbrillo-binder \
- libbrillo-dbus \
libbrillo-http \
- libchrome-dbus \
- libdbus \
libmetrics \
librootdev \
libweaved
diff --git a/metricsd/collectors/averaged_statistics_collector.cc b/metricsd/collectors/averaged_statistics_collector.cc
index bac2870..a3aaa98 100644
--- a/metricsd/collectors/averaged_statistics_collector.cc
+++ b/metricsd/collectors/averaged_statistics_collector.cc
@@ -16,6 +16,7 @@
#include "averaged_statistics_collector.h"
+#include <base/bind.h>
#include <base/files/file_util.h>
#include <base/files/file_path.h>
#include <base/strings/string_number_conversions.h>
diff --git a/metricsd/metrics_collector.cc b/metricsd/metrics_collector.cc
index ec7e040..c3f42dc 100644
--- a/metricsd/metrics_collector.cc
+++ b/metricsd/metrics_collector.cc
@@ -32,8 +32,6 @@
#include <base/strings/stringprintf.h>
#include <brillo/binder_watcher.h>
#include <brillo/osrelease_reader.h>
-#include <dbus/dbus.h>
-#include <dbus/message.h>
#include "constants.h"
#include "metrics_collector_service_impl.h"
@@ -142,7 +140,7 @@
// Watch Binder events in the main loop
brillo::BinderWatcher binder_watcher;
CHECK(binder_watcher.Init()) << "Binder FD watcher init failed";
- return brillo::DBusDaemon::Run();
+ return brillo::Daemon::Run();
}
uint32_t MetricsCollector::GetOsVersionHash() {
@@ -218,7 +216,7 @@
}
int MetricsCollector::OnInit() {
- int return_code = brillo::DBusDaemon::OnInit();
+ int return_code = brillo::Daemon::OnInit();
if (return_code != EX_OK)
return return_code;
@@ -232,9 +230,6 @@
if (testing_)
return EX_OK;
- bus_->AssertOnDBusThread();
- CHECK(bus_->SetUpAsyncOperations());
-
weave_service_subscription_ = weaved::Service::Connect(
brillo::MessageLoop::current(),
base::Bind(&MetricsCollector::OnWeaveServiceConnected,
@@ -249,10 +244,6 @@
return EX_OK;
}
-void MetricsCollector::OnShutdown(int* return_code) {
- brillo::DBusDaemon::OnShutdown(return_code);
-}
-
void MetricsCollector::OnWeaveServiceConnected(
const std::weak_ptr<weaved::Service>& service) {
service_ = service;
@@ -311,7 +302,8 @@
metrics_lib_->AreMetricsEnabled() ? "enabled" : "disabled";
if (!weave_service->SetStateProperty(kWeaveComponent, kWeaveTrait,
- "analyticsReportingState", enabled,
+ "analyticsReportingState",
+ *brillo::ToValue(enabled),
nullptr)) {
LOG(ERROR) << "failed to update weave's state";
}
diff --git a/metricsd/metrics_collector.h b/metricsd/metrics_collector.h
index ca4ae52..30659bd 100644
--- a/metricsd/metrics_collector.h
+++ b/metricsd/metrics_collector.h
@@ -28,7 +28,7 @@
#include <base/memory/weak_ptr.h>
#include <base/time/time.h>
#include <brillo/binder_watcher.h>
-#include <brillo/daemons/dbus_daemon.h>
+#include <brillo/daemons/daemon.h>
#include <libweaved/command.h>
#include <libweaved/service.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST
@@ -42,7 +42,7 @@
using chromeos_metrics::PersistentInteger;
using std::unique_ptr;
-class MetricsCollector : public brillo::DBusDaemon {
+class MetricsCollector : public brillo::Daemon {
public:
MetricsCollector();
~MetricsCollector();
@@ -54,12 +54,9 @@
const base::FilePath& private_metrics_directory,
const base::FilePath& shared_metrics_directory);
- // Initializes DBus and MessageLoop variables before running the MessageLoop.
+ // Initializes the daemon.
int OnInit() override;
- // Clean up data set up in OnInit before shutting down message loop.
- void OnShutdown(int* return_code) override;
-
// Does all the work.
int Run() override;
diff --git a/metricsd/metrics_collector.rc b/metricsd/metrics_collector.rc
index 3dcb2d7..2d7667d 100644
--- a/metricsd/metrics_collector.rc
+++ b/metricsd/metrics_collector.rc
@@ -1,4 +1,4 @@
service metricscollector /system/bin/metrics_collector --foreground --logtosyslog
class late_start
user metrics_coll
- group metrics_coll dbus
+ group metrics_coll
diff --git a/metricsd/metrics_collector_test.cc b/metricsd/metrics_collector_test.cc
index 5fb3ac8..8dda529 100644
--- a/metricsd/metrics_collector_test.cc
+++ b/metricsd/metrics_collector_test.cc
@@ -64,37 +64,6 @@
.RetiresOnSaturation();
}
- // Creates a new DBus signal message with zero or more string arguments.
- // The message can be deallocated through DeleteDBusMessage.
- //
- // |path| is the object emitting the signal.
- // |interface| is the interface the signal is emitted from.
- // |name| is the name of the signal.
- // |arg_values| contains the values of the string arguments.
- DBusMessage* NewDBusSignalString(const string& path,
- const string& interface,
- const string& name,
- const vector<string>& arg_values) {
- DBusMessage* msg = dbus_message_new_signal(path.c_str(),
- interface.c_str(),
- name.c_str());
- DBusMessageIter iter;
- dbus_message_iter_init_append(msg, &iter);
- for (vector<string>::const_iterator it = arg_values.begin();
- it != arg_values.end(); ++it) {
- const char* str_value = it->c_str();
- dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value);
- }
- return msg;
- }
-
- // Deallocates the DBus message |msg| previously allocated through
- // dbus_message_new*.
- void DeleteDBusMessage(DBusMessage* msg) {
- dbus_message_unref(msg);
- }
-
-
// Creates or overwrites the file in |path| so that it contains the printable
// representation of |value|.
void CreateUint64ValueFile(const base::FilePath& path, uint64_t value) {
diff --git a/metricsd/metricsd.rc b/metricsd/metricsd.rc
index 825c87f..3d3e695 100644
--- a/metricsd/metricsd.rc
+++ b/metricsd/metricsd.rc
@@ -6,4 +6,4 @@
service metricsd /system/bin/metricsd --foreground --logtosyslog
class late_start
user metricsd
- group system dbus inet
+ group system inet
diff --git a/toolbox/ps.c b/toolbox/ps.c
index ecc1c9f..7e70c71 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -264,9 +264,6 @@
int pidfilter = 0;
int threads = 0;
- d = opendir("/proc");
- if(d == 0) return -1;
-
while(argc > 1){
if(!strcmp(argv[1],"-t")) {
threads = 1;
@@ -287,7 +284,10 @@
} else if(!strcmp(argv[1],"--ppid")) {
ppid_filter = atoi(argv[2]);
if (ppid_filter == 0) {
- fprintf(stderr, "bad ppid '%s'\n", argv[2]);
+ /* Bug 26554285: Use printf because some apps require at least
+ * one line of output to stdout even for errors.
+ */
+ printf("bad ppid '%s'\n", argv[2]);
return 1;
}
argc--;
@@ -295,7 +295,10 @@
} else {
pidfilter = atoi(argv[1]);
if (pidfilter == 0) {
- fprintf(stderr, "bad pid '%s'\n", argv[1]);
+ /* Bug 26554285: Use printf because some apps require at least
+ * one line of output to stdout even for errors.
+ */
+ printf("bad pid '%s'\n", argv[1]);
return 1;
}
}
@@ -313,6 +316,9 @@
(int) PC_WIDTH, "PC",
(display_flags&SHOW_ABI)?"ABI " : "");
+ d = opendir("/proc");
+ if(d == 0) return -1;
+
while((de = readdir(d)) != 0){
if(isdigit(de->d_name[0])){
int pid = atoi(de->d_name);