OSDN Git Service

select(2) is bad, always use poll(2). Also remove unnecessary loop.
authorPaul Crowley <paulcrowley@google.com>
Thu, 9 Apr 2015 09:18:54 +0000 (10:18 +0100)
committerPaul Crowley <paulcrowley@google.com>
Thu, 9 Apr 2015 10:17:42 +0000 (11:17 +0100)
Change-Id: I18cb8d75945413e35dc904bc95362d9b579cb336

ext4_utils/ext4_crypt_init_extensions.cpp

index 1585911..284437f 100644 (file)
@@ -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,