diff options
Diffstat (limited to 'adb')
-rw-r--r-- | adb/Android.mk | 89 | ||||
-rw-r--r-- | adb/adb.cpp (renamed from adb/adb.c) | 30 | ||||
-rw-r--r-- | adb/adb.h | 9 | ||||
-rw-r--r-- | adb/adb_auth.cpp (renamed from adb/adb_auth.c) | 0 | ||||
-rw-r--r-- | adb/adb_auth.h | 14 | ||||
-rw-r--r-- | adb/adb_auth_client.cpp (renamed from adb/adb_auth_client.c) | 37 | ||||
-rw-r--r-- | adb/adb_auth_host.cpp (renamed from adb/adb_auth_host.c) | 41 | ||||
-rw-r--r-- | adb/adb_client.cpp (renamed from adb/adb_client.c) | 9 | ||||
-rw-r--r-- | adb/adb_client.h | 2 | ||||
-rw-r--r-- | adb/adb_io.cpp | 4 | ||||
-rw-r--r-- | adb/adb_io_test.cpp | 14 | ||||
-rw-r--r-- | adb/adb_listeners.cpp (renamed from adb/adb_listeners.c) | 94 | ||||
-rw-r--r-- | adb/adb_main.cpp (renamed from adb/adb_main.c) | 8 | ||||
-rw-r--r-- | adb/commandline.cpp (renamed from adb/commandline.c) | 158 | ||||
-rw-r--r-- | adb/console.cpp (renamed from adb/console.c) | 2 | ||||
-rw-r--r-- | adb/file_sync_client.cpp (renamed from adb/file_sync_client.c) | 17 | ||||
-rw-r--r-- | adb/file_sync_service.cpp (renamed from adb/file_sync_service.c) | 22 | ||||
-rw-r--r-- | adb/framebuffer_service.cpp (renamed from adb/framebuffer_service.c) | 3 | ||||
-rw-r--r-- | adb/get_my_path_darwin.c | 2 | ||||
-rw-r--r-- | adb/get_my_path_linux.cpp (renamed from adb/get_my_path_linux.c) | 6 | ||||
-rw-r--r-- | adb/get_my_path_windows.cpp (renamed from adb/get_my_path_windows.c) | 4 | ||||
-rw-r--r-- | adb/jdwp_service.cpp (renamed from adb/jdwp_service.c) | 9 | ||||
-rw-r--r-- | adb/qemu_tracing.cpp (renamed from adb/qemu_tracing.c) | 0 | ||||
-rw-r--r-- | adb/remount_service.cpp (renamed from adb/remount_service.c) | 109 | ||||
-rw-r--r-- | adb/remount_service.h | 25 | ||||
-rw-r--r-- | adb/services.cpp (renamed from adb/services.c) | 30 | ||||
-rw-r--r-- | adb/set_verity_enable_state_service.cpp (renamed from adb/set_verity_enable_state_service.c) | 5 | ||||
-rw-r--r-- | adb/sockets.cpp (renamed from adb/sockets.c) | 103 | ||||
-rw-r--r-- | adb/test_track_devices.cpp (renamed from adb/test_track_devices.c) | 0 | ||||
-rw-r--r-- | adb/test_track_jdwp.cpp (renamed from adb/test_track_jdwp.c) | 0 | ||||
-rwxr-xr-x | adb/tests/test_adb.py | 24 | ||||
-rw-r--r-- | adb/transport.cpp (renamed from adb/transport.c) | 18 | ||||
-rw-r--r-- | adb/transport.h | 2 | ||||
-rw-r--r-- | adb/transport_local.cpp (renamed from adb/transport_local.c) | 0 | ||||
-rw-r--r-- | adb/transport_usb.cpp (renamed from adb/transport_usb.c) | 0 | ||||
-rw-r--r-- | adb/usb_linux.cpp (renamed from adb/usb_linux.c) | 9 | ||||
-rw-r--r-- | adb/usb_windows.cpp (renamed from adb/usb_windows.c) | 0 |
37 files changed, 491 insertions, 408 deletions
diff --git a/adb/Android.mk b/adb/Android.mk index adad69f..8d38077 100644 --- a/adb/Android.mk +++ b/adb/Android.mk @@ -17,14 +17,14 @@ ADB_CLANG := # small by moving common files into a static library. Hopefully some day we can # get enough of adb in here that we no longer need minadb. https://b/17626262 LIBADB_SRC_FILES := \ - adb.c \ - adb_auth.c \ + adb.cpp \ + adb_auth.cpp \ adb_io.cpp \ - adb_listeners.c \ - sockets.c \ - transport.c \ - transport_local.c \ - transport_usb.c \ + adb_listeners.cpp \ + sockets.cpp \ + transport.cpp \ + transport_local.cpp \ + transport_usb.cpp \ LIBADB_CFLAGS := \ -Wall -Werror \ @@ -32,9 +32,20 @@ LIBADB_CFLAGS := \ -Wno-missing-field-initializers \ -fvisibility=hidden \ -LIBADB_darwin_SRC_FILES := fdevent.cpp get_my_path_darwin.c usb_osx.c -LIBADB_linux_SRC_FILES := fdevent.cpp get_my_path_linux.c usb_linux.c -LIBADB_windows_SRC_FILES := get_my_path_windows.c sysdeps_win32.c usb_windows.c +LIBADB_darwin_SRC_FILES := \ + fdevent.cpp \ + get_my_path_darwin.c \ + usb_osx.c \ + +LIBADB_linux_SRC_FILES := \ + fdevent.cpp \ + get_my_path_linux.cpp \ + usb_linux.cpp \ + +LIBADB_windows_SRC_FILES := \ + get_my_path_windows.cpp \ + sysdeps_win32.c \ + usb_windows.cpp \ include $(CLEAR_VARS) LOCAL_CLANG := $(ADB_CLANG) @@ -42,10 +53,10 @@ LOCAL_MODULE := libadbd LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST=0 LOCAL_SRC_FILES := \ $(LIBADB_SRC_FILES) \ - adb_auth_client.c \ + adb_auth_client.cpp \ fdevent.cpp \ - jdwp_service.c \ - qemu_tracing.c \ + jdwp_service.cpp \ + qemu_tracing.cpp \ usb_linux_client.c \ include $(BUILD_STATIC_LIBRARY) @@ -57,7 +68,7 @@ LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST=1 LOCAL_SRC_FILES := \ $(LIBADB_SRC_FILES) \ $(LIBADB_$(HOST_OS)_SRC_FILES) \ - adb_auth_host.c \ + adb_auth_host.cpp \ # Even though we're building a static library (and thus there's no link step for # this to take effect), this adds the SSL includes to our path. @@ -79,20 +90,19 @@ LOCAL_MODULE := adbd_test LOCAL_CFLAGS := -DADB_HOST=0 $(LIBADB_CFLAGS) LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) LOCAL_STATIC_LIBRARIES := libadbd -LOCAL_SHARED_LIBRARIES := liblog libcutils libutils +LOCAL_SHARED_LIBRARIES := liblog libbase libcutils include $(BUILD_NATIVE_TEST) include $(CLEAR_VARS) LOCAL_CLANG := $(ADB_CLANG) LOCAL_MODULE := adb_test LOCAL_CFLAGS := -DADB_HOST=1 $(LIBADB_CFLAGS) -LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.c -LOCAL_SHARED_LIBRARIES := liblog +LOCAL_SRC_FILES := $(LIBADB_TEST_SRCS) services.cpp +LOCAL_SHARED_LIBRARIES := liblog libbase LOCAL_STATIC_LIBRARIES := \ libadb \ libcrypto_static \ libcutils \ - libutils \ ifeq ($(HOST_OS),linux) LOCAL_LDLIBS += -lrt -ldl -lpthread @@ -126,16 +136,12 @@ endif LOCAL_CLANG := $(ADB_CLANG) LOCAL_SRC_FILES := \ - adb_main.c \ - console.c \ - commandline.c \ - adb_client.c \ - services.c \ - file_sync_client.c \ - -ifneq ($(USE_SYSDEPS_WIN32),) - LOCAL_SRC_FILES += sysdeps_win32.c -endif + adb_main.cpp \ + console.cpp \ + commandline.cpp \ + adb_client.cpp \ + services.cpp \ + file_sync_client.cpp \ LOCAL_CFLAGS += \ -Wall -Werror \ @@ -148,12 +154,11 @@ LOCAL_MODULE_TAGS := debug LOCAL_STATIC_LIBRARIES := \ libadb \ - libzipfile \ libcrypto_static \ $(EXTRA_STATIC_LIBS) \ ifeq ($(USE_SYSDEPS_WIN32),) - LOCAL_STATIC_LIBRARIES += libcutils + LOCAL_STATIC_LIBRARIES += libcutils endif LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk @@ -176,25 +181,25 @@ include $(CLEAR_VARS) LOCAL_CLANG := $(ADB_CLANG) LOCAL_SRC_FILES := \ - adb_main.c \ - services.c \ - file_sync_service.c \ - framebuffer_service.c \ - remount_service.c \ - set_verity_enable_state_service.c \ + adb_main.cpp \ + services.cpp \ + file_sync_service.cpp \ + framebuffer_service.cpp \ + remount_service.cpp \ + set_verity_enable_state_service.cpp \ LOCAL_CFLAGS := \ - -O2 \ - -g \ - -DADB_HOST=0 \ - -D_GNU_SOURCE \ - -Wall -Wno-unused-parameter -Werror -Wno-deprecated-declarations \ + -DADB_HOST=0 \ + -D_GNU_SOURCE \ + -Wall -Werror \ + -Wno-unused-parameter \ + -Wno-deprecated-declarations \ ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT))) LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1 endif -ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT))) +ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT))) LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1 endif @@ -140,10 +140,13 @@ void adb_trace_init(void) } } -apacket *get_apacket(void) +apacket* get_apacket(void) { - apacket *p = malloc(sizeof(apacket)); - if(p == 0) fatal("failed to allocate an apacket"); + apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket))); + if (p == nullptr) { + fatal("failed to allocate an apacket"); + } + memset(p, 0, sizeof(apacket) - MAX_PAYLOAD); return p; } @@ -293,7 +296,7 @@ void send_connect(atransport *t) } #if ADB_HOST -static char *connection_state_name(atransport *t) +static const char* connection_state_name(atransport *t) { if (t == NULL) { return "unknown"; @@ -713,8 +716,8 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri // Create the list of forward redirections. int buffer_size = format_listeners(NULL, 0); // Add one byte for the trailing zero. - char* buffer = malloc(buffer_size + 1); - if (buffer == NULL) { + char* buffer = reinterpret_cast<char*>(malloc(buffer_size + 1)); + if (buffer == nullptr) { sendfailmsg(reply_fd, "not enough memory"); return 1; } @@ -740,7 +743,7 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri if (!strncmp(service, "forward:",8) || !strncmp(service, "killforward:",12)) { - char *local, *remote, *err; + char *local, *remote; int r; atransport *transport; @@ -777,6 +780,7 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri } } + const char* err; transport = acquire_one_transport(CS_ANY, ttype, serial, &err); if (!transport) { sendfailmsg(reply_fd, err); @@ -835,7 +839,6 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r // "transport-local:" is used for switching transport to the only local transport // "transport-any:" is used for switching transport to the only transport if (!strncmp(service, "transport", strlen("transport"))) { - char* error_string = "unknown failure"; transport_type type = kTransportAny; if (!strncmp(service, "transport-usb", strlen("transport-usb"))) { @@ -849,6 +852,7 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r serial = service; } + const char* error_string = "unknown failure"; transport = acquire_one_transport(CS_ANY, type, serial, &error_string); if (transport) { @@ -911,8 +915,8 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r } if(!strncmp(service,"get-serialno",strlen("get-serialno"))) { - char *out = "unknown"; - transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); + const char *out = "unknown"; + transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); if (transport && transport->serial) { out = transport->serial; } @@ -920,8 +924,8 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r return 0; } if(!strncmp(service,"get-devpath",strlen("get-devpath"))) { - char *out = "unknown"; - transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); + const char *out = "unknown"; + transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); if (transport && transport->devpath) { out = transport->devpath; } @@ -938,7 +942,7 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r if(!strncmp(service,"get-state",strlen("get-state"))) { transport = acquire_one_transport(CS_ANY, ttype, serial, NULL); - char *state = connection_state_name(transport); + const char *state = connection_state_name(transport); send_msg_with_okay(reply_fd, state, strlen(state)); return 0; } @@ -239,8 +239,8 @@ struct alistener fdevent fde; int fd; - const char *local_name; - const char *connect_to; + char *local_name; + char *connect_to; atransport *transport; adisconnect disconnect; }; @@ -298,9 +298,6 @@ int handle_forward_request(const char* service, transport_type ttype, char* seri #if !ADB_HOST void framebuffer_service(int fd, void *cookie); -// Allow enable-verity to write to system and vendor block devices -int make_block_device_writable(const char* dev); -void remount_service(int fd, void *cookie); void set_verity_enabled_state_service(int fd, void* cookie); #endif @@ -348,7 +345,7 @@ void usb_kick(usb_handle *h); int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); #endif -int adb_commandline(int argc, char **argv); +int adb_commandline(int argc, const char **argv); int connection_state(atransport *t); diff --git a/adb/adb_auth.c b/adb/adb_auth.cpp index c236b64..c236b64 100644 --- a/adb/adb_auth.c +++ b/adb/adb_auth.cpp diff --git a/adb/adb_auth.h b/adb/adb_auth.h index cece5e3..e0425ad 100644 --- a/adb/adb_auth.h +++ b/adb/adb_auth.h @@ -23,7 +23,6 @@ extern "C" { extern int auth_enabled; -void adb_auth_init(void); int adb_auth_keygen(const char* filename); void adb_auth_verified(atransport *t); @@ -40,7 +39,9 @@ void send_auth_publickey(atransport *t); #if ADB_HOST -int adb_auth_sign(void *key, void *token, size_t token_size, void *sig); +void adb_auth_init(void); +int adb_auth_sign(void *key, const unsigned char* token, size_t token_size, + unsigned char* sig); void *adb_auth_nextkey(void *current); int adb_auth_get_userkey(unsigned char *data, size_t len); @@ -50,12 +51,17 @@ static inline void adb_auth_confirm_key(unsigned char *data, size_t len, atransp #else // !ADB_HOST -static inline int adb_auth_sign(void* key, void *token, size_t token_size, void *sig) { return 0; } +static inline int adb_auth_sign(void* key, const unsigned char* token, + size_t token_size, unsigned char* sig) { + return 0; +} static inline void *adb_auth_nextkey(void *current) { return NULL; } static inline int adb_auth_get_userkey(unsigned char *data, size_t len) { return 0; } +void adbd_auth_init(void); +void adbd_cloexec_auth_socket(); int adb_auth_generate_token(void *token, size_t token_size); -int adb_auth_verify(void *token, void *sig, int siglen); +int adb_auth_verify(uint8_t* token, uint8_t* sig, int siglen); void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t); #endif // ADB_HOST diff --git a/adb/adb_auth_client.c b/adb/adb_auth_client.cpp index 7be883c..5dadcd9 100644 --- a/adb/adb_auth_client.c +++ b/adb/adb_auth_client.cpp @@ -37,7 +37,7 @@ struct adb_public_key { RSAPublicKey key; }; -static char *key_paths[] = { +static const char *key_paths[] = { "/adb_keys", "/data/misc/adb/adb_keys", NULL @@ -53,7 +53,6 @@ static bool needs_retry = false; static void read_keys(const char *file, struct listnode *list) { - struct adb_public_key *key; FILE *f; char buf[MAX_PAYLOAD]; char *sep; @@ -67,8 +66,9 @@ static void read_keys(const char *file, struct listnode *list) while (fgets(buf, sizeof(buf), f)) { /* Allocate 4 extra bytes to decode the base64 data in-place */ - key = calloc(1, sizeof(*key) + 4); - if (!key) { + auto key = reinterpret_cast<adb_public_key*>( + calloc(1, sizeof(adb_public_key) + 4)); + if (key == nullptr) { D("Can't malloc key\n"); break; } @@ -109,8 +109,8 @@ static void free_keys(struct listnode *list) static void load_keys(struct listnode *list) { - char *path; - char **paths = key_paths; + const char* path; + const char** paths = key_paths; struct stat buf; list_init(list); @@ -138,10 +138,9 @@ int adb_auth_generate_token(void *token, size_t token_size) return ret * token_size; } -int adb_auth_verify(void *token, void *sig, int siglen) +int adb_auth_verify(uint8_t* token, uint8_t* sig, int siglen) { struct listnode *item; - struct adb_public_key *key; struct listnode key_list; int ret = 0; @@ -151,7 +150,7 @@ int adb_auth_verify(void *token, void *sig, int siglen) load_keys(&key_list); list_for_each(item, &key_list) { - key = node_to_item(item, struct adb_public_key, node); + adb_public_key* key = node_to_item(item, struct adb_public_key, node); ret = RSA_verify(&key->key, sig, siglen, token, SHA_DIGEST_SIZE); if (ret) break; @@ -250,19 +249,23 @@ static void adb_auth_listener(int fd, unsigned events, void *data) } } -void adb_auth_init(void) -{ - int fd, ret; - - fd = android_get_control_socket("adbd"); - if (fd < 0) { +void adbd_cloexec_auth_socket() { + int fd = android_get_control_socket("adbd"); + if (fd == -1) { D("Failed to get adbd socket\n"); return; } fcntl(fd, F_SETFD, FD_CLOEXEC); +} - ret = listen(fd, 4); - if (ret < 0) { +void adbd_auth_init(void) { + int fd = android_get_control_socket("adbd"); + if (fd == -1) { + D("Failed to get adbd socket\n"); + return; + } + + if (listen(fd, 4) == -1) { D("Failed to listen on '%d'\n", fd); return; } diff --git a/adb/adb_auth_host.c b/adb/adb_auth_host.cpp index 1d48667..aba23d4 100644 --- a/adb/adb_auth_host.c +++ b/adb/adb_auth_host.cpp @@ -157,7 +157,7 @@ static int write_public_keyfile(RSA *private_key, const char *private_key_path) RSAPublicKey pkey; FILE *outfile = NULL; char path[PATH_MAX], info[MAX_PAYLOAD]; - uint8_t *encoded = NULL; + uint8_t* encoded = nullptr; size_t encoded_length; int ret = 0; @@ -191,8 +191,8 @@ static int write_public_keyfile(RSA *private_key, const char *private_key_path) encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4); #endif - encoded = malloc(encoded_length); - if (encoded == NULL) { + encoded = reinterpret_cast<uint8_t*>(malloc(encoded_length)); + if (encoded == nullptr) { D("Allocation failure"); goto out; } @@ -272,18 +272,16 @@ out: static int read_key(const char *file, struct listnode *list) { - struct adb_private_key *key; - FILE *f; - D("read_key '%s'\n", file); - f = fopen(file, "r"); + FILE* f = fopen(file, "r"); if (!f) { D("Failed to open '%s'\n", file); return 0; } - key = malloc(sizeof(*key)); + adb_private_key* key = reinterpret_cast<adb_private_key*>( + malloc(sizeof(adb_private_key))); if (!key) { D("Failed to alloc key\n"); fclose(f); @@ -390,7 +388,8 @@ static void get_vendor_keys(struct listnode *list) } } -int adb_auth_sign(void *node, void *token, size_t token_size, void *sig) +int adb_auth_sign(void *node, const unsigned char* token, size_t token_size, + unsigned char* sig) { unsigned int len; struct adb_private_key *key = node_to_item(node, struct adb_private_key, node); @@ -433,31 +432,33 @@ void *adb_auth_nextkey(void *current) int adb_auth_get_userkey(unsigned char *data, size_t len) { char path[PATH_MAX]; - char *file; - int ret; - - ret = get_user_keyfilepath(path, sizeof(path) - 4); + int ret = get_user_keyfilepath(path, sizeof(path) - 4); if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) { D("Error getting user key filename"); return 0; } strcat(path, ".pub"); - file = load_file(path, (unsigned*)&ret); - if (!file) { + // TODO(danalbert): ReadFileToString + unsigned size; + char* file_data = reinterpret_cast<char*>(load_file(path, &size)); + if (file_data == nullptr) { D("Can't load '%s'\n", path); return 0; } - if (len < (size_t)(ret + 1)) { - D("%s: Content too large ret=%d\n", path, ret); + if (len < (size_t)(size + 1)) { + D("%s: Content too large ret=%d\n", path, size); + free(file_data); return 0; } - memcpy(data, file, ret); - data[ret] = '\0'; + memcpy(data, file_data, size); + free(file_data); + file_data = nullptr; + data[size] = '\0'; - return ret + 1; + return size + 1; } int adb_auth_keygen(const char* filename) { diff --git a/adb/adb_client.c b/adb/adb_client.cpp index 5d2bbd7..a485aa2 100644 --- a/adb/adb_client.c +++ b/adb/adb_client.cpp @@ -28,7 +28,6 @@ #define TRACE_TAG TRACE_ADB #include "adb_client.h" #include "adb_io.h" -#include "zipfile/zipfile.h" static transport_type __adb_transport = kTransportAny; static const char* __adb_serial = NULL; @@ -115,7 +114,7 @@ static int switch_socket_transport(int fd) if (__adb_serial) snprintf(service, sizeof service, "host:transport:%s", __adb_serial); else { - char* transport_type = "???"; + const char* transport_type = "???"; switch (__adb_transport) { case kTransportUsb: @@ -328,8 +327,8 @@ int adb_command(const char *service) char *adb_query(const char *service) { char buf[5]; - unsigned n; - char *tmp; + unsigned long n; + char* tmp; D("adb_query: %s\n", service); int fd = adb_connect(service); @@ -347,7 +346,7 @@ char *adb_query(const char *service) goto oops; } - tmp = malloc(n + 1); + tmp = reinterpret_cast<char*>(malloc(n + 1)); if(tmp == 0) goto oops; if(!ReadFdExactly(fd, tmp, n) == 0) { diff --git a/adb/adb_client.h b/adb/adb_client.h index 6ba3b38..9af176f 100644 --- a/adb/adb_client.h +++ b/adb/adb_client.h @@ -47,7 +47,7 @@ int adb_get_emulator_console_port(void); * is zero, or more than 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, char** argv); +int adb_send_emulator_command(int argc, const char** argv); /* return verbose error string from last operation */ const char *adb_error(void); diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp index ca208ad..4dd9f4d 100644 --- a/adb/adb_io.cpp +++ b/adb/adb_io.cpp @@ -33,7 +33,7 @@ bool ReadFdExactly(int fd, void* buf, size_t len) { D("readx: fd=%d wanted=%zu\n", fd, len); while (len > 0) { - int r = TEMP_FAILURE_RETRY(adb_read(fd, p, len)); + int r = adb_read(fd, p, len); if (r > 0) { len -= r; p += r; @@ -69,7 +69,7 @@ bool WriteFdExactly(int fd, const void* buf, size_t len) { #endif while (len > 0) { - r = TEMP_FAILURE_RETRY(adb_write(fd, p, len)); + r = adb_write(fd, p, len); if (r == -1) { D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno)); if (errno == EAGAIN) { diff --git a/adb/adb_io_test.cpp b/adb/adb_io_test.cpp index 330d9ce..0c69bc9 100644 --- a/adb/adb_io_test.cpp +++ b/adb/adb_io_test.cpp @@ -24,7 +24,7 @@ #include <string> -#include "utils/file.h" +#include "base/file.h" class TemporaryFile { public: @@ -55,7 +55,7 @@ TEST(io, ReadFdExactly_whole) { TemporaryFile tf; ASSERT_NE(-1, tf.fd); - ASSERT_TRUE(android::WriteStringToFd(expected, tf.fd)) << strerror(errno); + ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno); ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0)); // Test reading the whole file. @@ -69,7 +69,7 @@ TEST(io, ReadFdExactly_eof) { TemporaryFile tf; ASSERT_NE(-1, tf.fd); - ASSERT_TRUE(android::WriteStringToFd(expected, tf.fd)) << strerror(errno); + ASSERT_TRUE(android::base::WriteStringToFd(expected, tf.fd)) << strerror(errno); ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0)); // Test that not having enough data will fail. @@ -83,7 +83,7 @@ TEST(io, ReadFdExactly_partial) { TemporaryFile tf; ASSERT_NE(-1, tf.fd); - ASSERT_TRUE(android::WriteStringToFd(input, tf.fd)) << strerror(errno); + ASSERT_TRUE(android::base::WriteStringToFd(input, tf.fd)) << strerror(errno); ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0)); // Test reading a partial file. @@ -106,7 +106,7 @@ TEST(io, WriteFdExactly_whole) { ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0)); std::string s; - ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)); + ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); EXPECT_STREQ(expected, s.c_str()); } @@ -123,7 +123,7 @@ TEST(io, WriteFdExactly_partial) { expected.pop_back(); std::string s; - ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)); + ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); EXPECT_EQ(expected, s); } @@ -137,6 +137,6 @@ TEST(io, WriteStringFully) { ASSERT_EQ(0, lseek(tf.fd, SEEK_SET, 0)); std::string s; - ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)); + ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)); EXPECT_STREQ(str, s.c_str()); } diff --git a/adb/adb_listeners.c b/adb/adb_listeners.cpp index f68b876..84b9c64 100644 --- a/adb/adb_listeners.c +++ b/adb/adb_listeners.cpp @@ -54,24 +54,26 @@ void ss_listener_event_func(int _fd, unsigned ev, void *_l) } } -void listener_event_func(int _fd, unsigned ev, void *_l) +void listener_event_func(int _fd, unsigned ev, void* _l) { - alistener *l = _l; + alistener* listener = reinterpret_cast<alistener*>(_l); asocket *s; - if(ev & FDE_READ) { + if (ev & FDE_READ) { struct sockaddr addr; socklen_t alen; int fd; alen = sizeof(addr); fd = adb_socket_accept(_fd, &addr, &alen); - if(fd < 0) return; + if (fd < 0) { + return; + } s = create_local_socket(fd); - if(s) { - s->transport = l->transport; - connect_to_remote(s, l->connect_to); + if (s) { + s->transport = listener->transport; + connect_to_remote(s, listener->connect_to); return; } @@ -102,11 +104,9 @@ static void free_listener(alistener* l) free(l); } -void listener_disconnect(void* _l, atransport* t) +void listener_disconnect(void* listener, atransport* t) { - alistener* l = _l; - - free_listener(l); + free_listener(reinterpret_cast<alistener*>(listener)); } int local_name_to_fd(const char *name) @@ -220,20 +220,16 @@ install_status_t install_listener(const char *local_name, atransport* transport, int no_rebind) { - alistener *l; + for (alistener* l = listener_list.next; l != &listener_list; l = l->next) { + if (strcmp(local_name, l->local_name) == 0) { + char* cto; - //printf("install_listener('%s','%s')\n", local_name, connect_to); - - for(l = listener_list.next; l != &listener_list; l = l->next){ - if(strcmp(local_name, l->local_name) == 0) { - char *cto; - - /* can't repurpose a smartsocket */ + /* can't repurpose a smartsocket */ if(l->connect_to[0] == '*') { return INSTALL_STATUS_INTERNAL_ERROR; } - /* can't repurpose a listener if 'no_rebind' is true */ + /* can't repurpose a listener if 'no_rebind' is true */ if (no_rebind) { return INSTALL_STATUS_CANNOT_REBIND; } @@ -243,7 +239,6 @@ install_status_t install_listener(const char *local_name, return INSTALL_STATUS_INTERNAL_ERROR; } - //printf("rebinding '%s' to '%s'\n", local_name, connect_to); free((void*) l->connect_to); l->connect_to = cto; if (l->transport != transport) { @@ -255,38 +250,51 @@ install_status_t install_listener(const char *local_name, } } - if((l = calloc(1, sizeof(alistener))) == 0) goto nomem; - if((l->local_name = strdup(local_name)) == 0) goto nomem; - if((l->connect_to = strdup(connect_to)) == 0) goto nomem; + alistener* listener = reinterpret_cast<alistener*>( + calloc(1, sizeof(alistener))); + if (listener == nullptr) { + goto nomem; + } + + listener->local_name = strdup(local_name); + if (listener->local_name == nullptr) { + goto nomem; + } + listener->connect_to = strdup(connect_to); + if (listener->connect_to == nullptr) { + goto nomem; + } - l->fd = local_name_to_fd(local_name); - if(l->fd < 0) { - free((void*) l->local_name); - free((void*) l->connect_to); - free(l); + listener->fd = local_name_to_fd(local_name); + if (listener->fd < 0) { + free(listener->local_name); + free(listener->connect_to); + free(listener); printf("cannot bind '%s'\n", local_name); - return -2; + return INSTALL_STATUS_CANNOT_BIND; } - close_on_exec(l->fd); - if(!strcmp(l->connect_to, "*smartsocket*")) { - fdevent_install(&l->fde, l->fd, ss_listener_event_func, l); + close_on_exec(listener->fd); + if (!strcmp(listener->connect_to, "*smartsocket*")) { + fdevent_install(&listener->fde, listener->fd, ss_listener_event_func, + listener); } else { - fdevent_install(&l->fde, l->fd, listener_event_func, l); + fdevent_install(&listener->fde, listener->fd, listener_event_func, + listener); } - fdevent_set(&l->fde, FDE_READ); + fdevent_set(&listener->fde, FDE_READ); - l->next = &listener_list; - l->prev = listener_list.prev; - l->next->prev = l; - l->prev->next = l; - l->transport = transport; + listener->next = &listener_list; + listener->prev = listener_list.prev; + listener->next->prev = listener; + listener->prev->next = listener; + listener->transport = transport; if (transport) { - l->disconnect.opaque = l; - l->disconnect.func = listener_disconnect; - add_transport_disconnect(transport, &l->disconnect); + listener->disconnect.opaque = listener; + listener->disconnect.func = listener_disconnect; + add_transport_disconnect(transport, &listener->disconnect); } return INSTALL_STATUS_OK; diff --git a/adb/adb_main.c b/adb/adb_main.cpp index f8475c7..1d9cc3b 100644 --- a/adb/adb_main.c +++ b/adb/adb_main.cpp @@ -273,10 +273,14 @@ int adb_main(int is_daemon, int server_port) exit(1); } #else + // We need to call this even if auth isn't enabled because the file + // descriptor will always be open. + adbd_cloexec_auth_socket(); + property_get("ro.adb.secure", value, "0"); auth_enabled = !strcmp(value, "1"); if (auth_enabled) - adb_auth_init(); + adbd_auth_init(); // Our external storage path may be different than apps, since // we aren't able to bind mount after dropping root. @@ -387,7 +391,7 @@ int main(int argc, char **argv) adb_sysdeps_init(); adb_trace_init(); D("Handling commandline()\n"); - return adb_commandline(argc - 1, argv + 1); + return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); #else /* If adbd runs inside the emulator this will enable adb tracing via * adb-debug qemud service in the emulator. */ diff --git a/adb/commandline.c b/adb/commandline.cpp index a8ad7bd..4538b04 100644 --- a/adb/commandline.c +++ b/adb/commandline.cpp @@ -40,31 +40,31 @@ #include "adb_io.h" #include "file_sync_service.h" -static int do_cmd(transport_type ttype, char* serial, char *cmd, ...); +static int do_cmd(transport_type ttype, const char* serial, const char *cmd, ...); -void get_my_path(char *s, size_t maxLen); int find_sync_dirs(const char *srcarg, - char **android_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out); -int install_app(transport_type transport, char* serial, int argc, char** argv); -int install_multiple_app(transport_type transport, char* serial, int argc, char** argv); -int uninstall_app(transport_type transport, char* serial, int argc, char** argv); + char **system_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out, + char **oem_srcdir_out); +int install_app(transport_type transport, const char* serial, int argc, + const char** argv); +int install_multiple_app(transport_type transport, const char* serial, int argc, + const char** argv); +int uninstall_app(transport_type transport, const char* serial, int argc, + const char** argv); static const char *gProductOutPath = NULL; extern int gListenAll; static char *product_file(const char *extra) { - int n; - char *x; - if (gProductOutPath == NULL) { fprintf(stderr, "adb: Product directory not specified; " "use -p or define ANDROID_PRODUCT_OUT\n"); exit(1); } - n = strlen(gProductOutPath) + strlen(extra) + 2; - x = malloc(n); + int n = strlen(gProductOutPath) + strlen(extra) + 2; + char* x = reinterpret_cast<char*>(malloc(n)); if (x == 0) { fprintf(stderr, "adb: Out of memory (product_file())\n"); exit(1); @@ -207,7 +207,7 @@ void help() " adb get-serialno - prints: <serial-number>\n" " adb get-devpath - prints: <device-path>\n" " adb status-window - continuously print device status for a specified device\n" - " adb remount - remounts the /system and /vendor (if present) partitions on the device read-write\n" + " adb remount - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write\n" " adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n" " adb reboot-bootloader - reboots the device into the bootloader\n" " adb root - restarts the adbd daemon with root permissions\n" @@ -223,9 +223,9 @@ void help() "adb sync notes: adb sync [ <directory> ]\n" " <localdir> can be interpreted in several ways:\n" "\n" - " - If <directory> is not specified, /system, /vendor (if present), and /data partitions will be updated.\n" + " - If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.\n" "\n" - " - If it is \"system\", \"vendor\" or \"data\", only the corresponding partition\n" + " - If it is \"system\", \"vendor\", \"oem\" or \"data\", only the corresponding partition\n" " is updated.\n" "\n" "environmental variables:\n" @@ -422,7 +422,6 @@ int interactive_shell(void) { adb_thread_t thr; int fdi, fd; - int *fds; fd = adb_connect("shell:"); if(fd < 0) { @@ -431,7 +430,7 @@ int interactive_shell(void) } fdi = 0; //dup(0); - fds = malloc(sizeof(int) * 2); + int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2)); fds[0] = fd; fds[1] = fdi; @@ -464,7 +463,6 @@ int adb_download_buffer(const char *service, const char *fn, const void* data, i char buf[4096]; unsigned total; int fd; - const unsigned char *ptr; sprintf(buf,"%s:%d", service, sz); fd = adb_connect(buf); @@ -477,7 +475,7 @@ int adb_download_buffer(const char *service, const char *fn, const void* data, i opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); total = sz; - ptr = data; + const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data); if(progress) { char *x = strrchr(service, ':'); @@ -557,14 +555,15 @@ int adb_download(const char *service, const char *fn, unsigned progress) * we hang up. */ int adb_sideload_host(const char* fn) { - uint8_t* data; unsigned sz; size_t xfer = 0; int status; + int last_percent = -1; + int opt = SIDELOAD_HOST_BLOCK_SIZE; printf("loading: '%s'", fn); fflush(stdout); - data = load_file(fn, &sz); + uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz)); if (data == 0) { printf("\n"); fprintf(stderr, "* cannot read '%s' *\n", fn); @@ -582,10 +581,8 @@ int adb_sideload_host(const char* fn) { goto done; } - int opt = SIDELOAD_HOST_BLOCK_SIZE; opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); - int last_percent = -1; for (;;) { if (!ReadFdExactly(fd, buf, 8)) { fprintf(stderr, "* failed to read command: %s\n", adb_error()); @@ -739,13 +736,12 @@ static char *escape_arg(const char *s) * ppp dev:/dev/omap_csmi_tty0 <ppp options> * */ -int ppp(int argc, char **argv) +int ppp(int argc, const char **argv) { #if defined(_WIN32) fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]); return -1; #else - char *adb_service_name; pid_t pid; int fd; @@ -756,8 +752,7 @@ int ppp(int argc, char **argv) return 1; } - adb_service_name = argv[1]; - + const char* adb_service_name = argv[1]; fd = adb_connect(adb_service_name); if(fd < 0) { @@ -807,7 +802,8 @@ int ppp(int argc, char **argv) #endif /* !defined(_WIN32) */ } -static int send_shellcommand(transport_type transport, char* serial, char* buf) +static int send_shellcommand(transport_type transport, const char* serial, + char* buf) { int fd, ret; @@ -828,7 +824,8 @@ static int send_shellcommand(transport_type transport, char* serial, char* buf) return ret; } -static int logcat(transport_type transport, char* serial, int argc, char **argv) +static int logcat(transport_type transport, const char* serial, int argc, + const char** argv) { char buf[4096]; @@ -877,7 +874,7 @@ static int mkdirs(const char *path) return 0; } -static int backup(int argc, char** argv) { +static int backup(int argc, const char** argv) { char buf[4096]; char default_name[32]; const char* filename = strcpy(default_name, "./backup.ab"); @@ -933,7 +930,7 @@ static int backup(int argc, char** argv) { return 0; } -static int restore(int argc, char** argv) { +static int restore(int argc, const char** argv) { const char* filename; int fd, tarFd; @@ -1102,8 +1099,9 @@ static const char *find_product_out_path(const char *hint) return path_buf; } -static void parse_push_pull_args(char **arg, int narg, char const **path1, char const **path2, - int *show_progress, int *copy_attrs) { +static void parse_push_pull_args(const char **arg, int narg, char const **path1, + char const **path2, int *show_progress, + int *copy_attrs) { *show_progress = 0; *copy_attrs = 0; @@ -1130,7 +1128,7 @@ static void parse_push_pull_args(char **arg, int narg, char const **path1, char } } -int adb_commandline(int argc, char **argv) +int adb_commandline(int argc, const char **argv) { char buf[4096]; int no_daemon = 0; @@ -1139,8 +1137,8 @@ int adb_commandline(int argc, char **argv) int persist = 0; int r; transport_type ttype = kTransportAny; - char* serial = NULL; - char* server_port_str = NULL; + const char* serial = NULL; + const char* server_port_str = NULL; /* If defined, this should be an absolute path to * the directory containing all of the various system images @@ -1274,7 +1272,7 @@ int adb_commandline(int argc, char **argv) /* handle wait-for-* prefix */ if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) { - char* service = argv[0]; + const char* service = argv[0]; if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) { if (ttype == kTransportUsb) { service = "wait-for-usb"; @@ -1308,7 +1306,7 @@ int adb_commandline(int argc, char **argv) /* adb_connect() commands */ if (!strcmp(argv[0], "devices")) { char *tmp; - char *listopt; + const char *listopt; if (argc < 2) listopt = ""; else if (argc == 2 && !strcmp(argv[1], "-l")) @@ -1635,7 +1633,9 @@ int adb_commandline(int argc, char **argv) return uninstall_app(ttype, serial, argc, argv); } else if (!strcmp(argv[0], "sync")) { - char *srcarg, *android_srcpath, *data_srcpath, *vendor_srcpath; + const char* srcarg; + char *system_srcpath, *data_srcpath, *vendor_srcpath, *oem_srcpath; + int listonly = 0; int ret; @@ -1655,18 +1655,22 @@ int adb_commandline(int argc, char **argv) } else { return usage(); } - ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath, &vendor_srcpath); + ret = find_sync_dirs(srcarg, &system_srcpath, &data_srcpath, &vendor_srcpath, + &oem_srcpath); if (ret != 0) return usage(); - if (android_srcpath != NULL) - ret = do_sync_sync(android_srcpath, "/system", listonly); + if (system_srcpath != NULL) + ret = do_sync_sync(system_srcpath, "/system", listonly); if (ret == 0 && vendor_srcpath != NULL) ret = do_sync_sync(vendor_srcpath, "/vendor", listonly); + if(ret == 0 && oem_srcpath != NULL) + ret = do_sync_sync(oem_srcpath, "/oem", listonly); if (ret == 0 && data_srcpath != NULL) ret = do_sync_sync(data_srcpath, "/data", listonly); - free(android_srcpath); + free(system_srcpath); free(vendor_srcpath); + free(oem_srcpath); free(data_srcpath); return ret; } @@ -1736,9 +1740,9 @@ int adb_commandline(int argc, char **argv) } #define MAX_ARGV_LENGTH 16 -static int do_cmd(transport_type ttype, char* serial, char *cmd, ...) +static int do_cmd(transport_type ttype, const char* serial, const char *cmd, ...) { - char *argv[MAX_ARGV_LENGTH]; + const char *argv[MAX_ARGV_LENGTH]; int argc; va_list ap; @@ -1772,54 +1776,65 @@ static int do_cmd(transport_type ttype, char* serial, char *cmd, ...) } int find_sync_dirs(const char *srcarg, - char **android_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out) + char **system_srcdir_out, char **data_srcdir_out, char **vendor_srcdir_out, + char **oem_srcdir_out) { - char *android_srcdir = NULL, *data_srcdir = NULL, *vendor_srcdir = NULL; + char *system_srcdir = NULL, *data_srcdir = NULL, *vendor_srcdir = NULL, *oem_srcdir = NULL; struct stat st; if(srcarg == NULL) { - android_srcdir = product_file("system"); + system_srcdir = product_file("system"); data_srcdir = product_file("data"); vendor_srcdir = product_file("vendor"); - /* Check if vendor partition exists */ + oem_srcdir = product_file("oem"); + // Check if vendor partition exists. if (lstat(vendor_srcdir, &st) || !S_ISDIR(st.st_mode)) vendor_srcdir = NULL; + // Check if oem partition exists. + if (lstat(oem_srcdir, &st) || !S_ISDIR(st.st_mode)) + oem_srcdir = NULL; } else { - /* srcarg may be "data", "system" or NULL. - * if srcarg is NULL, then both data and system are synced - */ + // srcarg may be "data", "system", "vendor", "oem" or NULL. + // If srcarg is NULL, then all partitions are synced. if(strcmp(srcarg, "system") == 0) { - android_srcdir = product_file("system"); + system_srcdir = product_file("system"); } else if(strcmp(srcarg, "data") == 0) { data_srcdir = product_file("data"); } else if(strcmp(srcarg, "vendor") == 0) { vendor_srcdir = product_file("vendor"); + } else if(strcmp(srcarg, "oem") == 0) { + oem_srcdir = product_file("oem"); } else { - /* It's not "system", "vendor", or "data". - */ + // It's not "system", "data", "vendor", or "oem". return 1; } } - if(android_srcdir_out != NULL) - *android_srcdir_out = android_srcdir; + if(system_srcdir_out != NULL) + *system_srcdir_out = system_srcdir; else - free(android_srcdir); + free(system_srcdir); if(vendor_srcdir_out != NULL) *vendor_srcdir_out = vendor_srcdir; else free(vendor_srcdir); + if(oem_srcdir_out != NULL) + *oem_srcdir_out = oem_srcdir; + else + free(oem_srcdir); + if(data_srcdir_out != NULL) - *data_srcdir_out = data_srcdir; - else - free(data_srcdir); + *data_srcdir_out = data_srcdir; + else + free(data_srcdir); + return 0; } -static int pm_command(transport_type transport, char* serial, - int argc, char** argv) +static int pm_command(transport_type transport, const char* serial, + int argc, const char** argv) { char buf[4096]; @@ -1836,7 +1851,8 @@ static int pm_command(transport_type transport, char* serial, return 0; } -int uninstall_app(transport_type transport, char* serial, int argc, char** argv) +int uninstall_app(transport_type transport, const char* serial, int argc, + const char** argv) { /* if the user choose the -k option, we refuse to do it until devices are out with the option to uninstall the remaining data somehow (adb/ui) */ @@ -1854,7 +1870,7 @@ int uninstall_app(transport_type transport, char* serial, int argc, char** argv) return pm_command(transport, serial, argc, argv); } -static int delete_file(transport_type transport, char* serial, char* filename) +static int delete_file(transport_type transport, const char* serial, char* filename) { char buf[4096]; char* quoted; @@ -1879,7 +1895,8 @@ static const char* get_basename(const char* filename) } } -int install_app(transport_type transport, char* serial, int argc, char** argv) +int install_app(transport_type transport, const char* serial, int argc, + const char** argv) { static const char *const DATA_DEST = "/data/local/tmp/%s"; static const char *const SD_DEST = "/sdcard/tmp/%s"; @@ -1897,7 +1914,7 @@ int install_app(transport_type transport, char* serial, int argc, char** argv) // All other arguments passed through verbatim. int last_apk = -1; for (i = argc - 1; i >= 0; i--) { - char* file = argv[i]; + const char* file = argv[i]; char* dot = strrchr(file, '.'); if (dot && !strcasecmp(dot, ".apk")) { if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) { @@ -1915,7 +1932,7 @@ int install_app(transport_type transport, char* serial, int argc, char** argv) return -1; } - char* apk_file = argv[last_apk]; + const char* apk_file = argv[last_apk]; char apk_dest[PATH_MAX]; snprintf(apk_dest, sizeof apk_dest, where, get_basename(apk_file)); int err = do_sync_push(apk_file, apk_dest, 0 /* no show progress */); @@ -1932,7 +1949,8 @@ cleanup_apk: return err; } -int install_multiple_app(transport_type transport, char* serial, int argc, char** argv) +int install_multiple_app(transport_type transport, const char* serial, int argc, + const char** argv) { char buf[1024]; int i; @@ -1943,7 +1961,7 @@ int install_multiple_app(transport_type transport, char* serial, int argc, char* // All other arguments passed through verbatim. int first_apk = -1; for (i = argc - 1; i >= 0; i--) { - char* file = argv[i]; + const char* file = argv[i]; char* dot = strrchr(file, '.'); if (dot && !strcasecmp(dot, ".apk")) { if (stat(file, &sb) == -1 || !S_ISREG(sb.st_mode)) { @@ -1998,7 +2016,7 @@ int install_multiple_app(transport_type transport, char* serial, int argc, char* // Valid session, now stream the APKs int success = 1; for (i = first_apk; i < argc; i++) { - char* file = argv[i]; + const char* file = argv[i]; if (stat(file, &sb) == -1) { fprintf(stderr, "Failed to stat %s\n", file); success = 0; diff --git a/adb/console.c b/adb/console.cpp index b813d33..452ee41 100644 --- a/adb/console.c +++ b/adb/console.cpp @@ -24,7 +24,7 @@ static int connect_to_console(void) } -int adb_send_emulator_command(int argc, char** argv) +int adb_send_emulator_command(int argc, const char** argv) { int fd, nn; diff --git a/adb/file_sync_client.c b/adb/file_sync_client.cpp index 3a0c666..4ba730b 100644 --- a/adb/file_sync_client.c +++ b/adb/file_sync_client.cpp @@ -32,7 +32,6 @@ #include "adb_client.h" #include "adb_io.h" #include "file_sync_service.h" -#include "zipfile/zipfile.h" static unsigned long long total_bytes; static long long start_time; @@ -580,7 +579,8 @@ copyinfo *mkcopyinfo(const char *spath, const char *dpath, int ssize = slen + nlen + 2; int dsize = dlen + nlen + 2; - copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize); + copyinfo *ci = reinterpret_cast<copyinfo*>( + malloc(sizeof(copyinfo) + ssize + dsize)); if(ci == 0) { fprintf(stderr,"out of memory\n"); abort(); @@ -684,14 +684,14 @@ static int copy_local_dir_remote(int fd, const char *lpath, const char *rpath, i if((lpath[0] == 0) || (rpath[0] == 0)) return -1; if(lpath[strlen(lpath) - 1] != '/') { int tmplen = strlen(lpath)+2; - char *tmp = malloc(tmplen); + char *tmp = reinterpret_cast<char*>(malloc(tmplen)); if(tmp == 0) return -1; snprintf(tmp, tmplen, "%s/",lpath); lpath = tmp; } if(rpath[strlen(rpath) - 1] != '/') { int tmplen = strlen(rpath)+2; - char *tmp = malloc(tmplen); + char *tmp = reinterpret_cast<char*>(malloc(tmplen)); if(tmp == 0) return -1; snprintf(tmp, tmplen, "%s/",rpath); rpath = tmp; @@ -784,7 +784,8 @@ int do_sync_push(const char *lpath, const char *rpath, int show_progress) name++; } int tmplen = strlen(name) + strlen(rpath) + 2; - char *tmp = malloc(strlen(name) + strlen(rpath) + 2); + char *tmp = reinterpret_cast<char*>( + malloc(strlen(name) + strlen(rpath) + 2)); if(tmp == 0) return 1; snprintf(tmp, tmplen, "%s/%s", rpath, name); rpath = tmp; @@ -872,7 +873,7 @@ static int remote_build_list(int syncfd, copyinfo **filelist, return 0; } -static int set_time_and_mode(const char *lpath, unsigned int time, unsigned int mode) +static int set_time_and_mode(const char *lpath, time_t time, unsigned int mode) { struct utimbuf times = { time, time }; int r1 = utime(lpath, ×); @@ -890,7 +891,7 @@ static char *add_slash_to_path(const char *path) { if (path[strlen(path) - 1] != '/') { size_t len = strlen(path) + 2; - char *path_with_slash = malloc(len); + char *path_with_slash = reinterpret_cast<char*>(malloc(len)); if (path_with_slash == NULL) return NULL; snprintf(path_with_slash, len, "%s/", path); @@ -999,7 +1000,7 @@ int do_sync_pull(const char *rpath, const char *lpath, int show_progress, int co name++; } int tmplen = strlen(name) + strlen(lpath) + 2; - char *tmp = malloc(tmplen); + char *tmp = reinterpret_cast<char*>(malloc(tmplen)); if(tmp == 0) return 1; snprintf(tmp, tmplen, "%s/%s", lpath, name); lpath = tmp; diff --git a/adb/file_sync_service.c b/adb/file_sync_service.cpp index 0b289e8..ac01678 100644 --- a/adb/file_sync_service.c +++ b/adb/file_sync_service.cpp @@ -33,15 +33,11 @@ #include "file_sync_service.h" #include "private/android_filesystem_config.h" -/* TODO: use fs_config to configure permissions on /data */ -static bool is_on_system(const char *name) { - const char *SYSTEM = "/system/"; - return (strncmp(SYSTEM, name, strlen(SYSTEM)) == 0); -} - -static bool is_on_vendor(const char *name) { - const char *VENDOR = "/vendor/"; - return (strncmp(VENDOR, name, strlen(VENDOR)) == 0); +static bool should_use_fs_config(const char* path) { + // TODO: use fs_config to configure permissions on /data. + return strncmp("/system/", path, strlen("/system/")) == 0 || + strncmp("/vendor/", path, strlen("/vendor/")) == 0 || + strncmp("/oem/", path, strlen("/oem/")) == 0; } static int mkdirs(char *name) @@ -59,7 +55,7 @@ static int mkdirs(char *name) x = adb_dirstart(x); if(x == 0) return 0; *x = 0; - if (is_on_system(name) || is_on_vendor(name)) { + if (should_use_fs_config(name)) { fs_config(name, 1, &uid, &gid, &mode, &cap); } ret = adb_mkdir(name, mode); @@ -151,7 +147,7 @@ done: msg.dent.size = 0; msg.dent.time = 0; msg.dent.namelen = 0; - return !WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ? 0 : -1; + return WriteFdExactly(s, &msg.dent, sizeof(msg.dent)) ? 0 : -1; } static int fail_message(int s, const char *reason) @@ -368,7 +364,7 @@ static int do_send(int s, char *path, char *buffer) if(*tmp == '/') { tmp++; } - if (is_on_system(path) || is_on_vendor(path)) { + if (should_use_fs_config(path)) { fs_config(tmp, 0, &uid, &gid, &mode, &cap); } return handle_send_file(s, path, uid, gid, mode, buffer, do_unlink); @@ -420,7 +416,7 @@ void file_sync_service(int fd, void *cookie) char name[1025]; unsigned namelen; - char *buffer = malloc(SYNC_DATA_MAX); + char *buffer = reinterpret_cast<char*>(malloc(SYNC_DATA_MAX)); if(buffer == 0) goto fail; for(;;) { diff --git a/adb/framebuffer_service.c b/adb/framebuffer_service.cpp index 9d17d2c..7baad8b 100644 --- a/adb/framebuffer_service.c +++ b/adb/framebuffer_service.cpp @@ -62,10 +62,11 @@ void framebuffer_service(int fd, void *cookie) int fd_screencap; int w, h, f; int fds[2]; + pid_t pid; if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail; - pid_t pid = fork(); + pid = fork(); if (pid < 0) goto done; if (pid == 0) { diff --git a/adb/get_my_path_darwin.c b/adb/get_my_path_darwin.c index ff1396c..b0c962e 100644 --- a/adb/get_my_path_darwin.c +++ b/adb/get_my_path_darwin.c @@ -17,6 +17,8 @@ #import <Carbon/Carbon.h> #include <unistd.h> +#include "adb.h" + void get_my_path(char *s, size_t maxLen) { CFBundleRef mainBundle = CFBundleGetMainBundle(); diff --git a/adb/get_my_path_linux.c b/adb/get_my_path_linux.cpp index 179c3dd..11c0b21 100644 --- a/adb/get_my_path_linux.c +++ b/adb/get_my_path_linux.cpp @@ -14,10 +14,12 @@ * limitations under the License. */ -#include <sys/types.h> -#include <unistd.h> #include <limits.h> #include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +#include "adb.h" void get_my_path(char *exe, size_t maxLen) { diff --git a/adb/get_my_path_windows.c b/adb/get_my_path_windows.cpp index ddf2816..9d23e1c 100644 --- a/adb/get_my_path_windows.c +++ b/adb/get_my_path_windows.cpp @@ -14,10 +14,12 @@ * limitations under the License. */ -#include <limits.h> #include <assert.h> +#include <limits.h> #include <windows.h> +#include "adb.h" + void get_my_path(char *exe, size_t maxLen) { char *r; diff --git a/adb/jdwp_service.c b/adb/jdwp_service.cpp index 3074e42..f0b4ba7 100644 --- a/adb/jdwp_service.c +++ b/adb/jdwp_service.cpp @@ -194,7 +194,8 @@ static void jdwp_process_event(int, unsigned, void*); /* forward */ static JdwpProcess* jdwp_process_alloc( int socket ) { - JdwpProcess* proc = calloc(1,sizeof(*proc)); + JdwpProcess* proc = reinterpret_cast<JdwpProcess*>( + calloc(1, sizeof(*proc))); if (proc == NULL) { D("not enough memory to create new JDWP process\n"); @@ -234,7 +235,7 @@ jdwp_process_alloc( int socket ) static void jdwp_process_event( int socket, unsigned events, void* _proc ) { - JdwpProcess* proc = _proc; + JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc); if (events & FDE_READ) { if (proc->pid < 0) { @@ -601,7 +602,7 @@ jdwp_socket_ready( asocket* s ) asocket* create_jdwp_service_socket( void ) { - JdwpSocket* s = calloc(sizeof(*s),1); + JdwpSocket* s = reinterpret_cast<JdwpSocket*>(calloc(sizeof(*s), 1)); if (s == NULL) return NULL; @@ -696,7 +697,7 @@ jdwp_tracker_enqueue( asocket* s, apacket* p ) asocket* create_jdwp_tracker_service_socket( void ) { - JdwpTracker* t = calloc(sizeof(*t),1); + JdwpTracker* t = reinterpret_cast<JdwpTracker*>(calloc(sizeof(*t), 1)); if (t == NULL) return NULL; diff --git a/adb/qemu_tracing.c b/adb/qemu_tracing.cpp index f31eae8..f31eae8 100644 --- a/adb/qemu_tracing.c +++ b/adb/qemu_tracing.cpp diff --git a/adb/remount_service.c b/adb/remount_service.cpp index 414b316..a83d5b1 100644 --- a/adb/remount_service.c +++ b/adb/remount_service.cpp @@ -23,6 +23,8 @@ #include <sys/mount.h> #include <unistd.h> +#include <string> + #include "sysdeps.h" #define TRACE_TAG TRACE_ADB @@ -32,10 +34,10 @@ static int system_ro = 1; static int vendor_ro = 1; +static int oem_ro = 1; /* Returns the device used to mount a directory in /proc/mounts */ -static char *find_mount(const char *dir) -{ +static std::string find_mount(const char *dir) { FILE* fp; struct mntent* mentry; char* device = NULL; @@ -45,7 +47,7 @@ static char *find_mount(const char *dir) } while ((mentry = getmntent(fp)) != NULL) { if (strcmp(dir, mentry->mnt_dir) == 0) { - device = strdup(mentry->mnt_fsname); + device = mentry->mnt_fsname; break; } } @@ -53,64 +55,53 @@ static char *find_mount(const char *dir) return device; } -static int hasVendorPartition() -{ - struct stat info; - if (!lstat("/vendor", &info)) - if ((info.st_mode & S_IFMT) == S_IFDIR) - return true; - return false; +static bool has_partition(const char* path) { + struct stat sb; + return (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode)); } -int make_block_device_writable(const char* dev) -{ - int fd = -1; - int OFF = 0; - int rc = -1; - - if (!dev) - goto errout; - - fd = unix_open(dev, O_RDONLY | O_CLOEXEC); - if (fd < 0) - goto errout; - - if (ioctl(fd, BLKROSET, &OFF)) { - goto errout; +int make_block_device_writable(const std::string& dev) { + int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC); + if (fd == -1) { + return -1; } - rc = 0; - -errout: - if (fd >= 0) { - adb_close(fd); + int result = -1; + int OFF = 0; + if (!ioctl(fd, BLKROSET, &OFF)) { + result = 0; } - return rc; -} - -/* Init mounts /system as read only, remount to enable writes. */ -static int remount(const char* dir, int* dir_ro) -{ - char *dev = 0; - int rc = -1; + adb_close(fd); - dev = find_mount(dir); + return result; +} - if (!dev || make_block_device_writable(dev)) { - goto errout; +// Init mounts /system as read only, remount to enable writes. +static int remount(const char* dir, int* dir_ro) { + std::string dev(find_mount(dir)); + if (dev.empty() || make_block_device_writable(dev)) { + return -1; } - rc = mount(dev, dir, "none", MS_REMOUNT, NULL); + int rc = mount(dev.c_str(), dir, "none", MS_REMOUNT, NULL); *dir_ro = rc; - -errout: - free(dev); return rc; } -void remount_service(int fd, void *cookie) -{ - char buffer[200]; +static bool remount_partition(int fd, const char* partition, int* ro) { + if (!has_partition(partition)) { + return true; + } + if (remount(partition, ro)) { + char buf[200]; + snprintf(buf, sizeof(buf), "remount of %s failed: %s\n", partition, strerror(errno)); + WriteStringFully(fd, buf); + return false; + } + return true; +} + +void remount_service(int fd, void* cookie) { char prop_buf[PROPERTY_VALUE_MAX]; if (getuid() != 0) { @@ -133,6 +124,7 @@ 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" : "", @@ -147,23 +139,12 @@ void remount_service(int fd, void *cookie) WriteStringFully(fd, buffer); } - if (remount("/system", &system_ro)) { - snprintf(buffer, sizeof(buffer), "remount of system failed: %s\n",strerror(errno)); - WriteStringFully(fd, buffer); - } - - if (hasVendorPartition()) { - if (remount("/vendor", &vendor_ro)) { - snprintf(buffer, sizeof(buffer), "remount of vendor failed: %s\n",strerror(errno)); - WriteStringFully(fd, buffer); - } - } + bool success = true; + success &= remount_partition(fd, "/system", &system_ro); + success &= remount_partition(fd, "/vendor", &vendor_ro); + success &= remount_partition(fd, "/oem", &oem_ro); - if (!system_ro && (!vendor_ro || !hasVendorPartition())) - WriteStringFully(fd, "remount succeeded\n"); - else { - WriteStringFully(fd, "remount failed\n"); - } + WriteStringFully(fd, success ? "remount succeeded\n" : "remount failed\n"); adb_close(fd); } diff --git a/adb/remount_service.h b/adb/remount_service.h new file mode 100644 index 0000000..e1763cf --- /dev/null +++ b/adb/remount_service.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _REMOUNT_SERVICE_H_ +#define _REMOUNT_SERVICE_H_ + +#include <string> + +int make_block_device_writable(const std::string&); +void remount_service(int, void*); + +#endif diff --git a/adb/services.c b/adb/services.cpp index 84f2137..e7bf6b0 100644 --- a/adb/services.c +++ b/adb/services.cpp @@ -38,6 +38,7 @@ #include "adb.h" #include "adb_io.h" #include "file_sync_service.h" +#include "remount_service.h" #include "transport.h" typedef struct stinfo stinfo; @@ -51,7 +52,7 @@ struct stinfo { void *service_bootstrap_func(void *x) { - stinfo *sti = x; + stinfo* sti = reinterpret_cast<stinfo*>(x); sti->func(sti->fd, sti->cookie); free(sti); return 0; @@ -161,7 +162,7 @@ cleanup: void reverse_service(int fd, void* arg) { - const char* command = arg; + const char* command = reinterpret_cast<const char*>(arg); if (handle_forward_request(command, kTransportAny, NULL, fd) < 0) { sendfailmsg(fd, "not a reverse forwarding command"); @@ -174,23 +175,23 @@ void reverse_service(int fd, void* arg) static int create_service_thread(void (*func)(int, void *), void *cookie) { - stinfo *sti; - adb_thread_t t; int s[2]; - - if(adb_socketpair(s)) { + if (adb_socketpair(s)) { printf("cannot create service socket pair\n"); return -1; } D("socketpair: (%d,%d)", s[0], s[1]); - sti = malloc(sizeof(stinfo)); - if(sti == 0) fatal("cannot allocate stinfo"); + stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo))); + if (sti == nullptr) { + fatal("cannot allocate stinfo"); + } sti->func = func; sti->cookie = cookie; sti->fd = s[1]; - if(adb_thread_create( &t, service_bootstrap_func, sti)){ + adb_thread_t t; + if (adb_thread_create(&t, service_bootstrap_func, sti)) { free(sti); adb_close(s[0]); adb_close(s[1]); @@ -359,7 +360,6 @@ static void subproc_waiter_service(int fd, void *cookie) static int create_subproc_thread(const char *name, const subproc_mode mode) { - stinfo *sti; adb_thread_t t; int ret_fd; pid_t pid = -1; @@ -384,7 +384,7 @@ static int create_subproc_thread(const char *name, const subproc_mode mode) } D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid); - sti = malloc(sizeof(stinfo)); + stinfo* sti = reinterpret_cast<stinfo*>(malloc(sizeof(stinfo))); if(sti == 0) fatal("cannot allocate stinfo"); sti->func = subproc_waiter_service; sti->cookie = (void*) (uintptr_t) pid; @@ -512,11 +512,11 @@ struct state_info { static void wait_for_state(int fd, void* cookie) { - struct state_info* sinfo = cookie; - char* err = "unknown error"; + state_info* sinfo = reinterpret_cast<state_info*>(cookie); D("wait_for_state %d\n", sinfo->state); + const char* err = "unknown error"; atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err); if(t != 0) { WriteFdExactly(fd, "OKAY", 4); @@ -635,7 +635,7 @@ static void connect_service(int fd, void* cookie) { char buf[4096]; char resp[4096]; - char *host = cookie; + char *host = reinterpret_cast<char*>(cookie); if (!strncmp(host, "emu:", 4)) { connect_emulator(host + 4, buf, sizeof(buf)); @@ -656,7 +656,7 @@ asocket* host_service_to_socket(const char* name, const char *serial) if (!strcmp(name,"track-devices")) { return create_device_tracker(); } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { - struct state_info* sinfo = malloc(sizeof(struct state_info)); + auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info))); if (serial) sinfo->serial = strdup(serial); diff --git a/adb/set_verity_enable_state_service.c b/adb/set_verity_enable_state_service.cpp index 184674d..139b074 100644 --- a/adb/set_verity_enable_state_service.c +++ b/adb/set_verity_enable_state_service.cpp @@ -26,6 +26,7 @@ #include "cutils/properties.h" #include "ext4_sb.h" #include "fs_mgr.h" +#include "remount_service.h" #include "sysdeps.h" #define FSTAB_PREFIX "/fstab." @@ -90,7 +91,7 @@ static int set_verity_enabled_state(int fd, const char *block_device, uint32_t magic_number; const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER : VERITY_METADATA_MAGIC_DISABLE; - uint64_t device_length; + uint64_t device_length = 0; int device = -1; int retval = -1; @@ -140,7 +141,7 @@ static int set_verity_enabled_state(int fd, const char *block_device, 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", + "Couldn't find verity metadata at offset %" PRIu64 "!\n", device_length); goto errout; } diff --git a/adb/sockets.c b/adb/sockets.cpp index d34f8c6..12bc8d8 100644 --- a/adb/sockets.c +++ b/adb/sockets.cpp @@ -284,98 +284,101 @@ static void local_socket_close_locked(asocket *s) insert_local_socket(s, &local_socket_closing_list); } -static void local_socket_event_func(int fd, unsigned ev, void *_s) +static void local_socket_event_func(int fd, unsigned ev, void* _s) { - asocket *s = _s; - + asocket* s = reinterpret_cast<asocket*>(_s); D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev); /* put the FDE_WRITE processing before the FDE_READ ** in order to simplify the code. */ - if(ev & FDE_WRITE){ - apacket *p; - - while((p = s->pkt_first) != 0) { - while(p->len > 0) { + if (ev & FDE_WRITE) { + apacket* p; + while ((p = s->pkt_first) != nullptr) { + while (p->len > 0) { int r = adb_write(fd, p->ptr, p->len); - if(r > 0) { - p->ptr += r; - p->len -= r; - continue; - } - if(r < 0) { + if (r == -1) { /* returning here is ok because FDE_READ will ** be processed in the next iteration loop */ - if(errno == EAGAIN) return; - if(errno == EINTR) continue; + if (errno == EAGAIN) { + return; + } + } else if (r > 0) { + p->ptr += r; + p->len -= r; + continue; } + D(" closing after write because r=%d and errno is %d\n", r, errno); s->close(s); return; } - if(p->len == 0) { + if (p->len == 0) { s->pkt_first = p->next; - if(s->pkt_first == 0) s->pkt_last = 0; + if (s->pkt_first == 0) { + s->pkt_last = 0; + } put_apacket(p); } } - /* if we sent the last packet of a closing socket, - ** we can now destroy it. - */ + /* if we sent the last packet of a closing socket, + ** we can now destroy it. + */ if (s->closing) { D(" closing because 'closing' is set after write\n"); s->close(s); return; } - /* no more packets queued, so we can ignore - ** writable events again and tell our peer - ** to resume writing - */ + /* no more packets queued, so we can ignore + ** writable events again and tell our peer + ** to resume writing + */ fdevent_del(&s->fde, FDE_WRITE); s->peer->ready(s->peer); } - if(ev & FDE_READ){ + if (ev & FDE_READ) { apacket *p = get_apacket(); unsigned char *x = p->data; size_t avail = MAX_PAYLOAD; int r; int is_eof = 0; - while(avail > 0) { + while (avail > 0) { r = adb_read(fd, x, avail); - D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail); - if(r > 0) { + D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", + s->id, s->fd, r, r < 0 ? errno : 0, avail); + if (r == -1) { + if (errno == EAGAIN) { + break; + } + } else if (r > 0) { avail -= r; x += r; continue; } - if(r < 0) { - if(errno == EAGAIN) break; - if(errno == EINTR) continue; - } - /* r = 0 or unhandled error */ + /* r = 0 or unhandled error */ is_eof = 1; break; } D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", s->id, s->fd, r, is_eof, s->fde.force_eof); - if((avail == MAX_PAYLOAD) || (s->peer == 0)) { + if ((avail == MAX_PAYLOAD) || (s->peer == 0)) { put_apacket(p); } else { p->len = MAX_PAYLOAD - avail; r = s->peer->enqueue(s->peer, p); - D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r); + D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, + r); - if(r < 0) { + if (r < 0) { /* error return means they closed us as a side-effect ** and we must return immediately. ** @@ -387,7 +390,7 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) return; } - if(r > 0) { + if (r > 0) { /* if the remote cannot accept further events, ** we disable notification of READs. They'll ** be enabled again when we get a call to ready() @@ -396,13 +399,14 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) } } /* Don't allow a forced eof if data is still there */ - if((s->fde.force_eof && !r) || is_eof) { - D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof); + if ((s->fde.force_eof && !r) || is_eof) { + D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", + is_eof, r, s->fde.force_eof); s->close(s); } } - if(ev & FDE_ERROR){ + if (ev & FDE_ERROR){ /* this should be caught be the next read or write ** catching it here means we may skip the last few ** bytes of readable data. @@ -415,7 +419,7 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) asocket *create_local_socket(int fd) { - asocket *s = calloc(1, sizeof(asocket)); + asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); if (s == NULL) fatal("cannot allocate socket"); s->fd = fd; s->enqueue = local_socket_enqueue; @@ -539,8 +543,8 @@ static void remote_socket_close(asocket *s) static void remote_socket_disconnect(void* _s, atransport* t) { - asocket* s = _s; - asocket* peer = s->peer; + asocket* s = reinterpret_cast<asocket*>(_s); + asocket* peer = s->peer; D("remote_socket_disconnect RS(%d)\n", s->id); if (peer) { @@ -557,12 +561,9 @@ static void remote_socket_disconnect(void* _s, atransport* t) Returns a new non-NULL asocket handle. */ asocket *create_remote_socket(unsigned id, atransport *t) { - asocket* s; - adisconnect* dis; - if (id == 0) fatal("invalid remote socket id (0)"); - s = calloc(1, sizeof(aremotesocket)); - dis = &((aremotesocket*)s)->disconnect; + asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(aremotesocket))); + adisconnect* dis = &reinterpret_cast<aremotesocket*>(s)->disconnect; if (s == NULL) fatal("cannot allocate socket"); s->id = id; @@ -824,7 +825,7 @@ static int smart_socket_enqueue(asocket *s, apacket *p) } #else /* !ADB_HOST */ if (s->transport == NULL) { - char* error_string = "unknown failure"; + const char* error_string = "unknown failure"; s->transport = acquire_one_transport (CS_ANY, kTransportAny, NULL, &error_string); @@ -892,7 +893,7 @@ static void smart_socket_close(asocket *s) static asocket *create_smart_socket(void) { D("Creating smart socket \n"); - asocket *s = calloc(1, sizeof(asocket)); + asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); if (s == NULL) fatal("cannot allocate socket"); s->enqueue = smart_socket_enqueue; s->ready = smart_socket_ready; diff --git a/adb/test_track_devices.c b/adb/test_track_devices.cpp index 77b3ad9..77b3ad9 100644 --- a/adb/test_track_devices.c +++ b/adb/test_track_devices.cpp diff --git a/adb/test_track_jdwp.c b/adb/test_track_jdwp.cpp index 8ecc6b8..8ecc6b8 100644 --- a/adb/test_track_jdwp.c +++ b/adb/test_track_jdwp.cpp diff --git a/adb/tests/test_adb.py b/adb/tests/test_adb.py index 49ead73..f111b04 100755 --- a/adb/tests/test_adb.py +++ b/adb/tests/test_adb.py @@ -237,16 +237,36 @@ class AdbBasic(unittest.TestCase): version_num = True self.assertTrue(version_num) - def test_root_unroot(self): - """Make sure that adb root and adb unroot work, using id(1).""" + def _test_root(self): adb = AdbWrapper() adb.root() adb.wait() self.assertEqual("root", adb.shell("id -un").strip()) + + def _test_unroot(self): + adb = AdbWrapper() adb.unroot() adb.wait() self.assertEqual("shell", adb.shell("id -un").strip()) + def test_root_unroot(self): + """Make sure that adb root and adb unroot work, using id(1).""" + adb = AdbWrapper() + original_user = adb.shell("id -un").strip() + try: + if original_user == "root": + self._test_unroot() + self._test_root() + elif original_user == "shell": + self._test_root() + self._test_unroot() + finally: + if original_user == "root": + adb.root() + else: + adb.unroot() + adb.wait() + class AdbFile(unittest.TestCase): SCRATCH_DIR = "/data/local/tmp" diff --git a/adb/transport.c b/adb/transport.cpp index e2c204e..1f8ac03 100644 --- a/adb/transport.c +++ b/adb/transport.cpp @@ -221,7 +221,7 @@ write_packet(int fd, const char* name, apacket** ppacket) static void transport_socket_events(int fd, unsigned events, void *_t) { - atransport *t = _t; + atransport *t = reinterpret_cast<atransport*>(_t); D("transport_socket_events(fd=%d, events=%04x,...)\n", fd, events); if(events & FDE_READ){ apacket *p = 0; @@ -278,7 +278,7 @@ void send_packet(apacket *p, atransport *t) static void *output_thread(void *_t) { - atransport *t = _t; + atransport *t = reinterpret_cast<atransport*>(_t); apacket *p; D("%s: starting transport output thread on fd %d, SYNC online (%d)\n", @@ -333,7 +333,7 @@ oops: static void *input_thread(void *_t) { - atransport *t = _t; + atransport *t = reinterpret_cast<atransport*>(_t); apacket *p; int active = 0; @@ -494,7 +494,8 @@ device_tracker_ready( asocket* socket ) asocket* create_device_tracker(void) { - device_tracker* tracker = calloc(1,sizeof(*tracker)); + device_tracker* tracker = reinterpret_cast<device_tracker*>( + calloc(1, sizeof(*tracker))); if(tracker == 0) fatal("cannot allocate device tracker"); @@ -799,7 +800,8 @@ static int qual_match(const char *to_test, return !*to_test; } -atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char** error_out) +atransport *acquire_one_transport(int state, transport_type ttype, + const char* serial, const char** error_out) { atransport *t; atransport *result = NULL; @@ -1007,7 +1009,8 @@ void close_usb_devices() int register_socket_transport(int s, const char *serial, int port, int local) { - atransport *t = calloc(1, sizeof(atransport)); + atransport *t = reinterpret_cast<atransport*>( + calloc(1, sizeof(atransport))); atransport *n; char buff[32]; @@ -1106,7 +1109,8 @@ void unregister_all_tcp_transports() void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable) { - atransport *t = calloc(1, sizeof(atransport)); + atransport *t = reinterpret_cast<atransport*>( + calloc(1, sizeof(atransport))); D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, serial ? serial : ""); init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM)); diff --git a/adb/transport.h b/adb/transport.h index 352bbe4..36a0e40 100644 --- a/adb/transport.h +++ b/adb/transport.h @@ -37,7 +37,7 @@ void dump_hex(const unsigned char* ptr, size_t len); * If no suitable transport is found, error is set. */ atransport* acquire_one_transport(int state, transport_type ttype, - const char* serial, char** error_out); + const char* serial, const char** error_out); void add_transport_disconnect(atransport* t, adisconnect* dis); void remove_transport_disconnect(atransport* t, adisconnect* dis); void kick_transport(atransport* t); diff --git a/adb/transport_local.c b/adb/transport_local.cpp index 440d4c5..440d4c5 100644 --- a/adb/transport_local.c +++ b/adb/transport_local.cpp diff --git a/adb/transport_usb.c b/adb/transport_usb.cpp index 37a8219..37a8219 100644 --- a/adb/transport_usb.c +++ b/adb/transport_usb.cpp diff --git a/adb/usb_linux.c b/adb/usb_linux.cpp index d03f8be..c01ec8c 100644 --- a/adb/usb_linux.c +++ b/adb/usb_linux.cpp @@ -574,7 +574,6 @@ static void register_device(const char *dev_name, const char *devpath, unsigned char ep_in, unsigned char ep_out, int interface, int serial_index, unsigned zero_mask) { - usb_handle* usb = 0; int n = 0; char serial[256]; @@ -587,8 +586,9 @@ static void register_device(const char *dev_name, const char *devpath, ** name, we have no further work to do. */ adb_mutex_lock(&usb_lock); - for(usb = handle_list.next; usb != &handle_list; usb = usb->next){ - if(!strcmp(usb->fname, dev_name)) { + for (usb_handle* usb = handle_list.next; usb != &handle_list; + usb = usb->next) { + if (!strcmp(usb->fname, dev_name)) { adb_mutex_unlock(&usb_lock); return; } @@ -597,7 +597,8 @@ static void register_device(const char *dev_name, const char *devpath, D("[ usb located new device %s (%d/%d/%d) ]\n", dev_name, ep_in, ep_out, interface); - usb = calloc(1, sizeof(usb_handle)); + usb_handle* usb = reinterpret_cast<usb_handle*>( + calloc(1, sizeof(usb_handle))); strcpy(usb->fname, dev_name); usb->ep_in = ep_in; usb->ep_out = ep_out; diff --git a/adb/usb_windows.c b/adb/usb_windows.cpp index 3c5533b..3c5533b 100644 --- a/adb/usb_windows.c +++ b/adb/usb_windows.cpp |