2 * ismounted.c --- Check to see if the filesystem was mounted
4 * Copyright (C) 1995,1996,1997,1998,1999,2000,2008 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
24 #ifdef HAVE_LINUX_FD_H
37 * ext2fs_check_if_mounted flags
41 #include "et/com_err.h"
43 static char *skip_over_blank(char *cp)
45 while (*cp && isspace(*cp))
50 static char *skip_over_word(char *cp)
52 while (*cp && !isspace(*cp))
57 static char *parse_word(char **buf)
65 word = skip_over_blank(word);
66 next = skip_over_word(word);
74 * Helper function which checks a file in /etc/mtab format to see if a
75 * filesystem is mounted. Returns an error if the file doesn't exist
78 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
84 dev_t file_dev=0, file_rdev=0;
87 char buf[1024], *device = 0, *mnt_dir = 0, *cp;
90 if ((f = setmntent (mtab_file, "r")) == NULL)
92 if (stat(file, &st_buf) == 0) {
93 if (S_ISBLK(st_buf.st_mode)) {
94 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
95 file_rdev = st_buf.st_rdev;
98 file_dev = st_buf.st_dev;
99 file_ino = st_buf.st_ino;
103 if (!fgets(buf, sizeof(buf), f)) {
104 device = mnt_dir = 0;
107 buf[sizeof(buf)-1] = 0;
110 device = parse_word(&cp);
111 if (!device || *device == '#')
112 return 0; /* Ignore blank lines and comments */
113 mnt_dir = parse_word(&cp);
115 if (device[0] != '/')
118 if (strcmp(file, device) == 0)
120 if (stat(device, &st_buf) == 0) {
121 if (S_ISBLK(st_buf.st_mode)) {
123 if (file_rdev && (file_rdev == st_buf.st_rdev))
127 if (file_dev && ((file_dev == st_buf.st_dev) &&
128 (file_ino == st_buf.st_ino)))
135 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
137 * Do an extra check to see if this is the root device. We
138 * can't trust /etc/mtab, and /proc/mounts will only list
139 * /dev/root for the root filesystem. Argh. Instead we
140 * check if the given device has the same major/minor number
141 * as the device that the root directory is on.
143 if (file_rdev && (stat("/", &st_buf) == 0) &&
144 (st_buf.st_dev == file_rdev))
145 *mount_flags = MF_MOUNTED;
149 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
150 /* Validate the entry in case /etc/mtab is out of date */
152 * We need to be paranoid, because some broken distributions
153 * (read: Slackware) don't initialize /etc/mtab before checking
154 * all of the non-root filesystems on the disk.
156 if (stat(mnt_dir, &st_buf) < 0) {
158 if (retval == ENOENT) {
160 printf("Bogus entry in %s! (%s does not exist)\n",
167 if (file_rdev && (st_buf.st_dev != file_rdev)) {
169 printf("Bogus entry in %s! (%s not mounted on %s)\n",
170 mtab_file, file, mnt_dir);
175 *mount_flags = MF_MOUNTED;
181 #else /* !HAVE_MNTENT_H */
183 #endif /* HAVE_MNTENT_H */
186 int is_mounted(const char *file)
192 retval = check_mntent_file("/proc/mounts", file, &mount_flags);
197 #endif /* __linux__ */
198 retval = check_mntent_file("/etc/mtab", file, &mount_flags);
201 return (mount_flags);
205 int main(int argc, char **argv)
208 fprintf(stderr, "Usage: %s device\n", argv[0]);
212 if (is_mounted(argv[1]))
213 printf("\t%s is mounted.\n", argv[1]);