From 710081b07abac0f2f8c9a5502e93c24384b98959 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 20 Feb 2015 16:16:55 +0100 Subject: [PATCH] mount: change mounted() check - use getmntent_r instead of getmntent: because getmntent was nested it overwrote the previous internal mntent structure. - check mounted() first, if not try to mount: this also makes sure filesystems were not mounted multiple times (like tmpfs) and errno is not overwritten in mounted(). For this reason also mount() errno EBUSY can't be used (tested). --- mount.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/mount.c b/mount.c index db0f38a..2c51d18 100644 --- a/mount.c +++ b/mount.c @@ -74,20 +74,21 @@ static int mounted(const char *dir) { FILE *fp; - struct mntent *me; + struct mntent *me, mebuf; struct stat st1, st2; + char linebuf[256]; if (stat(dir, &st1) < 0) { - weprintf("stat %s:", dir); - return 0; + weprintf("stat %s:", dir); + return 0; } - fp = setmntent("/proc/mounts", "r"); - if (!fp) + if (!(fp = setmntent("/proc/mounts", "r"))) eprintf("setmntent %s:", "/proc/mounts"); - while ((me = getmntent(fp)) != NULL) { + + while ((me = getmntent_r(fp, &mebuf, linebuf, sizeof(linebuf)))) { if (stat(me->mnt_dir, &st2) < 0) { - weprintf("stat %s:", me->mnt_dir); - continue; + weprintf("stat %s:", me->mnt_dir); + continue; } if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) @@ -224,13 +225,14 @@ mountall: if (!(fp = setmntent("/etc/fstab", "r"))) eprintf("setmntent %s:", "/etc/fstab"); while ((me = getmntent(fp))) { + /* already mounted, skip */ + if (mounted(me->mnt_dir)) + continue; flags = 0; parseopts(me->mnt_opts, &flags, data, datasiz); if (mount(me->mnt_fsname, me->mnt_dir, me->mnt_type, flags, data) < 0) { - if (mounted(me->mnt_dir) == 0) { - weprintf("mount: %s:", me->mnt_fsname); - status = 1; - } + weprintf("mount: %s:", me->mnt_fsname); + status = 1; } } endmntent(fp);