2 * ismounted.c --- Check to see if the filesystem was mounted
4 * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
12 /* define BSD_SOURCE to make sure we get the major() macro */
16 #ifndef _DEFAULT_SOURCE
17 #define _DEFAULT_SOURCE /* since glibc 2.20 _SVID_SOURCE is deprecated */
29 #ifdef HAVE_LINUX_FD_H
32 #ifdef HAVE_LINUX_LOOP_H
33 #include <linux/loop.h>
34 #include <sys/ioctl.h>
35 #ifdef HAVE_LINUX_MAJOR_H
36 #include <linux/major.h>
37 #endif /* HAVE_LINUX_MAJOR_H */
38 #endif /* HAVE_LINUX_LOOP_H */
42 #ifdef HAVE_GETMNTINFO
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #endif /* HAVE_GETMNTINFO */
50 #include <sys/types.h>
52 #ifdef HAVE_SYS_SYSMACROS_H
53 #include <sys/sysmacros.h>
62 * Check to see if a regular file is mounted.
63 * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
64 * because the name in /proc/mounts is a loopback device not a regular file.
66 static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
67 dev_t file_dev, ino_t file_ino)
69 #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
70 struct loop_info64 loopinfo = {0, };
73 if (major(mnt_rdev) == LOOP_MAJOR) {
74 loop_fd = open(mnt_fsname, O_RDONLY);
78 ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
83 if (file_dev == loopinfo.lo_device &&
84 file_ino == loopinfo.lo_inode)
87 #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
92 * Helper function which checks a file in /etc/mtab format to see if a
93 * filesystem is mounted. Returns an error if the file doesn't exist
96 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
97 int *mount_flags, char *mtpt, int mtlen)
100 struct stat st_buf, dir_st_buf;
101 errcode_t retval = 0;
102 dev_t file_dev=0, file_rdev=0;
109 if ((f = setmntent (mtab_file, "r")) == NULL) {
110 if (errno == ENOENT) {
111 if (getenv("EXT2FS_NO_MTAB_OK"))
114 return EXT2_ET_NO_MTAB_FILE;
118 if (stat(file, &st_buf) == 0) {
119 if (ext2fsP_is_disk_device(st_buf.st_mode)) {
120 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
121 file_rdev = st_buf.st_rdev;
124 file_dev = st_buf.st_dev;
125 file_ino = st_buf.st_ino;
128 while ((mnt = getmntent (f)) != NULL) {
129 if (mnt->mnt_fsname[0] != '/')
131 if (strcmp(file, mnt->mnt_fsname) == 0) {
132 if (stat(mnt->mnt_dir, &st_buf) != 0)
134 if (file_rdev && (file_rdev != st_buf.st_dev)) {
136 printf("Bogus entry in %s! "
137 "(%s is not mounted on %s)\n",
138 mtab_file, file, mnt->mnt_dir);
144 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
145 if (ext2fsP_is_disk_device(st_buf.st_mode)) {
148 (file_rdev == st_buf.st_rdev)) {
149 if (stat(mnt->mnt_dir,
152 if (file_rdev == dir_st_buf.st_dev)
155 if (check_loop_mounted(mnt->mnt_fsname,
156 st_buf.st_rdev, file_dev,
161 if (file_dev && ((file_dev == st_buf.st_dev) &&
162 (file_ino == st_buf.st_ino)))
169 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
171 * Do an extra check to see if this is the root device. We
172 * can't trust /etc/mtab, and /proc/mounts will only list
173 * /dev/root for the root filesystem. Argh. Instead we
174 * check if the given device has the same major/minor number
175 * as the device that the root directory is on.
177 if (file_rdev && stat("/", &st_buf) == 0) {
178 if (st_buf.st_dev == file_rdev) {
179 *mount_flags = EXT2_MF_MOUNTED;
181 strncpy(mtpt, "/", mtlen);
188 *mount_flags = EXT2_MF_MOUNTED;
191 /* Check to see if the ro option is set */
192 if (hasmntopt(mnt, MNTOPT_RO))
193 *mount_flags |= EXT2_MF_READONLY;
197 strncpy(mtpt, mnt->mnt_dir, mtlen);
199 * Check to see if we're referring to the root filesystem.
200 * If so, do a manual check to see if we can open /etc/mtab
201 * read/write, since if the root is mounted read/only, the
202 * contents of /etc/mtab may not be accurate.
204 if (!strcmp(mnt->mnt_dir, "/")) {
206 #define TEST_FILE "/.ismount-test-file"
207 *mount_flags |= EXT2_MF_ISROOT;
208 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
211 *mount_flags |= EXT2_MF_READONLY;
214 (void) unlink(TEST_FILE);
217 if (mnt && mnt->mnt_type &&
218 (!strcmp(mnt->mnt_type, "ext4") ||
219 !strcmp(mnt->mnt_type, "ext3") ||
220 !strcmp(mnt->mnt_type, "ext2")))
221 *mount_flags |= EXT2_MF_EXTFS;
228 static errcode_t check_mntent(const char *file, int *mount_flags,
229 char *mtpt, int mtlen)
234 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
240 retval = check_mntent_file("/proc/mounts", file, mount_flags,
244 #endif /* __linux__ */
245 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
247 #define MOUNTED _PATH_MOUNTED
249 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
254 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
258 #if defined(HAVE_GETMNTINFO)
260 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
261 char *mtpt, int mtlen)
268 n = getmntinfo(&mp, MNT_NOWAIT);
272 len = sizeof(_PATH_DEV) - 1;
274 if (strncmp(_PATH_DEV, s1, len) == 0)
279 s2 = mp->f_mntfromname;
280 if (strncmp(_PATH_DEV, s2, len) == 0) {
284 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
285 *mount_flags = EXT2_MF_MOUNTED;
291 strncpy(mtpt, mp->f_mntonname, mtlen);
294 #endif /* HAVE_GETMNTINFO */
295 #endif /* HAVE_SETMNTENT */
298 * Check to see if we're dealing with the swap device.
300 static int is_swap_device(const char *file)
309 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
310 if ((stat(file, &st_buf) == 0) &&
311 ext2fsP_is_disk_device(st_buf.st_mode))
312 file_dev = st_buf.st_rdev;
315 if (!(f = fopen("/proc/swaps", "r")))
317 /* Skip the first line */
318 if (!fgets(buf, sizeof(buf), f))
320 if (*buf && strncmp(buf, "Filename\t", 9))
321 /* Linux <=2.6.19 contained a bug in the /proc/swaps
322 * code where the header would not be displayed
324 goto valid_first_line;
326 while (fgets(buf, sizeof(buf), f)) {
328 if ((cp = strchr(buf, ' ')) != NULL)
330 if ((cp = strchr(buf, '\t')) != NULL)
332 if (strcmp(buf, file) == 0) {
337 if (file_dev && (stat(buf, &st_buf) == 0) &&
338 ext2fsP_is_disk_device(st_buf.st_mode) &&
339 file_dev == st_buf.st_rdev) {
353 * ext2fs_check_mount_point() fills determines if the device is
354 * mounted or otherwise busy, and fills in mount_flags with one or
355 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
356 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
357 * non-NULL, the directory where the device is mounted is copied to
358 * where mtpt is pointing, up to mtlen characters.
363 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
364 char *mtpt, int mtlen)
366 errcode_t retval = 0;
369 if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
370 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
371 if (getenv("EXT2FS_PRETEND_ROOTFS"))
372 *mount_flags = EXT2_MF_ISROOT;
375 if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
376 *mount_flags = EXT2_MF_MOUNTED;
377 if (getenv("EXT2FS_PRETEND_ROOTFS"))
378 *mount_flags = EXT2_MF_ISROOT;
382 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
386 if (stat(device, &st_buf) == 0 &&
387 ext2fsP_is_disk_device(st_buf.st_mode)) {
388 int fd = open(device, O_RDONLY | O_EXCL);
392 * The device is not busy so it's
393 * definitelly not mounted. No need to
394 * to perform any more checks.
399 } else if (errno == EBUSY) {
406 if (is_swap_device(device)) {
407 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
409 strncpy(mtpt, "<swap>", mtlen);
411 #ifdef HAVE_SETMNTENT
412 retval = check_mntent(device, mount_flags, mtpt, mtlen);
414 #ifdef HAVE_GETMNTINFO
415 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
417 #if defined(__GNUC__) && !defined(_WIN32)
418 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
421 #endif /* HAVE_GETMNTINFO */
422 #endif /* HAVE_SETMNTENT */
428 *mount_flags |= EXT2_MF_BUSY;
434 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
435 * EXT2_MF_READONLY, and EXT2_MF_ROOT
438 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
440 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
444 int main(int argc, char **argv)
446 int retval, mount_flags;
450 fprintf(stderr, "Usage: %s device\n", argv[0]);
454 add_error_table(&et_ext2_error_table);
456 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
457 mntpt, sizeof(mntpt));
459 com_err(argv[0], retval,
460 "while calling ext2fs_check_if_mounted");
463 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
464 if (mount_flags & EXT2_MF_BUSY)
465 printf("\t%s is apparently in use.\n", argv[1]);
466 if (mount_flags & EXT2_MF_MOUNTED)
467 printf("\t%s is mounted.\n", argv[1]);
468 if (mount_flags & EXT2_MF_SWAP)
469 printf("\t%s is a swap device.\n", argv[1]);
470 if (mount_flags & EXT2_MF_READONLY)
471 printf("\t%s is read-only.\n", argv[1]);
472 if (mount_flags & EXT2_MF_ISROOT)
473 printf("\t%s is the root filesystem.\n", argv[1]);
475 printf("\t%s is mounted on %s.\n", argv[1], mntpt);