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)
54 if ((f = setmntent (mtab_file, "r")) == NULL)
57 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
58 if (stat(file, &st_buf) == 0)
59 file_dev = st_buf.st_rdev;
61 while ((mnt = getmntent (f)) != NULL) {
62 if (strcmp(file, mnt->mnt_fsname) == 0)
65 if (file_dev && (stat(mnt->mnt_fsname, &st_buf) == 0) &&
66 file_dev == st_buf.st_rdev)
72 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
74 * Do an extra check to see if this is the root device. We
75 * can't trust /etc/mtab, and /proc/mounts will only list
76 * /dev/root for the root filesystem. Argh. Instead we
77 * check if the given device has the same major/minor number
78 * as the device that the root directory is on.
80 if (file_dev && stat("/", &st_buf) == 0) {
81 if (st_buf.st_dev == file_dev) {
82 *mount_flags = EXT2_MF_MOUNTED;
84 strncpy(mtpt, "/", mtlen);
91 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
92 /* Validate the entry in case /etc/mtab is out of date */
94 * We need to be paranoid, because some broken distributions
95 * (read: Slackware) don't initialize /etc/mtab before checking
96 * all of the non-root filesystems on the disk.
98 if (stat(mnt->mnt_dir, &st_buf) < 0) {
100 if (retval == ENOENT) {
102 printf("Bogus entry in %s! (%s does not exist)\n",
103 mtab_file, mnt->mnt_dir);
109 if (file_dev && (st_buf.st_dev != file_dev)) {
111 printf("Bogus entry in %s! (%s not mounted on %s)\n",
112 mtab_file, file, mnt->mnt_dir);
117 *mount_flags = EXT2_MF_MOUNTED;
119 /* Check to see if the ro option is set */
120 if (hasmntopt(mnt, MNTOPT_RO))
121 *mount_flags |= EXT2_MF_READONLY;
124 strncpy(mtpt, mnt->mnt_dir, mtlen);
126 * Check to see if we're referring to the root filesystem.
127 * If so, do a manual check to see if we can open /etc/mtab
128 * read/write, since if the root is mounted read/only, the
129 * contents of /etc/mtab may not be accurate.
131 if (!strcmp(mnt->mnt_dir, "/")) {
133 #define TEST_FILE "/.ismount-test-file"
134 *mount_flags |= EXT2_MF_ISROOT;
135 fd = open(TEST_FILE, O_RDWR|O_CREAT);
138 *mount_flags |= EXT2_MF_READONLY;
141 (void) unlink(TEST_FILE);
150 * Check to see if we're dealing with the swap device.
152 static int is_swap_device(const char *file)
160 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
161 if (stat(file, &st_buf) == 0)
162 file_dev = st_buf.st_rdev;
165 f = fopen("/proc/swaps", "r");
168 /* Skip the first line */
169 fgets(buf, sizeof(buf), f);
171 if (!fgets(buf, sizeof(buf), f))
173 if ((cp = strchr(buf, ' ')) != NULL)
175 if ((cp = strchr(buf, '\t')) != NULL)
177 if (strcmp(buf, file) == 0)
180 if (file_dev && (stat(buf, &st_buf) == 0) &&
181 file_dev == st_buf.st_rdev)
188 static errcode_t check_mntent(const char *file, int *mount_flags,
189 char *mtpt, int mtlen)
194 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
200 retval = check_mntent_file("/proc/mounts", file, mount_flags,
205 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
209 #elif defined(HAVE_GETMNTINFO)
211 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
212 char *mtpt, int mtlen)
219 n = getmntinfo(&mp, MNT_NOWAIT);
223 len = sizeof(_PATH_DEV) - 1;
225 if (strncmp(_PATH_DEV, s1, len) == 0)
230 s2 = mp->f_mntfromname;
231 if (strncmp(_PATH_DEV, s2, len) == 0) {
235 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
236 *mount_flags = EXT2_MF_MOUNTED;
242 strncpy(mtpt, mp->f_mntonname, mtlen);
245 #endif /* HAVE_GETMNTINFO */
248 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
249 * otherwise. If mtpt is non-NULL, the directory where the device is
250 * mounted is copied to where mtpt is pointing, up to mtlen
256 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
257 char *mtpt, int mtlen)
259 if (is_swap_device(device)) {
260 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
261 strncpy(mtpt, "<swap>", mtlen);
265 return check_mntent(device, mount_flags, mtpt, mtlen);
267 #ifdef HAVE_GETMNTINFO
268 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
270 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
273 #endif /* HAVE_GETMNTINFO */
274 #endif /* HAVE_MNTENT_H */
278 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
279 * EXT2_MF_READONLY, and EXT2_MF_ROOT
282 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
284 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
288 int main(int argc, char **argv)
290 int retval, mount_flags;
294 fprintf(stderr, "Usage: %s device\n", argv[0]);
299 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
300 mntpt, sizeof(mntpt));
302 com_err(argv[0], retval,
303 "while calling ext2fs_check_if_mounted");
306 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
307 if (mount_flags & EXT2_MF_MOUNTED)
308 printf("\t%s is mounted.\n", argv[1]);
310 if (mount_flags & EXT2_MF_SWAP)
311 printf("\t%s is a swap device.\n", argv[1]);
313 if (mount_flags & EXT2_MF_READONLY)
314 printf("\t%s is read-only.\n", argv[1]);
316 if (mount_flags & EXT2_MF_ISROOT)
317 printf("\t%s is the root filesystem.\n", argv[1]);
319 printf("\t%s is mounted on %s.\n", argv[1], mntpt);