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_FD_H
26 #ifdef HAVE_GETMNTINFO
28 #include <sys/param.h>
29 #include <sys/mount.h>
30 #endif /* HAVE_GETMNTINFO */
33 #if EXT2_FLAT_INCLUDES
36 #include <linux/ext2_fs.h>
43 * XXX we assume that /etc/mtab is located on the root filesystem, and
44 * we only check to see if the mount is readonly for the root
47 static errcode_t check_mntent(const char *file, int *mount_flags,
48 char *mtpt, int mtlen)
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;
75 strncpy(mtpt, mnt->mnt_dir, mtlen);
80 #ifdef HAVE_GETMNTINFO
81 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
82 char *mtpt, int mtlen)
89 n = getmntinfo(&mp, MNT_NOWAIT);
93 len = sizeof(_PATH_DEV) - 1;
95 if (strncmp(_PATH_DEV, s1, len) == 0)
100 s2 = mp->f_mntfromname;
101 if (strncmp(_PATH_DEV, s2, len) == 0) {
105 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
106 *mount_flags = EXT2_MF_MOUNTED;
112 strncpy(mtpt, mp->f_mntonname, mtlen);
115 #endif /* HAVE_GETMNTINFO */
118 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
119 * otherwise. If mtpt is non-NULL, the directory where the device is
120 * mounted is copied to where mtpt is pointing, up to mtlen
126 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
127 char *mtpt, int mtlen)
130 return check_mntent(device, mount_flags, mtpt, mtlen);
132 #ifdef HAVE_GETMNTINFO
133 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
137 #endif /* HAVE_GETMNTINFO */
138 #endif /* HAVE_MNTENT_H */
142 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED and
149 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
152 return check_mntent(file, mount_flags, NULL, 0);
154 #ifdef HAVE_GETMNTINFO
155 return check_getmntinfo(file, mount_flags, NULL, 0);
159 #endif /* HAVE_GETMNTINFO */
160 #endif /* HAVE_MNTENT_H */