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
23 #ifdef HAVE_LINUX_FD_H
36 * ext2fs_check_if_mounted flags
40 #include "et/com_err.h"
42 static char *skip_over_blank(char *cp)
44 while (*cp && isspace(*cp))
49 static char *skip_over_word(char *cp)
51 while (*cp && !isspace(*cp))
56 static char *parse_word(char **buf)
64 word = skip_over_blank(word);
65 next = skip_over_word(word);
73 * Helper function which checks a file in /etc/mtab format to see if a
74 * filesystem is mounted. Returns an error if the file doesn't exist
77 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
83 dev_t file_dev=0, file_rdev=0;
86 char buf[1024], *device = 0, *mnt_dir = 0, *cp;
89 if ((f = setmntent (mtab_file, "r")) == NULL)
91 if (stat(file, &st_buf) == 0) {
92 if (S_ISBLK(st_buf.st_mode)) {
93 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
94 file_rdev = st_buf.st_rdev;
97 file_dev = st_buf.st_dev;
98 file_ino = st_buf.st_ino;
102 if (!fgets(buf, sizeof(buf), f)) {
103 device = mnt_dir = 0;
106 buf[sizeof(buf)-1] = 0;
109 device = parse_word(&cp);
110 if (!device || *device == '#')
111 return 0; /* Ignore blank lines and comments */
112 mnt_dir = parse_word(&cp);
114 if (device[0] != '/')
117 if (strcmp(file, device) == 0)
119 if (stat(device, &st_buf) == 0) {
120 if (S_ISBLK(st_buf.st_mode)) {
122 if (file_rdev && (file_rdev == st_buf.st_rdev))
126 if (file_dev && ((file_dev == st_buf.st_dev) &&
127 (file_ino == st_buf.st_ino)))
134 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
136 * Do an extra check to see if this is the root device. We
137 * can't trust /etc/mtab, and /proc/mounts will only list
138 * /dev/root for the root filesystem. Argh. Instead we
139 * check if the given device has the same major/minor number
140 * as the device that the root directory is on.
142 if (file_rdev && (stat("/", &st_buf) == 0) &&
143 (st_buf.st_dev == file_rdev))
144 *mount_flags = MF_MOUNTED;
148 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
149 /* Validate the entry in case /etc/mtab is out of date */
151 * We need to be paranoid, because some broken distributions
152 * (read: Slackware) don't initialize /etc/mtab before checking
153 * all of the non-root filesystems on the disk.
155 if (stat(mnt_dir, &st_buf) < 0) {
157 if (retval == ENOENT) {
159 printf("Bogus entry in %s! (%s does not exist)\n",
166 if (file_rdev && (st_buf.st_dev != file_rdev)) {
168 printf("Bogus entry in %s! (%s not mounted on %s)\n",
169 mtab_file, file, mnt_dir);
174 *mount_flags = MF_MOUNTED;
180 #else /* !HAVE_MNTENT_H */
182 #endif /* HAVE_MNTENT_H */
185 int is_mounted(const char *file)
191 retval = check_mntent_file("/proc/mounts", file, &mount_flags);
196 #endif /* __linux__ */
197 retval = check_mntent_file("/etc/mtab", file, &mount_flags);
200 return (mount_flags);
204 int main(int argc, char **argv)
207 fprintf(stderr, "Usage: %s device\n", argv[0]);
211 if (is_mounted(argv[1]))
212 printf("\t%s is mounted.\n", argv[1]);