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>
61 * Check to see if a regular file is mounted.
62 * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
63 * because the name in /proc/mounts is a loopback device not a regular file.
65 static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
66 dev_t file_dev, ino_t file_ino)
68 #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
69 struct loop_info64 loopinfo = {0, };
72 if (major(mnt_rdev) == LOOP_MAJOR) {
73 loop_fd = open(mnt_fsname, O_RDONLY);
77 ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
82 if (file_dev == loopinfo.lo_device &&
83 file_ino == loopinfo.lo_inode)
86 #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
91 * Helper function which checks a file in /etc/mtab format to see if a
92 * filesystem is mounted. Returns an error if the file doesn't exist
95 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
96 int *mount_flags, char *mtpt, int mtlen)
100 errcode_t retval = 0;
101 dev_t file_dev=0, file_rdev=0;
108 if ((f = setmntent (mtab_file, "r")) == NULL) {
109 if (errno == ENOENT) {
110 if (getenv("EXT2FS_NO_MTAB_OK"))
113 return EXT2_ET_NO_MTAB_FILE;
117 if (stat(file, &st_buf) == 0) {
118 if (S_ISBLK(st_buf.st_mode)) {
119 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
120 file_rdev = st_buf.st_rdev;
123 file_dev = st_buf.st_dev;
124 file_ino = st_buf.st_ino;
127 while ((mnt = getmntent (f)) != NULL) {
128 if (mnt->mnt_fsname[0] != '/')
130 if (strcmp(file, mnt->mnt_fsname) == 0)
132 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
133 if (S_ISBLK(st_buf.st_mode)) {
135 if (file_rdev && (file_rdev == st_buf.st_rdev))
137 if (check_loop_mounted(mnt->mnt_fsname,
138 st_buf.st_rdev, file_dev,
143 if (file_dev && ((file_dev == st_buf.st_dev) &&
144 (file_ino == st_buf.st_ino)))
151 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
153 * Do an extra check to see if this is the root device. We
154 * can't trust /etc/mtab, and /proc/mounts will only list
155 * /dev/root for the root filesystem. Argh. Instead we
156 * check if the given device has the same major/minor number
157 * as the device that the root directory is on.
159 if (file_rdev && stat("/", &st_buf) == 0) {
160 if (st_buf.st_dev == file_rdev) {
161 *mount_flags = EXT2_MF_MOUNTED;
163 strncpy(mtpt, "/", mtlen);
170 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
171 /* Validate the entry in case /etc/mtab is out of date */
173 * We need to be paranoid, because some broken distributions
174 * (read: Slackware) don't initialize /etc/mtab before checking
175 * all of the non-root filesystems on the disk.
177 if (stat(mnt->mnt_dir, &st_buf) < 0) {
179 if (retval == ENOENT) {
181 printf("Bogus entry in %s! (%s does not exist)\n",
182 mtab_file, mnt->mnt_dir);
188 if (file_rdev && (st_buf.st_dev != file_rdev)) {
190 printf("Bogus entry in %s! (%s not mounted on %s)\n",
191 mtab_file, file, mnt->mnt_dir);
196 *mount_flags = EXT2_MF_MOUNTED;
199 /* Check to see if the ro option is set */
200 if (hasmntopt(mnt, MNTOPT_RO))
201 *mount_flags |= EXT2_MF_READONLY;
205 strncpy(mtpt, mnt->mnt_dir, mtlen);
207 * Check to see if we're referring to the root filesystem.
208 * If so, do a manual check to see if we can open /etc/mtab
209 * read/write, since if the root is mounted read/only, the
210 * contents of /etc/mtab may not be accurate.
212 if (!strcmp(mnt->mnt_dir, "/")) {
214 #define TEST_FILE "/.ismount-test-file"
215 *mount_flags |= EXT2_MF_ISROOT;
216 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
219 *mount_flags |= EXT2_MF_READONLY;
222 (void) unlink(TEST_FILE);
230 static errcode_t check_mntent(const char *file, int *mount_flags,
231 char *mtpt, int mtlen)
236 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
242 retval = check_mntent_file("/proc/mounts", file, mount_flags,
244 if (retval == 0 && (*mount_flags != 0))
246 #endif /* __linux__ */
247 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
249 #define MOUNTED _PATH_MOUNTED
251 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
256 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
260 #if defined(HAVE_GETMNTINFO)
262 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
263 char *mtpt, int mtlen)
270 n = getmntinfo(&mp, MNT_NOWAIT);
274 len = sizeof(_PATH_DEV) - 1;
276 if (strncmp(_PATH_DEV, s1, len) == 0)
281 s2 = mp->f_mntfromname;
282 if (strncmp(_PATH_DEV, s2, len) == 0) {
286 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
287 *mount_flags = EXT2_MF_MOUNTED;
293 strncpy(mtpt, mp->f_mntonname, mtlen);
296 #endif /* HAVE_GETMNTINFO */
297 #endif /* HAVE_SETMNTENT */
300 * Check to see if we're dealing with the swap device.
302 static int is_swap_device(const char *file)
311 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
312 if ((stat(file, &st_buf) == 0) &&
313 S_ISBLK(st_buf.st_mode))
314 file_dev = st_buf.st_rdev;
317 if (!(f = fopen("/proc/swaps", "r")))
319 /* Skip the first line */
320 if (!fgets(buf, sizeof(buf), f))
322 if (*buf && strncmp(buf, "Filename\t", 9))
323 /* Linux <=2.6.19 contained a bug in the /proc/swaps
324 * code where the header would not be displayed
326 goto valid_first_line;
328 while (fgets(buf, sizeof(buf), f)) {
330 if ((cp = strchr(buf, ' ')) != NULL)
332 if ((cp = strchr(buf, '\t')) != NULL)
334 if (strcmp(buf, file) == 0) {
339 if (file_dev && (stat(buf, &st_buf) == 0) &&
340 S_ISBLK(st_buf.st_mode) &&
341 file_dev == st_buf.st_rdev) {
355 * ext2fs_check_mount_point() fills determines if the device is
356 * mounted or otherwise busy, and fills in mount_flags with one or
357 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
358 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
359 * non-NULL, the directory where the device is mounted is copied to
360 * where mtpt is pointing, up to mtlen characters.
365 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
366 char *mtpt, int mtlen)
368 errcode_t retval = 0;
370 if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
371 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
372 if (getenv("EXT2FS_PRETEND_ROOTFS"))
373 *mount_flags = EXT2_MF_ISROOT;
376 if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
377 *mount_flags = EXT2_MF_MOUNTED;
378 if (getenv("EXT2FS_PRETEND_ROOTFS"))
379 *mount_flags = EXT2_MF_ISROOT;
383 if (is_swap_device(device)) {
384 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
385 strncpy(mtpt, "<swap>", mtlen);
387 #ifdef HAVE_SETMNTENT
388 retval = check_mntent(device, mount_flags, mtpt, mtlen);
390 #ifdef HAVE_GETMNTINFO
391 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
394 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
397 #endif /* HAVE_GETMNTINFO */
398 #endif /* HAVE_SETMNTENT */
403 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
407 if (stat(device, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) {
408 int fd = open(device, O_RDONLY | O_EXCL);
412 else if (errno == EBUSY)
413 *mount_flags |= EXT2_MF_BUSY;
422 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
423 * EXT2_MF_READONLY, and EXT2_MF_ROOT
426 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
428 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
432 int main(int argc, char **argv)
434 int retval, mount_flags;
438 fprintf(stderr, "Usage: %s device\n", argv[0]);
442 add_error_table(&et_ext2_error_table);
444 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
445 mntpt, sizeof(mntpt));
447 com_err(argv[0], retval,
448 "while calling ext2fs_check_if_mounted");
451 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
452 if (mount_flags & EXT2_MF_BUSY)
453 printf("\t%s is apparently in use.\n", argv[1]);
454 if (mount_flags & EXT2_MF_MOUNTED)
455 printf("\t%s is mounted.\n", argv[1]);
456 if (mount_flags & EXT2_MF_SWAP)
457 printf("\t%s is a swap device.\n", argv[1]);
458 if (mount_flags & EXT2_MF_READONLY)
459 printf("\t%s is read-only.\n", argv[1]);
460 if (mount_flags & EXT2_MF_ISROOT)
461 printf("\t%s is the root filesystem.\n", argv[1]);
463 printf("\t%s is mounted on %s.\n", argv[1], mntpt);