OSDN Git Service

ismounted.c (is_swap_device): New function used by
authorTheodore Ts'o <tytso@mit.edu>
Mon, 24 Dec 2001 20:20:22 +0000 (15:20 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 24 Dec 2001 20:20:22 +0000 (15:20 -0500)
ext2fs_check_if_mounted and ext2fs_check_mount_point which
determines whether or not the specified device is a swap
device by using /proc/swaps.  More bulletproofing for
idiotic/careless system administrators!

TODO
lib/ext2fs/ChangeLog
lib/ext2fs/ext2fs.h
lib/ext2fs/ismounted.c

diff --git a/TODO b/TODO
index 8ce869e..5699771 100644 (file)
--- a/TODO
+++ b/TODO
@@ -190,11 +190,6 @@ TODO list.
 
                                                        - Ted
 
-----------------------------------------------------------------
-
-check_if_mounted() should check to see if the file is in /proc/swaps,
-to avoid mkfs's or checking a active swap partition.
-
 -----------------------------------------------------------------
 
 Debugfs's link command should set the file type information
index 474bd20..05dbd9a 100644 (file)
@@ -1,5 +1,11 @@
 2001-12-24  Theodore Tso  <tytso@valinux.com>
 
+       * ismounted.c (is_swap_device): New function used by
+               ext2fs_check_if_mounted and ext2fs_check_mount_point which
+               determines whether or not the specified device is a swap
+               device by using /proc/swaps.  More bulletproofing for
+               idiotic/careless system administrators!
+
        * ext2fs.h, openfs.c (ext2fs_open), initialize.c
                (ext2fs_initialize), mkdir.c (ext2fs_mkdir): Add a new
                field to struct_ext2_filsys, umask.  This field is
index 4b594fd..3ddd452 100644 (file)
@@ -325,6 +325,7 @@ typedef struct ext2_struct_inode_scan *ext2_inode_scan;
 #define EXT2_MF_MOUNTED                1
 #define EXT2_MF_ISROOT         2
 #define EXT2_MF_READONLY       4
+#define EXT2_MF_SWAP           8
 
 /*
  * Ext2/linux mode flags.  We define them here so that we don't need
index 8364401..28e33f5 100644 (file)
@@ -146,6 +146,45 @@ exit:
        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;
+       /* 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)
 {
@@ -217,6 +256,11 @@ 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)
 {
+       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 
@@ -262,7 +306,10 @@ int main(int argc, char **argv)
        printf("Device %s reports flags %02x\n", argv[1], mount_flags);
        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]);