Add WriteFdFmt and clean up more code.

Also say *which* device wasn't found.

Bug: http://b/20666660
Change-Id: I50e234ad89e39ae0a8995083c0b642c61275c5a3
(cherry picked from commit ab52c181fa)
This commit is contained in:
Elliott Hughes 2015-05-01 17:04:38 -07:00
parent 92af733ee2
commit e1a55004e9
14 changed files with 110 additions and 158 deletions

View File

@ -89,10 +89,8 @@ void start_device_log(void) {
char timestamp[PATH_MAX];
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);
char path[PATH_MAX];
snprintf(path, sizeof(path), "/data/adb/adb-%s-%d", timestamp, getpid());
int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
std::string path = android::base::StringPrintf("/data/adb/adb-%s-%d", timestamp, getpid());
int fd = unix_open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
if (fd == -1) {
return;
}

View File

@ -48,7 +48,6 @@ static std::string perror_str(const char* msg) {
static bool ReadProtocolString(int fd, std::string* s, std::string* error) {
char buf[5];
if (!ReadFdExactly(fd, buf, 4)) {
*error = perror_str("protocol fault (couldn't read status length)");
return false;
@ -154,7 +153,6 @@ static int switch_socket_transport(int fd, std::string* error) {
bool adb_status(int fd, std::string* error) {
char buf[5];
if (!ReadFdExactly(fd, buf, 4)) {
*error = perror_str("protocol fault (couldn't read status)");
return false;

View File

@ -16,13 +16,15 @@
#define TRACE_TAG TRACE_RWX
#include "sysdeps.h"
#include "adb_io.h"
#include <unistd.h>
#include <base/stringprintf.h>
#include "adb_trace.h"
#include "adb_utils.h"
#include "sysdeps.h"
bool SendProtocolString(int fd, const std::string& s) {
int length = s.size();
@ -30,9 +32,7 @@ bool SendProtocolString(int fd, const std::string& s) {
length = 0xffff;
}
char buf[5];
snprintf(buf, sizeof(buf), "%04x", length);
return WriteFdExactly(fd, buf, 4) && WriteFdExactly(fd, s);
return WriteFdFmt(fd, "%04x", length) && WriteFdExactly(fd, s);
}
bool SendOkay(int fd) {
@ -111,6 +111,13 @@ bool WriteFdExactly(int fd, const std::string& str) {
return WriteFdExactly(fd, str.c_str(), str.size());
}
bool WriteStringFully(int fd, const char* str) {
return WriteFdExactly(fd, str, strlen(str));
bool WriteFdFmt(int fd, const char* fmt, ...) {
std::string str;
va_list ap;
va_start(ap, fmt);
android::base::StringAppendV(&str, fmt, ap);
va_end(ap);
return WriteFdExactly(fd, str);
}

View File

@ -49,11 +49,11 @@ bool ReadFdExactly(int fd, void *buf, size_t len);
*/
bool WriteFdExactly(int fd, const void* buf, size_t len);
/* Same as above, but with an implicit len = strlen(buf). */
// Same as above, but for strings.
bool WriteFdExactly(int fd, const char* s);
bool WriteFdExactly(int fd, const std::string& s);
// TODO: move minadb off this and remove it.
bool WriteStringFully(int fd, const char* str);
// Same as above, but formats the string to send.
bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
#endif /* ADB_IO_H */

View File

@ -152,3 +152,16 @@ TEST(io, WriteFdExactly_string) {
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
EXPECT_STREQ(str, s.c_str());
}
TEST(io, WriteFdFmt) {
TemporaryFile tf;
ASSERT_NE(-1, tf.fd);
// Test writing a partial string to the file.
ASSERT_TRUE(WriteFdFmt(tf.fd, "Foo%s%d", "bar", 123)) << strerror(errno);
ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0));
std::string s;
ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s));
EXPECT_STREQ("Foobar123", s.c_str());
}

View File

@ -185,13 +185,13 @@ void remove_all_listeners(void)
}
}
install_status_t install_listener(const char *local_name,
install_status_t install_listener(const std::string& local_name,
const char *connect_to,
atransport* transport,
int no_rebind)
{
for (alistener* l = listener_list.next; l != &listener_list; l = l->next) {
if (strcmp(local_name, l->local_name) == 0) {
if (local_name == l->local_name) {
char* cto;
/* can't repurpose a smartsocket */
@ -226,7 +226,7 @@ install_status_t install_listener(const char *local_name,
goto nomem;
}
listener->local_name = strdup(local_name);
listener->local_name = strdup(local_name.c_str());
if (listener->local_name == nullptr) {
goto nomem;
}
@ -236,9 +236,9 @@ install_status_t install_listener(const char *local_name,
goto nomem;
}
listener->fd = local_name_to_fd(local_name);
listener->fd = local_name_to_fd(listener->local_name);
if (listener->fd < 0) {
printf("cannot bind '%s': %s\n", local_name, strerror(errno));
printf("cannot bind '%s': %s\n", listener->local_name, strerror(errno));
free(listener->local_name);
free(listener->connect_to);
free(listener);

View File

@ -36,8 +36,8 @@ void listener_disconnect(void* _l, atransport* t);
void listener_event_func(int _fd, unsigned ev, void *_l);
void ss_listener_event_func(int _fd, unsigned ev, void *_l);
install_status_t install_listener(const char *local_name,
const char *connect_to,
install_status_t install_listener(const std::string& local_name,
const char* connect_to,
atransport* transport,
int no_rebind);

View File

@ -28,6 +28,8 @@
#include "adb_listeners.h"
#include "transport.h"
#include <base/stringprintf.h>
#if !ADB_HOST
#include <getopt.h>
#include <sys/prctl.h>
@ -157,16 +159,6 @@ static bool should_drop_privileges() {
}
#endif /* ADB_HOST */
/* Constructs a local name of form tcp:port.
* target_str points to the target string, it's content will be overwritten.
* target_size is the capacity of the target string.
* server_port is the port number to use for the local name.
*/
void build_local_name(char* target_str, size_t target_size, int server_port)
{
snprintf(target_str, target_size, "tcp:%d", server_port);
}
void start_logging(void)
{
#if defined(_WIN32)
@ -238,9 +230,8 @@ int adb_main(int is_daemon, int server_port)
local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
adb_auth_init();
char local_name[30];
build_local_name(local_name, sizeof(local_name), server_port);
if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
if (install_listener(local_name, "*smartsocket*", NULL, 0)) {
exit(1);
}
#else
@ -295,15 +286,14 @@ int adb_main(int is_daemon, int server_port)
D("Local port disabled\n");
} else {
char local_name[30];
if ((root_seclabel != NULL) && (is_selinux_enabled() > 0)) {
// b/12587913: fix setcon to allow const pointers
if (setcon((char *)root_seclabel) < 0) {
exit(1);
}
}
build_local_name(local_name, sizeof(local_name), server_port);
if(install_listener(local_name, "*smartsocket*", NULL, 0)) {
std::string local_name = android::base::StringPrintf("tcp:%d", server_port);
if (install_listener(local_name, "*smartsocket*", NULL, 0)) {
exit(1);
}
}

View File

@ -929,9 +929,7 @@ static int adb_query_command(const std::string& command) {
return 0;
}
int adb_commandline(int argc, const char **argv)
{
char buf[4096];
int adb_commandline(int argc, const char **argv) {
int no_daemon = 0;
int is_daemon = 0;
int is_server = 0;
@ -1266,6 +1264,7 @@ int adb_commandline(int argc, const char **argv)
}
/* adb_command() wrapper commands */
else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
std::string cmd;
char host_prefix[64];
char reverse = (char) !strcmp(argv[0], "reverse");
char remove = 0;
@ -1323,31 +1322,27 @@ int adb_commandline(int argc, const char **argv)
// Implement forward --remove-all
else if (remove_all) {
if (argc != 1)
return usage();
snprintf(buf, sizeof buf, "%s:killforward-all", host_prefix);
if (argc != 1) return usage();
cmd = android::base::StringPrintf("%s:killforward-all", host_prefix);
}
// Implement forward --remove <local>
else if (remove) {
if (argc != 2)
return usage();
snprintf(buf, sizeof buf, "%s:killforward:%s", host_prefix, argv[1]);
if (argc != 2) return usage();
cmd = android::base::StringPrintf("%s:killforward:%s", host_prefix, argv[1]);
}
// Or implement one of:
// forward <local> <remote>
// forward --no-rebind <local> <remote>
else
{
if (argc != 3)
return usage();
const char* command = no_rebind ? "forward:norebind" : "forward";
snprintf(buf, sizeof buf, "%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]);
else {
if (argc != 3) return usage();
const char* command = no_rebind ? "forward:norebind" : "forward";
cmd = android::base::StringPrintf("%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]);
}
std::string error;
if (adb_command(buf, &error)) {
fprintf(stderr,"error: %s\n", error.c_str());
if (adb_command(cmd, &error)) {
fprintf(stderr, "error: %s\n", error.c_str());
return 1;
}
return 0;

View File

@ -90,9 +90,7 @@ static bool remount_partition(int fd, const char* partition, int* ro) {
return true;
}
if (remount(partition, ro)) {
char buf[200];
snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno));
WriteFdExactly(fd, buf);
WriteFdFmt(fd, "remount of %s failed: %s\n", partition, strerror(errno));
return false;
}
return true;
@ -121,14 +119,12 @@ void remount_service(int fd, void* cookie) {
if (system_verified || vendor_verified) {
// Allow remount but warn of likely bad effects
bool both = system_verified && vendor_verified;
char buffer[200];
snprintf(buffer, sizeof(buffer),
"dm_verity is enabled on the %s%s%s partition%s.\n",
system_verified ? "system" : "",
both ? " and " : "",
vendor_verified ? "vendor" : "",
both ? "s" : "");
WriteFdExactly(fd, buffer);
WriteFdFmt(fd,
"dm_verity is enabled on the %s%s%s partition%s.\n",
system_verified ? "system" : "",
both ? " and " : "",
vendor_verified ? "vendor" : "",
both ? "s" : "");
WriteFdExactly(fd,
"Use \"adb disable-verity\" to disable verity.\n"
"If you do not, remount may succeed, however, you will still "

View File

@ -63,74 +63,54 @@ void *service_bootstrap_func(void *x)
#if !ADB_HOST
void restart_root_service(int fd, void *cookie)
{
char buf[100];
char value[PROPERTY_VALUE_MAX];
void restart_root_service(int fd, void *cookie) {
if (getuid() == 0) {
snprintf(buf, sizeof(buf), "adbd is already running as root\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdExactly(fd, "adbd is already running as root\n");
adb_close(fd);
} else {
char value[PROPERTY_VALUE_MAX];
property_get("ro.debuggable", value, "");
if (strcmp(value, "1") != 0) {
snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdExactly(fd, "adbd cannot run as root in production builds\n");
adb_close(fd);
return;
}
property_set("service.adb.root", "1");
snprintf(buf, sizeof(buf), "restarting adbd as root\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdExactly(fd, "restarting adbd as root\n");
adb_close(fd);
}
}
void restart_unroot_service(int fd, void *cookie)
{
char buf[100];
void restart_unroot_service(int fd, void *cookie) {
if (getuid() != 0) {
snprintf(buf, sizeof(buf), "adbd not running as root\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdExactly(fd, "adbd not running as root\n");
adb_close(fd);
} else {
property_set("service.adb.root", "0");
snprintf(buf, sizeof(buf), "restarting adbd as non root\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdExactly(fd, "restarting adbd as non root\n");
adb_close(fd);
}
}
void restart_tcp_service(int fd, void *cookie)
{
char buf[100];
char value[PROPERTY_VALUE_MAX];
void restart_tcp_service(int fd, void *cookie) {
int port = (int) (uintptr_t) cookie;
if (port <= 0) {
snprintf(buf, sizeof(buf), "invalid port\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdFmt(fd, "invalid port %d\n", port);
adb_close(fd);
return;
}
char value[PROPERTY_VALUE_MAX];
snprintf(value, sizeof(value), "%d", port);
property_set("service.adb.tcp.port", value);
snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
WriteFdExactly(fd, buf, strlen(buf));
WriteFdFmt(fd, "restarting in TCP mode port: %d\n", port);
adb_close(fd);
}
void restart_usb_service(int fd, void *cookie)
{
char buf[100];
void restart_usb_service(int fd, void *cookie) {
property_set("service.adb.tcp.port", "0");
snprintf(buf, sizeof(buf), "restarting in USB mode\n");
WriteFdExactly(fd, buf, strlen(buf));
WriteFdExactly(fd, "restarting in USB mode\n");
adb_close(fd);
}
@ -143,7 +123,6 @@ static bool reboot_service_impl(int fd, const char* arg) {
reboot_arg = "sideload";
}
char buf[100];
// It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
// in the command file.
if (strcmp(reboot_arg, "sideload") == 0) {
@ -174,15 +153,13 @@ static bool reboot_service_impl(int fd, const char* arg) {
char property_val[PROPERTY_VALUE_MAX];
int ret = snprintf(property_val, sizeof(property_val), "reboot,%s", reboot_arg);
if (ret >= static_cast<int>(sizeof(property_val))) {
snprintf(buf, sizeof(buf), "reboot string too long. length=%d\n", ret);
WriteFdExactly(fd, buf);
WriteFdFmt(fd, "reboot string too long: %d\n", ret);
return false;
}
ret = property_set(ANDROID_RB_PROPERTY, property_val);
if (ret < 0) {
snprintf(buf, sizeof(buf), "reboot failed: %d\n", ret);
WriteFdExactly(fd, buf);
WriteFdFmt(fd, "reboot failed: %d\n", ret);
return false;
}

View File

@ -27,6 +27,7 @@
#include "cutils/properties.h"
#include "adb.h"
#include "adb_io.h"
#include "ext4_sb.h"
#include "fs_mgr.h"
#include "remount_service.h"
@ -40,18 +41,6 @@ static const bool kAllowDisableVerity = true;
static const bool kAllowDisableVerity = false;
#endif
__attribute__((__format__(printf, 2, 3))) __nonnull((2))
static void write_console(int fd, const char* format, ...)
{
char buffer[256];
va_list args;
va_start (args, format);
vsnprintf (buffer, sizeof(buffer), format, args);
va_end (args);
adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
}
static int get_target_device_size(int fd, const char *blk_device,
uint64_t *device_size)
{
@ -63,18 +52,18 @@ static int get_target_device_size(int fd, const char *blk_device,
data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
if (data_device < 0) {
write_console(fd, "Error opening block device (%s)\n", strerror(errno));
WriteFdFmt(fd, "Error opening block device (%s)\n", strerror(errno));
return -1;
}
if (lseek64(data_device, 1024, SEEK_SET) < 0) {
write_console(fd, "Error seeking to superblock\n");
WriteFdFmt(fd, "Error seeking to superblock\n");
adb_close(data_device);
return -1;
}
if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
write_console(fd, "Error reading superblock\n");
WriteFdFmt(fd, "Error reading superblock\n");
adb_close(data_device);
return -1;
}
@ -98,73 +87,64 @@ static int set_verity_enabled_state(int fd, const char *block_device,
int retval = -1;
if (make_block_device_writable(block_device)) {
write_console(fd, "Could not make block device %s writable (%s).\n",
block_device, strerror(errno));
WriteFdFmt(fd, "Could not make block device %s writable (%s).\n",
block_device, strerror(errno));
goto errout;
}
device = adb_open(block_device, O_RDWR | O_CLOEXEC);
if (device == -1) {
write_console(fd, "Could not open block device %s (%s).\n",
block_device, strerror(errno));
write_console(fd, "Maybe run adb remount?\n");
WriteFdFmt(fd, "Could not open block device %s (%s).\n", block_device, strerror(errno));
WriteFdFmt(fd, "Maybe run adb remount?\n");
goto errout;
}
// find the start of the verity metadata
if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
write_console(fd, "Could not get target device size.\n");
WriteFdFmt(fd, "Could not get target device size.\n");
goto errout;
}
if (lseek64(device, device_length, SEEK_SET) < 0) {
write_console(fd,
"Could not seek to start of verity metadata block.\n");
WriteFdFmt(fd, "Could not seek to start of verity metadata block.\n");
goto errout;
}
// check the magic number
if (adb_read(device, &magic_number, sizeof(magic_number))
!= sizeof(magic_number)) {
write_console(fd, "Couldn't read magic number!\n");
if (adb_read(device, &magic_number, sizeof(magic_number)) != sizeof(magic_number)) {
WriteFdFmt(fd, "Couldn't read magic number!\n");
goto errout;
}
if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
write_console(fd, "Verity already disabled on %s\n", mount_point);
WriteFdFmt(fd, "Verity already disabled on %s\n", mount_point);
goto errout;
}
if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
write_console(fd, "Verity already enabled on %s\n", mount_point);
WriteFdFmt(fd, "Verity already enabled on %s\n", mount_point);
goto errout;
}
if (magic_number != VERITY_METADATA_MAGIC_NUMBER
&& magic_number != VERITY_METADATA_MAGIC_DISABLE) {
write_console(fd,
"Couldn't find verity metadata at offset %" PRIu64 "!\n",
device_length);
WriteFdFmt(fd, "Couldn't find verity metadata at offset %" PRIu64 "!\n", device_length);
goto errout;
}
if (lseek64(device, device_length, SEEK_SET) < 0) {
write_console(fd,
"Could not seek to start of verity metadata block.\n");
WriteFdFmt(fd, "Could not seek to start of verity metadata block.\n");
goto errout;
}
if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
write_console(
fd, "Could not set verity %s flag on device %s with error %s\n",
enable ? "enabled" : "disabled",
block_device, strerror(errno));
WriteFdFmt(fd, "Could not set verity %s flag on device %s with error %s\n",
enable ? "enabled" : "disabled",
block_device, strerror(errno));
goto errout;
}
write_console(fd, "Verity %s on %s\n",
enable ? "enabled" : "disabled",
mount_point);
WriteFdFmt(fd, "Verity %s on %s\n", enable ? "enabled" : "disabled", mount_point);
retval = 0;
errout:
if (device != -1)
@ -183,14 +163,13 @@ void set_verity_enabled_state_service(int fd, void* cookie)
property_get("ro.secure", propbuf, "0");
if (strcmp(propbuf, "1")) {
write_console(fd, "verity not enabled - ENG build\n");
WriteFdFmt(fd, "verity not enabled - ENG build\n");
goto errout;
}
property_get("ro.debuggable", propbuf, "0");
if (strcmp(propbuf, "1")) {
write_console(
fd, "verity cannot be disabled/enabled - USER build\n");
WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
goto errout;
}
@ -200,8 +179,7 @@ void set_verity_enabled_state_service(int fd, void* cookie)
fstab = fs_mgr_read_fstab(fstab_filename);
if (!fstab) {
write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
fstab_filename);
WriteFdFmt(fd, "Failed to open %s\nMaybe run adb root?\n", fstab_filename);
goto errout;
}
@ -217,12 +195,11 @@ void set_verity_enabled_state_service(int fd, void* cookie)
}
if (any_changed) {
write_console(
fd, "Now reboot your device for settings to take effect\n");
WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
}
} else {
write_console(fd, "%s-verity only works for userdebug builds\n",
enable ? "enable" : "disable");
WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
enable ? "enable" : "disable");
}
errout:

View File

@ -748,7 +748,7 @@ atransport* acquire_one_transport(int state, transport_type ttype,
int ambiguous = 0;
retry:
if (error_out) *error_out = "device not found";
if (error_out) *error_out = android::base::StringPrintf("device '%s' not found", serial);
adb_mutex_lock(&transport_lock);
for (t = transport_list.next; t != &transport_list; t = t->next) {

View File

@ -25,6 +25,8 @@
#include <string.h>
#include <sys/types.h>
#include <base/stringprintf.h>
#if !ADB_HOST
#include "cutils/properties.h"
#endif
@ -88,7 +90,6 @@ int local_connect(int port) {
int local_connect_arbitrary_ports(int console_port, int adb_port)
{
char buf[64];
int fd = -1;
#if ADB_HOST
@ -105,8 +106,8 @@ int local_connect_arbitrary_ports(int console_port, int adb_port)
D("client: connected on remote on fd %d\n", fd);
close_on_exec(fd);
disable_tcp_nagle(fd);
snprintf(buf, sizeof buf, "emulator-%d", console_port);
register_socket_transport(fd, buf, adb_port, 1);
std::string serial = android::base::StringPrintf("emulator-%d", console_port);
register_socket_transport(fd, serial.c_str(), adb_port, 1);
return 0;
}
return -1;