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% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 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 |
| 23 | #ifdef HAVE_MNTENT_H |
| 24 | #include <mntent.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_GETMNTINFO |
| 27 | #include <paths.h> |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/mount.h> |
| 30 | #endif /* HAVE_GETMNTINFO */ |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 31 | #include <string.h> |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 32 | #include <sys/stat.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 33 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 34 | #include "ext2_fs.h" |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 35 | #include "ext2fs.h" |
| 36 | |
| 37 | #ifdef HAVE_MNTENT_H |
| 38 | /* |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 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 |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 41 | * or can't be opened. |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 42 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 43 | 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] | 44 | int *mount_flags, char *mtpt, int mtlen) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 45 | { |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 46 | struct mntent *mnt; |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 47 | struct stat st_buf; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 48 | errcode_t retval = 0; |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 49 | dev_t file_dev=0, file_rdev=0; |
| 50 | ino_t file_ino=0; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 51 | FILE *f; |
| 52 | int fd; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 53 | |
| 54 | *mount_flags = 0; |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 55 | if ((f = setmntent (mtab_file, "r")) == NULL) |
Theodore Ts'o | 8652228 | 2009-05-29 00:09:57 -0400 | [diff] [blame] | 56 | return (errno == ENOENT ? EXT2_NO_MTAB_FILE : errno); |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 57 | if (stat(file, &st_buf) == 0) { |
| 58 | if (S_ISBLK(st_buf.st_mode)) { |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 59 | #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] | 60 | file_rdev = st_buf.st_rdev; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 61 | #endif /* __GNU__ */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 62 | } else { |
| 63 | file_dev = st_buf.st_dev; |
| 64 | file_ino = st_buf.st_ino; |
| 65 | } |
| 66 | } |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 67 | while ((mnt = getmntent (f)) != NULL) { |
Theodore Ts'o | f9110f4 | 2009-04-22 22:20:22 -0400 | [diff] [blame] | 68 | if (mnt->mnt_fsname[0] != '/') |
| 69 | continue; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 70 | if (strcmp(file, mnt->mnt_fsname) == 0) |
| 71 | break; |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 72 | if (stat(mnt->mnt_fsname, &st_buf) == 0) { |
| 73 | if (S_ISBLK(st_buf.st_mode)) { |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 74 | #ifndef __GNU__ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 75 | if (file_rdev && (file_rdev == st_buf.st_rdev)) |
| 76 | break; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 77 | #endif /* __GNU__ */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 78 | } else { |
| 79 | if (file_dev && ((file_dev == st_buf.st_dev) && |
| 80 | (file_ino == st_buf.st_ino))) |
| 81 | break; |
| 82 | } |
| 83 | } |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 84 | } |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 85 | |
| 86 | if (mnt == 0) { |
Theodore Ts'o | 66a4614 | 2001-06-13 23:26:19 +0000 | [diff] [blame] | 87 | #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] | 88 | /* |
| 89 | * 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] | 90 | * can't trust /etc/mtab, and /proc/mounts will only list |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 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. |
| 94 | */ |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 95 | if (file_rdev && stat("/", &st_buf) == 0) { |
| 96 | if (st_buf.st_dev == file_rdev) { |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 97 | *mount_flags = EXT2_MF_MOUNTED; |
| 98 | if (mtpt) |
Theodore Ts'o | d5f858d | 2001-05-25 17:14:23 +0000 | [diff] [blame] | 99 | strncpy(mtpt, "/", mtlen); |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 100 | goto is_root; |
| 101 | } |
| 102 | } |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 103 | #endif /* __GNU__ */ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 104 | goto errout; |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 105 | } |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 106 | #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */ |
| 107 | /* Validate the entry in case /etc/mtab is out of date */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 108 | /* |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 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. |
| 112 | */ |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 113 | if (stat(mnt->mnt_dir, &st_buf) < 0) { |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 114 | retval = errno; |
| 115 | if (retval == ENOENT) { |
| 116 | #ifdef DEBUG |
| 117 | printf("Bogus entry in %s! (%s does not exist)\n", |
| 118 | mtab_file, mnt->mnt_dir); |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 119 | #endif /* DEBUG */ |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 120 | retval = 0; |
| 121 | } |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 122 | goto errout; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 123 | } |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 124 | if (file_rdev && (st_buf.st_dev != file_rdev)) { |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 125 | #ifdef DEBUG |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 126 | printf("Bogus entry in %s! (%s not mounted on %s)\n", |
| 127 | mtab_file, file, mnt->mnt_dir); |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 128 | #endif /* DEBUG */ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 129 | goto errout; |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 130 | } |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 131 | #endif /* __GNU__ */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 132 | *mount_flags = EXT2_MF_MOUNTED; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 133 | |
Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 134 | #ifdef MNTOPT_RO |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 135 | /* Check to see if the ro option is set */ |
| 136 | if (hasmntopt(mnt, MNTOPT_RO)) |
| 137 | *mount_flags |= EXT2_MF_READONLY; |
Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 138 | #endif |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 139 | |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 140 | if (mtpt) |
| 141 | strncpy(mtpt, mnt->mnt_dir, mtlen); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 142 | /* |
| 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 |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 145 | * read/write, since if the root is mounted read/only, the |
| 146 | * contents of /etc/mtab may not be accurate. |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 147 | */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 148 | if (!strcmp(mnt->mnt_dir, "/")) { |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 149 | is_root: |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 150 | #define TEST_FILE "/.ismount-test-file" |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 151 | *mount_flags |= EXT2_MF_ISROOT; |
Andreas Dilger | 8cdd6a6 | 2007-11-08 18:20:15 -0700 | [diff] [blame] | 152 | fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 153 | if (fd < 0) { |
| 154 | if (errno == EROFS) |
| 155 | *mount_flags |= EXT2_MF_READONLY; |
| 156 | } else |
| 157 | close(fd); |
Theodore Ts'o | 997b820 | 2001-06-15 18:33:43 +0000 | [diff] [blame] | 158 | (void) unlink(TEST_FILE); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 159 | } |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 160 | retval = 0; |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 161 | errout: |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 162 | endmntent (f); |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 163 | return retval; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 164 | } |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 165 | |
| 166 | static errcode_t check_mntent(const char *file, int *mount_flags, |
| 167 | char *mtpt, int mtlen) |
| 168 | { |
| 169 | errcode_t retval; |
| 170 | |
Theodore Ts'o | 2038b63 | 2001-09-14 07:44:25 -0400 | [diff] [blame] | 171 | #ifdef DEBUG |
| 172 | retval = check_mntent_file("/tmp/mtab", file, mount_flags, |
| 173 | mtpt, mtlen); |
| 174 | if (retval == 0) |
| 175 | return 0; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 176 | #endif /* DEBUG */ |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 177 | #ifdef __linux__ |
| 178 | retval = check_mntent_file("/proc/mounts", file, mount_flags, |
| 179 | mtpt, mtlen); |
Theodore Ts'o | 3a42fe2 | 2002-10-31 13:23:37 -0500 | [diff] [blame] | 180 | if (retval == 0 && (*mount_flags != 0)) |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 181 | return 0; |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 182 | #endif /* __linux__ */ |
Theodore Ts'o | 5818d67 | 2002-06-27 21:19:45 -0400 | [diff] [blame] | 183 | #if defined(MOUNTED) || defined(_PATH_MOUNTED) |
| 184 | #ifndef MOUNTED |
| 185 | #define MOUNTED _PATH_MOUNTED |
| 186 | #endif /* MOUNTED */ |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 187 | retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen); |
| 188 | return retval; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 189 | #else |
Theodore Ts'o | 5818d67 | 2002-06-27 21:19:45 -0400 | [diff] [blame] | 190 | *mount_flags = 0; |
| 191 | return 0; |
| 192 | #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */ |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 195 | #else |
| 196 | #if defined(HAVE_GETMNTINFO) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 197 | |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 198 | static errcode_t check_getmntinfo(const char *file, int *mount_flags, |
| 199 | char *mtpt, int mtlen) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 200 | { |
| 201 | struct statfs *mp; |
| 202 | int len, n; |
| 203 | const char *s1; |
| 204 | char *s2; |
| 205 | |
| 206 | n = getmntinfo(&mp, MNT_NOWAIT); |
| 207 | if (n == 0) |
| 208 | return errno; |
| 209 | |
| 210 | len = sizeof(_PATH_DEV) - 1; |
| 211 | s1 = file; |
| 212 | if (strncmp(_PATH_DEV, s1, len) == 0) |
| 213 | s1 += len; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 214 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 215 | *mount_flags = 0; |
| 216 | while (--n >= 0) { |
| 217 | s2 = mp->f_mntfromname; |
| 218 | if (strncmp(_PATH_DEV, s2, len) == 0) { |
| 219 | s2 += len - 1; |
| 220 | *s2 = 'r'; |
| 221 | } |
| 222 | if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) { |
| 223 | *mount_flags = EXT2_MF_MOUNTED; |
| 224 | break; |
| 225 | } |
| 226 | ++mp; |
| 227 | } |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 228 | if (mtpt) |
| 229 | strncpy(mtpt, mp->f_mntonname, mtlen); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | #endif /* HAVE_GETMNTINFO */ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 233 | #endif /* HAVE_MNTENT_H */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 236 | * Check to see if we're dealing with the swap device. |
| 237 | */ |
| 238 | static int is_swap_device(const char *file) |
| 239 | { |
| 240 | FILE *f; |
| 241 | char buf[1024], *cp; |
| 242 | dev_t file_dev; |
| 243 | struct stat st_buf; |
| 244 | int ret = 0; |
| 245 | |
| 246 | file_dev = 0; |
| 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; |
| 251 | #endif /* __GNU__ */ |
| 252 | |
| 253 | if (!(f = fopen("/proc/swaps", "r"))) |
| 254 | return 0; |
| 255 | /* Skip the first line */ |
Karel Zak | 5f91561 | 2009-10-13 13:52:59 +0000 | [diff] [blame^] | 256 | if (!fgets(buf, sizeof(buf), f)) |
| 257 | goto leave; |
| 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 |
| 261 | */ |
| 262 | goto valid_first_line; |
| 263 | |
Dmitry V. Levin | d9039ae | 2007-10-20 22:08:40 +0400 | [diff] [blame] | 264 | while (fgets(buf, sizeof(buf), f)) { |
Karel Zak | 5f91561 | 2009-10-13 13:52:59 +0000 | [diff] [blame^] | 265 | valid_first_line: |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 266 | if ((cp = strchr(buf, ' ')) != NULL) |
| 267 | *cp = 0; |
| 268 | if ((cp = strchr(buf, '\t')) != NULL) |
| 269 | *cp = 0; |
| 270 | if (strcmp(buf, file) == 0) { |
| 271 | ret++; |
| 272 | break; |
| 273 | } |
| 274 | #ifndef __GNU__ |
| 275 | if (file_dev && (stat(buf, &st_buf) == 0) && |
| 276 | S_ISBLK(st_buf.st_mode) && |
| 277 | file_dev == st_buf.st_rdev) { |
| 278 | ret++; |
| 279 | break; |
| 280 | } |
| 281 | #endif /* __GNU__ */ |
| 282 | } |
Karel Zak | 5f91561 | 2009-10-13 13:52:59 +0000 | [diff] [blame^] | 283 | |
| 284 | leave: |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 285 | fclose(f); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 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. |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 297 | */ |
| 298 | #ifdef __TURBOC__ |
Theodore Ts'o | 31dbecd | 2001-01-11 04:54:39 +0000 | [diff] [blame] | 299 | #pragma argsused |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 300 | #endif |
| 301 | errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags, |
| 302 | char *mtpt, int mtlen) |
| 303 | { |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 304 | struct stat st_buf; |
| 305 | errcode_t retval = 0; |
| 306 | int fd; |
| 307 | |
Theodore Ts'o | 07cefe7 | 2001-12-24 15:20:22 -0500 | [diff] [blame] | 308 | if (is_swap_device(device)) { |
| 309 | *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP; |
| 310 | strncpy(mtpt, "<swap>", mtlen); |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 311 | } else { |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 312 | #ifdef HAVE_MNTENT_H |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 313 | retval = check_mntent(device, mount_flags, mtpt, mtlen); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 314 | #else |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 315 | #ifdef HAVE_GETMNTINFO |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 316 | retval = check_getmntinfo(device, mount_flags, mtpt, mtlen); |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 317 | #else |
Theodore Ts'o | ed78c02 | 2003-03-06 11:09:18 -0500 | [diff] [blame] | 318 | #ifdef __GNUC__ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 319 | #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] | 320 | #endif |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 321 | *mount_flags = 0; |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 322 | #endif /* HAVE_GETMNTINFO */ |
| 323 | #endif /* HAVE_MNTENT_H */ |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 324 | } |
| 325 | if (retval) |
| 326 | return retval; |
| 327 | |
| 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)) |
| 331 | return 0; |
| 332 | fd = open(device, O_RDONLY | O_EXCL); |
| 333 | if (fd < 0) { |
| 334 | if (errno == EBUSY) |
| 335 | *mount_flags |= EXT2_MF_BUSY; |
| 336 | } else |
| 337 | close(fd); |
Matthias Andree | 406ba67 | 2006-05-30 16:28:22 +0200 | [diff] [blame] | 338 | #endif |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 339 | |
| 340 | return 0; |
Theodore Ts'o | 43ec873 | 2001-01-03 14:56:46 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
Theodore Ts'o | 6bd1348 | 2001-06-14 07:00:55 +0000 | [diff] [blame] | 344 | * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED, |
Theodore Ts'o | d5f858d | 2001-05-25 17:14:23 +0000 | [diff] [blame] | 345 | * EXT2_MF_READONLY, and EXT2_MF_ROOT |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 346 | * |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 347 | */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 348 | errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags) |
| 349 | { |
Theodore Ts'o | d5f858d | 2001-05-25 17:14:23 +0000 | [diff] [blame] | 350 | return ext2fs_check_mount_point(file, mount_flags, NULL, 0); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 351 | } |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 352 | |
| 353 | #ifdef DEBUG |
| 354 | int main(int argc, char **argv) |
| 355 | { |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 356 | int retval, mount_flags; |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 357 | char mntpt[80]; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 358 | |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 359 | if (argc < 2) { |
| 360 | fprintf(stderr, "Usage: %s device\n", argv[0]); |
| 361 | exit(1); |
| 362 | } |
| 363 | |
Theodore Ts'o | 04f13d6 | 2009-09-08 21:33:03 -0400 | [diff] [blame] | 364 | add_error_table(&et_ext2_error_table); |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 365 | mntpt[0] = 0; |
| 366 | retval = ext2fs_check_mount_point(argv[1], &mount_flags, |
| 367 | mntpt, sizeof(mntpt)); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 368 | if (retval) { |
| 369 | com_err(argv[0], retval, |
| 370 | "while calling ext2fs_check_if_mounted"); |
| 371 | exit(1); |
| 372 | } |
| 373 | printf("Device %s reports flags %02x\n", argv[1], mount_flags); |
Theodore Ts'o | 2fa8f37 | 2005-06-05 16:05:22 -0400 | [diff] [blame] | 374 | if (mount_flags & EXT2_MF_BUSY) |
| 375 | printf("\t%s is apparently in use.\n", argv[1]); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 376 | if (mount_flags & EXT2_MF_MOUNTED) |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 377 | printf("\t%s is mounted.\n", argv[1]); |
Theodore Ts'o | 07cefe7 | 2001-12-24 15:20:22 -0500 | [diff] [blame] | 378 | if (mount_flags & EXT2_MF_SWAP) |
| 379 | printf("\t%s is a swap device.\n", argv[1]); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 380 | if (mount_flags & EXT2_MF_READONLY) |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 381 | printf("\t%s is read-only.\n", argv[1]); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 382 | if (mount_flags & EXT2_MF_ISROOT) |
Theodore Ts'o | cc86017 | 2001-05-25 16:32:53 +0000 | [diff] [blame] | 383 | printf("\t%s is the root filesystem.\n", argv[1]); |
Theodore Ts'o | a8fd6e3 | 2001-12-24 01:40:35 -0500 | [diff] [blame] | 384 | if (mntpt[0]) |
| 385 | printf("\t%s is mounted on %s.\n", argv[1], mntpt); |
Theodore Ts'o | 52db0b4 | 2001-04-17 02:22:05 +0000 | [diff] [blame] | 386 | exit(0); |
| 387 | } |
Theodore Ts'o | f0efd29 | 2002-04-27 17:07:52 -0400 | [diff] [blame] | 388 | #endif /* DEBUG */ |