2 * ismounted.c --- Check to see if the filesystem was mounted
4 * Copyright (C) 1995 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
20 #ifdef HAVE_LINUX_FS_H
23 #ifdef HAVE_LINUX_FD_H
29 #ifdef HAVE_GETMNTINFO
31 #include <sys/param.h>
32 #include <sys/mount.h>
33 #endif /* HAVE_GETMNTINFO */
35 #if EXT2_FLAT_INCLUDES
38 #include <linux/ext2_fs.h>
45 * XXX we only check to see if the mount is readonly when it's the
48 static errcode_t check_mntent(const char *file, int *mount_flags)
55 if ((f = setmntent (MOUNTED, "r")) == NULL)
57 while ((mnt = getmntent (f)) != NULL)
58 if (strcmp(file, mnt->mnt_fsname) == 0)
63 *mount_flags = EXT2_MF_MOUNTED;
65 if (!strcmp(mnt->mnt_dir, "/")) {
66 *mount_flags |= EXT2_MF_ISROOT;
67 fd = open(MOUNTED, O_RDWR);
70 *mount_flags |= EXT2_MF_READONLY;
78 #ifdef HAVE_GETMNTINFO
79 static errcode_t check_getmntinfo(const char *file, int *mount_flags)
86 n = getmntinfo(&mp, MNT_NOWAIT);
90 len = sizeof(_PATH_DEV) - 1;
92 if (strncmp(_PATH_DEV, s1, len) == 0)
97 s2 = mp->f_mntfromname;
98 if (strncmp(_PATH_DEV, s2, len) == 0) {
102 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
103 *mount_flags = EXT2_MF_MOUNTED;
110 #endif /* HAVE_GETMNTINFO */
113 * Is_mounted is set to 1 if the device is mounted, 0 otherwise
118 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
121 return check_mntent(file, mount_flags);
123 #ifdef HAVE_GETMNTINFO
124 return check_getmntinfo(file, mount_flags);
128 #endif /* HAVE_GETMNTINFO */
129 #endif /* HAVE_MNTENT_H */