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 Library
8 * General Public License, version 2.
12 /* define BSD_SOURCE to make sure we get the major() macro */
16 #ifndef _DEFAULT_SOURCE
17 #define _DEFAULT_SOURCE /* since glibc 2.20 _SVID_SOURCE is deprecated */
29 #ifdef HAVE_LINUX_FD_H
32 #ifdef HAVE_LINUX_LOOP_H
33 #include <linux/loop.h>
34 #include <sys/ioctl.h>
35 #ifdef HAVE_LINUX_MAJOR_H
36 #include <linux/major.h>
37 #endif /* HAVE_LINUX_MAJOR_H */
38 #endif /* HAVE_LINUX_LOOP_H */
42 #ifdef HAVE_GETMNTINFO
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #endif /* HAVE_GETMNTINFO */
50 #include <sys/types.h>
58 * Check to see if a regular file is mounted.
59 * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
60 * because the name in /proc/mounts is a loopback device not a regular file.
62 static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
63 dev_t file_dev, ino_t file_ino)
65 #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
66 struct loop_info64 loopinfo;
69 if (major(mnt_rdev) == LOOP_MAJOR) {
70 loop_fd = open(mnt_fsname, O_RDONLY);
74 ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
79 if (file_dev == loopinfo.lo_device &&
80 file_ino == loopinfo.lo_inode)
83 #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
88 * Helper function which checks a file in /etc/mtab format to see if a
89 * filesystem is mounted. Returns an error if the file doesn't exist
92 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
93 int *mount_flags, char *mtpt, int mtlen)
98 dev_t file_dev=0, file_rdev=0;
105 if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
106 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
107 if (getenv("EXT2FS_PRETEND_ROOTFS"))
108 *mount_flags = EXT2_MF_ISROOT;
111 if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
112 *mount_flags = EXT2_MF_MOUNTED;
113 if (getenv("EXT2FS_PRETEND_ROOTFS"))
114 *mount_flags = EXT2_MF_ISROOT;
118 if ((f = setmntent (mtab_file, "r")) == NULL) {
119 if (errno == ENOENT) {
120 if (getenv("EXT2FS_NO_MTAB_OK"))
123 return EXT2_ET_NO_MTAB_FILE;
127 if (stat(file, &st_buf) == 0) {
128 if (S_ISBLK(st_buf.st_mode)) {
129 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
130 file_rdev = st_buf.st_rdev;
133 file_dev = st_buf.st_dev;
134 file_ino = st_buf.st_ino;
137 while ((mnt = getmntent (f)) != NULL) {
138 if (mnt->mnt_fsname[0] != '/')
140 if (strcmp(file, mnt->mnt_fsname) == 0)
142 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
143 if (S_ISBLK(st_buf.st_mode)) {
145 if (file_rdev && (file_rdev == st_buf.st_rdev))
147 if (check_loop_mounted(mnt->mnt_fsname,
148 st_buf.st_rdev, file_dev,
153 if (file_dev && ((file_dev == st_buf.st_dev) &&
154 (file_ino == st_buf.st_ino)))
161 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
163 * Do an extra check to see if this is the root device. We
164 * can't trust /etc/mtab, and /proc/mounts will only list
165 * /dev/root for the root filesystem. Argh. Instead we
166 * check if the given device has the same major/minor number
167 * as the device that the root directory is on.
169 if (file_rdev && stat("/", &st_buf) == 0) {
170 if (st_buf.st_dev == file_rdev) {
171 *mount_flags = EXT2_MF_MOUNTED;
173 strncpy(mtpt, "/", mtlen);
180 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
181 /* Validate the entry in case /etc/mtab is out of date */
183 * We need to be paranoid, because some broken distributions
184 * (read: Slackware) don't initialize /etc/mtab before checking
185 * all of the non-root filesystems on the disk.
187 if (stat(mnt->mnt_dir, &st_buf) < 0) {
189 if (retval == ENOENT) {
191 printf("Bogus entry in %s! (%s does not exist)\n",
192 mtab_file, mnt->mnt_dir);
198 if (file_rdev && (st_buf.st_dev != file_rdev)) {
200 printf("Bogus entry in %s! (%s not mounted on %s)\n",
201 mtab_file, file, mnt->mnt_dir);
206 *mount_flags = EXT2_MF_MOUNTED;
209 /* Check to see if the ro option is set */
210 if (hasmntopt(mnt, MNTOPT_RO))
211 *mount_flags |= EXT2_MF_READONLY;
215 strncpy(mtpt, mnt->mnt_dir, mtlen);
217 * Check to see if we're referring to the root filesystem.
218 * If so, do a manual check to see if we can open /etc/mtab
219 * read/write, since if the root is mounted read/only, the
220 * contents of /etc/mtab may not be accurate.
222 if (!strcmp(mnt->mnt_dir, "/")) {
224 #define TEST_FILE "/.ismount-test-file"
225 *mount_flags |= EXT2_MF_ISROOT;
226 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
229 *mount_flags |= EXT2_MF_READONLY;
232 (void) unlink(TEST_FILE);
240 static errcode_t check_mntent(const char *file, int *mount_flags,
241 char *mtpt, int mtlen)
246 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
252 retval = check_mntent_file("/proc/mounts", file, mount_flags,
254 if (retval == 0 && (*mount_flags != 0))
256 #endif /* __linux__ */
257 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
259 #define MOUNTED _PATH_MOUNTED
261 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
266 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
270 #if defined(HAVE_GETMNTINFO)
272 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
273 char *mtpt, int mtlen)
280 n = getmntinfo(&mp, MNT_NOWAIT);
284 len = sizeof(_PATH_DEV) - 1;
286 if (strncmp(_PATH_DEV, s1, len) == 0)
291 s2 = mp->f_mntfromname;
292 if (strncmp(_PATH_DEV, s2, len) == 0) {
296 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
297 *mount_flags = EXT2_MF_MOUNTED;
303 strncpy(mtpt, mp->f_mntonname, mtlen);
306 #endif /* HAVE_GETMNTINFO */
307 #endif /* HAVE_SETMNTENT */
310 * Check to see if we're dealing with the swap device.
312 static int is_swap_device(const char *file)
321 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
322 if ((stat(file, &st_buf) == 0) &&
323 S_ISBLK(st_buf.st_mode))
324 file_dev = st_buf.st_rdev;
327 if (!(f = fopen("/proc/swaps", "r")))
329 /* Skip the first line */
330 if (!fgets(buf, sizeof(buf), f))
332 if (*buf && strncmp(buf, "Filename\t", 9))
333 /* Linux <=2.6.19 contained a bug in the /proc/swaps
334 * code where the header would not be displayed
336 goto valid_first_line;
338 while (fgets(buf, sizeof(buf), f)) {
340 if ((cp = strchr(buf, ' ')) != NULL)
342 if ((cp = strchr(buf, '\t')) != NULL)
344 if (strcmp(buf, file) == 0) {
349 if (file_dev && (stat(buf, &st_buf) == 0) &&
350 S_ISBLK(st_buf.st_mode) &&
351 file_dev == st_buf.st_rdev) {
365 * ext2fs_check_mount_point() fills determines if the device is
366 * mounted or otherwise busy, and fills in mount_flags with one or
367 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
368 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
369 * non-NULL, the directory where the device is mounted is copied to
370 * where mtpt is pointing, up to mtlen characters.
375 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
376 char *mtpt, int mtlen)
378 errcode_t retval = 0;
380 if (is_swap_device(device)) {
381 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
382 strncpy(mtpt, "<swap>", mtlen);
384 #ifdef HAVE_SETMNTENT
385 retval = check_mntent(device, mount_flags, mtpt, mtlen);
387 #ifdef HAVE_GETMNTINFO
388 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
391 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
394 #endif /* HAVE_GETMNTINFO */
395 #endif /* HAVE_SETMNTENT */
400 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
404 if (stat(device, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) {
405 int fd = open(device, O_RDONLY | O_EXCL);
409 else if (errno == EBUSY)
410 *mount_flags |= EXT2_MF_BUSY;
419 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
420 * EXT2_MF_READONLY, and EXT2_MF_ROOT
423 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
425 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
429 int main(int argc, char **argv)
431 int retval, mount_flags;
435 fprintf(stderr, "Usage: %s device\n", argv[0]);
439 add_error_table(&et_ext2_error_table);
441 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
442 mntpt, sizeof(mntpt));
444 com_err(argv[0], retval,
445 "while calling ext2fs_check_if_mounted");
448 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
449 if (mount_flags & EXT2_MF_BUSY)
450 printf("\t%s is apparently in use.\n", argv[1]);
451 if (mount_flags & EXT2_MF_MOUNTED)
452 printf("\t%s is mounted.\n", argv[1]);
453 if (mount_flags & EXT2_MF_SWAP)
454 printf("\t%s is a swap device.\n", argv[1]);
455 if (mount_flags & EXT2_MF_READONLY)
456 printf("\t%s is read-only.\n", argv[1]);
457 if (mount_flags & EXT2_MF_ISROOT)
458 printf("\t%s is the root filesystem.\n", argv[1]);
460 printf("\t%s is mounted on %s.\n", argv[1], mntpt);