ismounted.c revision 9f8046fc6dfc13eee2f5c363214e60b533872cac
1/* 2 * ismounted.c --- Check to see if the filesystem was mounted 3 * 4 * Copyright (C) 1995 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#if HAVE_ERRNO_H 17#include <errno.h> 18#endif 19#include <fcntl.h> 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 */ 31#include <string.h> 32 33#include "ext2_fs.h" 34#include "ext2fs.h" 35 36#ifdef HAVE_MNTENT_H 37/* 38 * Helper function which checks a file in /etc/mtab format to see if a 39 * filesystem is mounted. Returns an error if the file doesn't exist 40 * or can't be opened. 41 */ 42static errcode_t check_mntent_file(const char *mtab_file, const char *file, 43 int *mount_flags, char *mtpt, int mtlen) 44{ 45 FILE * f; 46 struct mntent * mnt; 47 int fd; 48 49 *mount_flags = 0; 50 if ((f = setmntent (mtab_file, "r")) == NULL) 51 return errno; 52 while ((mnt = getmntent (f)) != NULL) 53 if (strcmp(file, mnt->mnt_fsname) == 0) 54 break; 55 endmntent (f); 56 if (mnt == 0) 57 return 0; 58 *mount_flags = EXT2_MF_MOUNTED; 59 60 /* Check to see if the ro option is set */ 61 if (hasmntopt(mnt, MNTOPT_RO)) 62 *mount_flags |= EXT2_MF_READONLY; 63 64 /* 65 * Check to see if we're referring to the root filesystem. 66 * If so, do a manual check to see if we can open /etc/mtab 67 * read/write, since if the root is mounted read/only, 68 * /etc/mtab may not be accurate. 69 */ 70 if (!strcmp(mnt->mnt_dir, "/")) { 71 *mount_flags |= EXT2_MF_ISROOT; 72 fd = open(MOUNTED, O_RDWR); 73 if (fd < 0) { 74 if (errno == EROFS) 75 *mount_flags |= EXT2_MF_READONLY; 76 } else 77 close(fd); 78 } 79 if (mtpt) 80 strncpy(mtpt, mnt->mnt_dir, mtlen); 81 return 0; 82} 83 84static errcode_t check_mntent(const char *file, int *mount_flags, 85 char *mtpt, int mtlen) 86{ 87 errcode_t retval; 88 89#ifdef __linux__ 90 retval = check_mntent_file("/proc/mounts", file, mount_flags, 91 mtpt, mtlen); 92 if (retval == 0) 93 return 0; 94#endif 95 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen); 96 return retval; 97} 98 99#endif 100 101#ifdef HAVE_GETMNTINFO 102static errcode_t check_getmntinfo(const char *file, int *mount_flags, 103 char *mtpt, int mtlen) 104{ 105 struct statfs *mp; 106 int len, n; 107 const char *s1; 108 char *s2; 109 110 n = getmntinfo(&mp, MNT_NOWAIT); 111 if (n == 0) 112 return errno; 113 114 len = sizeof(_PATH_DEV) - 1; 115 s1 = file; 116 if (strncmp(_PATH_DEV, s1, len) == 0) 117 s1 += len; 118 119 *mount_flags = 0; 120 while (--n >= 0) { 121 s2 = mp->f_mntfromname; 122 if (strncmp(_PATH_DEV, s2, len) == 0) { 123 s2 += len - 1; 124 *s2 = 'r'; 125 } 126 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) { 127 *mount_flags = EXT2_MF_MOUNTED; 128 break; 129 } 130 ++mp; 131 } 132 if (mtpt) 133 strncpy(mtpt, mp->f_mntonname, mtlen); 134 return 0; 135} 136#endif /* HAVE_GETMNTINFO */ 137 138/* 139 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0 140 * otherwise. If mtpt is non-NULL, the directory where the device is 141 * mounted is copied to where mtpt is pointing, up to mtlen 142 * characters. 143 */ 144#ifdef __TURBOC__ 145 #pragma argsused 146#endif 147errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags, 148 char *mtpt, int mtlen) 149{ 150#ifdef HAVE_MNTENT_H 151 return check_mntent(device, mount_flags, mtpt, mtlen); 152#else 153#ifdef HAVE_GETMNTINFO 154 return check_getmntinfo(device, mount_flags, mtpt, mtlen); 155#else 156 *mount_flags = 0; 157 return 0; 158#endif /* HAVE_GETMNTINFO */ 159#endif /* HAVE_MNTENT_H */ 160} 161 162/* 163 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED and 164 * EXT2_MF_READONLY 165 * 166 */ 167#ifdef __TURBOC__ 168 #pragma argsused 169#endif 170errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags) 171{ 172#ifdef HAVE_MNTENT_H 173 return check_mntent(file, mount_flags, NULL, 0); 174#else 175#ifdef HAVE_GETMNTINFO 176 return check_getmntinfo(file, mount_flags, NULL, 0); 177#else 178 *mount_flags = 0; 179 return 0; 180#endif /* HAVE_GETMNTINFO */ 181#endif /* HAVE_MNTENT_H */ 182} 183 184#ifdef DEBUG 185int main(int argc, char **argv) 186{ 187 blk_t blocks; 188 int retval, mount_flags; 189 190 if (argc < 2) { 191 fprintf(stderr, "Usage: %s device\n", argv[0]); 192 exit(1); 193 } 194 195 retval = ext2fs_check_if_mounted(argv[1], &mount_flags); 196 if (retval) { 197 com_err(argv[0], retval, 198 "while calling ext2fs_check_if_mounted"); 199 exit(1); 200 } 201 printf("Device %s reports flags %02x\n", argv[1], mount_flags); 202 if (mount_flags & EXT2_MF_MOUNTED) 203 printf("\t%s is mounted.\n"); 204 205 if (mount_flags & EXT2_MF_READONLY) 206 printf("\t%s is read-only.\n"); 207 208 if (mount_flags & EXT2_MF_ISROOT) 209 printf("\t%s is the root filesystem.\n"); 210 211 exit(0); 212} 213#endif 214