Whamcloud - gitweb
libext2fs: precompute FS UUID checksum seed
[tools/e2fsprogs.git] / lib / ext2fs / ismounted.c
index dd853e2..bf532ae 100644 (file)
@@ -1,14 +1,15 @@
 /*
  * ismounted.c --- Check to see if the filesystem was mounted
- * 
+ *
  * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
-#ifdef HAVE_MNTENT_H
+#ifdef HAVE_SETMNTENT
 /*
  * 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.  
+ * or can't be opened.
  */
-static errcode_t check_mntent_file(const char *mtab_file, const char *file, 
+static errcode_t check_mntent_file(const char *mtab_file, const char *file,
                                   int *mount_flags, char *mtpt, int mtlen)
 {
        struct mntent   *mnt;
        struct stat     st_buf;
        errcode_t       retval = 0;
-       dev_t           file_dev;
+       dev_t           file_dev=0, file_rdev=0;
+       ino_t           file_ino=0;
        FILE            *f;
        int             fd;
 
        *mount_flags = 0;
        if ((f = setmntent (mtab_file, "r")) == NULL)
-               return errno;
-       file_dev = 0;
+               return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno);
+       if (stat(file, &st_buf) == 0) {
+               if (S_ISBLK(st_buf.st_mode)) {
 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
-       if (stat(file, &st_buf) == 0)
-               file_dev = st_buf.st_rdev;
-#endif 
+                       file_rdev = st_buf.st_rdev;
+#endif /* __GNU__ */
+               } else {
+                       file_dev = st_buf.st_dev;
+                       file_ino = st_buf.st_ino;
+               }
+       }
        while ((mnt = getmntent (f)) != NULL) {
+               if (mnt->mnt_fsname[0] != '/')
+                       continue;
                if (strcmp(file, mnt->mnt_fsname) == 0)
                        break;
+               if (stat(mnt->mnt_fsname, &st_buf) == 0) {
+                       if (S_ISBLK(st_buf.st_mode)) {
 #ifndef __GNU__
-               if (file_dev && (stat(mnt->mnt_fsname, &st_buf) == 0) &&
-                   file_dev == st_buf.st_rdev)
-                       break;
-#endif
+                               if (file_rdev && (file_rdev == st_buf.st_rdev))
+                                       break;
+#endif /* __GNU__ */
+                       } else {
+                               if (file_dev && ((file_dev == st_buf.st_dev) &&
+                                                (file_ino == st_buf.st_ino)))
+                                       break;
+                       }
+               }
        }
 
        if (mnt == 0) {
@@ -77,20 +93,20 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
                 * check if the given device has the same major/minor number
                 * as the device that the root directory is on.
                 */
-               if (file_dev && stat("/", &st_buf) == 0) {
-                       if (st_buf.st_dev == file_dev) {
+               if (file_rdev && stat("/", &st_buf) == 0) {
+                       if (st_buf.st_dev == file_rdev) {
                                *mount_flags = EXT2_MF_MOUNTED;
                                if (mtpt)
                                        strncpy(mtpt, "/", mtlen);
                                goto is_root;
                        }
                }
-#endif
-               goto exit;
+#endif /* __GNU__ */
+               goto errout;
        }
 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
        /* Validate the entry in case /etc/mtab is out of date */
-       /* 
+       /*
         * We need to be paranoid, because some broken distributions
         * (read: Slackware) don't initialize /etc/mtab before checking
         * all of the non-root filesystems on the disk.
@@ -101,24 +117,26 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
 #ifdef DEBUG
                        printf("Bogus entry in %s!  (%s does not exist)\n",
                               mtab_file, mnt->mnt_dir);
-#endif
+#endif /* DEBUG */
                        retval = 0;
                }
-               goto exit;
+               goto errout;
        }
-       if (file_dev && (st_buf.st_dev != file_dev)) {
+       if (file_rdev && (st_buf.st_dev != file_rdev)) {
 #ifdef DEBUG
                printf("Bogus entry in %s!  (%s not mounted on %s)\n",
                       mtab_file, file, mnt->mnt_dir);
-#endif
-               goto exit;
+#endif /* DEBUG */
+               goto errout;
        }
-#endif
+#endif /* __GNU__ */
        *mount_flags = EXT2_MF_MOUNTED;
-       
+
+#ifdef MNTOPT_RO
        /* Check to see if the ro option is set */
        if (hasmntopt(mnt, MNTOPT_RO))
                *mount_flags |= EXT2_MF_READONLY;
+#endif
 
        if (mtpt)
                strncpy(mtpt, mnt->mnt_dir, mtlen);
@@ -130,9 +148,9 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
         */
        if (!strcmp(mnt->mnt_dir, "/")) {
 is_root:
-#define TEST_FILE "/.ismount-test-file"                
+#define TEST_FILE "/.ismount-test-file"
                *mount_flags |= EXT2_MF_ISROOT;
-               fd = open(TEST_FILE, O_RDWR|O_CREAT);
+               fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
                if (fd < 0) {
                        if (errno == EROFS)
                                *mount_flags |= EXT2_MF_READONLY;
@@ -141,50 +159,11 @@ is_root:
                (void) unlink(TEST_FILE);
        }
        retval = 0;
-exit:
+errout:
        endmntent (f);
        return retval;
 }
 
-/*
- * Check to see if we're dealing with the swap device.
- */
-static int is_swap_device(const char *file)
-{
-       FILE            *f;
-       char            buf[1024], *cp;
-       dev_t           file_dev;
-       struct stat     st_buf;
-
-       file_dev = 0;
-#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
-       if (stat(file, &st_buf) == 0)
-               file_dev = st_buf.st_rdev;
-#endif 
-
-       f = fopen("/proc/swaps", "r");
-       if (!f)
-               return 0;
-       /* Skip the first line */
-       fgets(buf, sizeof(buf), f);
-       while (!feof(f)) {
-               if (!fgets(buf, sizeof(buf), f))
-                       break;
-               if ((cp = strchr(buf, ' ')) != NULL)
-                       *cp = 0;
-               if ((cp = strchr(buf, '\t')) != NULL)
-                       *cp = 0;
-               if (strcmp(buf, file) == 0)
-                       return 1;
-#ifndef __GNU__
-               if (file_dev && (stat(buf, &st_buf) == 0) &&
-                   file_dev == st_buf.st_rdev)
-                       return 1;
-#endif
-       }
-       return 0;
-}
-
 static errcode_t check_mntent(const char *file, int *mount_flags,
                              char *mtpt, int mtlen)
 {
@@ -195,18 +174,27 @@ static errcode_t check_mntent(const char *file, int *mount_flags,
                                   mtpt, mtlen);
        if (retval == 0)
                return 0;
-#endif
+#endif /* DEBUG */
 #ifdef __linux__
        retval = check_mntent_file("/proc/mounts", file, mount_flags,
                                   mtpt, mtlen);
-       if (retval == 0)
+       if (retval == 0 && (*mount_flags != 0))
                return 0;
-#endif
+#endif /* __linux__ */
+#if defined(MOUNTED) || defined(_PATH_MOUNTED)
+#ifndef MOUNTED
+#define MOUNTED _PATH_MOUNTED
+#endif /* MOUNTED */
        retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
        return retval;
+#else
+       *mount_flags = 0;
+       return 0;
+#endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
 }
 
-#elif defined(HAVE_GETMNTINFO)
+#else
+#if defined(HAVE_GETMNTINFO)
 
 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
                                  char *mtpt, int mtlen)
@@ -224,7 +212,7 @@ static errcode_t check_getmntinfo(const char *file, int *mount_flags,
         s1 = file;
         if (strncmp(_PATH_DEV, s1, len) == 0)
                 s1 += len;
+
        *mount_flags = 0;
         while (--n >= 0) {
                 s2 = mp->f_mntfromname;
@@ -243,12 +231,70 @@ static errcode_t check_getmntinfo(const char *file, int *mount_flags,
        return 0;
 }
 #endif /* HAVE_GETMNTINFO */
+#endif /* HAVE_SETMNTENT */
 
 /*
- * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
- * otherwise.  If mtpt is non-NULL, the directory where the device is
- * mounted is copied to where mtpt is pointing, up to mtlen
- * characters.
+ * Check to see if we're dealing with the swap device.
+ */
+static int is_swap_device(const char *file)
+{
+       FILE            *f;
+       char            buf[1024], *cp;
+       dev_t           file_dev;
+       struct stat     st_buf;
+       int             ret = 0;
+
+       file_dev = 0;
+#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
+       if ((stat(file, &st_buf) == 0) &&
+           S_ISBLK(st_buf.st_mode))
+               file_dev = st_buf.st_rdev;
+#endif /* __GNU__ */
+
+       if (!(f = fopen("/proc/swaps", "r")))
+               return 0;
+       /* Skip the first line */
+       if (!fgets(buf, sizeof(buf), f))
+               goto leave;
+       if (*buf && strncmp(buf, "Filename\t", 9))
+               /* Linux <=2.6.19 contained a bug in the /proc/swaps
+                * code where the header would not be displayed
+                */
+               goto valid_first_line;
+
+       while (fgets(buf, sizeof(buf), f)) {
+valid_first_line:
+               if ((cp = strchr(buf, ' ')) != NULL)
+                       *cp = 0;
+               if ((cp = strchr(buf, '\t')) != NULL)
+                       *cp = 0;
+               if (strcmp(buf, file) == 0) {
+                       ret++;
+                       break;
+               }
+#ifndef __GNU__
+               if (file_dev && (stat(buf, &st_buf) == 0) &&
+                   S_ISBLK(st_buf.st_mode) &&
+                   file_dev == st_buf.st_rdev) {
+                       ret++;
+                       break;
+               }
+#endif         /* __GNU__ */
+       }
+
+leave:
+       fclose(f);
+       return ret;
+}
+
+
+/*
+ * ext2fs_check_mount_point() fills determines if the device is
+ * mounted or otherwise busy, and fills in mount_flags with one or
+ * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
+ * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY.  If mtpt is
+ * non-NULL, the directory where the device is mounted is copied to
+ * where mtpt is pointing, up to mtlen characters.
  */
 #ifdef __TURBOC__
  #pragma argsused
@@ -256,28 +302,49 @@ static errcode_t check_getmntinfo(const char *file, int *mount_flags,
 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
                                  char *mtpt, int mtlen)
 {
+       struct stat     st_buf;
+       errcode_t       retval = 0;
+       int             fd;
+
        if (is_swap_device(device)) {
                *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
                strncpy(mtpt, "<swap>", mtlen);
-               return 0;
-       }
-#ifdef HAVE_MNTENT_H
-       return check_mntent(device, mount_flags, mtpt, mtlen);
-#else 
+       } else {
+#ifdef HAVE_SETMNTENT
+               retval = check_mntent(device, mount_flags, mtpt, mtlen);
+#else
 #ifdef HAVE_GETMNTINFO
-       return check_getmntinfo(device, mount_flags, mtpt, mtlen);
+               retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
 #else
-#warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
-       *mount_flags = 0;
-       return 0;
+#ifdef __GNUC__
+ #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
+#endif
+               *mount_flags = 0;
 #endif /* HAVE_GETMNTINFO */
-#endif /* HAVE_MNTENT_H */
+#endif /* HAVE_SETMNTENT */
+       }
+       if (retval)
+               return retval;
+
+#ifdef __linux__ /* This only works on Linux 2.6+ systems */
+       if ((stat(device, &st_buf) != 0) ||
+           !S_ISBLK(st_buf.st_mode))
+               return 0;
+       fd = open(device, O_RDONLY | O_EXCL);
+       if (fd < 0) {
+               if (errno == EBUSY)
+                       *mount_flags |= EXT2_MF_BUSY;
+       } else
+               close(fd);
+#endif
+
+       return 0;
 }
 
 /*
  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
  * EXT2_MF_READONLY, and EXT2_MF_ROOT
- * 
+ *
  */
 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
 {
@@ -289,12 +356,13 @@ int main(int argc, char **argv)
 {
        int     retval, mount_flags;
        char    mntpt[80];
-       
+
        if (argc < 2) {
                fprintf(stderr, "Usage: %s device\n", argv[0]);
                exit(1);
        }
 
+       add_error_table(&et_ext2_error_table);
        mntpt[0] = 0;
        retval = ext2fs_check_mount_point(argv[1], &mount_flags,
                                          mntpt, sizeof(mntpt));
@@ -304,20 +372,18 @@ int main(int argc, char **argv)
                exit(1);
        }
        printf("Device %s reports flags %02x\n", argv[1], mount_flags);
+       if (mount_flags & EXT2_MF_BUSY)
+               printf("\t%s is apparently in use.\n", argv[1]);
        if (mount_flags & EXT2_MF_MOUNTED)
                printf("\t%s is mounted.\n", argv[1]);
-
        if (mount_flags & EXT2_MF_SWAP)
                printf("\t%s is a swap device.\n", argv[1]);
-
        if (mount_flags & EXT2_MF_READONLY)
                printf("\t%s is read-only.\n", argv[1]);
-       
        if (mount_flags & EXT2_MF_ISROOT)
                printf("\t%s is the root filesystem.\n", argv[1]);
        if (mntpt[0])
                printf("\t%s is mounted on %s.\n", argv[1], mntpt);
-       
        exit(0);
 }
-#endif
+#endif /* DEBUG */