Fix keyname generation issue
am: 5744dfe3cc

Change-Id: Iad7a5325249b609720a295473194e40ff1747d4a
diff --git a/CommandListener.cpp b/CommandListener.cpp
index 78973db..8da3f69 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -361,18 +361,8 @@
         return;
     }
 
-    size_t dirent_len = offsetof(struct dirent, d_name) +
-            fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
-
-    struct dirent *dent = (struct dirent *) malloc(dirent_len);
-    if (dent == NULL) {
-        cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
-        return;
-    }
-
-    struct dirent *result;
-
-    while (!readdir_r(d, dent, &result) && result != NULL) {
+    dirent* dent;
+    while ((dent = readdir(d)) != NULL) {
         if (dent->d_name[0] == '.')
             continue;
         if (dent->d_type != DT_REG)
@@ -387,8 +377,6 @@
         }
     }
     closedir(d);
-
-    free(dent);
 }
 
 int CommandListener::AsecCmd::runCommand(SocketClient *cli,
diff --git a/NetlinkManager.cpp b/NetlinkManager.cpp
index 0ad182e..90e3c6c 100644
--- a/NetlinkManager.cpp
+++ b/NetlinkManager.cpp
@@ -64,10 +64,11 @@
         return -1;
     }
 
-    // When running in a net/user namespace, SO_RCVBUFFORCE is not available.
-    // Try using SO_RCVBUF first.
-    if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0) &&
-        (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0)) {
+    // When running in a net/user namespace, SO_RCVBUFFORCE will fail because
+    // it will check for the CAP_NET_ADMIN capability in the root namespace.
+    // Try using SO_RCVBUF if that fails.
+    if ((setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) &&
+        (setsockopt(mSock, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz)) < 0)) {
         SLOGE("Unable to set uevent socket SO_RCVBUF/SO_RCVBUFFORCE option: %s", strerror(errno));
         goto out;
     }
diff --git a/cryptfs.cpp b/cryptfs.cpp
index 43c8177..a79909a 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -194,19 +194,19 @@
 
 extern struct fstab *fstab;
 
-enum RebootType {reboot, recovery, shutdown};
-static void cryptfs_reboot(enum RebootType rt)
+enum class RebootType {reboot, recovery, shutdown};
+static void cryptfs_reboot(RebootType rt)
 {
-  switch(rt) {
-      case reboot:
+  switch (rt) {
+      case RebootType::reboot:
           property_set(ANDROID_RB_PROPERTY, "reboot");
           break;
 
-      case recovery:
+      case RebootType::recovery:
           property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
           break;
 
-      case shutdown:
+      case RebootType::shutdown:
           property_set(ANDROID_RB_PROPERTY, "shutdown");
           break;
     }
@@ -1451,7 +1451,7 @@
                 } else {
                     /* Let's hope that a reboot clears away whatever is keeping
                        the mount busy */
-                    cryptfs_reboot(reboot);
+                    cryptfs_reboot(RebootType::reboot);
                 }
             } else {
                 SLOGE("Failed to mount decrypted data");
@@ -2049,7 +2049,7 @@
         if (rc == ENABLE_INPLACE_ERR_DEV) {
             /* Hack for b/17898962 */
             SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
-            cryptfs_reboot(reboot);
+            cryptfs_reboot(RebootType::reboot);
         }
 
         if (!rc) {
@@ -2282,7 +2282,7 @@
 
     if (onlyCreateHeader) {
         sleep(2);
-        cryptfs_reboot(reboot);
+        cryptfs_reboot(RebootType::reboot);
     }
 
     if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
@@ -2369,11 +2369,11 @@
             return 0;
           } else {
             sleep(2); /* Give the UI a chance to show 100% progress */
-            cryptfs_reboot(reboot);
+            cryptfs_reboot(RebootType::reboot);
           }
         } else {
             sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
-            cryptfs_reboot(shutdown);
+            cryptfs_reboot(RebootType::shutdown);
         }
     } else {
         char value[PROPERTY_VALUE_MAX];
@@ -2389,7 +2389,7 @@
             if (!write_bootloader_message(options, &err)) {
                 SLOGE("could not write bootloader message: %s", err.c_str());
             }
-            cryptfs_reboot(recovery);
+            cryptfs_reboot(RebootType::recovery);
         } else {
             /* set property to trigger dialog */
             property_set("vold.encrypt_progress", "error_partially_encrypted");
@@ -2419,7 +2419,7 @@
      * vold to restart the system.
      */
     SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
-    cryptfs_reboot(reboot);
+    cryptfs_reboot(RebootType::reboot);
 
     /* shouldn't get here */
     property_set("vold.encrypt_progress", "error_shutting_down");