[hackers] [ubase] mount: change mounted() check || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Fri, 20 Feb 2015 16:36:38 +0100 (CET)

commit 710081b07abac0f2f8c9a5502e93c24384b98959
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Fri Feb 20 16:16:55 2015 +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).

diff --git a/mount.c b/mount.c
index db0f38a..2c51d18 100644
--- a/mount.c
+++ b/mount.c
_AT_@ -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)
_AT_@ -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);
Received on Fri Feb 20 2015 - 16:36:38 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 20 2015 - 16:48:11 CET