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)
161 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
162 if (stat(file, &st_buf) == 0)
163 file_dev = st_buf.st_rdev;
166 if (!(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) {
182 if (file_dev && (stat(buf, &st_buf) == 0) &&
183 file_dev == st_buf.st_rdev) {
193 static errcode_t check_mntent(const char *file, int *mount_flags,
194 char *mtpt, int mtlen)
199 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
205 retval = check_mntent_file("/proc/mounts", file, mount_flags,
210 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
214 #elif defined(HAVE_GETMNTINFO)
216 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
217 char *mtpt, int mtlen)
224 n = getmntinfo(&mp, MNT_NOWAIT);
228 len = sizeof(_PATH_DEV) - 1;
230 if (strncmp(_PATH_DEV, s1, len) == 0)
235 s2 = mp->f_mntfromname;
236 if (strncmp(_PATH_DEV, s2, len) == 0) {
240 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
241 *mount_flags = EXT2_MF_MOUNTED;
247 strncpy(mtpt, mp->f_mntonname, mtlen);
250 #endif /* HAVE_GETMNTINFO */
253 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
254 * otherwise. If mtpt is non-NULL, the directory where the device is
255 * mounted is copied to where mtpt is pointing, up to mtlen
261 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
262 char *mtpt, int mtlen)
264 if (is_swap_device(device)) {
265 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
266 strncpy(mtpt, "<swap>", mtlen);
270 return check_mntent(device, mount_flags, mtpt, mtlen);
272 #ifdef HAVE_GETMNTINFO
273 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
275 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
278 #endif /* HAVE_GETMNTINFO */
279 #endif /* HAVE_MNTENT_H */
283 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
284 * EXT2_MF_READONLY, and EXT2_MF_ROOT
287 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
289 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
293 int main(int argc, char **argv)
295 int retval, mount_flags;
299 fprintf(stderr, "Usage: %s device\n", argv[0]);
304 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
305 mntpt, sizeof(mntpt));
307 com_err(argv[0], retval,
308 "while calling ext2fs_check_if_mounted");
311 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
312 if (mount_flags & EXT2_MF_MOUNTED)
313 printf("\t%s is mounted.\n", argv[1]);
315 if (mount_flags & EXT2_MF_SWAP)
316 printf("\t%s is a swap device.\n", argv[1]);
318 if (mount_flags & EXT2_MF_READONLY)
319 printf("\t%s is read-only.\n", argv[1]);
321 if (mount_flags & EXT2_MF_ISROOT)
322 printf("\t%s is the root filesystem.\n", argv[1]);
324 printf("\t%s is mounted on %s.\n", argv[1], mntpt);