2 * ismounted.c --- Check to see if the filesystem was mounted
4 * Copyright (C) 1995,1996,1997,1998,1999,2000,2008 Theodore Ts'o.
6 * This file may be redistributed under the terms of the GNU Public
19 #include "pathnames.h"
20 #include "ismounted.h"
23 * ext2fs_check_if_mounted flags
27 static char *skip_over_blank(char *cp)
29 while (*cp && isspace(*cp))
34 static char *skip_over_word(char *cp)
36 while (*cp && !isspace(*cp))
41 static char *parse_word(char **buf)
49 word = skip_over_blank(word);
50 next = skip_over_word(word);
58 * Helper function which checks a file in /etc/mtab format to see if a
59 * filesystem is mounted. Returns an error if the file doesn't exist
62 static int check_mntent_file(const char *mtab_file, const char *file,
67 dev_t file_dev=0, file_rdev=0;
70 char buf[1024], *device = 0, *mnt_dir = 0, *cp;
73 if ((f = setmntent (mtab_file, "r")) == NULL)
75 if (stat(file, &st_buf) == 0) {
76 if (S_ISBLK(st_buf.st_mode)) {
77 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
78 file_rdev = st_buf.st_rdev;
81 file_dev = st_buf.st_dev;
82 file_ino = st_buf.st_ino;
86 if (!fgets(buf, sizeof(buf), f)) {
90 buf[sizeof(buf)-1] = 0;
93 device = parse_word(&cp);
94 if (!device || *device == '#')
95 return 0; /* Ignore blank lines and comments */
96 mnt_dir = parse_word(&cp);
101 if (strcmp(file, device) == 0)
103 if (stat(device, &st_buf) == 0) {
104 if (S_ISBLK(st_buf.st_mode)) {
106 if (file_rdev && (file_rdev == st_buf.st_rdev))
110 if (file_dev && ((file_dev == st_buf.st_dev) &&
111 (file_ino == st_buf.st_ino)))
118 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
120 * Do an extra check to see if this is the root device. We
121 * can't trust /etc/mtab, and /proc/mounts will only list
122 * /dev/root for the root filesystem. Argh. Instead we
123 * check if the given device has the same major/minor number
124 * as the device that the root directory is on.
126 if (file_rdev && (stat("/", &st_buf) == 0) &&
127 (st_buf.st_dev == file_rdev))
128 *mount_flags = MF_MOUNTED;
132 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
133 /* Validate the entry in case /etc/mtab is out of date */
135 * We need to be paranoid, because some broken distributions
136 * (read: Slackware) don't initialize /etc/mtab before checking
137 * all of the non-root filesystems on the disk.
139 if (stat(mnt_dir, &st_buf) < 0) {
141 if (retval == ENOENT) {
143 printf("Bogus entry in %s! (%s does not exist)\n",
150 if (file_rdev && (st_buf.st_dev != file_rdev)) {
152 printf("Bogus entry in %s! (%s not mounted on %s)\n",
153 mtab_file, file, mnt_dir);
158 *mount_flags = MF_MOUNTED;
166 int is_mounted(const char *file)
172 retval = check_mntent_file(_PATH_PROC_MOUNTS, file, &mount_flags);
177 #endif /* __linux__ */
178 retval = check_mntent_file(_PATH_MOUNTED, file, &mount_flags);
185 int main(int argc, char **argv)
188 fprintf(stderr, "Usage: %s device\n", argv[0]);
192 if (is_mounted(argv[1])) {
196 printf("not mounted\n");