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 */
32 #if EXT2_FLAT_INCLUDES
35 #include <linux/ext2_fs.h>
42 * XXX we assume that /etc/mtab is located on the root filesystem, and
43 * we only check to see if the mount is readonly for the root
46 static errcode_t check_mntent(const char *file, int *mount_flags,
47 char *mtpt, int mtlen)
54 if ((f = setmntent (MOUNTED, "r")) == NULL)
56 while ((mnt = getmntent (f)) != NULL)
57 if (strcmp(file, mnt->mnt_fsname) == 0)
62 *mount_flags = EXT2_MF_MOUNTED;
64 if (!strcmp(mnt->mnt_dir, "/")) {
65 *mount_flags |= EXT2_MF_ISROOT;
66 fd = open(MOUNTED, O_RDWR);
69 *mount_flags |= EXT2_MF_READONLY;
74 strncpy(mtpt, mnt->mnt_dir, mtlen);
79 #ifdef HAVE_GETMNTINFO
80 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
81 char *mtpt, int mtlen)
88 n = getmntinfo(&mp, MNT_NOWAIT);
92 len = sizeof(_PATH_DEV) - 1;
94 if (strncmp(_PATH_DEV, s1, len) == 0)
99 s2 = mp->f_mntfromname;
100 if (strncmp(_PATH_DEV, s2, len) == 0) {
104 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
105 *mount_flags = EXT2_MF_MOUNTED;
111 strncpy(mtpt, mp->f_mntonname, mtlen);
114 #endif /* HAVE_GETMNTINFO */
117 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
118 * otherwise. If mtpt is non-NULL, the directory where the device is
119 * mounted is copied to where mtpt is pointing, up to mtlen
125 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
126 char *mtpt, int mtlen)
129 return check_mntent(device, mount_flags, mtpt, mtlen);
131 #ifdef HAVE_GETMNTINFO
132 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
136 #endif /* HAVE_GETMNTINFO */
137 #endif /* HAVE_MNTENT_H */
141 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED and
148 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
151 return check_mntent(file, mount_flags, NULL, 0);
153 #ifdef HAVE_GETMNTINFO
154 return check_getmntinfo(file, mount_flags, NULL, 0);
158 #endif /* HAVE_GETMNTINFO */
159 #endif /* HAVE_MNTENT_H */