Merge "Use getmntent when accessing /proc/mounts."

This commit is contained in:
Yabin Cui 2015-01-06 17:41:39 +00:00 committed by Gerrit Code Review
commit fdb3da5953
2 changed files with 22 additions and 53 deletions

View file

@ -18,6 +18,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <mntent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -36,38 +37,21 @@ static int vendor_ro = 1;
/* Returns the device used to mount a directory in /proc/mounts */ /* Returns the device used to mount a directory in /proc/mounts */
static char *find_mount(const char *dir) static char *find_mount(const char *dir)
{ {
int fd; FILE* fp;
int res; struct mntent* mentry;
char *token = NULL; char* device = NULL;
const char delims[] = "\n";
char buf[4096];
fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC); if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
if (fd < 0)
return 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() static int hasVendorPartition()

View file

@ -20,6 +20,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <mntent.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -33,37 +34,21 @@
*/ */
static int remount_ro_done(void) static int remount_ro_done(void)
{ {
FILE *f; FILE* fp;
char mount_dev[256]; struct mntent* mentry;
char mount_dir[256];
char mount_type[256];
char mount_opts[256];
int mount_freq;
int mount_passno;
int match;
int found_rw_fs = 0; int found_rw_fs = 0;
f = fopen("/proc/mounts", "r"); if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
if (! f) { /* If we can't read /proc/mounts, just give up. */
/* If we can't read /proc/mounts, just give up */
return 1; return 1;
} }
while ((mentry = getmntent(fp)) != NULL) {
do { if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
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,")) {
found_rw_fs = 1; found_rw_fs = 1;
break; break;
} }
} while (match != EOF); }
endmntent(fp);
fclose(f);
return !found_rw_fs; return !found_rw_fs;
} }