Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1 | /* |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 2 | * ismounted.c --- Check to see if the filesystem was mounted |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 4 | * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 5 | * |
| 6 | * %Begin-Header% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 7 | * This file may be redistributed under the terms of the GNU Library |
| 8 | * General Public License, version 2. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 9 | * %End-Header% |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #if HAVE_UNISTD_H |
| 14 | #include <unistd.h> |
| 15 | #endif |
Theodore Ts'o | c4e749a | 1998-02-20 05:33:14 +0000 | [diff] [blame] | 16 | #if HAVE_ERRNO_H |
| 17 | #include <errno.h> |
| 18 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 19 | #include <fcntl.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 20 | #ifdef HAVE_LINUX_FD_H |
| 21 | #include <linux/fd.h> |
| 22 | #endif |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 23 | #ifdef HAVE_LINUX_LOOP_H |
| 24 | #include <linux/loop.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #ifdef HAVE_LINUX_MAJOR_H |
| 27 | #include <linux/major.h> |
| 28 | #endif /* HAVE_LINUX_MAJOR_H */ |
| 29 | #endif /* HAVE_LINUX_LOOP_H */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 30 | #ifdef HAVE_MNTENT_H |
| 31 | #include <mntent.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_GETMNTINFO |
| 34 | #include <paths.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <sys/mount.h> |
| 37 | #endif /* HAVE_GETMNTINFO */ |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 38 | #include <string.h> |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 39 | #include <sys/stat.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 40 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 41 | #include "ext2_fs.h" |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 42 | #include "ext2fs.h" |
| 43 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Check to see if a regular file is mounted. |
| 46 | * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check |
| 47 | * because the name in /proc/mounts is a loopback device not a regular file. |
| 48 | */ |
| 49 | static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev, |
| 50 | dev_t file_dev, ino_t file_ino) |
| 51 | { |
| 52 | #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) |
| 53 | struct loop_info64 loopinfo; |
| 54 | int loop_fd, ret; |
| 55 | |
| 56 | if (major(mnt_rdev) == LOOP_MAJOR) { |
| 57 | loop_fd = open(mnt_fsname, O_RDONLY); |
| 58 | if (loop_fd < 0) |
| 59 | return -1; |
| 60 | |
| 61 | ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo); |
| 62 | close(loop_fd); |
| 63 | if (ret < 0) |
| 64 | return -1; |
| 65 | |
| 66 | if (file_dev == loopinfo.lo_device && |
| 67 | file_ino == loopinfo.lo_inode) |
| 68 | return 1; |
| 69 | } |
| 70 | #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */ |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | #ifdef HAVE_SETMNTENT |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 75 | /* |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 76 | * Helper function which checks a file in /etc/mtab format to see if a |
| 77 | * filesystem is mounted. Returns an error if the file doesn't exist |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 78 | * or can't be opened. |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 79 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 80 | static errcode_t check_mntent_file(const char *mtab_file, const char *file, |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 81 | int *mount_flags, char *mtpt, int mtlen) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 82 | { |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 83 | struct mntent *mnt; |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 84 | struct stat st_buf; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 85 | errcode_t retval = 0; |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 86 | dev_t file_dev=0, file_rdev=0; |
| 87 | ino_t file_ino=0; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 88 | FILE *f; |
| 89 | int fd; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 90 | |
| 91 | *mount_flags = 0; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 92 | if ((f = setmntent (mtab_file, "r")) == NULL) { |
| 93 | if (errno == ENOENT) { |
| 94 | if (getenv("EXT2FS_NO_MTAB_OK")) |
| 95 | return 0; |
| 96 | else |
| 97 | return EXT2_ET_NO_MTAB_FILE; |
| 98 | } |
| 99 | return errno; |
| 100 | } |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 101 | if (stat(file, &st_buf) == 0) { |
| 102 | if (S_ISBLK(st_buf.st_mode)) { |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 103 | #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 104 | file_rdev = st_buf.st_rdev; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 105 | #endif /* __GNU__ */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 106 | } else { |
| 107 | file_dev = st_buf.st_dev; |
| 108 | file_ino = st_buf.st_ino; |
| 109 | } |
| 110 | } |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 111 | while ((mnt = getmntent (f)) != NULL) { |
Theodore Ts'o | f9110f4 | 2009-04-22 22:20:22 -0400 | [diff] [blame] | 112 | if (mnt->mnt_fsname[0] != '/') |
| 113 | continue; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 114 | if (strcmp(file, mnt->mnt_fsname) == 0) |
| 115 | break; |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 116 | if (stat(mnt->mnt_fsname, &st_buf) == 0) { |
| 117 | if (S_ISBLK(st_buf.st_mode)) { |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 118 | #ifndef __GNU__ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 119 | if (file_rdev && (file_rdev == st_buf.st_rdev)) |
| 120 | break; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 121 | if (check_loop_mounted(mnt->mnt_fsname, |
| 122 | st_buf.st_rdev, file_dev, |
| 123 | file_ino) == 1) |
| 124 | break; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 125 | #endif /* __GNU__ */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 126 | } else { |
| 127 | if (file_dev && ((file_dev == st_buf.st_dev) && |
| 128 | (file_ino == st_buf.st_ino))) |
| 129 | break; |
| 130 | } |
| 131 | } |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 132 | } |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 133 | |
| 134 | if (mnt == 0) { |
Theodore Ts'o | 66a4614 | 2001-06-13 23:26:19 +0000 | [diff] [blame] | 135 | #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */ |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Do an extra check to see if this is the root device. We |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 138 | * can't trust /etc/mtab, and /proc/mounts will only list |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 139 | * /dev/root for the root filesystem. Argh. Instead we |
| 140 | * check if the given device has the same major/minor number |
| 141 | * as the device that the root directory is on. |
| 142 | */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 143 | if (file_rdev && stat("/", &st_buf) == 0) { |
| 144 | if (st_buf.st_dev == file_rdev) { |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 145 | *mount_flags = EXT2_MF_MOUNTED; |
| 146 | if (mtpt) |
Theodore Ts'o | d5f858d | 2001-05-25 17:14:23 +0000 | [diff] [blame] | 147 | strncpy(mtpt, "/", mtlen); |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 148 | goto is_root; |
| 149 | } |
| 150 | } |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 151 | #endif /* __GNU__ */ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 152 | goto errout; |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 153 | } |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 154 | #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */ |
| 155 | /* Validate the entry in case /etc/mtab is out of date */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 156 | /* |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 157 | * We need to be paranoid, because some broken distributions |
| 158 | * (read: Slackware) don't initialize /etc/mtab before checking |
| 159 | * all of the non-root filesystems on the disk. |
| 160 | */ |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 161 | if (stat(mnt->mnt_dir, &st_buf) < 0) { |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 162 | retval = errno; |
| 163 | if (retval == ENOENT) { |
| 164 | #ifdef DEBUG |
| 165 | printf("Bogus entry in %s! (%s does not exist)\n", |
| 166 | mtab_file, mnt->mnt_dir); |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 167 | #endif /* DEBUG */ |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 168 | retval = 0; |
| 169 | } |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 170 | goto errout; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 171 | } |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 172 | if (file_rdev && (st_buf.st_dev != file_rdev)) { |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 173 | #ifdef DEBUG |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 174 | printf("Bogus entry in %s! (%s not mounted on %s)\n", |
| 175 | mtab_file, file, mnt->mnt_dir); |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 176 | #endif /* DEBUG */ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 177 | goto errout; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 178 | } |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 179 | #endif /* __GNU__ */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 180 | *mount_flags = EXT2_MF_MOUNTED; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 181 | |
Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 182 | #ifdef MNTOPT_RO |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 183 | /* Check to see if the ro option is set */ |
| 184 | if (hasmntopt(mnt, MNTOPT_RO)) |
| 185 | *mount_flags |= EXT2_MF_READONLY; |
Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 186 | #endif |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 187 | |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 188 | if (mtpt) |
| 189 | strncpy(mtpt, mnt->mnt_dir, mtlen); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 190 | /* |
| 191 | * Check to see if we're referring to the root filesystem. |
| 192 | * If so, do a manual check to see if we can open /etc/mtab |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 193 | * read/write, since if the root is mounted read/only, the |
| 194 | * contents of /etc/mtab may not be accurate. |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 195 | */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 196 | if (!strcmp(mnt->mnt_dir, "/")) { |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 197 | is_root: |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 198 | #define TEST_FILE "/.ismount-test-file" |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 199 | *mount_flags |= EXT2_MF_ISROOT; |
Andreas Dilger | 8cdd6a6 | 2007-11-08 18:20:15 -0700 | [diff] [blame] | 200 | fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 201 | if (fd < 0) { |
| 202 | if (errno == EROFS) |
| 203 | *mount_flags |= EXT2_MF_READONLY; |
| 204 | } else |
| 205 | close(fd); |
Theodore Ts'o | 997b820 | 2001-06-15 18:33:43 +0000 | [diff] [blame] | 206 | (void) unlink(TEST_FILE); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 207 | } |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 208 | retval = 0; |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 209 | errout: |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 210 | endmntent (f); |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 211 | return retval; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 212 | } |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 213 | |
| 214 | static errcode_t check_mntent(const char *file, int *mount_flags, |
| 215 | char *mtpt, int mtlen) |
| 216 | { |
| 217 | errcode_t retval; |
| 218 | |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 219 | #ifdef DEBUG |
| 220 | retval = check_mntent_file("/tmp/mtab", file, mount_flags, |
| 221 | mtpt, mtlen); |
| 222 | if (retval == 0) |
| 223 | return 0; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 224 | #endif /* DEBUG */ |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 225 | #ifdef __linux__ |
| 226 | retval = check_mntent_file("/proc/mounts", file, mount_flags, |
| 227 | mtpt, mtlen); |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 228 | if (retval == 0 && (*mount_flags != 0)) |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 229 | return 0; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 230 | #endif /* __linux__ */ |
Theodore Ts'o | 5818d67 | 2002-06-27 21:19:45 -0400 | [diff] [blame] | 231 | #if defined(MOUNTED) || defined(_PATH_MOUNTED) |
| 232 | #ifndef MOUNTED |
| 233 | #define MOUNTED _PATH_MOUNTED |
| 234 | #endif /* MOUNTED */ |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 235 | retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen); |
| 236 | return retval; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 237 | #else |
Theodore Ts'o | 5818d67 | 2002-06-27 21:19:45 -0400 | [diff] [blame] | 238 | *mount_flags = 0; |
| 239 | return 0; |
| 240 | #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */ |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 243 | #else |
| 244 | #if defined(HAVE_GETMNTINFO) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 245 | |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 246 | static errcode_t check_getmntinfo(const char *file, int *mount_flags, |
| 247 | char *mtpt, int mtlen) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 248 | { |
| 249 | struct statfs *mp; |
| 250 | int len, n; |
| 251 | const char *s1; |
| 252 | char *s2; |
| 253 | |
| 254 | n = getmntinfo(&mp, MNT_NOWAIT); |
| 255 | if (n == 0) |
| 256 | return errno; |
| 257 | |
| 258 | len = sizeof(_PATH_DEV) - 1; |
| 259 | s1 = file; |
| 260 | if (strncmp(_PATH_DEV, s1, len) == 0) |
| 261 | s1 += len; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 262 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 263 | *mount_flags = 0; |
| 264 | while (--n >= 0) { |
| 265 | s2 = mp->f_mntfromname; |
| 266 | if (strncmp(_PATH_DEV, s2, len) == 0) { |
| 267 | s2 += len - 1; |
| 268 | *s2 = 'r'; |
| 269 | } |
| 270 | if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) { |
| 271 | *mount_flags = EXT2_MF_MOUNTED; |
| 272 | break; |
| 273 | } |
| 274 | ++mp; |
| 275 | } |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 276 | if (mtpt) |
| 277 | strncpy(mtpt, mp->f_mntonname, mtlen); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | #endif /* HAVE_GETMNTINFO */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 281 | #endif /* HAVE_SETMNTENT */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 282 | |
| 283 | /* |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 284 | * Check to see if we're dealing with the swap device. |
| 285 | */ |
| 286 | static int is_swap_device(const char *file) |
| 287 | { |
| 288 | FILE *f; |
| 289 | char buf[1024], *cp; |
| 290 | dev_t file_dev; |
| 291 | struct stat st_buf; |
| 292 | int ret = 0; |
| 293 | |
| 294 | file_dev = 0; |
| 295 | #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */ |
| 296 | if ((stat(file, &st_buf) == 0) && |
| 297 | S_ISBLK(st_buf.st_mode)) |
| 298 | file_dev = st_buf.st_rdev; |
| 299 | #endif /* __GNU__ */ |
| 300 | |
| 301 | if (!(f = fopen("/proc/swaps", "r"))) |
| 302 | return 0; |
| 303 | /* Skip the first line */ |
Karel Zak | 5f91561 | 2009-10-13 13:52:59 +0000 | [diff] [blame] | 304 | if (!fgets(buf, sizeof(buf), f)) |
| 305 | goto leave; |
| 306 | if (*buf && strncmp(buf, "Filename\t", 9)) |
| 307 | /* Linux <=2.6.19 contained a bug in the /proc/swaps |
| 308 | * code where the header would not be displayed |
| 309 | */ |
| 310 | goto valid_first_line; |
| 311 | |
Dmitry V. Levin | d9039ae | 2007-10-20 22:08:40 +0400 | [diff] [blame] | 312 | while (fgets(buf, sizeof(buf), f)) { |
Karel Zak | 5f91561 | 2009-10-13 13:52:59 +0000 | [diff] [blame] | 313 | valid_first_line: |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 314 | if ((cp = strchr(buf, ' ')) != NULL) |
| 315 | *cp = 0; |
| 316 | if ((cp = strchr(buf, '\t')) != NULL) |
| 317 | *cp = 0; |
| 318 | if (strcmp(buf, file) == 0) { |
| 319 | ret++; |
| 320 | break; |
| 321 | } |
| 322 | #ifndef __GNU__ |
| 323 | if (file_dev && (stat(buf, &st_buf) == 0) && |
| 324 | S_ISBLK(st_buf.st_mode) && |
| 325 | file_dev == st_buf.st_rdev) { |
| 326 | ret++; |
| 327 | break; |
| 328 | } |
| 329 | #endif /* __GNU__ */ |
| 330 | } |
Karel Zak | 5f91561 | 2009-10-13 13:52:59 +0000 | [diff] [blame] | 331 | |
| 332 | leave: |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 333 | fclose(f); |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 339 | * ext2fs_check_mount_point() fills determines if the device is |
| 340 | * mounted or otherwise busy, and fills in mount_flags with one or |
| 341 | * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT, |
| 342 | * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is |
| 343 | * non-NULL, the directory where the device is mounted is copied to |
| 344 | * where mtpt is pointing, up to mtlen characters. |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 345 | */ |
| 346 | #ifdef __TURBOC__ |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 347 | #pragma argsused |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 348 | #endif |
| 349 | errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags, |
| 350 | char *mtpt, int mtlen) |
| 351 | { |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 352 | errcode_t retval = 0; |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 353 | |
Theodore Ts'o | 07cefe7 | 2001-12-24 15:20:22 -0500 | [diff] [blame] | 354 | if (is_swap_device(device)) { |
| 355 | *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP; |
| 356 | strncpy(mtpt, "<swap>", mtlen); |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 357 | } else { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 358 | #ifdef HAVE_SETMNTENT |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 359 | retval = check_mntent(device, mount_flags, mtpt, mtlen); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 360 | #else |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 361 | #ifdef HAVE_GETMNTINFO |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 362 | retval = check_getmntinfo(device, mount_flags, mtpt, mtlen); |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 363 | #else |
Theodore Ts'o | ed78c02 | 2003-03-06 11:09:18 -0500 | [diff] [blame] | 364 | #ifdef __GNUC__ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 365 | #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!" |
Theodore Ts'o | ed78c02 | 2003-03-06 11:09:18 -0500 | [diff] [blame] | 366 | #endif |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 367 | *mount_flags = 0; |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 368 | #endif /* HAVE_GETMNTINFO */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 369 | #endif /* HAVE_SETMNTENT */ |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 370 | } |
| 371 | if (retval) |
| 372 | return retval; |
| 373 | |
| 374 | #ifdef __linux__ /* This only works on Linux 2.6+ systems */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 375 | { |
| 376 | struct stat st_buf; |
| 377 | |
| 378 | if (stat(device, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) { |
| 379 | int fd = open(device, O_RDONLY | O_EXCL); |
| 380 | |
| 381 | if (fd >= 0) |
| 382 | close(fd); |
| 383 | else if (errno == EBUSY) |
| 384 | *mount_flags |= EXT2_MF_BUSY; |
| 385 | } |
| 386 | } |
Matthias Andree | 406ba67 | 2006-05-30 16:28:22 +0200 | [diff] [blame] | 387 | #endif |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 388 | |
| 389 | return 0; |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* |
Theodore Ts'o | 6bd1348 | 2001-06-14 07:00:55 +0000 | [diff] [blame] | 393 | * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED, |
Theodore Ts'o | d5f858d | 2001-05-25 17:14:23 +0000 | [diff] [blame] | 394 | * EXT2_MF_READONLY, and EXT2_MF_ROOT |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 395 | * |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 396 | */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 397 | errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags) |
| 398 | { |
Theodore Ts'o | d5f858d | 2001-05-25 17:14:23 +0000 | [diff] [blame] | 399 | return ext2fs_check_mount_point(file, mount_flags, NULL, 0); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 400 | } |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 401 | |
| 402 | #ifdef DEBUG |
| 403 | int main(int argc, char **argv) |
| 404 | { |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 405 | int retval, mount_flags; |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 406 | char mntpt[80]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 407 | |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 408 | if (argc < 2) { |
| 409 | fprintf(stderr, "Usage: %s device\n", argv[0]); |
| 410 | exit(1); |
| 411 | } |
| 412 | |
Theodore Ts'o | 04f13d6 | 2009-09-08 21:33:03 -0400 | [diff] [blame] | 413 | add_error_table(&et_ext2_error_table); |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 414 | mntpt[0] = 0; |
| 415 | retval = ext2fs_check_mount_point(argv[1], &mount_flags, |
| 416 | mntpt, sizeof(mntpt)); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 417 | if (retval) { |
| 418 | com_err(argv[0], retval, |
| 419 | "while calling ext2fs_check_if_mounted"); |
| 420 | exit(1); |
| 421 | } |
| 422 | printf("Device %s reports flags %02x\n", argv[1], mount_flags); |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 423 | if (mount_flags & EXT2_MF_BUSY) |
| 424 | printf("\t%s is apparently in use.\n", argv[1]); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 425 | if (mount_flags & EXT2_MF_MOUNTED) |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 426 | printf("\t%s is mounted.\n", argv[1]); |
Theodore Ts'o | 07cefe7 | 2001-12-24 15:20:22 -0500 | [diff] [blame] | 427 | if (mount_flags & EXT2_MF_SWAP) |
| 428 | printf("\t%s is a swap device.\n", argv[1]); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 429 | if (mount_flags & EXT2_MF_READONLY) |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 430 | printf("\t%s is read-only.\n", argv[1]); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 431 | if (mount_flags & EXT2_MF_ISROOT) |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 432 | printf("\t%s is the root filesystem.\n", argv[1]); |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 433 | if (mntpt[0]) |
| 434 | printf("\t%s is mounted on %s.\n", argv[1], mntpt); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 435 | exit(0); |
| 436 | } |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 437 | #endif /* DEBUG */ |