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