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)
49 dev_t file_dev=0, file_rdev=0;
55 if ((f = setmntent (mtab_file, "r")) == NULL)
57 if (stat(file, &st_buf) == 0) {
58 if (S_ISBLK(st_buf.st_mode)) {
59 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
60 file_rdev = st_buf.st_rdev;
63 file_dev = st_buf.st_dev;
64 file_ino = st_buf.st_ino;
67 while ((mnt = getmntent (f)) != NULL) {
68 if (strcmp(file, mnt->mnt_fsname) == 0)
70 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
71 if (S_ISBLK(st_buf.st_mode)) {
73 if (file_rdev && (file_rdev == st_buf.st_rdev))
77 if (file_dev && ((file_dev == st_buf.st_dev) &&
78 (file_ino == st_buf.st_ino)))
85 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
87 * Do an extra check to see if this is the root device. We
88 * can't trust /etc/mtab, and /proc/mounts will only list
89 * /dev/root for the root filesystem. Argh. Instead we
90 * check if the given device has the same major/minor number
91 * as the device that the root directory is on.
93 if (file_rdev && stat("/", &st_buf) == 0) {
94 if (st_buf.st_dev == file_rdev) {
95 *mount_flags = EXT2_MF_MOUNTED;
97 strncpy(mtpt, "/", mtlen);
104 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
105 /* Validate the entry in case /etc/mtab is out of date */
107 * We need to be paranoid, because some broken distributions
108 * (read: Slackware) don't initialize /etc/mtab before checking
109 * all of the non-root filesystems on the disk.
111 if (stat(mnt->mnt_dir, &st_buf) < 0) {
113 if (retval == ENOENT) {
115 printf("Bogus entry in %s! (%s does not exist)\n",
116 mtab_file, mnt->mnt_dir);
122 if (file_rdev && (st_buf.st_dev != file_rdev)) {
124 printf("Bogus entry in %s! (%s not mounted on %s)\n",
125 mtab_file, file, mnt->mnt_dir);
130 *mount_flags = EXT2_MF_MOUNTED;
133 /* Check to see if the ro option is set */
134 if (hasmntopt(mnt, MNTOPT_RO))
135 *mount_flags |= EXT2_MF_READONLY;
139 strncpy(mtpt, mnt->mnt_dir, mtlen);
141 * Check to see if we're referring to the root filesystem.
142 * If so, do a manual check to see if we can open /etc/mtab
143 * read/write, since if the root is mounted read/only, the
144 * contents of /etc/mtab may not be accurate.
146 if (!strcmp(mnt->mnt_dir, "/")) {
148 #define TEST_FILE "/.ismount-test-file"
149 *mount_flags |= EXT2_MF_ISROOT;
150 fd = open(TEST_FILE, O_RDWR|O_CREAT);
153 *mount_flags |= EXT2_MF_READONLY;
156 (void) unlink(TEST_FILE);
164 static errcode_t check_mntent(const char *file, int *mount_flags,
165 char *mtpt, int mtlen)
170 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
176 retval = check_mntent_file("/proc/mounts", file, mount_flags,
178 if (retval == 0 && (*mount_flags != 0))
180 #endif /* __linux__ */
181 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
183 #define MOUNTED _PATH_MOUNTED
185 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
190 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
194 #if defined(HAVE_GETMNTINFO)
196 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
197 char *mtpt, int mtlen)
204 n = getmntinfo(&mp, MNT_NOWAIT);
208 len = sizeof(_PATH_DEV) - 1;
210 if (strncmp(_PATH_DEV, s1, len) == 0)
215 s2 = mp->f_mntfromname;
216 if (strncmp(_PATH_DEV, s2, len) == 0) {
220 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
221 *mount_flags = EXT2_MF_MOUNTED;
227 strncpy(mtpt, mp->f_mntonname, mtlen);
230 #endif /* HAVE_GETMNTINFO */
231 #endif /* HAVE_MNTENT_H */
234 * Check to see if we're dealing with the swap device.
236 static int is_swap_device(const char *file)
245 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
246 if ((stat(file, &st_buf) == 0) &&
247 S_ISBLK(st_buf.st_mode))
248 file_dev = st_buf.st_rdev;
251 if (!(f = fopen("/proc/swaps", "r")))
253 /* Skip the first line */
254 fgets(buf, sizeof(buf), f);
256 if (!fgets(buf, sizeof(buf), f))
258 if ((cp = strchr(buf, ' ')) != NULL)
260 if ((cp = strchr(buf, '\t')) != NULL)
262 if (strcmp(buf, file) == 0) {
267 if (file_dev && (stat(buf, &st_buf) == 0) &&
268 S_ISBLK(st_buf.st_mode) &&
269 file_dev == st_buf.st_rdev) {
281 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
282 * otherwise. If mtpt is non-NULL, the directory where the device is
283 * mounted is copied to where mtpt is pointing, up to mtlen
289 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
290 char *mtpt, int mtlen)
292 if (is_swap_device(device)) {
293 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
294 strncpy(mtpt, "<swap>", mtlen);
298 return check_mntent(device, mount_flags, mtpt, mtlen);
300 #ifdef HAVE_GETMNTINFO
301 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
304 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
308 #endif /* HAVE_GETMNTINFO */
309 #endif /* HAVE_MNTENT_H */
313 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
314 * EXT2_MF_READONLY, and EXT2_MF_ROOT
317 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
319 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
323 int main(int argc, char **argv)
325 int retval, mount_flags;
329 fprintf(stderr, "Usage: %s device\n", argv[0]);
334 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
335 mntpt, sizeof(mntpt));
337 com_err(argv[0], retval,
338 "while calling ext2fs_check_if_mounted");
341 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
342 if (mount_flags & EXT2_MF_MOUNTED)
343 printf("\t%s is mounted.\n", argv[1]);
345 if (mount_flags & EXT2_MF_SWAP)
346 printf("\t%s is a swap device.\n", argv[1]);
348 if (mount_flags & EXT2_MF_READONLY)
349 printf("\t%s is read-only.\n", argv[1]);
351 if (mount_flags & EXT2_MF_ISROOT)
352 printf("\t%s is the root filesystem.\n", argv[1]);
354 printf("\t%s is mounted on %s.\n", argv[1], mntpt);