From d6bd9bf45968cb97ed88a4e06c40a127625897b1 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Fri, 2 Jan 2015 14:02:14 -0800 Subject: [PATCH] Use getmntent when accessing /proc/mounts. Bug: 18887435 Change-Id: I6d7f95bbdb976428d4722bd640745e73c9839160 --- adb/remount_service.c | 42 ++++++++++++-------------------------- libcutils/android_reboot.c | 33 ++++++++---------------------- 2 files changed, 22 insertions(+), 53 deletions(-) diff --git a/adb/remount_service.c b/adb/remount_service.c index 9746f9afc..5b3fa9f7e 100644 --- a/adb/remount_service.c +++ b/adb/remount_service.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -36,38 +37,21 @@ static int vendor_ro = 1; /* Returns the device used to mount a directory in /proc/mounts */ static char *find_mount(const char *dir) { - int fd; - int res; - char *token = NULL; - const char delims[] = "\n"; - char buf[4096]; + FILE* fp; + struct mntent* mentry; + char* device = NULL; - fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC); - if (fd < 0) + if ((fp = setmntent("/proc/mounts", "r")) == NULL) { return NULL; - - buf[sizeof(buf) - 1] = '\0'; - adb_read(fd, buf, sizeof(buf) - 1); - adb_close(fd); - - token = strtok(buf, delims); - - while (token) { - char mount_dev[256]; - char mount_dir[256]; - int mount_freq; - int mount_passno; - - res = sscanf(token, "%255s %255s %*s %*s %d %d\n", - mount_dev, mount_dir, &mount_freq, &mount_passno); - mount_dev[255] = 0; - mount_dir[255] = 0; - if (res == 4 && (strcmp(dir, mount_dir) == 0)) - return strdup(mount_dev); - - token = strtok(NULL, delims); } - return NULL; + while ((mentry = getmntent(fp)) != NULL) { + if (strcmp(dir, mentry->mnt_dir) == 0) { + device = strdup(mentry->mnt_fsname); + break; + } + } + endmntent(fp); + return device; } static int hasVendorPartition() diff --git a/libcutils/android_reboot.c b/libcutils/android_reboot.c index 5d982958d..aa8620616 100644 --- a/libcutils/android_reboot.c +++ b/libcutils/android_reboot.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -33,37 +34,21 @@ */ static int remount_ro_done(void) { - FILE *f; - char mount_dev[256]; - char mount_dir[256]; - char mount_type[256]; - char mount_opts[256]; - int mount_freq; - int mount_passno; - int match; + FILE* fp; + struct mntent* mentry; int found_rw_fs = 0; - f = fopen("/proc/mounts", "r"); - if (! f) { - /* If we can't read /proc/mounts, just give up */ + if ((fp = setmntent("/proc/mounts", "r")) == NULL) { + /* If we can't read /proc/mounts, just give up. */ return 1; } - - do { - match = fscanf(f, "%255s %255s %255s %255s %d %d\n", - mount_dev, mount_dir, mount_type, - mount_opts, &mount_freq, &mount_passno); - mount_dev[255] = 0; - mount_dir[255] = 0; - mount_type[255] = 0; - mount_opts[255] = 0; - if ((match == 6) && !strncmp(mount_dev, "/dev/block", 10) && strstr(mount_opts, "rw,")) { + while ((mentry = getmntent(fp)) != NULL) { + if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) { found_rw_fs = 1; break; } - } while (match != EOF); - - fclose(f); + } + endmntent(fp); return !found_rw_fs; }