ismounted.c revision 2d328bb76d2d63bdfdba923b54c28bd686bd8fec
1/* 2 * ismounted.c --- Check to see if the filesystem was mounted 3 * 4 * Copyright (C) 1995,1996,1997,1998,1999,2000,2008 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Public 8 * License. 9 * %End-Header% 10 */ 11 12#include <stdio.h> 13#if HAVE_UNISTD_H 14#include <unistd.h> 15#endif 16#ifdef HAVE_STDLIB_H 17#include <stdlib.h> 18#endif 19#if HAVE_ERRNO_H 20#include <errno.h> 21#endif 22#include <fcntl.h> 23#ifdef HAVE_LINUX_FD_H 24#include <linux/fd.h> 25#endif 26#ifdef HAVE_MNTENT_H 27#include <mntent.h> 28#endif 29#include <string.h> 30#include <sys/stat.h> 31#include <ctype.h> 32 33#include "fsck.h" 34 35/* 36 * ext2fs_check_if_mounted flags 37 */ 38#define MF_MOUNTED 1 39 40#include "et/com_err.h" 41 42static char *skip_over_blank(char *cp) 43{ 44 while (*cp && isspace(*cp)) 45 cp++; 46 return cp; 47} 48 49static char *skip_over_word(char *cp) 50{ 51 while (*cp && !isspace(*cp)) 52 cp++; 53 return cp; 54} 55 56static char *parse_word(char **buf) 57{ 58 char *word, *next; 59 60 word = *buf; 61 if (*word == 0) 62 return 0; 63 64 word = skip_over_blank(word); 65 next = skip_over_word(word); 66 if (*next) 67 *next++ = 0; 68 *buf = next; 69 return word; 70} 71 72/* 73 * Helper function which checks a file in /etc/mtab format to see if a 74 * filesystem is mounted. Returns an error if the file doesn't exist 75 * or can't be opened. 76 */ 77static errcode_t check_mntent_file(const char *mtab_file, const char *file, 78 int *mount_flags) 79{ 80 struct stat st_buf; 81 errcode_t retval = 0; 82 dev_t file_dev=0, file_rdev=0; 83 ino_t file_ino=0; 84 FILE *f; 85 char buf[1024], *device = 0, *mnt_dir = 0, *cp; 86 87 *mount_flags = 0; 88 if ((f = fopen(mtab_file, "r")) == NULL) 89 return errno; 90 91 if ((f = setmntent (mtab_file, "r")) == NULL) 92 return errno; 93 if (stat(file, &st_buf) == 0) { 94 if (S_ISBLK(st_buf.st_mode)) { 95#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */ 96 file_rdev = st_buf.st_rdev; 97#endif /* __GNU__ */ 98 } else { 99 file_dev = st_buf.st_dev; 100 file_ino = st_buf.st_ino; 101 } 102 } 103 while (1) { 104 if (!fgets(buf, sizeof(buf), f)) { 105 device = mnt_dir = 0; 106 break; 107 } 108 buf[sizeof(buf)-1] = 0; 109 110 cp = buf; 111 device = parse_word(&cp); 112 if (!device || *device == '#') 113 return 0; /* Ignore blank lines and comments */ 114 mnt_dir = parse_word(&cp); 115 116 if (device[0] != '/') 117 continue; 118 119 if (strcmp(file, device) == 0) 120 break; 121 if (stat(device, &st_buf) == 0) { 122 if (S_ISBLK(st_buf.st_mode)) { 123#ifndef __GNU__ 124 if (file_rdev && (file_rdev == st_buf.st_rdev)) 125 break; 126#endif /* __GNU__ */ 127 } else { 128 if (file_dev && ((file_dev == st_buf.st_dev) && 129 (file_ino == st_buf.st_ino))) 130 break; 131 } 132 } 133 } 134 135 if (mnt_dir == 0) { 136#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */ 137 /* 138 * Do an extra check to see if this is the root device. We 139 * can't trust /etc/mtab, and /proc/mounts will only list 140 * /dev/root for the root filesystem. Argh. Instead we 141 * check if the given device has the same major/minor number 142 * as the device that the root directory is on. 143 */ 144 if (file_rdev && (stat("/", &st_buf) == 0) && 145 (st_buf.st_dev == file_rdev)) 146 *mount_flags = MF_MOUNTED; 147#endif /* __GNU__ */ 148 goto errout; 149 } 150#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */ 151 /* Validate the entry in case /etc/mtab is out of date */ 152 /* 153 * We need to be paranoid, because some broken distributions 154 * (read: Slackware) don't initialize /etc/mtab before checking 155 * all of the non-root filesystems on the disk. 156 */ 157 if (stat(mnt_dir, &st_buf) < 0) { 158 retval = errno; 159 if (retval == ENOENT) { 160#ifdef DEBUG 161 printf("Bogus entry in %s! (%s does not exist)\n", 162 mtab_file, mnt_dir); 163#endif /* DEBUG */ 164 retval = 0; 165 } 166 goto errout; 167 } 168 if (file_rdev && (st_buf.st_dev != file_rdev)) { 169#ifdef DEBUG 170 printf("Bogus entry in %s! (%s not mounted on %s)\n", 171 mtab_file, file, mnt_dir); 172#endif /* DEBUG */ 173 goto errout; 174 } 175#endif /* __GNU__ */ 176 *mount_flags = MF_MOUNTED; 177 178 retval = 0; 179errout: 180 endmntent (f); 181 return retval; 182} 183 184int is_mounted(const char *file) 185{ 186 errcode_t retval; 187 int mount_flags = 0; 188 189#ifdef __linux__ 190 retval = check_mntent_file("/proc/mounts", file, &mount_flags); 191 if (retval) 192 return 0; 193 if (mount_flags) 194 return 1; 195#endif /* __linux__ */ 196 retval = check_mntent_file("/etc/mtab", file, &mount_flags); 197 if (retval) 198 return 0; 199 return (mount_flags); 200} 201 202#ifdef DEBUG 203int main(int argc, char **argv) 204{ 205 if (argc < 2) { 206 fprintf(stderr, "Usage: %s device\n", argv[0]); 207 exit(1); 208 } 209 210 if (is_mounted(argv[1])) 211 printf("\t%s is mounted.\n", argv[1]); 212 exit(0); 213} 214#endif /* DEBUG */ 215