1997-04-26 17:58:21 +04:00
|
|
|
/*
|
1997-04-29 20:17:09 +04:00
|
|
|
* ismounted.c --- Check to see if the filesystem was mounted
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
2001-05-25 20:32:53 +04:00
|
|
|
* Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
|
1997-04-29 20:17:09 +04:00
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
1997-04-26 17:58:21 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-02-20 08:33:14 +03:00
|
|
|
#if HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
1997-04-26 17:58:21 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_LINUX_FD_H
|
|
|
|
#include <linux/fd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MNTENT_H
|
|
|
|
#include <mntent.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GETMNTINFO
|
|
|
|
#include <paths.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif /* HAVE_GETMNTINFO */
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
#include <string.h>
|
2001-05-25 20:32:53 +04:00
|
|
|
#include <sys/stat.h>
|
1997-04-26 17:58:21 +04:00
|
|
|
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
#include "ext2_fs.h"
|
1997-04-26 17:58:21 +04:00
|
|
|
#include "ext2fs.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_MNTENT_H
|
|
|
|
/*
|
2001-04-17 06:22:05 +04:00
|
|
|
* 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
|
2008-08-28 07:07:54 +04:00
|
|
|
* or can't be opened.
|
1997-04-26 17:58:21 +04:00
|
|
|
*/
|
2008-08-28 07:07:54 +04:00
|
|
|
static errcode_t check_mntent_file(const char *mtab_file, const char *file,
|
2001-04-17 06:22:05 +04:00
|
|
|
int *mount_flags, char *mtpt, int mtlen)
|
1997-04-26 17:58:21 +04:00
|
|
|
{
|
2001-09-14 15:44:25 +04:00
|
|
|
struct mntent *mnt;
|
2001-12-24 09:40:35 +03:00
|
|
|
struct stat st_buf;
|
2001-09-14 15:44:25 +04:00
|
|
|
errcode_t retval = 0;
|
2002-10-31 21:23:37 +03:00
|
|
|
dev_t file_dev=0, file_rdev=0;
|
|
|
|
ino_t file_ino=0;
|
2001-09-14 15:44:25 +04:00
|
|
|
FILE *f;
|
|
|
|
int fd;
|
1997-04-26 17:58:21 +04:00
|
|
|
|
|
|
|
*mount_flags = 0;
|
2001-04-17 06:22:05 +04:00
|
|
|
if ((f = setmntent (mtab_file, "r")) == NULL)
|
2009-05-29 08:09:57 +04:00
|
|
|
return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno);
|
2002-10-31 21:23:37 +03:00
|
|
|
if (stat(file, &st_buf) == 0) {
|
|
|
|
if (S_ISBLK(st_buf.st_mode)) {
|
2001-12-24 09:40:35 +03:00
|
|
|
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
|
2002-10-31 21:23:37 +03:00
|
|
|
file_rdev = st_buf.st_rdev;
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* __GNU__ */
|
2002-10-31 21:23:37 +03:00
|
|
|
} else {
|
|
|
|
file_dev = st_buf.st_dev;
|
|
|
|
file_ino = st_buf.st_ino;
|
|
|
|
}
|
|
|
|
}
|
2001-12-24 09:40:35 +03:00
|
|
|
while ((mnt = getmntent (f)) != NULL) {
|
2009-04-23 06:20:22 +04:00
|
|
|
if (mnt->mnt_fsname[0] != '/')
|
|
|
|
continue;
|
1997-04-26 17:58:21 +04:00
|
|
|
if (strcmp(file, mnt->mnt_fsname) == 0)
|
|
|
|
break;
|
2002-10-31 21:23:37 +03:00
|
|
|
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
|
|
|
|
if (S_ISBLK(st_buf.st_mode)) {
|
2001-12-24 09:40:35 +03:00
|
|
|
#ifndef __GNU__
|
2002-10-31 21:23:37 +03:00
|
|
|
if (file_rdev && (file_rdev == st_buf.st_rdev))
|
|
|
|
break;
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* __GNU__ */
|
2002-10-31 21:23:37 +03:00
|
|
|
} else {
|
|
|
|
if (file_dev && ((file_dev == st_buf.st_dev) &&
|
|
|
|
(file_ino == st_buf.st_ino)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-12-24 09:40:35 +03:00
|
|
|
}
|
2001-05-25 20:32:53 +04:00
|
|
|
|
|
|
|
if (mnt == 0) {
|
2001-06-14 03:26:19 +04:00
|
|
|
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
|
2001-05-25 20:32:53 +04:00
|
|
|
/*
|
|
|
|
* Do an extra check to see if this is the root device. We
|
2001-09-14 15:44:25 +04:00
|
|
|
* can't trust /etc/mtab, and /proc/mounts will only list
|
2001-05-25 20:32:53 +04:00
|
|
|
* /dev/root for the root filesystem. Argh. Instead we
|
|
|
|
* check if the given device has the same major/minor number
|
|
|
|
* as the device that the root directory is on.
|
|
|
|
*/
|
2002-10-31 21:23:37 +03:00
|
|
|
if (file_rdev && stat("/", &st_buf) == 0) {
|
|
|
|
if (st_buf.st_dev == file_rdev) {
|
2001-05-25 20:32:53 +04:00
|
|
|
*mount_flags = EXT2_MF_MOUNTED;
|
|
|
|
if (mtpt)
|
2001-05-25 21:14:23 +04:00
|
|
|
strncpy(mtpt, "/", mtlen);
|
2001-05-25 20:32:53 +04:00
|
|
|
goto is_root;
|
|
|
|
}
|
|
|
|
}
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* __GNU__ */
|
2003-07-06 08:36:48 +04:00
|
|
|
goto errout;
|
2001-09-14 15:44:25 +04:00
|
|
|
}
|
|
|
|
#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
|
|
|
|
/* Validate the entry in case /etc/mtab is out of date */
|
2008-08-28 07:07:54 +04:00
|
|
|
/*
|
2001-09-14 15:44:25 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2001-12-24 09:40:35 +03:00
|
|
|
if (stat(mnt->mnt_dir, &st_buf) < 0) {
|
2001-09-14 15:44:25 +04:00
|
|
|
retval = errno;
|
|
|
|
if (retval == ENOENT) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Bogus entry in %s! (%s does not exist)\n",
|
|
|
|
mtab_file, mnt->mnt_dir);
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* DEBUG */
|
2001-09-14 15:44:25 +04:00
|
|
|
retval = 0;
|
|
|
|
}
|
2003-07-06 08:36:48 +04:00
|
|
|
goto errout;
|
2001-09-14 15:44:25 +04:00
|
|
|
}
|
2002-10-31 21:23:37 +03:00
|
|
|
if (file_rdev && (st_buf.st_dev != file_rdev)) {
|
2001-09-14 15:44:25 +04:00
|
|
|
#ifdef DEBUG
|
2001-12-24 09:40:35 +03:00
|
|
|
printf("Bogus entry in %s! (%s not mounted on %s)\n",
|
|
|
|
mtab_file, file, mnt->mnt_dir);
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* DEBUG */
|
2003-07-06 08:36:48 +04:00
|
|
|
goto errout;
|
2001-05-25 20:32:53 +04:00
|
|
|
}
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* __GNU__ */
|
1997-04-26 17:58:21 +04:00
|
|
|
*mount_flags = EXT2_MF_MOUNTED;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2003-04-13 08:44:19 +04:00
|
|
|
#ifdef MNTOPT_RO
|
2001-04-17 06:22:05 +04:00
|
|
|
/* Check to see if the ro option is set */
|
|
|
|
if (hasmntopt(mnt, MNTOPT_RO))
|
|
|
|
*mount_flags |= EXT2_MF_READONLY;
|
2003-04-13 08:44:19 +04:00
|
|
|
#endif
|
2001-04-17 06:22:05 +04:00
|
|
|
|
2001-05-25 20:32:53 +04:00
|
|
|
if (mtpt)
|
|
|
|
strncpy(mtpt, mnt->mnt_dir, mtlen);
|
2001-04-17 06:22:05 +04:00
|
|
|
/*
|
|
|
|
* Check to see if we're referring to the root filesystem.
|
|
|
|
* If so, do a manual check to see if we can open /etc/mtab
|
2001-05-25 20:32:53 +04:00
|
|
|
* read/write, since if the root is mounted read/only, the
|
|
|
|
* contents of /etc/mtab may not be accurate.
|
2001-04-17 06:22:05 +04:00
|
|
|
*/
|
1997-04-26 17:58:21 +04:00
|
|
|
if (!strcmp(mnt->mnt_dir, "/")) {
|
2001-05-25 20:32:53 +04:00
|
|
|
is_root:
|
2008-08-28 07:07:54 +04:00
|
|
|
#define TEST_FILE "/.ismount-test-file"
|
1997-04-26 17:58:21 +04:00
|
|
|
*mount_flags |= EXT2_MF_ISROOT;
|
2007-11-09 04:20:15 +03:00
|
|
|
fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
|
1997-04-26 17:58:21 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EROFS)
|
|
|
|
*mount_flags |= EXT2_MF_READONLY;
|
|
|
|
} else
|
|
|
|
close(fd);
|
2001-06-15 22:33:43 +04:00
|
|
|
(void) unlink(TEST_FILE);
|
1997-04-26 17:58:21 +04:00
|
|
|
}
|
2001-09-14 15:44:25 +04:00
|
|
|
retval = 0;
|
2003-07-06 08:36:48 +04:00
|
|
|
errout:
|
2001-05-25 20:32:53 +04:00
|
|
|
endmntent (f);
|
2001-09-14 15:44:25 +04:00
|
|
|
return retval;
|
1997-04-26 17:58:21 +04:00
|
|
|
}
|
2001-04-17 06:22:05 +04:00
|
|
|
|
|
|
|
static errcode_t check_mntent(const char *file, int *mount_flags,
|
|
|
|
char *mtpt, int mtlen)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
|
2001-09-14 15:44:25 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
retval = check_mntent_file("/tmp/mtab", file, mount_flags,
|
|
|
|
mtpt, mtlen);
|
|
|
|
if (retval == 0)
|
|
|
|
return 0;
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* DEBUG */
|
2001-04-17 06:22:05 +04:00
|
|
|
#ifdef __linux__
|
|
|
|
retval = check_mntent_file("/proc/mounts", file, mount_flags,
|
|
|
|
mtpt, mtlen);
|
2002-10-31 21:23:37 +03:00
|
|
|
if (retval == 0 && (*mount_flags != 0))
|
2001-04-17 06:22:05 +04:00
|
|
|
return 0;
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* __linux__ */
|
2002-06-28 05:19:45 +04:00
|
|
|
#if defined(MOUNTED) || defined(_PATH_MOUNTED)
|
|
|
|
#ifndef MOUNTED
|
|
|
|
#define MOUNTED _PATH_MOUNTED
|
|
|
|
#endif /* MOUNTED */
|
2001-04-17 06:22:05 +04:00
|
|
|
retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
|
|
|
|
return retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
#else
|
2002-06-28 05:19:45 +04:00
|
|
|
*mount_flags = 0;
|
|
|
|
return 0;
|
|
|
|
#endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
|
2001-04-17 06:22:05 +04:00
|
|
|
}
|
|
|
|
|
2003-07-06 08:36:48 +04:00
|
|
|
#else
|
|
|
|
#if defined(HAVE_GETMNTINFO)
|
1997-04-26 17:58:21 +04:00
|
|
|
|
2001-01-03 17:56:46 +03:00
|
|
|
static errcode_t check_getmntinfo(const char *file, int *mount_flags,
|
|
|
|
char *mtpt, int mtlen)
|
1997-04-26 17:58:21 +04:00
|
|
|
{
|
|
|
|
struct statfs *mp;
|
|
|
|
int len, n;
|
|
|
|
const char *s1;
|
|
|
|
char *s2;
|
|
|
|
|
|
|
|
n = getmntinfo(&mp, MNT_NOWAIT);
|
|
|
|
if (n == 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
len = sizeof(_PATH_DEV) - 1;
|
|
|
|
s1 = file;
|
|
|
|
if (strncmp(_PATH_DEV, s1, len) == 0)
|
|
|
|
s1 += len;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-26 17:58:21 +04:00
|
|
|
*mount_flags = 0;
|
|
|
|
while (--n >= 0) {
|
|
|
|
s2 = mp->f_mntfromname;
|
|
|
|
if (strncmp(_PATH_DEV, s2, len) == 0) {
|
|
|
|
s2 += len - 1;
|
|
|
|
*s2 = 'r';
|
|
|
|
}
|
|
|
|
if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
|
|
|
|
*mount_flags = EXT2_MF_MOUNTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++mp;
|
|
|
|
}
|
2001-01-03 17:56:46 +03:00
|
|
|
if (mtpt)
|
|
|
|
strncpy(mtpt, mp->f_mntonname, mtlen);
|
1997-04-26 17:58:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GETMNTINFO */
|
2003-07-06 08:36:48 +04:00
|
|
|
#endif /* HAVE_MNTENT_H */
|
1997-04-26 17:58:21 +04:00
|
|
|
|
2002-04-28 01:07:52 +04:00
|
|
|
/*
|
|
|
|
* 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 */
|
2007-10-20 22:08:40 +04:00
|
|
|
if (fgets(buf, sizeof(buf), f))
|
|
|
|
while (fgets(buf, sizeof(buf), f)) {
|
2002-04-28 01:07:52 +04:00
|
|
|
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__ */
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-26 17:58:21 +04:00
|
|
|
/*
|
2005-06-06 00:05:22 +04:00
|
|
|
* 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.
|
2001-01-03 17:56:46 +03:00
|
|
|
*/
|
|
|
|
#ifdef __TURBOC__
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
#pragma argsused
|
2001-01-03 17:56:46 +03:00
|
|
|
#endif
|
|
|
|
errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
|
|
|
|
char *mtpt, int mtlen)
|
|
|
|
{
|
2005-06-06 00:05:22 +04:00
|
|
|
struct stat st_buf;
|
|
|
|
errcode_t retval = 0;
|
|
|
|
int fd;
|
|
|
|
|
2001-12-24 23:20:22 +03:00
|
|
|
if (is_swap_device(device)) {
|
|
|
|
*mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
|
|
|
|
strncpy(mtpt, "<swap>", mtlen);
|
2005-06-06 00:05:22 +04:00
|
|
|
} else {
|
2001-01-03 17:56:46 +03:00
|
|
|
#ifdef HAVE_MNTENT_H
|
2005-06-06 00:05:22 +04:00
|
|
|
retval = check_mntent(device, mount_flags, mtpt, mtlen);
|
2008-08-28 07:07:54 +04:00
|
|
|
#else
|
2001-01-03 17:56:46 +03:00
|
|
|
#ifdef HAVE_GETMNTINFO
|
2005-06-06 00:05:22 +04:00
|
|
|
retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
|
2001-01-03 17:56:46 +03:00
|
|
|
#else
|
2003-03-06 19:09:18 +03:00
|
|
|
#ifdef __GNUC__
|
2003-07-06 08:36:48 +04:00
|
|
|
#warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
|
2003-03-06 19:09:18 +03:00
|
|
|
#endif
|
2005-06-06 00:05:22 +04:00
|
|
|
*mount_flags = 0;
|
2001-01-03 17:56:46 +03:00
|
|
|
#endif /* HAVE_GETMNTINFO */
|
|
|
|
#endif /* HAVE_MNTENT_H */
|
2005-06-06 00:05:22 +04:00
|
|
|
}
|
|
|
|
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);
|
2006-05-30 18:28:22 +04:00
|
|
|
#endif
|
2005-06-06 00:05:22 +04:00
|
|
|
|
|
|
|
return 0;
|
2001-01-03 17:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-06-14 11:00:55 +04:00
|
|
|
* ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
|
2001-05-25 21:14:23 +04:00
|
|
|
* EXT2_MF_READONLY, and EXT2_MF_ROOT
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
1997-04-26 17:58:21 +04:00
|
|
|
*/
|
|
|
|
errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
|
|
|
|
{
|
2001-05-25 21:14:23 +04:00
|
|
|
return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
|
1997-04-26 17:58:21 +04:00
|
|
|
}
|
2001-04-17 06:22:05 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int retval, mount_flags;
|
2001-12-24 09:40:35 +03:00
|
|
|
char mntpt[80];
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-04-17 06:22:05 +04:00
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "Usage: %s device\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-12-24 09:40:35 +03:00
|
|
|
mntpt[0] = 0;
|
|
|
|
retval = ext2fs_check_mount_point(argv[1], &mount_flags,
|
|
|
|
mntpt, sizeof(mntpt));
|
2001-04-17 06:22:05 +04:00
|
|
|
if (retval) {
|
|
|
|
com_err(argv[0], retval,
|
|
|
|
"while calling ext2fs_check_if_mounted");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Device %s reports flags %02x\n", argv[1], mount_flags);
|
2005-06-06 00:05:22 +04:00
|
|
|
if (mount_flags & EXT2_MF_BUSY)
|
|
|
|
printf("\t%s is apparently in use.\n", argv[1]);
|
2001-04-17 06:22:05 +04:00
|
|
|
if (mount_flags & EXT2_MF_MOUNTED)
|
2001-05-25 20:32:53 +04:00
|
|
|
printf("\t%s is mounted.\n", argv[1]);
|
2001-12-24 23:20:22 +03:00
|
|
|
if (mount_flags & EXT2_MF_SWAP)
|
|
|
|
printf("\t%s is a swap device.\n", argv[1]);
|
2001-04-17 06:22:05 +04:00
|
|
|
if (mount_flags & EXT2_MF_READONLY)
|
2001-05-25 20:32:53 +04:00
|
|
|
printf("\t%s is read-only.\n", argv[1]);
|
2001-04-17 06:22:05 +04:00
|
|
|
if (mount_flags & EXT2_MF_ISROOT)
|
2001-05-25 20:32:53 +04:00
|
|
|
printf("\t%s is the root filesystem.\n", argv[1]);
|
2001-12-24 09:40:35 +03:00
|
|
|
if (mntpt[0])
|
|
|
|
printf("\t%s is mounted on %s.\n", argv[1], mntpt);
|
2001-04-17 06:22:05 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
2002-04-28 01:07:52 +04:00
|
|
|
#endif /* DEBUG */
|