Merge "liblog: remove strdup from logging calls"
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 5679711..85ab4d1 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -428,49 +428,6 @@
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) {
// Old devices can't handle window size changes.
if (shell == nullptr) return;
@@ -738,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;
}
@@ -1129,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.
@@ -1608,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");
@@ -1764,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;
}
@@ -1780,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;
}
@@ -1883,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/sysdeps.h b/adb/sysdeps.h
index 0abade4..16796cd 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -27,6 +27,7 @@
#include <errno.h>
#include <string>
+#include <vector>
// Include this before open/unlink are defined as macros below.
#include <android-base/utf8.h>
@@ -61,6 +62,10 @@
#ifdef _WIN32
+// Clang-only nullability specifiers
+#define _Nonnull
+#define _Nullable
+
#include <ctype.h>
#include <direct.h>
#include <dirent.h>
@@ -253,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);
@@ -287,6 +289,8 @@
extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
__attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
+extern int adb_vprintf(const char *format, va_list ap)
+ __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
extern int adb_fprintf(FILE *stream, const char *format, ...)
__attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
extern int adb_printf(const char *format, ...)
@@ -294,6 +298,8 @@
extern int adb_fputs(const char* buf, FILE* stream);
extern int adb_fputc(int ch, FILE* stream);
+extern int adb_putchar(int ch);
+extern int adb_puts(const char* buf);
extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
FILE* stream);
@@ -320,13 +326,20 @@
#define chmod adb_chmod
#define vfprintf adb_vfprintf
+#define vprintf adb_vprintf
#define fprintf adb_fprintf
#define printf adb_printf
#define fputs adb_fputs
#define fputc adb_fputc
+// putc may be a macro, so if so, undefine it, so that we can redefine it.
+#undef putc
+#define putc(c, s) adb_fputc(c, s)
+#define putchar adb_putchar
+#define puts adb_puts
#define fwrite adb_fwrite
#define fopen adb_fopen
+#define freopen freopen_utf8_not_yet_implemented
#define getenv adb_getenv
#define putenv putenv_utf8_not_yet_implemented
@@ -385,6 +398,12 @@
// but does not check if the handle != INVALID_HANDLE_VALUE.
typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
+namespace internal {
+
+size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
+
+}
+
#else /* !_WIN32 a.k.a. Unix */
#include "fdevent.h"
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index bea47a2..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;
}
@@ -2483,12 +2451,15 @@
}
+static adb_mutex_t g_console_output_buffer_lock;
+
void
adb_sysdeps_init( void )
{
#define ADB_MUTEX(x) InitializeCriticalSection( & x );
#include "mutex_list.h"
InitializeCriticalSection( &_win32_lock );
+ InitializeCriticalSection( &g_console_output_buffer_lock );
}
/**************************************************************************/
@@ -2557,6 +2528,8 @@
// Returns a console handle if |stream| is a console, otherwise returns nullptr.
static HANDLE _get_console_handle(FILE* const stream) {
+ // Save and restore errno to make it easier for callers to prevent from overwriting errno.
+ android::base::ErrnoRestorer er;
const int fd = fileno(stream);
if (fd < 0) {
return nullptr;
@@ -2575,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;
}
@@ -3328,6 +3301,9 @@
void stdin_raw_init() {
const HANDLE in = _get_console_handle(STDIN_FILENO, &_old_console_mode);
+ if (in == nullptr) {
+ return;
+ }
// Disable ENABLE_PROCESSED_INPUT so that Ctrl-C is read instead of
// calling the process Ctrl-C routine (configured by
@@ -3344,7 +3320,7 @@
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
@@ -3364,7 +3340,7 @@
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());
}
}
}
@@ -3649,16 +3625,98 @@
return _wchmod(path_wide.c_str(), mode);
}
-// Internal helper function to write UTF-8 bytes to a console. Returns -1
-// on error.
-static int _console_write_utf8(const char* buf, size_t size, FILE* stream,
- HANDLE console) {
- std::wstring output;
+// From libutils/Unicode.cpp, get the length of a UTF-8 sequence given the lead byte.
+static inline size_t utf8_codepoint_len(uint8_t ch) {
+ return ((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1;
+}
- // Try to convert from data that might be UTF-8 to UTF-16, ignoring errors.
- // Data might not be UTF-8 if the user cat's random data, runs dmesg, etc.
+namespace internal {
+
+// Given a sequence of UTF-8 bytes (denoted by the range [first, last)), return the number of bytes
+// (from the beginning) that are complete UTF-8 sequences and append the remaining bytes to
+// remaining_bytes.
+size_t ParseCompleteUTF8(const char* const first, const char* const last,
+ std::vector<char>* const remaining_bytes) {
+ // Walk backwards from the end of the sequence looking for the beginning of a UTF-8 sequence.
+ // Current_after points one byte past the current byte to be examined.
+ for (const char* current_after = last; current_after != first; --current_after) {
+ const char* const current = current_after - 1;
+ const char ch = *current;
+ const char kHighBit = 0x80u;
+ const char kTwoHighestBits = 0xC0u;
+ if ((ch & kHighBit) == 0) { // high bit not set
+ // The buffer ends with a one-byte UTF-8 sequence, possibly followed by invalid trailing
+ // bytes with no leading byte, so return the entire buffer.
+ break;
+ } else if ((ch & kTwoHighestBits) == kTwoHighestBits) { // top two highest bits set
+ // Lead byte in UTF-8 sequence, so check if we have all the bytes in the sequence.
+ const size_t bytes_available = last - current;
+ if (bytes_available < utf8_codepoint_len(ch)) {
+ // We don't have all the bytes in the UTF-8 sequence, so return all the bytes
+ // preceding the current incomplete UTF-8 sequence and append the remaining bytes
+ // to remaining_bytes.
+ remaining_bytes->insert(remaining_bytes->end(), current, last);
+ return current - first;
+ } else {
+ // The buffer ends with a complete UTF-8 sequence, possibly followed by invalid
+ // trailing bytes with no lead byte, so return the entire buffer.
+ break;
+ }
+ } else {
+ // Trailing byte, so keep going backwards looking for the lead byte.
+ }
+ }
+
+ // Return the size of the entire buffer. It is possible that we walked backward past invalid
+ // trailing bytes with no lead byte, in which case we want to return all those invalid bytes
+ // so that they can be processed.
+ return last - first;
+}
+
+}
+
+// Bytes that have not yet been output to the console because they are incomplete UTF-8 sequences.
+// Note that we use only one buffer even though stderr and stdout are logically separate streams.
+// This matches the behavior of Linux.
+// Protected by g_console_output_buffer_lock.
+static auto& g_console_output_buffer = *new std::vector<char>();
+
+// Internal helper function to write UTF-8 bytes to a console. Returns -1 on error.
+static int _console_write_utf8(const char* const buf, const size_t buf_size, FILE* stream,
+ HANDLE console) {
+ const int saved_errno = errno;
+ std::vector<char> combined_buffer;
+
+ // Complete UTF-8 sequences that should be immediately written to the console.
+ const char* utf8;
+ size_t utf8_size;
+
+ adb_mutex_lock(&g_console_output_buffer_lock);
+ if (g_console_output_buffer.empty()) {
+ // If g_console_output_buffer doesn't have a buffered up incomplete UTF-8 sequence (the
+ // common case with plain ASCII), parse buf directly.
+ utf8 = buf;
+ utf8_size = internal::ParseCompleteUTF8(buf, buf + buf_size, &g_console_output_buffer);
+ } else {
+ // If g_console_output_buffer has a buffered up incomplete UTF-8 sequence, move it to
+ // combined_buffer (and effectively clear g_console_output_buffer) and append buf to
+ // combined_buffer, then parse it all together.
+ combined_buffer.swap(g_console_output_buffer);
+ combined_buffer.insert(combined_buffer.end(), buf, buf + buf_size);
+
+ utf8 = combined_buffer.data();
+ utf8_size = internal::ParseCompleteUTF8(utf8, utf8 + combined_buffer.size(),
+ &g_console_output_buffer);
+ }
+ adb_mutex_unlock(&g_console_output_buffer_lock);
+
+ std::wstring utf16;
+
+ // Try to convert from data that might be UTF-8 to UTF-16, ignoring errors (just like Linux
+ // which does not return an error on bad UTF-8). Data might not be UTF-8 if the user cat's
+ // random data, runs dmesg (which might have non-UTF-8), etc.
// This could throw std::bad_alloc.
- (void)android::base::UTF8ToWide(buf, size, &output);
+ (void)android::base::UTF8ToWide(utf8, utf8_size, &utf16);
// Note that this does not do \n => \r\n translation because that
// doesn't seem necessary for the Windows console. For the Windows
@@ -3671,16 +3729,16 @@
// Write UTF-16 to the console.
DWORD written = 0;
- if (!WriteConsoleW(console, output.c_str(), output.length(), &written,
- NULL)) {
+ if (!WriteConsoleW(console, utf16.c_str(), utf16.length(), &written, NULL)) {
errno = EIO;
return -1;
}
- // This is the number of UTF-16 chars written, which might be different
- // than the number of UTF-8 chars passed in. It doesn't seem practical to
- // get this count correct.
- return written;
+ // Return the size of the original buffer passed in, signifying that we consumed it all, even
+ // if nothing was displayed, in the case of being passed an incomplete UTF-8 sequence. This
+ // matches the Linux behavior.
+ errno = saved_errno;
+ return buf_size;
}
// Function prototype because attributes cannot be placed on func definitions.
@@ -3692,14 +3750,21 @@
// Returns -1 on error.
static int _console_vfprintf(const HANDLE console, FILE* stream,
const char *format, va_list ap) {
+ const int saved_errno = errno;
std::string output_utf8;
// Format the string.
// This could throw std::bad_alloc.
android::base::StringAppendV(&output_utf8, format, ap);
- return _console_write_utf8(output_utf8.c_str(), output_utf8.length(),
- stream, console);
+ const int result = _console_write_utf8(output_utf8.c_str(), output_utf8.length(), stream,
+ console);
+ if (result != -1) {
+ errno = saved_errno;
+ } else {
+ // If -1 was returned, errno has been set.
+ }
+ return result;
}
// Version of vfprintf() that takes UTF-8 and can write Unicode to a
@@ -3721,6 +3786,11 @@
}
}
+// Version of vprintf() that takes UTF-8 and can write Unicode to a Windows console.
+int adb_vprintf(const char *format, va_list ap) {
+ return adb_vfprintf(stdout, format, ap);
+}
+
// Version of fprintf() that takes UTF-8 and can write Unicode to a
// Windows console.
int adb_fprintf(FILE *stream, const char *format, ...) {
@@ -3748,6 +3818,7 @@
int adb_fputs(const char* buf, FILE* stream) {
// adb_fprintf returns -1 on error, which is conveniently the same as EOF
// which fputs (and hence adb_fputs) should return on error.
+ static_assert(EOF == -1, "EOF is not -1, so this code needs to be fixed");
return adb_fprintf(stream, "%s", buf);
}
@@ -3755,32 +3826,32 @@
// Windows console.
int adb_fputc(int ch, FILE* stream) {
const int result = adb_fprintf(stream, "%c", ch);
- if (result <= 0) {
- // If there was an error, or if nothing was printed (which should be an
- // error), return an error, which fprintf signifies with EOF.
+ if (result == -1) {
return EOF;
}
// For success, fputc returns the char, cast to unsigned char, then to int.
return static_cast<unsigned char>(ch);
}
+// Version of putchar() that takes UTF-8 and can write Unicode to a Windows console.
+int adb_putchar(int ch) {
+ return adb_fputc(ch, stdout);
+}
+
+// Version of puts() that takes UTF-8 and can write Unicode to a Windows console.
+int adb_puts(const char* buf) {
+ // adb_printf returns -1 on error, which is conveniently the same as EOF
+ // which puts (and hence adb_puts) should return on error.
+ static_assert(EOF == -1, "EOF is not -1, so this code needs to be fixed");
+ return adb_printf("%s\n", buf);
+}
+
// Internal function to write UTF-8 to a Win32 console. Returns the number of
// items (of length size) written. On error, returns a short item count or 0.
static size_t _console_fwrite(const void* ptr, size_t size, size_t nmemb,
FILE* stream, HANDLE console) {
- // TODO: Note that a Unicode character could be several UTF-8 bytes. But
- // if we're passed only some of the bytes of a character (for example, from
- // the network socket for adb shell), we won't be able to convert the char
- // to a complete UTF-16 char (or surrogate pair), so the output won't look
- // right.
- //
- // To fix this, see libutils/Unicode.cpp for hints on decoding UTF-8.
- //
- // For now we ignore this problem because the alternative is that we'd have
- // to parse UTF-8 and buffer things up (doable). At least this is better
- // than what we had before -- always incorrect multi-byte UTF-8 output.
- int result = _console_write_utf8(reinterpret_cast<const char*>(ptr),
- size * nmemb, stream, console);
+ const int result = _console_write_utf8(reinterpret_cast<const char*>(ptr), size * nmemb, stream,
+ console);
if (result == -1) {
return 0;
}
diff --git a/adb/sysdeps_win32_test.cpp b/adb/sysdeps_win32_test.cpp
index 1d40281..8f610cf 100755
--- a/adb/sysdeps_win32_test.cpp
+++ b/adb/sysdeps_win32_test.cpp
@@ -137,3 +137,60 @@
// Make sure an invalid FD is handled correctly.
EXPECT_EQ(0, unix_isatty(-1));
}
+
+void TestParseCompleteUTF8(const char* buf, const size_t buf_size,
+ const size_t expected_complete_bytes,
+ const std::vector<char>& expected_remaining_bytes) {
+ std::vector<char> remaining_bytes;
+ const size_t complete_bytes = internal::ParseCompleteUTF8(buf, buf + buf_size,
+ &remaining_bytes);
+ EXPECT_EQ(expected_complete_bytes, complete_bytes);
+ EXPECT_EQ(expected_remaining_bytes, remaining_bytes);
+}
+
+TEST(sysdeps_win32, ParseCompleteUTF8) {
+ const std::vector<std::vector<char>> multi_byte_sequences = {
+ { '\xc2', '\xa9' }, // 2 byte UTF-8 sequence
+ { '\xe1', '\xb4', '\xa8' }, // 3 byte UTF-8 sequence
+ { '\xf0', '\x9f', '\x98', '\x80' }, // 4 byte UTF-8 sequence
+ };
+ std::vector<std::vector<char>> all_sequences = {
+ {}, // 0 bytes
+ { '\0' }, // NULL byte
+ { 'a' }, // 1 byte UTF-8 sequence
+ };
+ all_sequences.insert(all_sequences.end(), multi_byte_sequences.begin(),
+ multi_byte_sequences.end());
+
+ // Vary a prefix of bytes in front of the sequence that we're actually interested in parsing.
+ for (const auto& prefix : all_sequences) {
+ // Parse (prefix + one byte of the sequence at a time)
+ for (const auto& seq : multi_byte_sequences) {
+ std::vector<char> buffer(prefix);
+
+ // For every byte of the sequence except the last
+ for (size_t i = 0; i < seq.size() - 1; ++i) {
+ buffer.push_back(seq[i]);
+
+ // When parsing an incomplete UTF-8 sequence, the amount of the buffer preceding
+ // the start of the incomplete UTF-8 sequence is valid. The remaining bytes are the
+ // bytes of the incomplete UTF-8 sequence.
+ TestParseCompleteUTF8(buffer.data(), buffer.size(), prefix.size(),
+ std::vector<char>(seq.begin(), seq.begin() + i + 1));
+ }
+
+ // For the last byte of the sequence
+ buffer.push_back(seq.back());
+ TestParseCompleteUTF8(buffer.data(), buffer.size(), buffer.size(), std::vector<char>());
+ }
+
+ // Parse (prefix (aka sequence) + invalid trailing bytes) to verify that the invalid
+ // trailing bytes are immediately "returned" to prevent them from being stuck in some
+ // buffer.
+ std::vector<char> buffer(prefix);
+ for (size_t i = 0; i < 8; ++i) {
+ buffer.push_back(0x80); // trailing byte
+ TestParseCompleteUTF8(buffer.data(), buffer.size(), buffer.size(), std::vector<char>());
+ }
+ }
+}
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/crash_reporter/crash_reporter.cc b/crash_reporter/crash_reporter.cc
index b69492a..16e70d8 100644
--- a/crash_reporter/crash_reporter.cc
+++ b/crash_reporter/crash_reporter.cc
@@ -43,7 +43,8 @@
#endif
static const char kCrashCounterHistogram[] = "Logging.CrashCounter";
-static const char kKernelCrashDetected[] = "/var/run/kernel-crash-detected";
+static const char kKernelCrashDetected[] =
+ "/data/misc/crash_reporter/run/kernel-crash-detected";
static const char kUncleanShutdownDetected[] =
"/var/run/unclean-shutdown-detected";
static const char kGUIDFileName[] = "/data/misc/crash_reporter/guid";
diff --git a/crash_reporter/crash_reporter.rc b/crash_reporter/crash_reporter.rc
index 57c1d40..e6d1ec5 100644
--- a/crash_reporter/crash_reporter.rc
+++ b/crash_reporter/crash_reporter.rc
@@ -13,6 +13,10 @@
# Remove any previous orphaned locks.
rmdir /data/misc/crash_reporter/lock/crash_sender
+ # Remove any previous run files.
+ rm /data/misc/crash_reporter/run/kernel-crash-detected
+ rmdir /data/misc/crash_reporter/run
+
# Create crash directories.
# These directories are group-writable by root so that crash_reporter can
# still access them when it switches users.
diff --git a/crash_reporter/kernel_collector.cc b/crash_reporter/kernel_collector.cc
index cb3a315..68f2d9e 100644
--- a/crash_reporter/kernel_collector.cc
+++ b/crash_reporter/kernel_collector.cc
@@ -30,8 +30,8 @@
namespace {
const char kDefaultKernelStackSignature[] = "kernel-UnspecifiedStackSignature";
-const char kDumpParentPath[] = "/dev";
-const char kDumpPath[] = "/dev/pstore";
+const char kDumpParentPath[] = "/sys/fs";
+const char kDumpPath[] = "/sys/fs/pstore";
const char kDumpFormat[] = "dmesg-ramoops-%zu";
const char kKernelExecName[] = "kernel";
// Maximum number of records to examine in the kDumpPath.
diff --git a/debuggerd/debuggerd.rc b/debuggerd/debuggerd.rc
index e43fe96..1c6b9ff 100644
--- a/debuggerd/debuggerd.rc
+++ b/debuggerd/debuggerd.rc
@@ -1,4 +1,3 @@
service debuggerd /system/bin/debuggerd
- class main
group root readproc
writepid /dev/cpuset/system-background/tasks
diff --git a/debuggerd/debuggerd64.rc b/debuggerd/debuggerd64.rc
index 35b5af3..3e8847a 100644
--- a/debuggerd/debuggerd64.rc
+++ b/debuggerd/debuggerd64.rc
@@ -1,4 +1,3 @@
service debuggerd64 /system/bin/debuggerd64
- class main
group root readproc
writepid /dev/cpuset/system-background/tasks
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index fcec5b1..11d769b 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -65,6 +65,7 @@
libdiagnose_usb \
libbase \
libcutils \
+ libgtest_host \
# libf2fs_dlutils_host will dlopen("libf2fs_fmt_host_dyn")
LOCAL_CFLAGS_linux := -DUSE_F2FS
@@ -106,7 +107,11 @@
LOCAL_MODULE := fastboot_test
LOCAL_MODULE_HOST_OS := darwin linux windows
-LOCAL_SRC_FILES := socket.cpp socket_test.cpp
+LOCAL_SRC_FILES := \
+ socket.cpp \
+ socket_mock.cpp \
+ socket_test.cpp \
+
LOCAL_STATIC_LIBRARIES := libbase libcutils
LOCAL_CFLAGS += -Wall -Wextra -Werror -Wunreachable-code
diff --git a/fastboot/socket.cpp b/fastboot/socket.cpp
index d41f1fe..d49f47f 100644
--- a/fastboot/socket.cpp
+++ b/fastboot/socket.cpp
@@ -28,6 +28,7 @@
#include "socket.h"
+#include <android-base/errors.h>
#include <android-base/stringprintf.h>
Socket::Socket(cutils_socket_t sock) : sock_(sock) {}
@@ -77,6 +78,10 @@
return total;
}
+int Socket::GetLocalPort() {
+ return socket_get_local_port(sock_);
+}
+
// Implements the Socket interface for UDP.
class UdpSocket : public Socket {
public:
@@ -84,7 +89,8 @@
UdpSocket(Type type, cutils_socket_t sock);
- ssize_t Send(const void* data, size_t length) override;
+ bool Send(const void* data, size_t length) override;
+ bool Send(std::vector<cutils_socket_buffer_t> buffers) override;
ssize_t Receive(void* data, size_t length, int timeout_ms) override;
private:
@@ -104,9 +110,20 @@
}
}
-ssize_t UdpSocket::Send(const void* data, size_t length) {
+bool UdpSocket::Send(const void* data, size_t length) {
return TEMP_FAILURE_RETRY(sendto(sock_, reinterpret_cast<const char*>(data), length, 0,
- reinterpret_cast<sockaddr*>(addr_.get()), addr_size_));
+ reinterpret_cast<sockaddr*>(addr_.get()), addr_size_)) ==
+ static_cast<ssize_t>(length);
+}
+
+bool UdpSocket::Send(std::vector<cutils_socket_buffer_t> buffers) {
+ size_t total_length = 0;
+ for (const auto& buffer : buffers) {
+ total_length += buffer.length;
+ }
+
+ return TEMP_FAILURE_RETRY(socket_send_buffers_function_(
+ sock_, buffers.data(), buffers.size())) == static_cast<ssize_t>(total_length);
}
ssize_t UdpSocket::Receive(void* data, size_t length, int timeout_ms) {
@@ -130,7 +147,8 @@
public:
TcpSocket(cutils_socket_t sock) : Socket(sock) {}
- ssize_t Send(const void* data, size_t length) override;
+ bool Send(const void* data, size_t length) override;
+ bool Send(std::vector<cutils_socket_buffer_t> buffers) override;
ssize_t Receive(void* data, size_t length, int timeout_ms) override;
std::unique_ptr<Socket> Accept() override;
@@ -139,23 +157,52 @@
DISALLOW_COPY_AND_ASSIGN(TcpSocket);
};
-ssize_t TcpSocket::Send(const void* data, size_t length) {
- size_t total = 0;
+bool TcpSocket::Send(const void* data, size_t length) {
+ while (length > 0) {
+ ssize_t sent =
+ TEMP_FAILURE_RETRY(send(sock_, reinterpret_cast<const char*>(data), length, 0));
- while (total < length) {
- ssize_t bytes = TEMP_FAILURE_RETRY(
- send(sock_, reinterpret_cast<const char*>(data) + total, length - total, 0));
-
- if (bytes == -1) {
- if (total == 0) {
- return -1;
- }
- break;
+ if (sent == -1) {
+ return false;
}
- total += bytes;
+ length -= sent;
}
- return total;
+ return true;
+}
+
+bool TcpSocket::Send(std::vector<cutils_socket_buffer_t> buffers) {
+ while (!buffers.empty()) {
+ ssize_t sent = TEMP_FAILURE_RETRY(
+ socket_send_buffers_function_(sock_, buffers.data(), buffers.size()));
+
+ if (sent == -1) {
+ return false;
+ }
+
+ // Adjust the buffers to skip past the bytes we've just sent.
+ auto iter = buffers.begin();
+ while (sent > 0) {
+ if (iter->length > static_cast<size_t>(sent)) {
+ // Incomplete buffer write; adjust the buffer to point to the next byte to send.
+ iter->length -= sent;
+ iter->data = reinterpret_cast<const char*>(iter->data) + sent;
+ break;
+ }
+
+ // Complete buffer write; move on to the next buffer.
+ sent -= iter->length;
+ ++iter;
+ }
+
+ // Shortcut the common case: we've written everything remaining.
+ if (iter == buffers.end()) {
+ break;
+ }
+ buffers.erase(buffers.begin(), iter);
+ }
+
+ return true;
}
ssize_t TcpSocket::Receive(void* data, size_t length, int timeout_ms) {
@@ -210,3 +257,12 @@
return nullptr;
}
+
+std::string Socket::GetErrorMessage() {
+#if defined(_WIN32)
+ DWORD error_code = WSAGetLastError();
+#else
+ int error_code = errno;
+#endif
+ return android::base::SystemErrorCodeToString(error_code);
+}
diff --git a/fastboot/socket.h b/fastboot/socket.h
index 3e66c27..c0bd7c9 100644
--- a/fastboot/socket.h
+++ b/fastboot/socket.h
@@ -33,17 +33,25 @@
#ifndef SOCKET_H_
#define SOCKET_H_
+#include <functional>
#include <memory>
#include <string>
+#include <utility>
+#include <vector>
#include <android-base/macros.h>
#include <cutils/sockets.h>
+#include <gtest/gtest_prod.h>
// Socket interface to be implemented for each platform.
class Socket {
public:
enum class Protocol { kTcp, kUdp };
+ // Returns the socket error message. This must be called immediately after a socket failure
+ // before any other system calls are made.
+ static std::string GetErrorMessage();
+
// Creates a new client connection. Clients are connected to a specific hostname/port and can
// only send to that destination.
// On failure, |error| is filled (if non-null) and nullptr is returned.
@@ -60,8 +68,17 @@
virtual ~Socket();
// Sends |length| bytes of |data|. For TCP sockets this will continue trying to send until all
- // bytes are transmitted. Returns the number of bytes actually sent or -1 on error.
- virtual ssize_t Send(const void* data, size_t length) = 0;
+ // bytes are transmitted. Returns true on success.
+ virtual bool Send(const void* data, size_t length) = 0;
+
+ // Sends |buffers| using multi-buffer write, which can be significantly faster than making
+ // multiple calls. For UDP sockets |buffers| are all combined into a single datagram; for
+ // TCP sockets this will continue sending until all buffers are fully transmitted. Returns true
+ // on success.
+ //
+ // Note: This is non-functional for UDP server Sockets because it's not currently needed and
+ // would require an additional sendto() variation of multi-buffer write.
+ virtual bool Send(std::vector<cutils_socket_buffer_t> buffers) = 0;
// Waits up to |timeout_ms| to receive up to |length| bytes of data. |timout_ms| of 0 will
// block forever. Returns the number of bytes received or -1 on error/timeout. On timeout
@@ -78,6 +95,9 @@
// connected to the client on success, nullptr on failure.
virtual std::unique_ptr<Socket> Accept() { return nullptr; }
+ // Returns the local port the Socket is bound to or -1 on error.
+ int GetLocalPort();
+
protected:
// Protected constructor to force factory function use.
Socket(cutils_socket_t sock);
@@ -87,9 +107,17 @@
cutils_socket_t sock_ = INVALID_SOCKET;
+ // Non-class functions we want to override during tests to verify functionality. Implementation
+ // should call this rather than using socket_send_buffers() directly.
+ std::function<ssize_t(cutils_socket_t, cutils_socket_buffer_t*, size_t)>
+ socket_send_buffers_function_ = &socket_send_buffers;
+
private:
int receive_timeout_ms_ = 0;
+ FRIEND_TEST(SocketTest, TestTcpSendBuffers);
+ FRIEND_TEST(SocketTest, TestUdpSendBuffers);
+
DISALLOW_COPY_AND_ASSIGN(Socket);
};
diff --git a/fastboot/socket_mock.cpp b/fastboot/socket_mock.cpp
new file mode 100644
index 0000000..c962f30
--- /dev/null
+++ b/fastboot/socket_mock.cpp
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "socket_mock.h"
+
+#include <gtest/gtest.h>
+
+SocketMock::SocketMock() : Socket(INVALID_SOCKET) {}
+
+SocketMock::~SocketMock() {
+ if (!events_.empty()) {
+ ADD_FAILURE() << events_.size() << " event(s) were not handled";
+ }
+}
+
+bool SocketMock::Send(const void* data, size_t length) {
+ if (events_.empty()) {
+ ADD_FAILURE() << "Send() was called when no message was expected";
+ return false;
+ }
+
+ if (events_.front().type != EventType::kSend) {
+ ADD_FAILURE() << "Send() was called out-of-order";
+ return false;
+ }
+
+ std::string message(reinterpret_cast<const char*>(data), length);
+ if (events_.front().message != message) {
+ ADD_FAILURE() << "Send() expected " << events_.front().message << ", but got " << message;
+ return false;
+ }
+
+ bool return_value = events_.front().return_value;
+ events_.pop();
+ return return_value;
+}
+
+// Mock out multi-buffer send to be one large send, since that's what it should looks like from
+// the user's perspective.
+bool SocketMock::Send(std::vector<cutils_socket_buffer_t> buffers) {
+ std::string data;
+ for (const auto& buffer : buffers) {
+ data.append(reinterpret_cast<const char*>(buffer.data), buffer.length);
+ }
+ return Send(data.data(), data.size());
+}
+
+ssize_t SocketMock::Receive(void* data, size_t length, int /*timeout_ms*/) {
+ if (events_.empty()) {
+ ADD_FAILURE() << "Receive() was called when no message was ready";
+ return -1;
+ }
+
+ if (events_.front().type != EventType::kReceive) {
+ ADD_FAILURE() << "Receive() was called out-of-order";
+ return -1;
+ }
+
+ if (events_.front().return_value > static_cast<ssize_t>(length)) {
+ ADD_FAILURE() << "Receive(): not enough bytes (" << length << ") for "
+ << events_.front().message;
+ return -1;
+ }
+
+ ssize_t return_value = events_.front().return_value;
+ if (return_value > 0) {
+ memcpy(data, events_.front().message.data(), return_value);
+ }
+ events_.pop();
+ return return_value;
+}
+
+int SocketMock::Close() {
+ return 0;
+}
+
+std::unique_ptr<Socket> SocketMock::Accept() {
+ if (events_.empty()) {
+ ADD_FAILURE() << "Accept() was called when no socket was ready";
+ return nullptr;
+ }
+
+ if (events_.front().type != EventType::kAccept) {
+ ADD_FAILURE() << "Accept() was called out-of-order";
+ return nullptr;
+ }
+
+ std::unique_ptr<Socket> sock = std::move(events_.front().sock);
+ events_.pop();
+ return sock;
+}
+
+void SocketMock::ExpectSend(std::string message) {
+ events_.push(Event(EventType::kSend, std::move(message), true, nullptr));
+}
+
+void SocketMock::ExpectSendFailure(std::string message) {
+ events_.push(Event(EventType::kSend, std::move(message), false, nullptr));
+}
+
+void SocketMock::AddReceive(std::string message) {
+ ssize_t return_value = message.length();
+ events_.push(Event(EventType::kReceive, std::move(message), return_value, nullptr));
+}
+
+void SocketMock::AddReceiveFailure() {
+ events_.push(Event(EventType::kReceive, "", -1, nullptr));
+}
+
+void SocketMock::AddAccept(std::unique_ptr<Socket> sock) {
+ events_.push(Event(EventType::kAccept, "", 0, std::move(sock)));
+}
+
+SocketMock::Event::Event(EventType _type, std::string _message, ssize_t _return_value,
+ std::unique_ptr<Socket> _sock)
+ : type(_type), message(_message), return_value(_return_value), sock(std::move(_sock)) {}
diff --git a/fastboot/socket_mock.h b/fastboot/socket_mock.h
new file mode 100644
index 0000000..41fe06d
--- /dev/null
+++ b/fastboot/socket_mock.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef SOCKET_MOCK_H_
+#define SOCKET_MOCK_H_
+
+#include <memory>
+#include <queue>
+#include <string>
+
+#include <android-base/macros.h>
+
+#include "socket.h"
+
+// A mock Socket implementation to be used for testing. Tests can set expectations for messages
+// to be sent and provide messages to be received in order to verify protocol behavior.
+//
+// Example: testing sending "foo" and receiving "bar".
+// SocketMock mock;
+// mock.ExpectSend("foo");
+// mock.AddReceive("bar");
+// EXPECT_TRUE(DoFooBar(&mock));
+//
+// Example: testing sending "foo" and expecting "bar", but receiving "baz" instead.
+// SocketMock mock;
+// mock.ExpectSend("foo");
+// mock.AddReceive("baz");
+// EXPECT_FALSE(DoFooBar(&mock));
+class SocketMock : public Socket {
+ public:
+ SocketMock();
+ ~SocketMock() override;
+
+ bool Send(const void* data, size_t length) override;
+ bool Send(std::vector<cutils_socket_buffer_t> buffers) override;
+ ssize_t Receive(void* data, size_t length, int timeout_ms) override;
+ int Close() override;
+ virtual std::unique_ptr<Socket> Accept();
+
+ // Adds an expectation for Send().
+ void ExpectSend(std::string message);
+
+ // Adds an expectation for Send() that returns false.
+ void ExpectSendFailure(std::string message);
+
+ // Adds data to provide for Receive().
+ void AddReceive(std::string message);
+
+ // Adds a Receive() failure.
+ void AddReceiveFailure();
+
+ // Adds a Socket to return from Accept().
+ void AddAccept(std::unique_ptr<Socket> sock);
+
+ private:
+ enum class EventType { kSend, kReceive, kAccept };
+
+ struct Event {
+ Event(EventType _type, std::string _message, ssize_t _return_value,
+ std::unique_ptr<Socket> _sock);
+
+ EventType type;
+ std::string message;
+ ssize_t return_value;
+ std::unique_ptr<Socket> sock;
+ };
+
+ std::queue<Event> events_;
+
+ DISALLOW_COPY_AND_ASSIGN(SocketMock);
+};
+
+#endif // SOCKET_MOCK_H_
diff --git a/fastboot/socket_test.cpp b/fastboot/socket_test.cpp
index 1fd9d7c..cc71075 100644
--- a/fastboot/socket_test.cpp
+++ b/fastboot/socket_test.cpp
@@ -14,33 +14,33 @@
* limitations under the License.
*/
-// Tests UDP functionality using loopback connections. Requires that kTestPort is available
-// for loopback communication on the host. These tests also assume that no UDP packets are lost,
-// which should be the case for loopback communication, but is not guaranteed.
+// Tests socket functionality using loopback connections. The UDP tests assume that no packets are
+// lost, which should be the case for loopback communication, but is not guaranteed.
+//
+// Also tests our SocketMock class to make sure it works as expected and reports errors properly
+// if the mock expectations aren't met during a test.
#include "socket.h"
+#include "socket_mock.h"
+#include <list>
+
+#include <gtest/gtest-spi.h>
#include <gtest/gtest.h>
-enum {
- // This port must be available for loopback communication.
- kTestPort = 54321,
-
- // Don't wait forever in a unit test.
- kTestTimeoutMs = 3000,
-};
+enum { kTestTimeoutMs = 3000 };
// Creates connected sockets |server| and |client|. Returns true on success.
bool MakeConnectedSockets(Socket::Protocol protocol, std::unique_ptr<Socket>* server,
- std::unique_ptr<Socket>* client, const std::string hostname = "localhost",
- int port = kTestPort) {
- *server = Socket::NewServer(protocol, port);
+ std::unique_ptr<Socket>* client,
+ const std::string hostname = "localhost") {
+ *server = Socket::NewServer(protocol, 0);
if (*server == nullptr) {
ADD_FAILURE() << "Failed to create server.";
return false;
}
- *client = Socket::NewClient(protocol, hostname, port, nullptr);
+ *client = Socket::NewClient(protocol, hostname, (*server)->GetLocalPort(), nullptr);
if (*client == nullptr) {
ADD_FAILURE() << "Failed to create client.";
return false;
@@ -61,7 +61,7 @@
// Sends a string over a Socket. Returns true if the full string (without terminating char)
// was sent.
static bool SendString(Socket* sock, const std::string& message) {
- return sock->Send(message.c_str(), message.length()) == static_cast<ssize_t>(message.length());
+ return sock->Send(message.c_str(), message.length());
}
// Receives a string from a Socket. Returns true if the full string (without terminating char)
@@ -124,3 +124,212 @@
EXPECT_EQ(-1, bytes);
}
}
+
+// Tests UDP multi-buffer send.
+TEST(SocketTest, TestUdpSendBuffers) {
+ std::unique_ptr<Socket> sock = Socket::NewServer(Socket::Protocol::kUdp, 0);
+ std::vector<std::string> data{"foo", "bar", "12345"};
+ std::vector<cutils_socket_buffer_t> buffers{{data[0].data(), data[0].length()},
+ {data[1].data(), data[1].length()},
+ {data[2].data(), data[2].length()}};
+ ssize_t mock_return_value = 0;
+
+ // Mock out socket_send_buffers() to verify we're sending in the correct buffers and
+ // return |mock_return_value|.
+ sock->socket_send_buffers_function_ = [&buffers, &mock_return_value](
+ cutils_socket_t /*cutils_sock*/, cutils_socket_buffer_t* sent_buffers,
+ size_t num_sent_buffers) -> ssize_t {
+ EXPECT_EQ(buffers.size(), num_sent_buffers);
+ for (size_t i = 0; i < num_sent_buffers; ++i) {
+ EXPECT_EQ(buffers[i].data, sent_buffers[i].data);
+ EXPECT_EQ(buffers[i].length, sent_buffers[i].length);
+ }
+ return mock_return_value;
+ };
+
+ mock_return_value = strlen("foobar12345");
+ EXPECT_TRUE(sock->Send(buffers));
+
+ mock_return_value -= 1;
+ EXPECT_FALSE(sock->Send(buffers));
+
+ mock_return_value = 0;
+ EXPECT_FALSE(sock->Send(buffers));
+
+ mock_return_value = -1;
+ EXPECT_FALSE(sock->Send(buffers));
+}
+
+// Tests TCP re-sending until socket_send_buffers() sends all data. This is a little complicated,
+// but the general idea is that we intercept calls to socket_send_buffers() using a lambda mock
+// function that simulates partial writes.
+TEST(SocketTest, TestTcpSendBuffers) {
+ std::unique_ptr<Socket> sock = Socket::NewServer(Socket::Protocol::kTcp, 0);
+ std::vector<std::string> data{"foo", "bar", "12345"};
+ std::vector<cutils_socket_buffer_t> buffers{{data[0].data(), data[0].length()},
+ {data[1].data(), data[1].length()},
+ {data[2].data(), data[2].length()}};
+
+ // Test breaking up the buffered send at various points.
+ std::list<std::string> test_sends[] = {
+ // Successes.
+ {"foobar12345"},
+ {"f", "oob", "ar12345"},
+ {"fo", "obar12", "345"},
+ {"foo", "bar12345"},
+ {"foob", "ar123", "45"},
+ {"f", "o", "o", "b", "a", "r", "1", "2", "3", "4", "5"},
+
+ // Failures.
+ {},
+ {"f"},
+ {"foo", "bar"},
+ {"fo", "obar12"},
+ {"foobar1234"}
+ };
+
+ for (auto& test : test_sends) {
+ ssize_t bytes_sent = 0;
+ bool expect_success = true;
+
+ // Create a mock function for custom socket_send_buffers() behavior. This function will
+ // check to make sure the input buffers start at the next unsent byte, then return the
+ // number of bytes indicated by the next entry in |test|.
+ sock->socket_send_buffers_function_ = [&bytes_sent, &data, &expect_success, &test](
+ cutils_socket_t /*cutils_sock*/, cutils_socket_buffer_t* buffers,
+ size_t num_buffers) -> ssize_t {
+ EXPECT_TRUE(num_buffers > 0);
+
+ // Failure case - pretend we errored out before sending all the buffers.
+ if (test.empty()) {
+ expect_success = false;
+ return -1;
+ }
+
+ // Count the bytes we've sent to find where the next buffer should start and how many
+ // bytes should be left in it.
+ size_t byte_count = bytes_sent, data_index = 0;
+ while (data_index < data.size()) {
+ if (byte_count >= data[data_index].length()) {
+ byte_count -= data[data_index].length();
+ ++data_index;
+ } else {
+ break;
+ }
+ }
+ void* expected_next_byte = &data[data_index][byte_count];
+ size_t expected_next_size = data[data_index].length() - byte_count;
+
+ EXPECT_EQ(data.size() - data_index, num_buffers);
+ EXPECT_EQ(expected_next_byte, buffers[0].data);
+ EXPECT_EQ(expected_next_size, buffers[0].length);
+
+ std::string to_send = std::move(test.front());
+ test.pop_front();
+ bytes_sent += to_send.length();
+ return to_send.length();
+ };
+
+ EXPECT_EQ(expect_success, sock->Send(buffers));
+ EXPECT_TRUE(test.empty());
+ }
+}
+
+TEST(SocketMockTest, TestSendSuccess) {
+ SocketMock mock;
+
+ mock.ExpectSend("foo");
+ EXPECT_TRUE(SendString(&mock, "foo"));
+
+ mock.ExpectSend("abc");
+ mock.ExpectSend("123");
+ EXPECT_TRUE(SendString(&mock, "abc"));
+ EXPECT_TRUE(SendString(&mock, "123"));
+}
+
+TEST(SocketMockTest, TestSendFailure) {
+ SocketMock* mock = new SocketMock;
+
+ mock->ExpectSendFailure("foo");
+ EXPECT_FALSE(SendString(mock, "foo"));
+
+ EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "no message was expected");
+
+ mock->ExpectSend("foo");
+ EXPECT_NONFATAL_FAILURE(SendString(mock, "bar"), "expected foo, but got bar");
+ EXPECT_TRUE(SendString(mock, "foo"));
+
+ mock->AddReceive("foo");
+ EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "called out-of-order");
+ EXPECT_TRUE(ReceiveString(mock, "foo"));
+
+ mock->ExpectSend("foo");
+ EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled");
+}
+
+TEST(SocketMockTest, TestReceiveSuccess) {
+ SocketMock mock;
+
+ mock.AddReceive("foo");
+ EXPECT_TRUE(ReceiveString(&mock, "foo"));
+
+ mock.AddReceive("abc");
+ mock.AddReceive("123");
+ EXPECT_TRUE(ReceiveString(&mock, "abc"));
+ EXPECT_TRUE(ReceiveString(&mock, "123"));
+
+ // Make sure ReceiveAll() can piece together multiple receives.
+ mock.AddReceive("foo");
+ mock.AddReceive("bar");
+ mock.AddReceive("123");
+ EXPECT_TRUE(ReceiveString(&mock, "foobar123"));
+}
+
+TEST(SocketMockTest, TestReceiveFailure) {
+ SocketMock* mock = new SocketMock;
+
+ mock->AddReceiveFailure();
+ EXPECT_FALSE(ReceiveString(mock, "foo"));
+
+ mock->AddReceive("foo");
+ mock->AddReceiveFailure();
+ EXPECT_FALSE(ReceiveString(mock, "foobar"));
+
+ EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "no message was ready");
+
+ mock->ExpectSend("foo");
+ EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "called out-of-order");
+ EXPECT_TRUE(SendString(mock, "foo"));
+
+ char c;
+ mock->AddReceive("foo");
+ EXPECT_NONFATAL_FAILURE(mock->Receive(&c, 1, 0), "not enough bytes (1) for foo");
+ EXPECT_TRUE(ReceiveString(mock, "foo"));
+
+ mock->AddReceive("foo");
+ EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled");
+}
+
+TEST(SocketMockTest, TestAcceptSuccess) {
+ SocketMock mock;
+
+ SocketMock* mock_handler = new SocketMock;
+ mock.AddAccept(std::unique_ptr<SocketMock>(mock_handler));
+ EXPECT_EQ(mock_handler, mock.Accept().get());
+
+ mock.AddAccept(nullptr);
+ EXPECT_EQ(nullptr, mock.Accept().get());
+}
+
+TEST(SocketMockTest, TestAcceptFailure) {
+ SocketMock* mock = new SocketMock;
+
+ EXPECT_NONFATAL_FAILURE(mock->Accept(), "no socket was ready");
+
+ mock->ExpectSend("foo");
+ EXPECT_NONFATAL_FAILURE(mock->Accept(), "called out-of-order");
+ EXPECT_TRUE(SendString(mock, "foo"));
+
+ mock->AddAccept(nullptr);
+ EXPECT_NONFATAL_FAILURE(delete mock, "1 event(s) were not handled");
+}
diff --git a/include/cutils/sockets.h b/include/cutils/sockets.h
index e25c555..783bd0b 100644
--- a/include/cutils/sockets.h
+++ b/include/cutils/sockets.h
@@ -132,6 +132,36 @@
int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms);
/*
+ * Returns the local port the socket is bound to or -1 on error.
+ */
+int socket_get_local_port(cutils_socket_t sock);
+
+/*
+ * Sends to a socket from multiple buffers; wraps writev() on Unix or WSASend()
+ * on Windows. This can give significant speedup compared to calling send()
+ * multiple times.
+ *
+ * Example usage:
+ * cutils_socket_buffer_t buffers[2] = { {data0, len0}, {data1, len1} };
+ * socket_send_buffers(sock, buffers, 2);
+ *
+ * If you try to pass more than SOCKET_SEND_BUFFERS_MAX_BUFFERS buffers into
+ * this function it will return -1 without sending anything.
+ *
+ * Returns the number of bytes written or -1 on error.
+ */
+typedef struct {
+ const void* data;
+ size_t length;
+} cutils_socket_buffer_t;
+
+#define SOCKET_SEND_BUFFERS_MAX_BUFFERS 16
+
+ssize_t socket_send_buffers(cutils_socket_t sock,
+ const cutils_socket_buffer_t* buffers,
+ size_t num_buffers);
+
+/*
* socket_peer_is_trusted - Takes a socket which is presumed to be a
* connected local socket (e.g. AF_LOCAL) and returns whether the peer
* (the userid that owns the process on the other end of that socket)
diff --git a/init/devices.cpp b/init/devices.cpp
index d556e30..39cd706 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -40,6 +40,7 @@
#include <sys/time.h>
#include <sys/wait.h>
+#include <android-base/file.h>
#include <cutils/list.h>
#include <cutils/uevent.h>
@@ -769,21 +770,13 @@
ret = -1;
break;
}
-
- len_to_copy -= nr;
- while (nr > 0) {
- ssize_t nw = 0;
-
- nw = write(data_fd, buf + nw, nr);
- if(nw <= 0) {
- ret = -1;
- goto out;
- }
- nr -= nw;
+ if (!android::base::WriteFully(data_fd, buf, nr)) {
+ ret = -1;
+ break;
}
+ len_to_copy -= nr;
}
-out:
if(!ret)
write(loading_fd, "0", 1); /* successful end of transfer */
else
diff --git a/libbacktrace/Android.mk b/libbacktrace/Android.mk
index 6cffb11..ee56a5e 100644
--- a/libbacktrace/Android.mk
+++ b/libbacktrace/Android.mk
@@ -60,6 +60,9 @@
liblog \
libunwind \
+libbacktrace_static_libraries := \
+ libcutils
+
module := libbacktrace
module_tag := optional
build_type := target
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 25b056b..51c6d9d 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -17,21 +17,22 @@
include $(CLEAR_VARS)
libcutils_common_sources := \
- hashmap.c \
atomic.c.arm \
- native_handle.c \
config_utils.c \
+ fs_config.c \
+ hashmap.c \
+ iosched_policy.c \
load_file.c \
- strlcpy.c \
+ native_handle.c \
open_memstream.c \
+ process_name.c \
+ record_stream.c \
+ sched_policy.c \
+ sockets.cpp \
strdup16to8.c \
strdup8to16.c \
- record_stream.c \
- process_name.c \
+ strlcpy.c \
threads.c \
- sched_policy.c \
- iosched_policy.c \
- fs_config.c
# some files must not be compiled when building against Mingw
# they correspond to features not used by our host development tools
@@ -45,7 +46,7 @@
socket_loopback_client_unix.c \
socket_loopback_server_unix.c \
socket_network_client_unix.c \
- sockets_unix.c \
+ sockets_unix.cpp \
str_parms.c \
libcutils_nonwindows_host_sources := \
@@ -55,7 +56,7 @@
libcutils_windows_host_sources := \
socket_inaddr_any_server_windows.c \
socket_network_client_windows.c \
- sockets_windows.c \
+ sockets_windows.cpp \
# Shared and static library for host
# Note: when linking this library on Windows, you must also link to Winsock2
diff --git a/libcutils/ashmem-dev.c b/libcutils/ashmem-dev.c
index 3089a94..a5203e1 100644
--- a/libcutils/ashmem-dev.c
+++ b/libcutils/ashmem-dev.c
@@ -20,17 +20,19 @@
* used by the simulator.
*/
-#include <unistd.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
+#include <errno.h>
#include <fcntl.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <linux/ashmem.h>
+
#include <cutils/ashmem.h>
-#define ASHMEM_DEVICE "/dev/ashmem"
+#define ASHMEM_DEVICE "/dev/ashmem"
/*
* ashmem_create_region - creates a new ashmem region and returns the file
@@ -41,50 +43,55 @@
*/
int ashmem_create_region(const char *name, size_t size)
{
- int fd, ret;
+ int ret, save_errno;
- fd = open(ASHMEM_DEVICE, O_RDWR);
- if (fd < 0)
- return fd;
+ int fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR));
+ if (fd < 0) {
+ return fd;
+ }
- if (name) {
- char buf[ASHMEM_NAME_LEN] = {0};
+ if (name) {
+ char buf[ASHMEM_NAME_LEN] = {0};
- strlcpy(buf, name, sizeof(buf));
- ret = ioctl(fd, ASHMEM_SET_NAME, buf);
- if (ret < 0)
- goto error;
- }
+ strlcpy(buf, name, sizeof(buf));
+ ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));
+ if (ret < 0) {
+ goto error;
+ }
+ }
- ret = ioctl(fd, ASHMEM_SET_SIZE, size);
- if (ret < 0)
- goto error;
+ ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
+ if (ret < 0) {
+ goto error;
+ }
- return fd;
+ return fd;
error:
- close(fd);
- return ret;
+ save_errno = errno;
+ close(fd);
+ errno = save_errno;
+ return ret;
}
int ashmem_set_prot_region(int fd, int prot)
{
- return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
+ return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
}
int ashmem_pin_region(int fd, size_t offset, size_t len)
{
- struct ashmem_pin pin = { offset, len };
- return ioctl(fd, ASHMEM_PIN, &pin);
+ struct ashmem_pin pin = { offset, len };
+ return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
}
int ashmem_unpin_region(int fd, size_t offset, size_t len)
{
- struct ashmem_pin pin = { offset, len };
- return ioctl(fd, ASHMEM_UNPIN, &pin);
+ struct ashmem_pin pin = { offset, len };
+ return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
}
int ashmem_get_size_region(int fd)
{
- return ioctl(fd, ASHMEM_GET_SIZE, NULL);
+ return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
}
diff --git a/libcutils/ashmem-host.c b/libcutils/ashmem-host.c
index 15dd43e..c85f06b 100644
--- a/libcutils/ashmem-host.c
+++ b/libcutils/ashmem-host.c
@@ -78,8 +78,11 @@
return -1;
}
- // Check if this is an "ashmem" region.
- // TODO: This is very hacky, and can easily break. We need some reliable indicator.
+ /*
+ * Check if this is an "ashmem" region.
+ * TODO: This is very hacky, and can easily break.
+ * We need some reliable indicator.
+ */
if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
errno = ENOTTY;
return -1;
diff --git a/libcutils/sockets.cpp b/libcutils/sockets.cpp
new file mode 100644
index 0000000..d9ab146
--- /dev/null
+++ b/libcutils/sockets.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+// This file contains socket implementation that can be shared between
+// platforms as long as the correct headers are included.
+
+#include <cutils/sockets.h>
+
+#if !defined(_WIN32)
+#include <netinet/in.h>
+#endif
+
+int socket_get_local_port(cutils_socket_t sock) {
+ sockaddr_storage addr;
+ socklen_t addr_size = sizeof(addr);
+
+ if (getsockname(sock, reinterpret_cast<sockaddr*>(&addr), &addr_size) == 0) {
+ // sockaddr_in and sockaddr_in6 always overlap the port field.
+ return ntohs(reinterpret_cast<sockaddr_in*>(&addr)->sin_port);
+ }
+ return -1;
+}
diff --git a/libcutils/sockets_unix.c b/libcutils/sockets_unix.c
deleted file mode 100644
index 5eddc4b..0000000
--- a/libcutils/sockets_unix.c
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2011 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 <cutils/sockets.h>
-#include <log/log.h>
-
-#if defined(__ANDROID__)
-/* For the socket trust (credentials) check */
-#include <private/android_filesystem_config.h>
-#define __android_unused
-#else
-#define __android_unused __attribute__((__unused__))
-#endif
-
-bool socket_peer_is_trusted(int fd __android_unused)
-{
-#if defined(__ANDROID__)
- struct ucred cr;
- socklen_t len = sizeof(cr);
- int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
-
- if (n != 0) {
- ALOGE("could not get socket credentials: %s\n", strerror(errno));
- return false;
- }
-
- if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
- ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
- return false;
- }
-#endif
-
- return true;
-}
-
-int socket_close(int sock) {
- return close(sock);
-}
-
-int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
- struct timeval tv;
- tv.tv_sec = timeout_ms / 1000;
- tv.tv_usec = (timeout_ms % 1000) * 1000;
- return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
-}
diff --git a/libcutils/sockets_unix.cpp b/libcutils/sockets_unix.cpp
new file mode 100644
index 0000000..8747d69
--- /dev/null
+++ b/libcutils/sockets_unix.cpp
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2011 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 <cutils/sockets.h>
+
+#include <sys/uio.h>
+
+#include <log/log.h>
+
+#if defined(__ANDROID__)
+/* For the socket trust (credentials) check */
+#include <private/android_filesystem_config.h>
+#define __android_unused
+#else
+#define __android_unused __attribute__((__unused__))
+#endif
+
+bool socket_peer_is_trusted(int fd __android_unused) {
+#if defined(__ANDROID__)
+ ucred cr;
+ socklen_t len = sizeof(cr);
+ int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
+
+ if (n != 0) {
+ ALOGE("could not get socket credentials: %s\n", strerror(errno));
+ return false;
+ }
+
+ if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
+ ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
+ return false;
+ }
+#endif
+
+ return true;
+}
+
+int socket_close(int sock) {
+ return close(sock);
+}
+
+int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
+ timeval tv;
+ tv.tv_sec = timeout_ms / 1000;
+ tv.tv_usec = (timeout_ms % 1000) * 1000;
+ return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+}
+
+ssize_t socket_send_buffers(cutils_socket_t sock,
+ const cutils_socket_buffer_t* buffers,
+ size_t num_buffers) {
+ if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) {
+ return -1;
+ }
+
+ iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS];
+ for (size_t i = 0; i < num_buffers; ++i) {
+ // It's safe to cast away const here; iovec declares non-const
+ // void* because it's used for both send and receive, but since
+ // we're only sending, the data won't be modified.
+ iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data);
+ iovec_buffers[i].iov_len = buffers[i].length;
+ }
+
+ return writev(sock, iovec_buffers, num_buffers);
+}
diff --git a/libcutils/sockets_windows.c b/libcutils/sockets_windows.cpp
similarity index 69%
rename from libcutils/sockets_windows.c
rename to libcutils/sockets_windows.cpp
index 1bf2933..ed6b1a7 100644
--- a/libcutils/sockets_windows.c
+++ b/libcutils/sockets_windows.cpp
@@ -37,7 +37,7 @@
// Both adb (1) and Chrome (2) purposefully avoid WSACleanup() with no issues.
// (1) https://android.googlesource.com/platform/system/core.git/+/master/adb/sysdeps_win32.cpp
// (2) https://code.google.com/p/chromium/codesearch#chromium/src/net/base/winsock_init.cc
-bool initialize_windows_sockets() {
+extern "C" bool initialize_windows_sockets() {
// There's no harm in calling WSAStartup() multiple times but no benefit
// either, we may as well skip it after the first.
static bool init_success = false;
@@ -55,6 +55,32 @@
}
int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
- return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout_ms,
- sizeof(timeout_ms));
+ return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
+ reinterpret_cast<char*>(&timeout_ms), sizeof(timeout_ms));
+}
+
+ssize_t socket_send_buffers(cutils_socket_t sock,
+ const cutils_socket_buffer_t* buffers,
+ size_t num_buffers) {
+ if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) {
+ return -1;
+ }
+
+ WSABUF wsa_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS];
+ for (size_t i = 0; i < num_buffers; ++i) {
+ // It's safe to cast away const here; WSABUF declares non-const
+ // void* because it's used for both send and receive, but since
+ // we're only sending, the data won't be modified.
+ wsa_buffers[i].buf =
+ reinterpret_cast<char*>(const_cast<void*>(buffers[i].data));
+ wsa_buffers[i].len = buffers[i].length;
+ }
+
+ DWORD bytes_sent = 0;
+ if (WSASend(sock, wsa_buffers, num_buffers, &bytes_sent, 0, nullptr,
+ nullptr) != SOCKET_ERROR) {
+ return bytes_sent;
+ }
+
+ return -1;
}
diff --git a/libcutils/tests/sockets_test.cpp b/libcutils/tests/sockets_test.cpp
index 966dfe7..0f682a2 100644
--- a/libcutils/tests/sockets_test.cpp
+++ b/libcutils/tests/sockets_test.cpp
@@ -15,9 +15,8 @@
*/
// Tests socket functionality using loopback connections. Requires IPv4 and
-// IPv6 capabilities, and that kTestPort is available for loopback
-// communication. These tests also assume that no UDP packets are lost,
-// which should be the case for loopback communication, but is not guaranteed.
+// IPv6 capabilities. These tests assume that no UDP packets are lost, which
+// should be the case for loopback communication, but is not guaranteed.
#include <cutils/sockets.h>
@@ -25,11 +24,6 @@
#include <gtest/gtest.h>
-enum {
- // This port must be available for loopback communication.
- kTestPort = 54321
-};
-
// Makes sure the passed sockets are valid, sends data between them, and closes
// them. Any failures are logged with gtest.
//
@@ -40,30 +34,39 @@
ASSERT_NE(INVALID_SOCKET, server);
ASSERT_NE(INVALID_SOCKET, client);
- char buffer[3];
+ char buffer[128];
sockaddr_storage addr;
socklen_t addr_size = sizeof(addr);
// Send client -> server first to get the UDP client's address.
ASSERT_EQ(3, send(client, "foo", 3, 0));
if (type == SOCK_DGRAM) {
- EXPECT_EQ(3, recvfrom(server, buffer, 3, 0,
- reinterpret_cast<sockaddr*>(&addr), &addr_size));
+ EXPECT_EQ(3, recvfrom(server, buffer, sizeof(buffer), 0,
+ reinterpret_cast<sockaddr*>(&addr), &addr_size));
} else {
- EXPECT_EQ(3, recv(server, buffer, 3, 0));
+ EXPECT_EQ(3, recv(server, buffer, sizeof(buffer), 0));
}
EXPECT_EQ(0, memcmp(buffer, "foo", 3));
// Now send server -> client.
if (type == SOCK_DGRAM) {
- ASSERT_EQ(3, sendto(server, "bar", 3, 0,
- reinterpret_cast<sockaddr*>(&addr), addr_size));
+ ASSERT_EQ(3, sendto(server, "bar", 3, 0,
+ reinterpret_cast<sockaddr*>(&addr), addr_size));
} else {
- ASSERT_EQ(3, send(server, "bar", 3, 0));
+ ASSERT_EQ(3, send(server, "bar", 3, 0));
}
- EXPECT_EQ(3, recv(client, buffer, 3, 0));
+ EXPECT_EQ(3, recv(client, buffer, sizeof(buffer), 0));
EXPECT_EQ(0, memcmp(buffer, "bar", 3));
+ // Send multiple buffers using socket_send_buffers().
+ std::string data[] = {"foo", "bar", "12345"};
+ cutils_socket_buffer_t socket_buffers[] = { {data[0].data(), data[0].length()},
+ {data[1].data(), data[1].length()},
+ {data[2].data(), data[2].length()} };
+ EXPECT_EQ(11, socket_send_buffers(client, socket_buffers, 3));
+ EXPECT_EQ(11, recv(server, buffer, sizeof(buffer), 0));
+ EXPECT_EQ(0, memcmp(buffer, "foobar12345", 11));
+
EXPECT_EQ(0, socket_close(server));
EXPECT_EQ(0, socket_close(client));
}
@@ -87,22 +90,43 @@
EXPECT_LE(1.0, difftime(time(nullptr), start_time));
}
+// Tests socket_get_local_port().
+TEST(SocketsTest, TestGetLocalPort) {
+ cutils_socket_t server;
+
+ // Check a bunch of ports so that we can ignore any conflicts in case
+ // of ports already being taken, but if a server is able to start up we
+ // should always be able to read its port.
+ for (int port : {10000, 12345, 15999, 20202, 25000}) {
+ for (int type : {SOCK_DGRAM, SOCK_STREAM}) {
+ server = socket_inaddr_any_server(port, SOCK_DGRAM);
+ if (server != INVALID_SOCKET) {
+ EXPECT_EQ(port, socket_get_local_port(server));
+ }
+ socket_close(server);
+ }
+ }
+
+ // Check expected failure for an invalid socket.
+ EXPECT_EQ(-1, socket_get_local_port(INVALID_SOCKET));
+}
+
// Tests socket_inaddr_any_server() and socket_network_client() for IPv4 UDP.
TEST(SocketsTest, TestIpv4UdpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(kTestPort, SOCK_DGRAM);
- cutils_socket_t client = socket_network_client("127.0.0.1", kTestPort,
- SOCK_DGRAM);
+ cutils_socket_t server = socket_inaddr_any_server(0, SOCK_DGRAM);
+ cutils_socket_t client = socket_network_client(
+ "127.0.0.1", socket_get_local_port(server), SOCK_DGRAM);
TestConnectedSockets(server, client, SOCK_DGRAM);
}
// Tests socket_inaddr_any_server() and socket_network_client() for IPv4 TCP.
TEST(SocketsTest, TestIpv4TcpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(kTestPort, SOCK_STREAM);
+ cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
ASSERT_NE(INVALID_SOCKET, server);
- cutils_socket_t client = socket_network_client("127.0.0.1", kTestPort,
- SOCK_STREAM);
+ cutils_socket_t client = socket_network_client(
+ "127.0.0.1", socket_get_local_port(server), SOCK_STREAM);
cutils_socket_t handler = accept(server, nullptr, nullptr);
EXPECT_EQ(0, socket_close(server));
@@ -111,20 +135,20 @@
// Tests socket_inaddr_any_server() and socket_network_client() for IPv6 UDP.
TEST(SocketsTest, TestIpv6UdpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(kTestPort, SOCK_DGRAM);
- cutils_socket_t client = socket_network_client("::1", kTestPort,
- SOCK_DGRAM);
+ cutils_socket_t server = socket_inaddr_any_server(0, SOCK_DGRAM);
+ cutils_socket_t client = socket_network_client(
+ "::1", socket_get_local_port(server), SOCK_DGRAM);
TestConnectedSockets(server, client, SOCK_DGRAM);
}
// Tests socket_inaddr_any_server() and socket_network_client() for IPv6 TCP.
TEST(SocketsTest, TestIpv6TcpLoopback) {
- cutils_socket_t server = socket_inaddr_any_server(kTestPort, SOCK_STREAM);
+ cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
ASSERT_NE(INVALID_SOCKET, server);
- cutils_socket_t client = socket_network_client("::1", kTestPort,
- SOCK_STREAM);
+ cutils_socket_t client = socket_network_client(
+ "::1", socket_get_local_port(server), SOCK_STREAM);
cutils_socket_t handler = accept(server, nullptr, nullptr);
EXPECT_EQ(0, socket_close(server));
@@ -133,7 +157,7 @@
// Tests setting a receive timeout for UDP sockets.
TEST(SocketsTest, TestUdpReceiveTimeout) {
- cutils_socket_t sock = socket_inaddr_any_server(kTestPort, SOCK_DGRAM);
+ cutils_socket_t sock = socket_inaddr_any_server(0, SOCK_DGRAM);
ASSERT_NE(INVALID_SOCKET, sock);
TestReceiveTimeout(sock);
@@ -143,11 +167,11 @@
// Tests setting a receive timeout for TCP sockets.
TEST(SocketsTest, TestTcpReceiveTimeout) {
- cutils_socket_t server = socket_inaddr_any_server(kTestPort, SOCK_STREAM);
+ cutils_socket_t server = socket_inaddr_any_server(0, SOCK_STREAM);
ASSERT_NE(INVALID_SOCKET, server);
- cutils_socket_t client = socket_network_client("localhost", kTestPort,
- SOCK_STREAM);
+ cutils_socket_t client = socket_network_client(
+ "localhost", socket_get_local_port(server), SOCK_STREAM);
cutils_socket_t handler = accept(server, nullptr, nullptr);
EXPECT_EQ(0, socket_close(server));
@@ -156,3 +180,8 @@
EXPECT_EQ(0, socket_close(client));
EXPECT_EQ(0, socket_close(handler));
}
+
+// Tests socket_send_buffers() failure.
+TEST(SocketsTest, TestSocketSendBuffersFailure) {
+ EXPECT_EQ(-1, socket_send_buffers(INVALID_SOCKET, nullptr, 0));
+}
diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp
index a936455..8517c9f 100644
--- a/liblog/tests/liblog_test.cpp
+++ b/liblog/tests/liblog_test.cpp
@@ -18,6 +18,8 @@
#include <inttypes.h>
#include <signal.h>
#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <cutils/properties.h>
#include <gtest/gtest.h>
@@ -25,6 +27,7 @@
#include <log/logger.h>
#include <log/log_read.h>
#include <log/logprint.h>
+#include <private/android_filesystem_config.h>
#include <private/android_logger.h>
// enhanced version of LOG_FAILURE_RETRY to add support for EAGAIN and
@@ -368,6 +371,48 @@
return;
}
+ /* Matches clientHasLogCredentials() in logd */
+ uid_t uid = getuid();
+ gid_t gid = getgid();
+ bool clientHasLogCredentials = true;
+ if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)
+ && (gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
+ uid_t euid = geteuid();
+ if ((euid != AID_SYSTEM) && (euid != AID_ROOT) && (euid != AID_LOG)) {
+ gid_t egid = getegid();
+ if ((egid != AID_SYSTEM) && (egid != AID_ROOT) && (egid != AID_LOG)) {
+ int num_groups = getgroups(0, NULL);
+ if (num_groups > 0) {
+ gid_t groups[num_groups];
+ num_groups = getgroups(num_groups, groups);
+ while (num_groups > 0) {
+ if (groups[num_groups - 1] == AID_LOG) {
+ break;
+ }
+ --num_groups;
+ }
+ }
+ if (num_groups <= 0) {
+ clientHasLogCredentials = false;
+ }
+ }
+ }
+ }
+ if (!clientHasLogCredentials) {
+ fprintf(stderr, "WARNING: "
+ "not in system context, bypassing end-to-end test\n");
+
+ log_time ts(CLOCK_MONOTONIC);
+
+ buffer.type = EVENT_TYPE_LONG;
+ buffer.data = *(static_cast<uint64_t *>((void *)&ts));
+
+ // expect failure!
+ ASSERT_GE(0, __android_log_security_bwrite(0, &buffer, sizeof(buffer)));
+
+ return;
+ }
+
pid_t pid = getpid();
ASSERT_TRUE(NULL != (logger_list = android_logger_list_open(
@@ -415,7 +460,12 @@
android_logger_list_close(logger_list);
- EXPECT_EQ(1, count);
+ bool clientHasSecurityCredentials = (uid == AID_SYSTEM) || (gid == AID_SYSTEM);
+ if (!clientHasSecurityCredentials) {
+ fprintf(stderr, "WARNING: "
+ "not system, content submitted but can not check end-to-end\n");
+ }
+ EXPECT_EQ(clientHasSecurityCredentials ? 1 : 0, count);
}
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index 6e6b0b9..209ff1c 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -42,6 +42,8 @@
"libGLESv1_CM.so:"
"libGLESv2.so:"
"libGLESv3.so:"
+ "libicui18n.so:"
+ "libicuuc.so:"
"libjnigraphics.so:"
"liblog.so:"
"libmediandk.so:"
diff --git a/libsparse/sparse_read.c b/libsparse/sparse_read.c
index ec63850..dbb4dab 100644
--- a/libsparse/sparse_read.c
+++ b/libsparse/sparse_read.c
@@ -199,7 +199,7 @@
return 0;
}
-static int process_crc32_chunk(int fd, unsigned int chunk_size, uint32_t crc32)
+static int process_crc32_chunk(int fd, unsigned int chunk_size, uint32_t *crc32)
{
uint32_t file_crc32;
int ret;
@@ -213,7 +213,7 @@
return ret;
}
- if (file_crc32 != crc32) {
+ if (crc32 != NULL && file_crc32 != *crc32) {
return -EINVAL;
}
@@ -257,7 +257,7 @@
}
return chunk_header->chunk_sz;
case CHUNK_TYPE_CRC32:
- ret = process_crc32_chunk(fd, chunk_data_size, *crc_ptr);
+ ret = process_crc32_chunk(fd, chunk_data_size, crc_ptr);
if (ret < 0) {
verbose_error(s->verbose, -EINVAL, "crc block at %" PRId64,
offset);
@@ -374,6 +374,7 @@
ret = read_all(fd, buf, to_read);
if (ret < 0) {
error("failed to read sparse file");
+ free(buf);
return ret;
}
@@ -401,6 +402,7 @@
block++;
}
+ free(buf);
return 0;
}
diff --git a/logd/FlushCommand.cpp b/logd/FlushCommand.cpp
index 48036d3..fd45c4a 100644
--- a/logd/FlushCommand.cpp
+++ b/logd/FlushCommand.cpp
@@ -93,3 +93,11 @@
bool FlushCommand::hasReadLogs(SocketClient *client) {
return clientHasLogCredentials(client);
}
+
+static bool clientHasSecurityCredentials(SocketClient *client) {
+ return (client->getUid() == AID_SYSTEM) || (client->getGid() == AID_SYSTEM);
+}
+
+bool FlushCommand::hasSecurityLogs(SocketClient *client) {
+ return clientHasSecurityCredentials(client);
+}
diff --git a/logd/FlushCommand.h b/logd/FlushCommand.h
index e0f2212..9224773 100644
--- a/logd/FlushCommand.h
+++ b/logd/FlushCommand.h
@@ -45,6 +45,7 @@
virtual void runSocketCommand(SocketClient *client);
static bool hasReadLogs(SocketClient *client);
+ static bool hasSecurityLogs(SocketClient *client);
};
#endif
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index 9e0d451..8c30f79 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -907,7 +907,8 @@
}
uint64_t LogBuffer::flushTo(
- SocketClient *reader, const uint64_t start, bool privileged,
+ SocketClient *reader, const uint64_t start,
+ bool privileged, bool security,
int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
LogBufferElementCollection::iterator it;
uint64_t max = start;
@@ -938,6 +939,10 @@
continue;
}
+ if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
+ continue;
+ }
+
if (element->getSequence() <= start) {
continue;
}
diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h
index 03739c7..7e99236 100644
--- a/logd/LogBuffer.h
+++ b/logd/LogBuffer.h
@@ -111,7 +111,7 @@
uid_t uid, pid_t pid, pid_t tid,
const char *msg, unsigned short len);
uint64_t flushTo(SocketClient *writer, const uint64_t start,
- bool privileged,
+ bool privileged, bool security,
int (*filter)(const LogBufferElement *element, void *arg) = NULL,
void *arg = NULL);
diff --git a/logd/LogReader.cpp b/logd/LogReader.cpp
index c2d65b6..667a3f2 100644
--- a/logd/LogReader.cpp
+++ b/logd/LogReader.cpp
@@ -163,6 +163,7 @@
logbuf().isMonotonic() && android::isMonotonic(start));
logbuf().flushTo(cli, sequence, FlushCommand::hasReadLogs(cli),
+ FlushCommand::hasSecurityLogs(cli),
logFindStart.callback, &logFindStart);
if (!logFindStart.found()) {
diff --git a/logd/LogTimes.cpp b/logd/LogTimes.cpp
index b4c97a9..a4b96d3 100644
--- a/logd/LogTimes.cpp
+++ b/logd/LogTimes.cpp
@@ -126,6 +126,7 @@
LogBuffer &logbuf = me->mReader.logbuf();
bool privileged = FlushCommand::hasReadLogs(client);
+ bool security = FlushCommand::hasSecurityLogs(client);
me->leadingDropped = true;
@@ -150,10 +151,10 @@
unlock();
if (me->mTail) {
- logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
+ logbuf.flushTo(client, start, privileged, security, FilterFirstPass, me);
me->leadingDropped = true;
}
- start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
+ start = logbuf.flushTo(client, start, privileged, security, FilterSecondPass, me);
lock();
diff --git a/logd/logd.rc b/logd/logd.rc
index 10f3553..31ed4df 100644
--- a/logd/logd.rc
+++ b/logd/logd.rc
@@ -1,5 +1,4 @@
service logd /system/bin/logd
- class core
socket logd stream 0666 logd logd
socket logdr seqpacket 0666 logd logd
socket logdw dgram 0222 logd logd
diff --git a/metricsd/metrics_collector.cc b/metricsd/metrics_collector.cc
index c3f42dc..45ae0a4 100644
--- a/metricsd/metrics_collector.cc
+++ b/metricsd/metrics_collector.cc
@@ -59,7 +59,8 @@
// Interval between calls to UpdateStats().
const uint32_t kUpdateStatsIntervalMs = 300000;
-const char kKernelCrashDetectedFile[] = "/var/run/kernel-crash-detected";
+const char kKernelCrashDetectedFile[] =
+ "/data/misc/crash_reporter/run/kernel-crash-detected";
const char kUncleanShutdownDetectedFile[] =
"/var/run/unclean-shutdown-detected";
diff --git a/rootdir/init.rc b/rootdir/init.rc
index e400c85..1f63fcf 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -201,6 +201,10 @@
# enable armv8_deprecated instruction hooks
write /proc/sys/abi/swp 1
+ # Linux's execveat() syscall may construct paths containing /dev/fd
+ # expecting it to point to /proc/self/fd
+ symlink /proc/self/fd /dev/fd
+
# Healthd can trigger a full boot from charger mode by signaling this
# property when the power button is held.
on property:sys.boot_from_charger_mode=1
@@ -293,8 +297,11 @@
# We restorecon /data in case the userdata partition has been reset.
restorecon /data
- # Make sure we have the device encryption key
- start logd
+ # start debuggerd to make debugging early-boot crashes easier.
+ start debuggerd
+ start debuggerd64
+
+ # Make sure we have the device encryption key.
start vold
installkey /data