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 Public
20
#ifdef HAVE_LINUX_FD_H
26
#ifdef HAVE_GETMNTINFO
28
#include <sys/param.h>
29
#include <sys/mount.h>
30
#endif /* HAVE_GETMNTINFO */
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
43
static errcode_t check_mntent_file(const char *mtab_file, const char *file,
44
int *mount_flags, char *mtpt, int mtlen)
47
struct stat st_mntpnt, st_file;
53
if ((f = setmntent (mtab_file, "r")) == NULL)
55
while ((mnt = getmntent (f)) != NULL)
56
if (strcmp(file, mnt->mnt_fsname) == 0)
60
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
62
* Do an extra check to see if this is the root device. We
63
* can't trust /etc/mtab, and /proc/mounts will only list
64
* /dev/root for the root filesystem. Argh. Instead we
65
* check if the given device has the same major/minor number
66
* as the device that the root directory is on.
68
if (stat("/", &st_mntpnt) == 0 && stat(file, &st_file) == 0) {
69
if (st_mntpnt.st_dev == st_file.st_rdev) {
70
*mount_flags = EXT2_MF_MOUNTED;
72
strncpy(mtpt, "/", mtlen);
79
#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
80
/* Validate the entry in case /etc/mtab is out of date */
82
* We need to be paranoid, because some broken distributions
83
* (read: Slackware) don't initialize /etc/mtab before checking
84
* all of the non-root filesystems on the disk.
86
if (stat(mnt->mnt_dir, &st_mntpnt) < 0) {
88
if (retval == ENOENT) {
90
printf("Bogus entry in %s! (%s does not exist)\n",
91
mtab_file, mnt->mnt_dir);
97
if (stat(file, &st_file) == 0) {
98
if (st_mntpnt.st_dev != st_file.st_rdev) {
100
printf("Bogus entry in %s! (%s not mounted on %s)\n",
101
mtab_file, file, mnt->mnt_dir);
107
*mount_flags = EXT2_MF_MOUNTED;
109
/* Check to see if the ro option is set */
110
if (hasmntopt(mnt, MNTOPT_RO))
111
*mount_flags |= EXT2_MF_READONLY;
114
strncpy(mtpt, mnt->mnt_dir, mtlen);
116
* Check to see if we're referring to the root filesystem.
117
* If so, do a manual check to see if we can open /etc/mtab
118
* read/write, since if the root is mounted read/only, the
119
* contents of /etc/mtab may not be accurate.
121
if (!strcmp(mnt->mnt_dir, "/")) {
123
#define TEST_FILE "/.ismount-test-file"
124
*mount_flags |= EXT2_MF_ISROOT;
125
fd = open(TEST_FILE, O_RDWR|O_CREAT);
128
*mount_flags |= EXT2_MF_READONLY;
131
(void) unlink(TEST_FILE);
139
static errcode_t check_mntent(const char *file, int *mount_flags,
140
char *mtpt, int mtlen)
145
retval = check_mntent_file("/tmp/mtab", file, mount_flags,
151
retval = check_mntent_file("/proc/mounts", file, mount_flags,
156
retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
160
#elif defined(HAVE_GETMNTINFO)
162
static errcode_t check_getmntinfo(const char *file, int *mount_flags,
163
char *mtpt, int mtlen)
170
n = getmntinfo(&mp, MNT_NOWAIT);
174
len = sizeof(_PATH_DEV) - 1;
176
if (strncmp(_PATH_DEV, s1, len) == 0)
181
s2 = mp->f_mntfromname;
182
if (strncmp(_PATH_DEV, s2, len) == 0) {
186
if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
187
*mount_flags = EXT2_MF_MOUNTED;
193
strncpy(mtpt, mp->f_mntonname, mtlen);
196
#endif /* HAVE_GETMNTINFO */
199
* ext2fs_check_mount_point() returns 1 if the device is mounted, 0
200
* otherwise. If mtpt is non-NULL, the directory where the device is
201
* mounted is copied to where mtpt is pointing, up to mtlen
207
errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
208
char *mtpt, int mtlen)
211
return check_mntent(device, mount_flags, mtpt, mtlen);
213
#ifdef HAVE_GETMNTINFO
214
return check_getmntinfo(device, mount_flags, mtpt, mtlen);
216
#warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
219
#endif /* HAVE_GETMNTINFO */
220
#endif /* HAVE_MNTENT_H */
224
* ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
225
* EXT2_MF_READONLY, and EXT2_MF_ROOT
228
errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
230
return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
234
int main(int argc, char **argv)
236
int retval, mount_flags;
239
fprintf(stderr, "Usage: %s device\n", argv[0]);
243
retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
245
com_err(argv[0], retval,
246
"while calling ext2fs_check_if_mounted");
249
printf("Device %s reports flags %02x\n", argv[1], mount_flags);
250
if (mount_flags & EXT2_MF_MOUNTED)
251
printf("\t%s is mounted.\n", argv[1]);
253
if (mount_flags & EXT2_MF_READONLY)
254
printf("\t%s is read-only.\n", argv[1]);
256
if (mount_flags & EXT2_MF_ISROOT)
257
printf("\t%s is the root filesystem.\n", argv[1]);