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)
51 if ((f = setmntent (mtab_file, "r")) == NULL)
53 while ((mnt = getmntent (f)) != NULL)
54 if (strcmp(file, mnt->mnt_fsname) == 0)
58 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
59 struct stat st_root, st_file;
61 * Do an extra check to see if this is the root device. We
62 * can't trust /etc/fstab, and /proc/mounts will only list
63 * /dev/root for the root filesystem. Argh. Instead we
64 * check if the given device has the same major/minor number
65 * as the device that the root directory is on.
67 if (stat("/", &st_root) == 0 && stat(file, &st_file) == 0) {
68 if (st_root.st_dev == st_file.st_rdev) {
69 *mount_flags = EXT2_MF_MOUNTED;
71 strncpy(mtpt, "/", mtlen);
79 *mount_flags = EXT2_MF_MOUNTED;
81 /* Check to see if the ro option is set */
82 if (hasmntopt(mnt, MNTOPT_RO))
83 *mount_flags |= EXT2_MF_READONLY;
86 strncpy(mtpt, mnt->mnt_dir, mtlen);
88 * Check to see if we're referring to the root filesystem.
89 * If so, do a manual check to see if we can open /etc/mtab
90 * read/write, since if the root is mounted read/only, the
91 * contents of /etc/mtab may not be accurate.
93 if (!strcmp(mnt->mnt_dir, "/")) {
95 *mount_flags |= EXT2_MF_ISROOT;
96 fd = open(MOUNTED, O_RDWR);
99 *mount_flags |= EXT2_MF_READONLY;
107 static errcode_t check_mntent(const char *file, int *mount_flags,
108 char *mtpt, int mtlen)
113 retval = check_mntent_file("/proc/mounts", file, mount_flags,
118 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
122 #elif defined(HAVE_GETMNTINFO)
124 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
125 char *mtpt, int mtlen)
132 n = getmntinfo(&mp, MNT_NOWAIT);
136 len = sizeof(_PATH_DEV) - 1;
138 if (strncmp(_PATH_DEV, s1, len) == 0)
143 s2 = mp->f_mntfromname;
144 if (strncmp(_PATH_DEV, s2, len) == 0) {
148 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
149 *mount_flags = EXT2_MF_MOUNTED;
155 strncpy(mtpt, mp->f_mntonname, mtlen);
158 #endif /* HAVE_GETMNTINFO */
161 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
162 * otherwise. If mtpt is non-NULL, the directory where the device is
163 * mounted is copied to where mtpt is pointing, up to mtlen
169 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
170 char *mtpt, int mtlen)
173 return check_mntent(device, mount_flags, mtpt, mtlen);
175 #ifdef HAVE_GETMNTINFO
176 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
178 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
181 #endif /* HAVE_GETMNTINFO */
182 #endif /* HAVE_MNTENT_H */
186 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED and
187 * EXT2_MF_READONLY, and EXT2_MF_ROOT
193 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
195 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
199 int main(int argc, char **argv)
201 int retval, mount_flags;
204 fprintf(stderr, "Usage: %s device\n", argv[0]);
208 retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
210 com_err(argv[0], retval,
211 "while calling ext2fs_check_if_mounted");
214 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
215 if (mount_flags & EXT2_MF_MOUNTED)
216 printf("\t%s is mounted.\n", argv[1]);
218 if (mount_flags & EXT2_MF_READONLY)
219 printf("\t%s is read-only.\n", argv[1]);
221 if (mount_flags & EXT2_MF_ISROOT)
222 printf("\t%s is the root filesystem.\n", argv[1]);