Whamcloud - gitweb
ChangeLog, Makefile.in, ismounted.c:
authorTheodore Ts'o <tytso@mit.edu>
Tue, 17 Apr 2001 02:22:05 +0000 (02:22 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 17 Apr 2001 02:22:05 +0000 (02:22 +0000)
  ismounted.c (check_mntent): Check /proc/mounts on Linux systems before
   checking /etc/mtab.  The EXT2_MF_READONLY flag is now set from the
   /etc/mtab options field for all filesystems, not just the root
   filesystem.  Add debugging code to make it easier to test
   ext2fs_check_if_mounted().

lib/ext2fs/ChangeLog
lib/ext2fs/Makefile.in
lib/ext2fs/ismounted.c

index c2c0887..7a5e037 100644 (file)
@@ -1,5 +1,11 @@
 2001-04-16  Theodore Tso  <tytso@valinux.com>
 
+       * ismounted.c (check_mntent): Check /proc/mounts on Linux systems
+               before checking /etc/mtab.  The EXT2_MF_READONLY flag is
+               now set from the /etc/mtab options field for all
+               filesystems, not just the root filesystem.  Add debugging
+               code to make it easier to test ext2fs_check_if_mounted().
+
        * mkjournal.c (ext2fs_create_journal_superblock): Add safety
                check; return an error if there's an attempt to create a
                journal less than 1024 filesystem blocks.
index dc0590f..894b703 100644 (file)
@@ -177,6 +177,9 @@ tst_getsize: tst_getsize.o getsize.o $(STATIC_LIBEXT2FS)
        $(CC) -o tst_getsize tst_getsize.o getsize.o $(STATIC_LIBEXT2FS) \
                $(LIBCOM_ERR)
 
+tst_ismounted: $(srcdir)/ismounted.c $(STATIC_LIBEXT2FS)
+       $(CC) -o tst_ismounted $(srcdir)/ismounted.c -DDEBUG $(ALL_CFLAGS) $(LIBCOM_ERR) 
+
 tst_byteswap: tst_byteswap.o bitops.o $(STATIC_LIBEXT2FS)
        $(CC) -o tst_byteswap tst_byteswap.o bitops.o $(STATIC_LIBEXT2FS) \
                $(LIBCOM_ERR)
index c3b1070..346d89b 100644 (file)
 
 #ifdef HAVE_MNTENT_H
 /*
- * XXX we assume that /etc/mtab is located on the root filesystem, and
- * we only check to see if the mount is readonly for the root
- * filesystem.
+ * Helper function which checks a file in /etc/mtab format to see if a
+ * filesystem is mounted.  Returns an error if the file doesn't exist
+ * or can't be opened.  
  */
-static errcode_t check_mntent(const char *file, int *mount_flags,
-                             char *mtpt, int mtlen)
+static errcode_t check_mntent_file(const char *mtab_file, const char *file, 
+                                  int *mount_flags, char *mtpt, int mtlen)
 {
        FILE * f;
        struct mntent * mnt;
        int     fd;
 
        *mount_flags = 0;
-       if ((f = setmntent (MOUNTED, "r")) == NULL)
+       if ((f = setmntent (mtab_file, "r")) == NULL)
                return errno;
        while ((mnt = getmntent (f)) != NULL)
                if (strcmp(file, mnt->mnt_fsname) == 0)
@@ -62,6 +62,16 @@ static errcode_t check_mntent(const char *file, int *mount_flags,
                return 0;
        *mount_flags = EXT2_MF_MOUNTED;
        
+       /* Check to see if the ro option is set */
+       if (hasmntopt(mnt, MNTOPT_RO))
+               *mount_flags |= EXT2_MF_READONLY;
+
+       /*
+        * Check to see if we're referring to the root filesystem.
+        * If so, do a manual check to see if we can open /etc/mtab
+        * read/write, since if the root is mounted read/only,
+        * /etc/mtab may not be accurate.
+        */
        if (!strcmp(mnt->mnt_dir, "/")) {
                *mount_flags |= EXT2_MF_ISROOT;
                fd = open(MOUNTED, O_RDWR);
@@ -75,6 +85,22 @@ static errcode_t check_mntent(const char *file, int *mount_flags,
                strncpy(mtpt, mnt->mnt_dir, mtlen);
        return 0;
 }
+
+static errcode_t check_mntent(const char *file, int *mount_flags,
+                             char *mtpt, int mtlen)
+{
+       errcode_t       retval;
+
+#ifdef __linux__
+       retval = check_mntent_file("/proc/mounts", file, mount_flags,
+                                  mtpt, mtlen);
+       if (retval == 0)
+               return 0;
+#endif
+       retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
+       return retval;
+}
+
 #endif
 
 #ifdef HAVE_GETMNTINFO
@@ -159,3 +185,34 @@ errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
 #endif /* HAVE_GETMNTINFO */
 #endif /* HAVE_MNTENT_H */
 }
+
+#ifdef DEBUG
+int main(int argc, char **argv)
+{
+       blk_t   blocks;
+       int     retval, mount_flags;
+       
+       if (argc < 2) {
+               fprintf(stderr, "Usage: %s device\n", argv[0]);
+               exit(1);
+       }
+
+       retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
+       if (retval) {
+               com_err(argv[0], retval,
+                       "while calling ext2fs_check_if_mounted");
+               exit(1);
+       }
+       printf("Device %s reports flags %02x\n", argv[1], mount_flags);
+       if (mount_flags & EXT2_MF_MOUNTED)
+               printf("\t%s is mounted.\n");
+       
+       if (mount_flags & EXT2_MF_READONLY)
+               printf("\t%s is read-only.\n");
+       
+       if (mount_flags & EXT2_MF_ISROOT)
+               printf("\t%s is the root filesystem.\n");
+       
+       exit(0);
+}
+#endif