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 #define TEST_FILE "/.ismount-test-file"
96 *mount_flags |= EXT2_MF_ISROOT;
97 fd = open(TEST_FILE, O_RDWR|O_CREAT);
100 *mount_flags |= EXT2_MF_READONLY;
103 (void) unlink(TEST_FILE);
109 static errcode_t check_mntent(const char *file, int *mount_flags,
110 char *mtpt, int mtlen)
115 retval = check_mntent_file("/proc/mounts", file, mount_flags,
120 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
124 #elif defined(HAVE_GETMNTINFO)
126 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
127 char *mtpt, int mtlen)
134 n = getmntinfo(&mp, MNT_NOWAIT);
138 len = sizeof(_PATH_DEV) - 1;
140 if (strncmp(_PATH_DEV, s1, len) == 0)
145 s2 = mp->f_mntfromname;
146 if (strncmp(_PATH_DEV, s2, len) == 0) {
150 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
151 *mount_flags = EXT2_MF_MOUNTED;
157 strncpy(mtpt, mp->f_mntonname, mtlen);
160 #endif /* HAVE_GETMNTINFO */
163 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
164 * otherwise. If mtpt is non-NULL, the directory where the device is
165 * mounted is copied to where mtpt is pointing, up to mtlen
171 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
172 char *mtpt, int mtlen)
175 return check_mntent(device, mount_flags, mtpt, mtlen);
177 #ifdef HAVE_GETMNTINFO
178 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
180 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
183 #endif /* HAVE_GETMNTINFO */
184 #endif /* HAVE_MNTENT_H */
188 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
189 * EXT2_MF_READONLY, and EXT2_MF_ROOT
192 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
194 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
198 int main(int argc, char **argv)
200 int retval, mount_flags;
203 fprintf(stderr, "Usage: %s device\n", argv[0]);
207 retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
209 com_err(argv[0], retval,
210 "while calling ext2fs_check_if_mounted");
213 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
214 if (mount_flags & EXT2_MF_MOUNTED)
215 printf("\t%s is mounted.\n", argv[1]);
217 if (mount_flags & EXT2_MF_READONLY)
218 printf("\t%s is read-only.\n", argv[1]);
220 if (mount_flags & EXT2_MF_ISROOT)
221 printf("\t%s is the root filesystem.\n", argv[1]);