Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * ismounted.c --- Check to see if the filesystem was mounted
      3  *
      4  * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  * %End-Header%
     10  */
     11 
     12 #include <stdio.h>
     13 #if HAVE_UNISTD_H
     14 #include <unistd.h>
     15 #endif
     16 #if HAVE_ERRNO_H
     17 #include <errno.h>
     18 #endif
     19 #include <fcntl.h>
     20 #ifdef HAVE_LINUX_FD_H
     21 #include <linux/fd.h>
     22 #endif
     23 #ifdef HAVE_MNTENT_H
     24 #include <mntent.h>
     25 #endif
     26 #ifdef HAVE_GETMNTINFO
     27 #include <paths.h>
     28 #include <sys/param.h>
     29 #include <sys/mount.h>
     30 #endif /* HAVE_GETMNTINFO */
     31 #include <string.h>
     32 #include <sys/stat.h>
     33 
     34 #include "ext2_fs.h"
     35 #include "ext2fs.h"
     36 
     37 #ifdef HAVE_MNTENT_H
     38 /*
     39  * Helper function which checks a file in /etc/mtab format to see if a
     40  * filesystem is mounted.  Returns an error if the file doesn't exist
     41  * or can't be opened.
     42  */
     43 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
     44 				   int *mount_flags, char *mtpt, int mtlen)
     45 {
     46 	struct mntent 	*mnt;
     47 	struct stat	st_buf;
     48 	errcode_t	retval = 0;
     49 	dev_t		file_dev=0, file_rdev=0;
     50 	ino_t		file_ino=0;
     51 	FILE 		*f;
     52 	int		fd;
     53 
     54 	*mount_flags = 0;
     55 	if ((f = setmntent (mtab_file, "r")) == NULL)
     56 		return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno);
     57 	if (stat(file, &st_buf) == 0) {
     58 		if (S_ISBLK(st_buf.st_mode)) {
     59 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
     60 			file_rdev = st_buf.st_rdev;
     61 #endif	/* __GNU__ */
     62 		} else {
     63 			file_dev = st_buf.st_dev;
     64 			file_ino = st_buf.st_ino;
     65 		}
     66 	}
     67 	while ((mnt = getmntent (f)) != NULL) {
     68 		if (mnt->mnt_fsname[0] != '/')
     69 			continue;
     70 		if (strcmp(file, mnt->mnt_fsname) == 0)
     71 			break;
     72 		if (stat(mnt->mnt_fsname, &st_buf) == 0) {
     73 			if (S_ISBLK(st_buf.st_mode)) {
     74 #ifndef __GNU__
     75 				if (file_rdev && (file_rdev == st_buf.st_rdev))
     76 					break;
     77 #endif	/* __GNU__ */
     78 			} else {
     79 				if (file_dev && ((file_dev == st_buf.st_dev) &&
     80 						 (file_ino == st_buf.st_ino)))
     81 					break;
     82 			}
     83 		}
     84 	}
     85 
     86 	if (mnt == 0) {
     87 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
     88 		/*
     89 		 * Do an extra check to see if this is the root device.  We
     90 		 * can't trust /etc/mtab, and /proc/mounts will only list
     91 		 * /dev/root for the root filesystem.  Argh.  Instead we
     92 		 * check if the given device has the same major/minor number
     93 		 * as the device that the root directory is on.
     94 		 */
     95 		if (file_rdev && stat("/", &st_buf) == 0) {
     96 			if (st_buf.st_dev == file_rdev) {
     97 				*mount_flags = EXT2_MF_MOUNTED;
     98 				if (mtpt)
     99 					strncpy(mtpt, "/", mtlen);
    100 				goto is_root;
    101 			}
    102 		}
    103 #endif	/* __GNU__ */
    104 		goto errout;
    105 	}
    106 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
    107 	/* Validate the entry in case /etc/mtab is out of date */
    108 	/*
    109 	 * We need to be paranoid, because some broken distributions
    110 	 * (read: Slackware) don't initialize /etc/mtab before checking
    111 	 * all of the non-root filesystems on the disk.
    112 	 */
    113 	if (stat(mnt->mnt_dir, &st_buf) < 0) {
    114 		retval = errno;
    115 		if (retval == ENOENT) {
    116 #ifdef DEBUG
    117 			printf("Bogus entry in %s!  (%s does not exist)\n",
    118 			       mtab_file, mnt->mnt_dir);
    119 #endif /* DEBUG */
    120 			retval = 0;
    121 		}
    122 		goto errout;
    123 	}
    124 	if (file_rdev && (st_buf.st_dev != file_rdev)) {
    125 #ifdef DEBUG
    126 		printf("Bogus entry in %s!  (%s not mounted on %s)\n",
    127 		       mtab_file, file, mnt->mnt_dir);
    128 #endif /* DEBUG */
    129 		goto errout;
    130 	}
    131 #endif /* __GNU__ */
    132 	*mount_flags = EXT2_MF_MOUNTED;
    133 
    134 #ifdef MNTOPT_RO
    135 	/* Check to see if the ro option is set */
    136 	if (hasmntopt(mnt, MNTOPT_RO))
    137 		*mount_flags |= EXT2_MF_READONLY;
    138 #endif
    139 
    140 	if (mtpt)
    141 		strncpy(mtpt, mnt->mnt_dir, mtlen);
    142 	/*
    143 	 * Check to see if we're referring to the root filesystem.
    144 	 * If so, do a manual check to see if we can open /etc/mtab
    145 	 * read/write, since if the root is mounted read/only, the
    146 	 * contents of /etc/mtab may not be accurate.
    147 	 */
    148 	if (!strcmp(mnt->mnt_dir, "/")) {
    149 is_root:
    150 #define TEST_FILE "/.ismount-test-file"
    151 		*mount_flags |= EXT2_MF_ISROOT;
    152 		fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
    153 		if (fd < 0) {
    154 			if (errno == EROFS)
    155 				*mount_flags |= EXT2_MF_READONLY;
    156 		} else
    157 			close(fd);
    158 		(void) unlink(TEST_FILE);
    159 	}
    160 	retval = 0;
    161 errout:
    162 	endmntent (f);
    163 	return retval;
    164 }
    165 
    166 static errcode_t check_mntent(const char *file, int *mount_flags,
    167 			      char *mtpt, int mtlen)
    168 {
    169 	errcode_t	retval;
    170 
    171 #ifdef DEBUG
    172 	retval = check_mntent_file("/tmp/mtab", file, mount_flags,
    173 				   mtpt, mtlen);
    174 	if (retval == 0)
    175 		return 0;
    176 #endif /* DEBUG */
    177 #ifdef __linux__
    178 	retval = check_mntent_file("/proc/mounts", file, mount_flags,
    179 				   mtpt, mtlen);
    180 	if (retval == 0 && (*mount_flags != 0))
    181 		return 0;
    182 #endif /* __linux__ */
    183 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
    184 #ifndef MOUNTED
    185 #define MOUNTED _PATH_MOUNTED
    186 #endif /* MOUNTED */
    187 	retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
    188 	return retval;
    189 #else
    190 	*mount_flags = 0;
    191 	return 0;
    192 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
    193 }
    194 
    195 #else
    196 #if defined(HAVE_GETMNTINFO)
    197 
    198 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
    199 				  char *mtpt, int mtlen)
    200 {
    201 	struct statfs *mp;
    202         int    len, n;
    203         const  char   *s1;
    204 	char	*s2;
    205 
    206         n = getmntinfo(&mp, MNT_NOWAIT);
    207         if (n == 0)
    208 		return errno;
    209 
    210         len = sizeof(_PATH_DEV) - 1;
    211         s1 = file;
    212         if (strncmp(_PATH_DEV, s1, len) == 0)
    213                 s1 += len;
    214 
    215 	*mount_flags = 0;
    216         while (--n >= 0) {
    217                 s2 = mp->f_mntfromname;
    218                 if (strncmp(_PATH_DEV, s2, len) == 0) {
    219                         s2 += len - 1;
    220                         *s2 = 'r';
    221                 }
    222                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
    223 			*mount_flags = EXT2_MF_MOUNTED;
    224 			break;
    225 		}
    226                 ++mp;
    227 	}
    228 	if (mtpt)
    229 		strncpy(mtpt, mp->f_mntonname, mtlen);
    230 	return 0;
    231 }
    232 #endif /* HAVE_GETMNTINFO */
    233 #endif /* HAVE_MNTENT_H */
    234 
    235 /*
    236  * Check to see if we're dealing with the swap device.
    237  */
    238 static int is_swap_device(const char *file)
    239 {
    240 	FILE		*f;
    241 	char		buf[1024], *cp;
    242 	dev_t		file_dev;
    243 	struct stat	st_buf;
    244 	int		ret = 0;
    245 
    246 	file_dev = 0;
    247 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
    248 	if ((stat(file, &st_buf) == 0) &&
    249 	    S_ISBLK(st_buf.st_mode))
    250 		file_dev = st_buf.st_rdev;
    251 #endif	/* __GNU__ */
    252 
    253 	if (!(f = fopen("/proc/swaps", "r")))
    254 		return 0;
    255 	/* Skip the first line */
    256 	if (!fgets(buf, sizeof(buf), f))
    257 		goto leave;
    258 	if (*buf && strncmp(buf, "Filename\t", 9))
    259 		/* Linux <=2.6.19 contained a bug in the /proc/swaps
    260 		 * code where the header would not be displayed
    261 		 */
    262 		goto valid_first_line;
    263 
    264 	while (fgets(buf, sizeof(buf), f)) {
    265 valid_first_line:
    266 		if ((cp = strchr(buf, ' ')) != NULL)
    267 			*cp = 0;
    268 		if ((cp = strchr(buf, '\t')) != NULL)
    269 			*cp = 0;
    270 		if (strcmp(buf, file) == 0) {
    271 			ret++;
    272 			break;
    273 		}
    274 #ifndef __GNU__
    275 		if (file_dev && (stat(buf, &st_buf) == 0) &&
    276 		    S_ISBLK(st_buf.st_mode) &&
    277 		    file_dev == st_buf.st_rdev) {
    278 			ret++;
    279 			break;
    280 		}
    281 #endif 	/* __GNU__ */
    282 	}
    283 
    284 leave:
    285 	fclose(f);
    286 	return ret;
    287 }
    288 
    289 
    290 /*
    291  * ext2fs_check_mount_point() fills determines if the device is
    292  * mounted or otherwise busy, and fills in mount_flags with one or
    293  * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
    294  * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY.  If mtpt is
    295  * non-NULL, the directory where the device is mounted is copied to
    296  * where mtpt is pointing, up to mtlen characters.
    297  */
    298 #ifdef __TURBOC__
    299  #pragma argsused
    300 #endif
    301 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
    302 				  char *mtpt, int mtlen)
    303 {
    304 	struct stat	st_buf;
    305 	errcode_t	retval = 0;
    306 	int		fd;
    307 
    308 	if (is_swap_device(device)) {
    309 		*mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
    310 		strncpy(mtpt, "<swap>", mtlen);
    311 	} else {
    312 #ifdef HAVE_MNTENT_H
    313 		retval = check_mntent(device, mount_flags, mtpt, mtlen);
    314 #else
    315 #ifdef HAVE_GETMNTINFO
    316 		retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
    317 #else
    318 #ifdef __GNUC__
    319  #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
    320 #endif
    321 		*mount_flags = 0;
    322 #endif /* HAVE_GETMNTINFO */
    323 #endif /* HAVE_MNTENT_H */
    324 	}
    325 	if (retval)
    326 		return retval;
    327 
    328 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
    329 	if ((stat(device, &st_buf) != 0) ||
    330 	    !S_ISBLK(st_buf.st_mode))
    331 		return 0;
    332 	fd = open(device, O_RDONLY | O_EXCL);
    333 	if (fd < 0) {
    334 		if (errno == EBUSY)
    335 			*mount_flags |= EXT2_MF_BUSY;
    336 	} else
    337 		close(fd);
    338 #endif
    339 
    340 	return 0;
    341 }
    342 
    343 /*
    344  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
    345  * EXT2_MF_READONLY, and EXT2_MF_ROOT
    346  *
    347  */
    348 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
    349 {
    350 	return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
    351 }
    352 
    353 #ifdef DEBUG
    354 int main(int argc, char **argv)
    355 {
    356 	int	retval, mount_flags;
    357 	char	mntpt[80];
    358 
    359 	if (argc < 2) {
    360 		fprintf(stderr, "Usage: %s device\n", argv[0]);
    361 		exit(1);
    362 	}
    363 
    364 	add_error_table(&et_ext2_error_table);
    365 	mntpt[0] = 0;
    366 	retval = ext2fs_check_mount_point(argv[1], &mount_flags,
    367 					  mntpt, sizeof(mntpt));
    368 	if (retval) {
    369 		com_err(argv[0], retval,
    370 			"while calling ext2fs_check_if_mounted");
    371 		exit(1);
    372 	}
    373 	printf("Device %s reports flags %02x\n", argv[1], mount_flags);
    374 	if (mount_flags & EXT2_MF_BUSY)
    375 		printf("\t%s is apparently in use.\n", argv[1]);
    376 	if (mount_flags & EXT2_MF_MOUNTED)
    377 		printf("\t%s is mounted.\n", argv[1]);
    378 	if (mount_flags & EXT2_MF_SWAP)
    379 		printf("\t%s is a swap device.\n", argv[1]);
    380 	if (mount_flags & EXT2_MF_READONLY)
    381 		printf("\t%s is read-only.\n", argv[1]);
    382 	if (mount_flags & EXT2_MF_ISROOT)
    383 		printf("\t%s is the root filesystem.\n", argv[1]);
    384 	if (mntpt[0])
    385 		printf("\t%s is mounted on %s.\n", argv[1], mntpt);
    386 	exit(0);
    387 }
    388 #endif /* DEBUG */
    389