Whamcloud - gitweb
lib/ext2fs: don't warn about lack of getmntent on Windows
[tools/e2fsprogs.git] / lib / ext2fs / ismounted.c
index b7f17b8..8fc0b99 100644 (file)
@@ -4,11 +4,20 @@
  * 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%
  */
 
+/* define BSD_SOURCE to make sure we get the major() macro */
+#ifndef _BSD_SOURCE
+#define _BSD_SOURCE
+#endif
+#ifndef _DEFAULT_SOURCE
+#define _DEFAULT_SOURCE        /* since glibc 2.20 _SVID_SOURCE is deprecated */
+#endif
+
+#include "config.h"
 #include <stdio.h>
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #ifdef HAVE_LINUX_FD_H
 #include <linux/fd.h>
 #endif
+#ifdef HAVE_LINUX_LOOP_H
+#include <linux/loop.h>
+#include <sys/ioctl.h>
+#ifdef HAVE_LINUX_MAJOR_H
+#include <linux/major.h>
+#endif /* HAVE_LINUX_MAJOR_H */
+#endif /* HAVE_LINUX_LOOP_H */
 #ifdef HAVE_MNTENT_H
 #include <mntent.h>
 #endif
 #endif /* HAVE_GETMNTINFO */
 #include <string.h>
 #include <sys/stat.h>
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_SYSMACROS_H
+#include <sys/sysmacros.h>
+#endif
 
 #include "ext2_fs.h"
 #include "ext2fs.h"
+#include "ext2fsP.h"
+
+#ifdef HAVE_SETMNTENT
+/*
+ * Check to see if a regular file is mounted.
+ * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
+ * because the name in /proc/mounts is a loopback device not a regular file.
+ */
+static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
+                               dev_t file_dev, ino_t file_ino)
+{
+#if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
+       struct loop_info64 loopinfo = {0, };
+       int loop_fd, ret;
+
+       if (major(mnt_rdev) == LOOP_MAJOR) {
+               loop_fd = open(mnt_fsname, O_RDONLY);
+               if (loop_fd < 0)
+                       return -1;
+
+               ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
+               close(loop_fd);
+               if (ret < 0)
+                       return -1;
+
+               if (file_dev == loopinfo.lo_device &&
+                               file_ino == loopinfo.lo_inode)
+                       return 1;
+       }
+#endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
+       return 0;
+}
 
-#ifdef HAVE_MNTENT_H
 /*
  * 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
@@ -44,7 +97,7 @@ 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;
+       struct stat     st_buf, dir_st_buf;
        errcode_t       retval = 0;
        dev_t           file_dev=0, file_rdev=0;
        ino_t           file_ino=0;
@@ -52,10 +105,18 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
        int             fd;
 
        *mount_flags = 0;
-       if ((f = setmntent (mtab_file, "r")) == NULL)
-               return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno);
+
+       if ((f = setmntent (mtab_file, "r")) == NULL) {
+               if (errno == ENOENT) {
+                       if (getenv("EXT2FS_NO_MTAB_OK"))
+                               return 0;
+                       else
+                               return EXT2_ET_NO_MTAB_FILE;
+               }
+               return errno;
+       }
        if (stat(file, &st_buf) == 0) {
-               if (S_ISBLK(st_buf.st_mode)) {
+               if (ext2fsP_is_disk_device(st_buf.st_mode)) {
 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
                        file_rdev = st_buf.st_rdev;
 #endif /* __GNU__ */
@@ -67,12 +128,33 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
        while ((mnt = getmntent (f)) != NULL) {
                if (mnt->mnt_fsname[0] != '/')
                        continue;
-               if (strcmp(file, mnt->mnt_fsname) == 0)
+               if (strcmp(file, mnt->mnt_fsname) == 0) {
+                       if (stat(mnt->mnt_dir, &st_buf) != 0)
+                               continue;
+                       if (file_rdev && (file_rdev != st_buf.st_dev)) {
+#ifdef DEBUG
+                               printf("Bogus entry in %s!  "
+                                      "(%s is not mounted on %s)\n",
+                                      mtab_file, file, mnt->mnt_dir);
+#endif /* DEBUG */
+                               continue;
+                       }
                        break;
+               }
                if (stat(mnt->mnt_fsname, &st_buf) == 0) {
-                       if (S_ISBLK(st_buf.st_mode)) {
+                       if (ext2fsP_is_disk_device(st_buf.st_mode)) {
 #ifndef __GNU__
-                               if (file_rdev && (file_rdev == st_buf.st_rdev))
+                               if (file_rdev &&
+                                   (file_rdev == st_buf.st_rdev)) {
+                                       if (stat(mnt->mnt_dir,
+                                                &dir_st_buf) != 0)
+                                               continue;
+                                       if (file_rdev == dir_st_buf.st_dev)
+                                               break;
+                               }
+                               if (check_loop_mounted(mnt->mnt_fsname,
+                                               st_buf.st_rdev, file_dev,
+                                               file_ino) == 1)
                                        break;
 #endif /* __GNU__ */
                        } else {
@@ -103,32 +185,6 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
 #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.
-        */
-       if (stat(mnt->mnt_dir, &st_buf) < 0) {
-               retval = errno;
-               if (retval == ENOENT) {
-#ifdef DEBUG
-                       printf("Bogus entry in %s!  (%s does not exist)\n",
-                              mtab_file, mnt->mnt_dir);
-#endif /* DEBUG */
-                       retval = 0;
-               }
-               goto errout;
-       }
-       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 /* DEBUG */
-               goto errout;
-       }
-#endif /* __GNU__ */
        *mount_flags = EXT2_MF_MOUNTED;
 
 #ifdef MNTOPT_RO
@@ -177,7 +233,7 @@ static errcode_t check_mntent(const char *file, int *mount_flags,
 #ifdef __linux__
        retval = check_mntent_file("/proc/mounts", file, mount_flags,
                                   mtpt, mtlen);
-       if (retval == 0 && (*mount_flags != 0))
+       if (retval == 0)
                return 0;
 #endif /* __linux__ */
 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
@@ -230,7 +286,7 @@ static errcode_t check_getmntinfo(const char *file, int *mount_flags,
        return 0;
 }
 #endif /* HAVE_GETMNTINFO */
-#endif /* HAVE_MNTENT_H */
+#endif /* HAVE_SETMNTENT */
 
 /*
  * Check to see if we're dealing with the swap device.
@@ -246,15 +302,23 @@ static int is_swap_device(const char *file)
        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))
+           ext2fsP_is_disk_device(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))
+       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)
@@ -265,13 +329,15 @@ static int is_swap_device(const char *file)
                }
 #ifndef __GNU__
                if (file_dev && (stat(buf, &st_buf) == 0) &&
-                   S_ISBLK(st_buf.st_mode) &&
+                   ext2fsP_is_disk_device(st_buf.st_mode) &&
                    file_dev == st_buf.st_rdev) {
                        ret++;
                        break;
                }
 #endif         /* __GNU__ */
        }
+
+leave:
        fclose(f);
        return ret;
 }
@@ -291,41 +357,69 @@ static int is_swap_device(const char *file)
 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;
+       int             busy = 0;
+
+       if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
+               *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
+               if (getenv("EXT2FS_PRETEND_ROOTFS"))
+                       *mount_flags = EXT2_MF_ISROOT;
+               return 0;
+       }
+       if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
+               *mount_flags = EXT2_MF_MOUNTED;
+               if (getenv("EXT2FS_PRETEND_ROOTFS"))
+                       *mount_flags = EXT2_MF_ISROOT;
+               return 0;
+       }
+
+#ifdef __linux__ /* This only works on Linux 2.6+ systems */
+       {
+               struct stat st_buf;
+
+               if (stat(device, &st_buf) == 0 &&
+                   ext2fsP_is_disk_device(st_buf.st_mode)) {
+                       int fd = open(device, O_RDONLY | O_EXCL);
+
+                       if (fd >= 0) {
+                               /*
+                                * The device is not busy so it's
+                                * definitelly not mounted. No need to
+                                * to perform any more checks.
+                                */
+                               close(fd);
+                               *mount_flags = 0;
+                               return 0;
+                       } else if (errno == EBUSY) {
+                               busy = 1;
+                       }
+               }
+       }
+#endif
 
        if (is_swap_device(device)) {
                *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
-               strncpy(mtpt, "<swap>", mtlen);
+               if (mtpt)
+                       strncpy(mtpt, "<swap>", mtlen);
        } else {
-#ifdef HAVE_MNTENT_H
+#ifdef HAVE_SETMNTENT
                retval = check_mntent(device, mount_flags, mtpt, mtlen);
 #else
 #ifdef HAVE_GETMNTINFO
                retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
 #else
-#ifdef __GNUC__
+#if defined(__GNUC__) && !defined(_WIN32)
  #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
+       if (busy)
+               *mount_flags |= EXT2_MF_BUSY;
 
        return 0;
 }
@@ -351,6 +445,7 @@ int main(int argc, char **argv)
                exit(1);
        }
 
+       add_error_table(&et_ext2_error_table);
        mntpt[0] = 0;
        retval = ext2fs_check_mount_point(argv[1], &mount_flags,
                                          mntpt, sizeof(mntpt));