Use getmntent when accessing /proc/mounts.

Bug: 18887435
Change-Id: Ibcb446fac954d9c42ebdfc4b684e6f3503337ab4
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 0409ce8..a41e583 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -14,18 +14,19 @@
  * limitations under the License.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <fts.h>
-#include <unistd.h>
+#include <mntent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/mount.h>
-#include <sys/ioctl.h>
-#include <dirent.h>
+#include <unistd.h>
 
 #include <linux/kdev_t.h>
 
@@ -1466,13 +1467,8 @@
 }
 
 int VolumeManager::listMountedObbs(SocketClient* cli) {
-    char device[256];
-    char mount_path[256];
-    char rest[256];
-    FILE *fp;
-    char line[1024];
-
-    if (!(fp = fopen("/proc/mounts", "r"))) {
+    FILE *fp = setmntent("/proc/mounts", "r");
+    if (fp == NULL) {
         SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
         return -1;
     }
@@ -1484,17 +1480,10 @@
     loopDir[loopDirLen++] = '/';
     loopDir[loopDirLen] = '\0';
 
-    while(fgets(line, sizeof(line), fp)) {
-        line[strlen(line)-1] = '\0';
-
-        /*
-         * Should look like:
-         * /dev/block/loop0 /mnt/obb/fc99df1323fd36424f864dcb76b76d65 ...
-         */
-        sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
-
-        if (!strncmp(mount_path, loopDir, loopDirLen)) {
-            int fd = open(device, O_RDONLY);
+    mntent* mentry;
+    while ((mentry = getmntent(fp)) != NULL) {
+        if (!strncmp(mentry->mnt_dir, loopDir, loopDirLen)) {
+            int fd = open(mentry->mnt_fsname, O_RDONLY);
             if (fd >= 0) {
                 struct loop_info64 li;
                 if (ioctl(fd, LOOP_GET_STATUS64, &li) >= 0) {
@@ -1505,8 +1494,7 @@
             }
         }
     }
-
-    fclose(fp);
+    endmntent(fp);
     return 0;
 }
 
@@ -1807,28 +1795,22 @@
 
 bool VolumeManager::isMountpointMounted(const char *mp)
 {
-    char device[256];
-    char mount_path[256];
-    char rest[256];
-    FILE *fp;
-    char line[1024];
-
-    if (!(fp = fopen("/proc/mounts", "r"))) {
+    FILE *fp = setmntent("/proc/mounts", "r");
+    if (fp == NULL) {
         SLOGE("Error opening /proc/mounts (%s)", strerror(errno));
         return false;
     }
 
-    while(fgets(line, sizeof(line), fp)) {
-        line[strlen(line)-1] = '\0';
-        sscanf(line, "%255s %255s %255s\n", device, mount_path, rest);
-        if (!strcmp(mount_path, mp)) {
-            fclose(fp);
-            return true;
+    bool found_mp = false;
+    mntent* mentry;
+    while ((mentry = getmntent(fp)) != NULL) {
+        if (strcmp(mentry->mnt_dir, mp) == 0) {
+            found_mp = true;
+            break;
         }
     }
-
-    fclose(fp);
-    return false;
+    endmntent(fp);
+    return found_mp;
 }
 
 int VolumeManager::cleanupAsec(Volume *v, bool force) {