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)
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 (stat(mnt->mnt_dir, &st_buf) != 0)
133 if (strcmp(file, mnt->mnt_fsname) == 0) {
134 if (file_rdev && (file_rdev != st_buf.st_dev)) {
136 printf("Bogus entry in %s! "
137 "(%s does not exist)\n",
138 mtab_file, mnt->mnt_dir);
144 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
145 if (ext2fsP_is_disk_device(st_buf.st_mode)) {
147 if (file_rdev && (file_rdev == st_buf.st_rdev))
149 if (check_loop_mounted(mnt->mnt_fsname,
150 st_buf.st_rdev, file_dev,
155 if (file_dev && ((file_dev == st_buf.st_dev) &&
156 (file_ino == st_buf.st_ino)))
163 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
165 * Do an extra check to see if this is the root device. We
166 * can't trust /etc/mtab, and /proc/mounts will only list
167 * /dev/root for the root filesystem. Argh. Instead we
168 * check if the given device has the same major/minor number
169 * as the device that the root directory is on.
171 if (file_rdev && stat("/", &st_buf) == 0) {
172 if (st_buf.st_dev == file_rdev) {
173 *mount_flags = EXT2_MF_MOUNTED;
175 strncpy(mtpt, "/", mtlen);
182 *mount_flags = EXT2_MF_MOUNTED;
185 /* Check to see if the ro option is set */
186 if (hasmntopt(mnt, MNTOPT_RO))
187 *mount_flags |= EXT2_MF_READONLY;
191 strncpy(mtpt, mnt->mnt_dir, mtlen);
193 * Check to see if we're referring to the root filesystem.
194 * If so, do a manual check to see if we can open /etc/mtab
195 * read/write, since if the root is mounted read/only, the
196 * contents of /etc/mtab may not be accurate.
198 if (!strcmp(mnt->mnt_dir, "/")) {
200 #define TEST_FILE "/.ismount-test-file"
201 *mount_flags |= EXT2_MF_ISROOT;
202 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
205 *mount_flags |= EXT2_MF_READONLY;
208 (void) unlink(TEST_FILE);
216 static errcode_t check_mntent(const char *file, int *mount_flags,
217 char *mtpt, int mtlen)
222 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
228 retval = check_mntent_file("/proc/mounts", file, mount_flags,
232 #endif /* __linux__ */
233 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
235 #define MOUNTED _PATH_MOUNTED
237 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
242 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
246 #if defined(HAVE_GETMNTINFO)
248 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
249 char *mtpt, int mtlen)
256 n = getmntinfo(&mp, MNT_NOWAIT);
260 len = sizeof(_PATH_DEV) - 1;
262 if (strncmp(_PATH_DEV, s1, len) == 0)
267 s2 = mp->f_mntfromname;
268 if (strncmp(_PATH_DEV, s2, len) == 0) {
272 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
273 *mount_flags = EXT2_MF_MOUNTED;
279 strncpy(mtpt, mp->f_mntonname, mtlen);
282 #endif /* HAVE_GETMNTINFO */
283 #endif /* HAVE_SETMNTENT */
286 * Check to see if we're dealing with the swap device.
288 static int is_swap_device(const char *file)
297 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
298 if ((stat(file, &st_buf) == 0) &&
299 ext2fsP_is_disk_device(st_buf.st_mode))
300 file_dev = st_buf.st_rdev;
303 if (!(f = fopen("/proc/swaps", "r")))
305 /* Skip the first line */
306 if (!fgets(buf, sizeof(buf), f))
308 if (*buf && strncmp(buf, "Filename\t", 9))
309 /* Linux <=2.6.19 contained a bug in the /proc/swaps
310 * code where the header would not be displayed
312 goto valid_first_line;
314 while (fgets(buf, sizeof(buf), f)) {
316 if ((cp = strchr(buf, ' ')) != NULL)
318 if ((cp = strchr(buf, '\t')) != NULL)
320 if (strcmp(buf, file) == 0) {
325 if (file_dev && (stat(buf, &st_buf) == 0) &&
326 ext2fsP_is_disk_device(st_buf.st_mode) &&
327 file_dev == st_buf.st_rdev) {
341 * ext2fs_check_mount_point() fills determines if the device is
342 * mounted or otherwise busy, and fills in mount_flags with one or
343 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
344 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
345 * non-NULL, the directory where the device is mounted is copied to
346 * where mtpt is pointing, up to mtlen characters.
351 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
352 char *mtpt, int mtlen)
354 errcode_t retval = 0;
356 if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
357 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
358 if (getenv("EXT2FS_PRETEND_ROOTFS"))
359 *mount_flags = EXT2_MF_ISROOT;
362 if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
363 *mount_flags = EXT2_MF_MOUNTED;
364 if (getenv("EXT2FS_PRETEND_ROOTFS"))
365 *mount_flags = EXT2_MF_ISROOT;
369 if (is_swap_device(device)) {
370 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
371 strncpy(mtpt, "<swap>", mtlen);
373 #ifdef HAVE_SETMNTENT
374 retval = check_mntent(device, mount_flags, mtpt, mtlen);
376 #ifdef HAVE_GETMNTINFO
377 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
380 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
383 #endif /* HAVE_GETMNTINFO */
384 #endif /* HAVE_SETMNTENT */
389 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
393 if (stat(device, &st_buf) == 0 &&
394 ext2fsP_is_disk_device(st_buf.st_mode)) {
395 int fd = open(device, O_RDONLY | O_EXCL);
399 else if (errno == EBUSY)
400 *mount_flags |= EXT2_MF_BUSY;
409 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
410 * EXT2_MF_READONLY, and EXT2_MF_ROOT
413 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
415 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
419 int main(int argc, char **argv)
421 int retval, mount_flags;
425 fprintf(stderr, "Usage: %s device\n", argv[0]);
429 add_error_table(&et_ext2_error_table);
431 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
432 mntpt, sizeof(mntpt));
434 com_err(argv[0], retval,
435 "while calling ext2fs_check_if_mounted");
438 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
439 if (mount_flags & EXT2_MF_BUSY)
440 printf("\t%s is apparently in use.\n", argv[1]);
441 if (mount_flags & EXT2_MF_MOUNTED)
442 printf("\t%s is mounted.\n", argv[1]);
443 if (mount_flags & EXT2_MF_SWAP)
444 printf("\t%s is a swap device.\n", argv[1]);
445 if (mount_flags & EXT2_MF_READONLY)
446 printf("\t%s is read-only.\n", argv[1]);
447 if (mount_flags & EXT2_MF_ISROOT)
448 printf("\t%s is the root filesystem.\n", argv[1]);
450 printf("\t%s is mounted on %s.\n", argv[1], mntpt);