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