ubase

suckless linux base utils
git clone git://git.2f30.org/ubase.git
Log | Files | Refs | README | LICENSE

commit 0c3436161f4407b7c07ca062cba9a83ebd9c9e55
parent 131cc5da8e1c8db28426bd6f5a3acf8a5f213835
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri Feb 20 16:16:55 +0100

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).

Diffstat:
mount.c | 26++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git 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);