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"
44 static char *skip_over_blank(char *cp)
46 while (*cp && isspace(*cp))
51 static char *skip_over_word(char *cp)
53 while (*cp && !isspace(*cp))
58 static char *parse_word(char **buf)
66 word = skip_over_blank(word);
67 next = skip_over_word(word);
76 * Helper function which checks a file in /etc/mtab format to see if a
77 * filesystem is mounted. Returns an error if the file doesn't exist
80 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
86 dev_t file_dev=0, file_rdev=0;
89 char buf[1024], *device = 0, *mnt_dir = 0, *cp;
92 if ((f = setmntent (mtab_file, "r")) == NULL)
94 if (stat(file, &st_buf) == 0) {
95 if (S_ISBLK(st_buf.st_mode)) {
96 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
97 file_rdev = st_buf.st_rdev;
100 file_dev = st_buf.st_dev;
101 file_ino = st_buf.st_ino;
105 if (!fgets(buf, sizeof(buf), f)) {
106 device = mnt_dir = 0;
109 buf[sizeof(buf)-1] = 0;
112 device = parse_word(&cp);
113 if (!device || *device == '#')
114 return 0; /* Ignore blank lines and comments */
115 mnt_dir = parse_word(&cp);
117 if (device[0] != '/')
120 if (strcmp(file, device) == 0)
122 if (stat(device, &st_buf) == 0) {
123 if (S_ISBLK(st_buf.st_mode)) {
125 if (file_rdev && (file_rdev == st_buf.st_rdev))
129 if (file_dev && ((file_dev == st_buf.st_dev) &&
130 (file_ino == st_buf.st_ino)))
137 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
139 * Do an extra check to see if this is the root device. We
140 * can't trust /etc/mtab, and /proc/mounts will only list
141 * /dev/root for the root filesystem. Argh. Instead we
142 * check if the given device has the same major/minor number
143 * as the device that the root directory is on.
145 if (file_rdev && (stat("/", &st_buf) == 0) &&
146 (st_buf.st_dev == file_rdev))
147 *mount_flags = MF_MOUNTED;
151 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
152 /* Validate the entry in case /etc/mtab is out of date */
154 * We need to be paranoid, because some broken distributions
155 * (read: Slackware) don't initialize /etc/mtab before checking
156 * all of the non-root filesystems on the disk.
158 if (stat(mnt_dir, &st_buf) < 0) {
160 if (retval == ENOENT) {
162 printf("Bogus entry in %s! (%s does not exist)\n",
169 if (file_rdev && (st_buf.st_dev != file_rdev)) {
171 printf("Bogus entry in %s! (%s not mounted on %s)\n",
172 mtab_file, file, mnt_dir);
177 *mount_flags = MF_MOUNTED;
183 #else /* !HAVE_SETMNTENT */
185 #endif /* HAVE_MNTENT_H */
188 int is_mounted(const char *file)
194 retval = check_mntent_file("/proc/mounts", file, &mount_flags);
199 #endif /* __linux__ */
200 retval = check_mntent_file("/etc/mtab", file, &mount_flags);
203 return (mount_flags);
207 int main(int argc, char **argv)
210 fprintf(stderr, "Usage: %s device\n", argv[0]);
214 if (is_mounted(argv[1]))
215 printf("\t%s is mounted.\n", argv[1]);