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 Library
8 * General Public License, version 2.
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)
49 dev_t file_dev=0, file_rdev=0;
55 if ((f = setmntent (mtab_file, "r")) == NULL)
56 return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno);
57 if (stat(file, &st_buf) == 0) {
58 if (S_ISBLK(st_buf.st_mode)) {
59 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
60 file_rdev = st_buf.st_rdev;
63 file_dev = st_buf.st_dev;
64 file_ino = st_buf.st_ino;
67 while ((mnt = getmntent (f)) != NULL) {
68 if (mnt->mnt_fsname[0] != '/')
70 if (strcmp(file, mnt->mnt_fsname) == 0)
72 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
73 if (S_ISBLK(st_buf.st_mode)) {
75 if (file_rdev && (file_rdev == st_buf.st_rdev))
79 if (file_dev && ((file_dev == st_buf.st_dev) &&
80 (file_ino == st_buf.st_ino)))
87 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
89 * Do an extra check to see if this is the root device. We
90 * can't trust /etc/mtab, and /proc/mounts will only list
91 * /dev/root for the root filesystem. Argh. Instead we
92 * check if the given device has the same major/minor number
93 * as the device that the root directory is on.
95 if (file_rdev && stat("/", &st_buf) == 0) {
96 if (st_buf.st_dev == file_rdev) {
97 *mount_flags = EXT2_MF_MOUNTED;
99 strncpy(mtpt, "/", mtlen);
106 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
107 /* Validate the entry in case /etc/mtab is out of date */
109 * We need to be paranoid, because some broken distributions
110 * (read: Slackware) don't initialize /etc/mtab before checking
111 * all of the non-root filesystems on the disk.
113 if (stat(mnt->mnt_dir, &st_buf) < 0) {
115 if (retval == ENOENT) {
117 printf("Bogus entry in %s! (%s does not exist)\n",
118 mtab_file, mnt->mnt_dir);
124 if (file_rdev && (st_buf.st_dev != file_rdev)) {
126 printf("Bogus entry in %s! (%s not mounted on %s)\n",
127 mtab_file, file, mnt->mnt_dir);
132 *mount_flags = EXT2_MF_MOUNTED;
135 /* Check to see if the ro option is set */
136 if (hasmntopt(mnt, MNTOPT_RO))
137 *mount_flags |= EXT2_MF_READONLY;
141 strncpy(mtpt, mnt->mnt_dir, mtlen);
143 * Check to see if we're referring to the root filesystem.
144 * If so, do a manual check to see if we can open /etc/mtab
145 * read/write, since if the root is mounted read/only, the
146 * contents of /etc/mtab may not be accurate.
148 if (!strcmp(mnt->mnt_dir, "/")) {
150 #define TEST_FILE "/.ismount-test-file"
151 *mount_flags |= EXT2_MF_ISROOT;
152 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
155 *mount_flags |= EXT2_MF_READONLY;
158 (void) unlink(TEST_FILE);
166 static errcode_t check_mntent(const char *file, int *mount_flags,
167 char *mtpt, int mtlen)
172 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
178 retval = check_mntent_file("/proc/mounts", file, mount_flags,
180 if (retval == 0 && (*mount_flags != 0))
182 #endif /* __linux__ */
183 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
185 #define MOUNTED _PATH_MOUNTED
187 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
192 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
196 #if defined(HAVE_GETMNTINFO)
198 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
199 char *mtpt, int mtlen)
206 n = getmntinfo(&mp, MNT_NOWAIT);
210 len = sizeof(_PATH_DEV) - 1;
212 if (strncmp(_PATH_DEV, s1, len) == 0)
217 s2 = mp->f_mntfromname;
218 if (strncmp(_PATH_DEV, s2, len) == 0) {
222 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
223 *mount_flags = EXT2_MF_MOUNTED;
229 strncpy(mtpt, mp->f_mntonname, mtlen);
232 #endif /* HAVE_GETMNTINFO */
233 #endif /* HAVE_MNTENT_H */
236 * Check to see if we're dealing with the swap device.
238 static int is_swap_device(const char *file)
247 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
248 if ((stat(file, &st_buf) == 0) &&
249 S_ISBLK(st_buf.st_mode))
250 file_dev = st_buf.st_rdev;
253 if (!(f = fopen("/proc/swaps", "r")))
255 /* Skip the first line */
256 if (!fgets(buf, sizeof(buf), f))
258 if (*buf && strncmp(buf, "Filename\t", 9))
259 /* Linux <=2.6.19 contained a bug in the /proc/swaps
260 * code where the header would not be displayed
262 goto valid_first_line;
264 while (fgets(buf, sizeof(buf), f)) {
266 if ((cp = strchr(buf, ' ')) != NULL)
268 if ((cp = strchr(buf, '\t')) != NULL)
270 if (strcmp(buf, file) == 0) {
275 if (file_dev && (stat(buf, &st_buf) == 0) &&
276 S_ISBLK(st_buf.st_mode) &&
277 file_dev == st_buf.st_rdev) {
291 * ext2fs_check_mount_point() fills determines if the device is
292 * mounted or otherwise busy, and fills in mount_flags with one or
293 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
294 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
295 * non-NULL, the directory where the device is mounted is copied to
296 * where mtpt is pointing, up to mtlen characters.
301 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
302 char *mtpt, int mtlen)
305 errcode_t retval = 0;
308 if (is_swap_device(device)) {
309 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
310 strncpy(mtpt, "<swap>", mtlen);
313 retval = check_mntent(device, mount_flags, mtpt, mtlen);
315 #ifdef HAVE_GETMNTINFO
316 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
319 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
322 #endif /* HAVE_GETMNTINFO */
323 #endif /* HAVE_MNTENT_H */
328 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
329 if ((stat(device, &st_buf) != 0) ||
330 !S_ISBLK(st_buf.st_mode))
332 fd = open(device, O_RDONLY | O_EXCL);
335 *mount_flags |= EXT2_MF_BUSY;
344 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
345 * EXT2_MF_READONLY, and EXT2_MF_ROOT
348 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
350 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
354 int main(int argc, char **argv)
356 int retval, mount_flags;
360 fprintf(stderr, "Usage: %s device\n", argv[0]);
364 add_error_table(&et_ext2_error_table);
366 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
367 mntpt, sizeof(mntpt));
369 com_err(argv[0], retval,
370 "while calling ext2fs_check_if_mounted");
373 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
374 if (mount_flags & EXT2_MF_BUSY)
375 printf("\t%s is apparently in use.\n", argv[1]);
376 if (mount_flags & EXT2_MF_MOUNTED)
377 printf("\t%s is mounted.\n", argv[1]);
378 if (mount_flags & EXT2_MF_SWAP)
379 printf("\t%s is a swap device.\n", argv[1]);
380 if (mount_flags & EXT2_MF_READONLY)
381 printf("\t%s is read-only.\n", argv[1]);
382 if (mount_flags & EXT2_MF_ISROOT)
383 printf("\t%s is the root filesystem.\n", argv[1]);
385 printf("\t%s is mounted on %s.\n", argv[1], mntpt);