summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Crowley <paulcrowley@google.com>2015-04-09 10:18:54 +0100
committerPaul Crowley <paulcrowley@google.com>2015-04-09 11:17:42 +0100
commit36d056c52e78565747786e9870c3c0a9fbf80612 (patch)
tree9c380e6e05c2038ad8bd408a6e34a51b4943f245
parent46b4f7d4288acfbf4c8a4aabcd70a47fa92744d9 (diff)
downloadextras-36d056c52e78565747786e9870c3c0a9fbf80612.tar.gz
select(2) is bad, always use poll(2). Also remove unnecessary loop.
Change-Id: I18cb8d75945413e35dc904bc95362d9b579cb336
-rw-r--r--ext4_utils/ext4_crypt_init_extensions.cpp64
1 files changed, 29 insertions, 35 deletions
diff --git a/ext4_utils/ext4_crypt_init_extensions.cpp b/ext4_utils/ext4_crypt_init_extensions.cpp
index 1585911a..284437f9 100644
--- a/ext4_utils/ext4_crypt_init_extensions.cpp
+++ b/ext4_utils/ext4_crypt_init_extensions.cpp
@@ -14,6 +14,7 @@
#include <cutils/klog.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
+#include <poll.h>
#include "unencrypted_properties.h"
@@ -28,6 +29,7 @@ struct ext4_encryption_key {
static const std::string keyring = "@s";
static const std::string arbitrary_sequence_number = "42";
+static const int vold_command_timeout_ms = 10 * 1000;
static key_serial_t device_keyring = -1;
@@ -61,43 +63,35 @@ static std::string vold_command(std::string const& command)
return "";
}
- while (1) {
- struct timeval to;
- to.tv_sec = 10;
- to.tv_usec = 0;
-
- fd_set read_fds;
- FD_ZERO(&read_fds);
- FD_SET(sock, &read_fds);
-
- int rc = select(sock + 1, &read_fds, NULL, NULL, &to);
- if (rc < 0) {
- KLOG_ERROR(TAG, "Error in select %s\n", strerror(errno));
- return "";
- } else if (!rc) {
- KLOG_ERROR(TAG, "Timeout\n");
- return "";
- } else if (FD_ISSET(sock, &read_fds)) {
- char buffer[4096];
- memset(buffer, 0, sizeof(buffer));
- rc = read(sock, buffer, sizeof(buffer));
- if (rc <= 0) {
- if (rc == 0) {
- KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n");
- } else {
- KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno));
- }
- return "";
- }
-
- // We don't truly know that this is the correct result. However,
- // since this will only be used when the framework is down,
- // it should be OK unless someone is running vdc at the same time.
- // Worst case we force a reboot in the very rare synchronization
- // error
- return std::string(buffer, rc);
+ struct pollfd poll_sock = {sock, POLLIN, 0};
+
+ int rc = poll(&poll_sock, 1, vold_command_timeout_ms);
+ if (rc < 0) {
+ KLOG_ERROR(TAG, "Error in poll %s\n", strerror(errno));
+ return "";
+ }
+ if (!(poll_sock.revents & POLLIN)) {
+ KLOG_ERROR(TAG, "Timeout\n");
+ return "";
+ }
+ char buffer[4096];
+ memset(buffer, 0, sizeof(buffer));
+ rc = read(sock, buffer, sizeof(buffer));
+ if (rc <= 0) {
+ if (rc == 0) {
+ KLOG_ERROR(TAG, "Lost connection to Vold - did it crash?\n");
+ } else {
+ KLOG_ERROR(TAG, "Error reading data (%s)\n", strerror(errno));
}
+ return "";
}
+
+ // We don't truly know that this is the correct result. However,
+ // since this will only be used when the framework is down,
+ // it should be OK unless someone is running vdc at the same time.
+ // Worst case we force a reboot in the very rare synchronization
+ // error
+ return std::string(buffer, rc);
}
int e4crypt_create_device_key(const char* dir,