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>
63 * Check to see if a regular file is mounted.
64 * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
65 * because the name in /proc/mounts is a loopback device not a regular file.
67 static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
68 dev_t file_dev, ino_t file_ino)
70 #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
71 struct loop_info64 loopinfo = {0, };
74 if (major(mnt_rdev) == LOOP_MAJOR) {
75 loop_fd = open(mnt_fsname, O_RDONLY);
79 ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
84 if (file_dev == loopinfo.lo_device &&
85 file_ino == loopinfo.lo_inode)
88 #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
93 * Helper function which checks a file in /etc/mtab format to see if a
94 * filesystem is mounted. Returns an error if the file doesn't exist
97 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
98 int *mount_flags, char *mtpt, int mtlen)
102 errcode_t retval = 0;
103 dev_t file_dev=0, file_rdev=0;
110 if ((f = setmntent (mtab_file, "r")) == NULL) {
111 if (errno == ENOENT) {
112 if (getenv("EXT2FS_NO_MTAB_OK"))
115 return EXT2_ET_NO_MTAB_FILE;
119 if (stat(file, &st_buf) == 0) {
120 if (ext2fsP_is_disk_device(st_buf.st_mode)) {
121 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
122 file_rdev = st_buf.st_rdev;
125 file_dev = st_buf.st_dev;
126 file_ino = st_buf.st_ino;
129 while ((mnt = getmntent (f)) != NULL) {
130 if (mnt->mnt_fsname[0] != '/')
132 if (stat(mnt->mnt_dir, &st_buf) != 0)
134 if (strcmp(file, mnt->mnt_fsname) == 0) {
135 if (file_rdev && (file_rdev != st_buf.st_dev)) {
137 printf("Bogus entry in %s! "
138 "(%s does not exist)\n",
139 mtab_file, mnt->mnt_dir);
145 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
146 if (ext2fsP_is_disk_device(st_buf.st_mode)) {
148 if (file_rdev && (file_rdev == st_buf.st_rdev))
150 if (check_loop_mounted(mnt->mnt_fsname,
151 st_buf.st_rdev, file_dev,
156 if (file_dev && ((file_dev == st_buf.st_dev) &&
157 (file_ino == st_buf.st_ino)))
164 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
166 * Do an extra check to see if this is the root device. We
167 * can't trust /etc/mtab, and /proc/mounts will only list
168 * /dev/root for the root filesystem. Argh. Instead we
169 * check if the given device has the same major/minor number
170 * as the device that the root directory is on.
172 if (file_rdev && stat("/", &st_buf) == 0) {
173 if (st_buf.st_dev == file_rdev) {
174 *mount_flags = EXT2_MF_MOUNTED;
176 strncpy(mtpt, "/", mtlen);
183 *mount_flags = EXT2_MF_MOUNTED;
186 /* Check to see if the ro option is set */
187 if (hasmntopt(mnt, MNTOPT_RO))
188 *mount_flags |= EXT2_MF_READONLY;
192 strncpy(mtpt, mnt->mnt_dir, mtlen);
194 * Check to see if we're referring to the root filesystem.
195 * If so, do a manual check to see if we can open /etc/mtab
196 * read/write, since if the root is mounted read/only, the
197 * contents of /etc/mtab may not be accurate.
199 if (!strcmp(mnt->mnt_dir, "/")) {
201 #define TEST_FILE "/.ismount-test-file"
202 *mount_flags |= EXT2_MF_ISROOT;
203 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
206 *mount_flags |= EXT2_MF_READONLY;
209 (void) unlink(TEST_FILE);
217 static errcode_t check_mntent(const char *file, int *mount_flags,
218 char *mtpt, int mtlen)
223 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
229 retval = check_mntent_file("/proc/mounts", file, mount_flags,
233 #endif /* __linux__ */
234 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
236 #define MOUNTED _PATH_MOUNTED
238 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
243 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
247 #if defined(HAVE_GETMNTINFO)
249 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
250 char *mtpt, int mtlen)
257 n = getmntinfo(&mp, MNT_NOWAIT);
261 len = sizeof(_PATH_DEV) - 1;
263 if (strncmp(_PATH_DEV, s1, len) == 0)
268 s2 = mp->f_mntfromname;
269 if (strncmp(_PATH_DEV, s2, len) == 0) {
273 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
274 *mount_flags = EXT2_MF_MOUNTED;
280 strncpy(mtpt, mp->f_mntonname, mtlen);
283 #endif /* HAVE_GETMNTINFO */
284 #endif /* HAVE_SETMNTENT */
287 * Check to see if we're dealing with the swap device.
289 static int is_swap_device(const char *file)
298 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
299 if ((stat(file, &st_buf) == 0) &&
300 ext2fsP_is_disk_device(st_buf.st_mode))
301 file_dev = st_buf.st_rdev;
304 if (!(f = fopen("/proc/swaps", "r")))
306 /* Skip the first line */
307 if (!fgets(buf, sizeof(buf), f))
309 if (*buf && strncmp(buf, "Filename\t", 9))
310 /* Linux <=2.6.19 contained a bug in the /proc/swaps
311 * code where the header would not be displayed
313 goto valid_first_line;
315 while (fgets(buf, sizeof(buf), f)) {
317 if ((cp = strchr(buf, ' ')) != NULL)
319 if ((cp = strchr(buf, '\t')) != NULL)
321 if (strcmp(buf, file) == 0) {
326 if (file_dev && (stat(buf, &st_buf) == 0) &&
327 ext2fsP_is_disk_device(st_buf.st_mode) &&
328 file_dev == st_buf.st_rdev) {
340 static int check_lustre_zfs(char *mnt_device, const char *real_devname)
342 char *real_mnt_device, *buf, *ptr;
346 real_mnt_device = malloc(PATH_MAX);
347 if (real_mnt_device == NULL) {
348 fprintf(stderr, "Cannot allocate memory to store path\n");
352 buf = malloc(PATH_MAX);
354 fprintf(stderr, "Cannot allocate memory to store command\n");
355 free(real_mnt_device);
359 /* Get the pool name from mnt_device */
360 ptr = strchr(mnt_device, '/');
364 memset(buf, 0, PATH_MAX);
365 sprintf(buf, "zpool list -Hv %s", mnt_device);
367 fp = popen(buf, "r");
371 memset(buf, 0, PATH_MAX);
372 fscanf(fp, "%s%*[^\n]", buf);
373 /* skip the first line */
376 /* skip empty line */
377 if (strlen(buf) == 0)
380 if (realpath(buf, real_mnt_device) == NULL)
382 if (strcmp(real_devname, real_mnt_device) == 0) {
383 fprintf(stderr, "device %s mounted by Lustre\n",
395 static int check_lustre_ldiskfs(const char *mnt_device,
396 const char *real_devname)
398 char *real_mnt_device;
400 real_mnt_device = malloc(PATH_MAX);
401 if (real_mnt_device == NULL) {
402 fprintf(stderr, "Cannot allocate memory to store path\n");
406 memset(real_mnt_device, 0, PATH_MAX);
407 if (realpath(mnt_device, real_mnt_device) == NULL) {
408 fprintf(stderr, "Cannot resolve mntdev %s\n", mnt_device);
409 free(real_mnt_device);
413 if (strcmp(real_devname, real_mnt_device) == 0) {
414 fprintf(stderr, "device %s mounted by lustre\n", real_devname);
415 free(real_mnt_device);
419 free(real_mnt_device);
423 static int check_lustre_proc_vals(const char *procname,const char *real_devname)
425 struct dirent *direntp;
428 char *mnt_device, *proc_val;
430 mnt_device = malloc(PATH_MAX);
431 if (mnt_device == NULL) {
432 fprintf(stderr, "Cannot allocate memory to store device\n");
435 proc_val = malloc(PATH_MAX);
436 if (proc_val == NULL) {
437 fprintf(stderr, "Cannot allocate memory to store proc\n");
442 dirp = opendir(procname);
448 direntp = readdir(dirp);
451 if ((strncmp(direntp->d_name, ".", 1) == 0) ||
452 (strncmp(direntp->d_name, "..", 2) == 0)) {
456 memset(proc_val, 0, PATH_MAX);
457 snprintf(proc_val, PATH_MAX, "%s/%s/mntdev", procname,
459 fd = open(proc_val, O_RDONLY);
461 if (errno == ENOENT || errno == ENOTDIR)
463 fprintf(stderr, "Cannot open %s: %s\n",
464 proc_val, strerror(errno));
469 memset(mnt_device, 0, PATH_MAX);
470 numr = read(fd, mnt_device, PATH_MAX);
472 fprintf(stderr, "Failure to get mntdev info\n");
479 ptr = strchr(mnt_device, '\n');
483 if (strstr(procname, "osd-zfs") != NULL)
484 rc = check_lustre_zfs(mnt_device, real_devname);
486 rc = check_lustre_ldiskfs(mnt_device,
489 } while (direntp != NULL && rc == 0);
500 static errcode_t check_if_lustre_mounted(const char *device, int *mount_flags)
505 real_device = malloc(PATH_MAX);
506 if (real_device == NULL) {
507 fprintf(stderr, "Cannot allocate memory to store path\n");
508 return EXT2_ET_NO_MEMORY;
511 if (realpath(device, real_device) == NULL) {
512 fprintf(stderr, "Cannot resolve path %s\n", device);
513 rc = EXT2_ET_BAD_DEVICE_NAME;
517 rc = check_lustre_proc_vals("/proc/fs/lustre/osd-ldiskfs", real_device);
519 rc = check_lustre_proc_vals("/proc/fs/lustre/osd-zfs",
522 *mount_flags |= EXT2_MF_MOUNTED;
532 * ext2fs_check_mount_point() fills determines if the device is
533 * mounted or otherwise busy, and fills in mount_flags with one or
534 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
535 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
536 * non-NULL, the directory where the device is mounted is copied to
537 * where mtpt is pointing, up to mtlen characters.
542 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
543 char *mtpt, int mtlen)
545 errcode_t retval = 0;
548 if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
549 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
550 if (getenv("EXT2FS_PRETEND_ROOTFS"))
551 *mount_flags = EXT2_MF_ISROOT;
554 if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
555 *mount_flags = EXT2_MF_MOUNTED;
556 if (getenv("EXT2FS_PRETEND_ROOTFS"))
557 *mount_flags = EXT2_MF_ISROOT;
561 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
565 if (stat(device, &st_buf) == 0 &&
566 ext2fsP_is_disk_device(st_buf.st_mode)) {
567 int fd = open(device, O_RDONLY | O_EXCL);
571 * The device is not busy so it's
572 * definitelly not mounted. No need to
573 * to perform any more checks.
578 } else if (errno == EBUSY) {
585 if (is_swap_device(device)) {
586 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
587 strncpy(mtpt, "<swap>", mtlen);
589 #ifdef HAVE_SETMNTENT
590 retval = check_mntent(device, mount_flags, mtpt, mtlen);
592 #ifdef HAVE_GETMNTINFO
593 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
596 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
599 #endif /* HAVE_GETMNTINFO */
600 #endif /* HAVE_SETMNTENT */
606 *mount_flags |= EXT2_MF_BUSY;
608 retval = check_if_lustre_mounted(device, mount_flags);
614 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
615 * EXT2_MF_READONLY, and EXT2_MF_ROOT
618 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
620 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
624 int main(int argc, char **argv)
626 int retval, mount_flags;
630 fprintf(stderr, "Usage: %s device\n", argv[0]);
634 add_error_table(&et_ext2_error_table);
636 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
637 mntpt, sizeof(mntpt));
639 com_err(argv[0], retval,
640 "while calling ext2fs_check_if_mounted");
643 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
644 if (mount_flags & EXT2_MF_BUSY)
645 printf("\t%s is apparently in use.\n", argv[1]);
646 if (mount_flags & EXT2_MF_MOUNTED)
647 printf("\t%s is mounted.\n", argv[1]);
648 if (mount_flags & EXT2_MF_SWAP)
649 printf("\t%s is a swap device.\n", argv[1]);
650 if (mount_flags & EXT2_MF_READONLY)
651 printf("\t%s is read-only.\n", argv[1]);
652 if (mount_flags & EXT2_MF_ISROOT)
653 printf("\t%s is the root filesystem.\n", argv[1]);
655 printf("\t%s is mounted on %s.\n", argv[1], mntpt);