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.
21
#ifdef HAVE_LINUX_FD_H
27
#ifdef HAVE_GETMNTINFO
29
#include <sys/param.h>
30
#include <sys/mount.h>
31
#endif /* HAVE_GETMNTINFO */
40
* Helper function which checks a file in /etc/mtab format to see if a
41
* filesystem is mounted. Returns an error if the file doesn't exist
44
static errcode_t check_mntent_file(const char *mtab_file, const char *file,
45
int *mount_flags, char *mtpt, int mtlen)
50
dev_t file_dev=0, file_rdev=0;
56
if ((f = setmntent (mtab_file, "r")) == NULL)
57
return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno);
58
if (stat(file, &st_buf) == 0) {
59
if (S_ISBLK(st_buf.st_mode)) {
60
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
61
file_rdev = st_buf.st_rdev;
64
file_dev = st_buf.st_dev;
65
file_ino = st_buf.st_ino;
68
while ((mnt = getmntent (f)) != NULL) {
69
if (mnt->mnt_fsname[0] != '/')
71
if (strcmp(file, mnt->mnt_fsname) == 0)
73
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
74
if (S_ISBLK(st_buf.st_mode)) {
76
if (file_rdev && (file_rdev == st_buf.st_rdev))
80
if (file_dev && ((file_dev == st_buf.st_dev) &&
81
(file_ino == st_buf.st_ino)))
88
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
90
* Do an extra check to see if this is the root device. We
91
* can't trust /etc/mtab, and /proc/mounts will only list
92
* /dev/root for the root filesystem. Argh. Instead we
93
* check if the given device has the same major/minor number
94
* as the device that the root directory is on.
96
if (file_rdev && stat("/", &st_buf) == 0) {
97
if (st_buf.st_dev == file_rdev) {
98
*mount_flags = EXT2_MF_MOUNTED;
100
strncpy(mtpt, "/", mtlen);
107
#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
108
/* Validate the entry in case /etc/mtab is out of date */
110
* We need to be paranoid, because some broken distributions
111
* (read: Slackware) don't initialize /etc/mtab before checking
112
* all of the non-root filesystems on the disk.
114
if (stat(mnt->mnt_dir, &st_buf) < 0) {
116
if (retval == ENOENT) {
118
printf("Bogus entry in %s! (%s does not exist)\n",
119
mtab_file, mnt->mnt_dir);
125
if (file_rdev && (st_buf.st_dev != file_rdev)) {
127
printf("Bogus entry in %s! (%s not mounted on %s)\n",
128
mtab_file, file, mnt->mnt_dir);
133
*mount_flags = EXT2_MF_MOUNTED;
136
/* Check to see if the ro option is set */
137
if (hasmntopt(mnt, MNTOPT_RO))
138
*mount_flags |= EXT2_MF_READONLY;
142
strncpy(mtpt, mnt->mnt_dir, mtlen);
144
* Check to see if we're referring to the root filesystem.
145
* If so, do a manual check to see if we can open /etc/mtab
146
* read/write, since if the root is mounted read/only, the
147
* contents of /etc/mtab may not be accurate.
149
if (!strcmp(mnt->mnt_dir, "/")) {
151
#define TEST_FILE "/.ismount-test-file"
152
*mount_flags |= EXT2_MF_ISROOT;
153
fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
156
*mount_flags |= EXT2_MF_READONLY;
159
(void) unlink(TEST_FILE);
167
static errcode_t check_mntent(const char *file, int *mount_flags,
168
char *mtpt, int mtlen)
173
retval = check_mntent_file("/tmp/mtab", file, mount_flags,
179
retval = check_mntent_file("/proc/mounts", file, mount_flags,
181
if (retval == 0 && (*mount_flags != 0))
183
#endif /* __linux__ */
184
#if defined(MOUNTED) || defined(_PATH_MOUNTED)
186
#define MOUNTED _PATH_MOUNTED
188
retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
193
#endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
197
#if defined(HAVE_GETMNTINFO)
199
static errcode_t check_getmntinfo(const char *file, int *mount_flags,
200
char *mtpt, int mtlen)
207
n = getmntinfo(&mp, MNT_NOWAIT);
211
len = sizeof(_PATH_DEV) - 1;
213
if (strncmp(_PATH_DEV, s1, len) == 0)
218
s2 = mp->f_mntfromname;
219
if (strncmp(_PATH_DEV, s2, len) == 0) {
223
if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
224
*mount_flags = EXT2_MF_MOUNTED;
230
strncpy(mtpt, mp->f_mntonname, mtlen);
233
#endif /* HAVE_GETMNTINFO */
234
#endif /* HAVE_MNTENT_H */
237
* Check to see if we're dealing with the swap device.
239
static int is_swap_device(const char *file)
248
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
249
if ((stat(file, &st_buf) == 0) &&
250
S_ISBLK(st_buf.st_mode))
251
file_dev = st_buf.st_rdev;
254
if (!(f = fopen("/proc/swaps", "r")))
256
/* Skip the first line */
257
if (!fgets(buf, sizeof(buf), f))
259
if (*buf && strncmp(buf, "Filename\t", 9))
260
/* Linux <=2.6.19 contained a bug in the /proc/swaps
261
* code where the header would not be displayed
263
goto valid_first_line;
265
while (fgets(buf, sizeof(buf), f)) {
267
if ((cp = strchr(buf, ' ')) != NULL)
269
if ((cp = strchr(buf, '\t')) != NULL)
271
if (strcmp(buf, file) == 0) {
276
if (file_dev && (stat(buf, &st_buf) == 0) &&
277
S_ISBLK(st_buf.st_mode) &&
278
file_dev == st_buf.st_rdev) {
292
* ext2fs_check_mount_point() fills determines if the device is
293
* mounted or otherwise busy, and fills in mount_flags with one or
294
* more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
295
* EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
296
* non-NULL, the directory where the device is mounted is copied to
297
* where mtpt is pointing, up to mtlen characters.
302
errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
303
char *mtpt, int mtlen)
306
errcode_t retval = 0;
309
if (is_swap_device(device)) {
310
*mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
311
strncpy(mtpt, "<swap>", mtlen);
314
retval = check_mntent(device, mount_flags, mtpt, mtlen);
316
#ifdef HAVE_GETMNTINFO
317
retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
320
#warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
323
#endif /* HAVE_GETMNTINFO */
324
#endif /* HAVE_MNTENT_H */
329
#ifdef __linux__ /* This only works on Linux 2.6+ systems */
330
if ((stat(device, &st_buf) != 0) ||
331
!S_ISBLK(st_buf.st_mode))
333
fd = open(device, O_RDONLY | O_EXCL);
336
*mount_flags |= EXT2_MF_BUSY;
345
* ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
346
* EXT2_MF_READONLY, and EXT2_MF_ROOT
349
errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
351
return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
355
int main(int argc, char **argv)
357
int retval, mount_flags;
361
fprintf(stderr, "Usage: %s device\n", argv[0]);
365
add_error_table(&et_ext2_error_table);
367
retval = ext2fs_check_mount_point(argv[1], &mount_flags,
368
mntpt, sizeof(mntpt));
370
com_err(argv[0], retval,
371
"while calling ext2fs_check_if_mounted");
374
printf("Device %s reports flags %02x\n", argv[1], mount_flags);
375
if (mount_flags & EXT2_MF_BUSY)
376
printf("\t%s is apparently in use.\n", argv[1]);
377
if (mount_flags & EXT2_MF_MOUNTED)
378
printf("\t%s is mounted.\n", argv[1]);
379
if (mount_flags & EXT2_MF_SWAP)
380
printf("\t%s is a swap device.\n", argv[1]);
381
if (mount_flags & EXT2_MF_READONLY)
382
printf("\t%s is read-only.\n", argv[1]);
383
if (mount_flags & EXT2_MF_ISROOT)
384
printf("\t%s is the root filesystem.\n", argv[1]);
386
printf("\t%s is mounted on %s.\n", argv[1], mntpt);