Vold: Add fsync in writeStringToFile()
am: 701d05d32c

Change-Id: I24ab82c29abd56e35d1016b2b3aa0e199528efb3
diff --git a/Android.mk b/Android.mk
index d0b199d..bd457f6 100644
--- a/Android.mk
+++ b/Android.mk
@@ -14,7 +14,6 @@
 	Loop.cpp \
 	Devmapper.cpp \
 	ResponseCode.cpp \
-	CheckBattery.cpp \
 	Ext4Crypt.cpp \
 	VoldUtil.c \
 	cryptfs.cpp \
@@ -72,7 +71,6 @@
 	libfec_rs \
 	libsquashfs_utils \
 	libscrypt_static \
-	libbatteryservice \
 	libavb \
 
 # TODO: include "cert-err34-c" once we move to Binder
diff --git a/CheckBattery.cpp b/CheckBattery.cpp
deleted file mode 100644
index 6390d02..0000000
--- a/CheckBattery.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (C) 2014 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.
- */
-
-#define LOG_TAG "VoldCheckBattery"
-#include <cutils/log.h>
-
-#include <binder/IServiceManager.h>
-#include <batteryservice/IBatteryPropertiesRegistrar.h>
-
-using namespace android;
-
-extern "C"
-{
-    int is_battery_ok_to_start()
-    {
-      // Bug 16868177 exists to purge this code completely
-      return true; //is_battery_ok(START_THRESHOLD);
-    }
-
-    int is_battery_ok_to_continue()
-    {
-      // Bug 16868177 exists to purge this code completely
-      return true; //is_battery_ok(CONTINUE_THRESHOLD);
-    }
-}
diff --git a/CheckBattery.h b/CheckBattery.h
deleted file mode 100644
index dd11ba9..0000000
--- a/CheckBattery.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2014 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 _CHECKBATTERY_H__
-#define _CHECKBATTERY_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int is_battery_ok_to_start();
-int is_battery_ok_to_continue();
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
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/EncryptInplace.cpp b/EncryptInplace.cpp
index 9aa2a21..6ef1cb0 100644
--- a/EncryptInplace.cpp
+++ b/EncryptInplace.cpp
@@ -33,7 +33,6 @@
 #include "cutils/properties.h"
 #define LOG_TAG "EncryptInplace"
 #include "cutils/log.h"
-#include "CheckBattery.h"
 
 // HORRIBLE HACK, FIXME
 #include "cryptfs.h"
@@ -245,13 +244,6 @@
                     goto errout;
                 }
             }
-
-            if (!is_battery_ok_to_continue()) {
-                SLOGE("Stopping encryption due to low battery");
-                rc = 0;
-                goto errout;
-            }
-
         }
         if (flush_outstanding_data(data)) {
             goto errout;
@@ -574,13 +566,6 @@
                   CRYPT_SECTORS_PER_BUFSIZE,
                   i * CRYPT_SECTORS_PER_BUFSIZE);
         }
-
-       if (!is_battery_ok_to_continue()) {
-            SLOGE("Stopping encryption due to low battery");
-            *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
-            rc = 0;
-            goto errout;
-        }
     }
 
     /* Do any remaining sectors */
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/VolumeManager.cpp b/VolumeManager.cpp
index 13a943f..c3c95f6 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -37,6 +37,7 @@
 #include <openssl/md5.h>
 
 #include <android-base/logging.h>
+#include <android-base/parseint.h>
 #include <android-base/stringprintf.h>
 #include <cutils/fs.h>
 #include <cutils/log.h>
@@ -596,6 +597,10 @@
 
     // Poke through all running PIDs look for apps running as UID
     while ((de = readdir(dir))) {
+        pid_t pid;
+        if (de->d_type != DT_DIR) continue;
+        if (!android::base::ParseInt(de->d_name, &pid)) continue;
+
         pidFd = -1;
         nsFd = -1;
 
diff --git a/cryptfs.cpp b/cryptfs.cpp
index 43c8177..eb89e1b 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -58,7 +58,6 @@
 #include "VoldUtil.h"
 #include "Ext4Crypt.h"
 #include "f2fs_sparseblock.h"
-#include "CheckBattery.h"
 #include "EncryptInplace.h"
 #include "Process.h"
 #include "Keymaster.h"
@@ -194,19 +193,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 +1450,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");
@@ -2024,11 +2023,6 @@
     off64_t cur_encryption_done=0, tot_encryption_size=0;
     int rc = -1;
 
-    if (!is_battery_ok_to_start()) {
-        SLOGW("Not starting encryption due to low battery");
-        return 0;
-    }
-
     /* The size of the userdata partition, and add in the vold volumes below */
     tot_encryption_size = crypt_ftr->fs_size;
 
@@ -2049,7 +2043,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 +2276,7 @@
 
     if (onlyCreateHeader) {
         sleep(2);
-        cryptfs_reboot(reboot);
+        cryptfs_reboot(RebootType::reboot);
     }
 
     if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
@@ -2369,11 +2363,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 +2383,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 +2413,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");
diff --git a/tests/Android.mk b/tests/Android.mk
index 4b6573e..5b8ff09 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -13,6 +13,7 @@
 LOCAL_MODULE := vold_tests
 LOCAL_MODULE_TAGS := eng tests
 
+LOCAL_CFLAGS := -Wall -Werror
 include $(BUILD_NATIVE_TEST)
 
 include $(CLEAR_VARS)