3
0

ismounted.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ismounted.c --- Check to see if the filesystem was mounted
  4. *
  5. * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  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. #include <sys/stat.h>
  33. #include "ext2_fs.h"
  34. #include "ext2fs.h"
  35. #ifdef HAVE_MNTENT_H
  36. /*
  37. * Helper function which checks a file in /etc/mtab format to see if a
  38. * filesystem is mounted. Returns an error if the file doesn't exist
  39. * or can't be opened.
  40. */
  41. static errcode_t check_mntent_file(const char *mtab_file, const char *file,
  42. int *mount_flags, char *mtpt, int mtlen)
  43. {
  44. struct mntent *mnt;
  45. struct stat st_buf;
  46. errcode_t retval = 0;
  47. dev_t file_dev=0, file_rdev=0;
  48. ino_t file_ino=0;
  49. FILE *f;
  50. int fd;
  51. *mount_flags = 0;
  52. if ((f = setmntent (mtab_file, "r")) == NULL)
  53. return errno;
  54. if (stat(file, &st_buf) == 0) {
  55. if (S_ISBLK(st_buf.st_mode)) {
  56. #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
  57. file_rdev = st_buf.st_rdev;
  58. #endif /* __GNU__ */
  59. } else {
  60. file_dev = st_buf.st_dev;
  61. file_ino = st_buf.st_ino;
  62. }
  63. }
  64. while ((mnt = getmntent (f)) != NULL) {
  65. if (strcmp(file, mnt->mnt_fsname) == 0)
  66. break;
  67. if (stat(mnt->mnt_fsname, &st_buf) == 0) {
  68. if (S_ISBLK(st_buf.st_mode)) {
  69. #ifndef __GNU__
  70. if (file_rdev && (file_rdev == st_buf.st_rdev))
  71. break;
  72. #endif /* __GNU__ */
  73. } else {
  74. if (file_dev && ((file_dev == st_buf.st_dev) &&
  75. (file_ino == st_buf.st_ino)))
  76. break;
  77. }
  78. }
  79. }
  80. if (mnt == 0) {
  81. #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
  82. /*
  83. * Do an extra check to see if this is the root device. We
  84. * can't trust /etc/mtab, and /proc/mounts will only list
  85. * /dev/root for the root filesystem. Argh. Instead we
  86. * check if the given device has the same major/minor number
  87. * as the device that the root directory is on.
  88. */
  89. if (file_rdev && stat("/", &st_buf) == 0) {
  90. if (st_buf.st_dev == file_rdev) {
  91. *mount_flags = EXT2_MF_MOUNTED;
  92. if (mtpt)
  93. strncpy(mtpt, "/", mtlen);
  94. goto is_root;
  95. }
  96. }
  97. #endif /* __GNU__ */
  98. goto errout;
  99. }
  100. #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
  101. /* Validate the entry in case /etc/mtab is out of date */
  102. /*
  103. * We need to be paranoid, because some broken distributions
  104. * (read: Slackware) don't initialize /etc/mtab before checking
  105. * all of the non-root filesystems on the disk.
  106. */
  107. if (stat(mnt->mnt_dir, &st_buf) < 0) {
  108. retval = errno;
  109. if (retval == ENOENT) {
  110. #ifdef DEBUG
  111. printf("Bogus entry in %s! (%s does not exist)\n",
  112. mtab_file, mnt->mnt_dir);
  113. #endif /* DEBUG */
  114. retval = 0;
  115. }
  116. goto errout;
  117. }
  118. if (file_rdev && (st_buf.st_dev != file_rdev)) {
  119. #ifdef DEBUG
  120. printf("Bogus entry in %s! (%s not mounted on %s)\n",
  121. mtab_file, file, mnt->mnt_dir);
  122. #endif /* DEBUG */
  123. goto errout;
  124. }
  125. #endif /* __GNU__ */
  126. *mount_flags = EXT2_MF_MOUNTED;
  127. #ifdef MNTOPT_RO
  128. /* Check to see if the ro option is set */
  129. if (hasmntopt(mnt, MNTOPT_RO))
  130. *mount_flags |= EXT2_MF_READONLY;
  131. #endif
  132. if (mtpt)
  133. strncpy(mtpt, mnt->mnt_dir, mtlen);
  134. /*
  135. * Check to see if we're referring to the root filesystem.
  136. * If so, do a manual check to see if we can open /etc/mtab
  137. * read/write, since if the root is mounted read/only, the
  138. * contents of /etc/mtab may not be accurate.
  139. */
  140. if (LONE_CHAR(mnt->mnt_dir, '/')) {
  141. is_root:
  142. #define TEST_FILE "/.ismount-test-file"
  143. *mount_flags |= EXT2_MF_ISROOT;
  144. fd = open(TEST_FILE, O_RDWR|O_CREAT);
  145. if (fd < 0) {
  146. if (errno == EROFS)
  147. *mount_flags |= EXT2_MF_READONLY;
  148. } else
  149. close(fd);
  150. (void) unlink(TEST_FILE);
  151. }
  152. retval = 0;
  153. errout:
  154. endmntent (f);
  155. return retval;
  156. }
  157. static errcode_t check_mntent(const char *file, int *mount_flags,
  158. char *mtpt, int mtlen)
  159. {
  160. errcode_t retval;
  161. #ifdef DEBUG
  162. retval = check_mntent_file("/tmp/mtab", file, mount_flags,
  163. mtpt, mtlen);
  164. if (retval == 0)
  165. return 0;
  166. #endif /* DEBUG */
  167. #ifdef __linux__
  168. retval = check_mntent_file("/proc/mounts", file, mount_flags,
  169. mtpt, mtlen);
  170. if (retval == 0 && (*mount_flags != 0))
  171. return 0;
  172. #endif /* __linux__ */
  173. #if defined(MOUNTED) || defined(_PATH_MOUNTED)
  174. #ifndef MOUNTED
  175. #define MOUNTED _PATH_MOUNTED
  176. #endif /* MOUNTED */
  177. retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
  178. return retval;
  179. #else
  180. *mount_flags = 0;
  181. return 0;
  182. #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
  183. }
  184. #else
  185. #if defined(HAVE_GETMNTINFO)
  186. static errcode_t check_getmntinfo(const char *file, int *mount_flags,
  187. char *mtpt, int mtlen)
  188. {
  189. struct statfs *mp;
  190. int len, n;
  191. const char *s1;
  192. char *s2;
  193. n = getmntinfo(&mp, MNT_NOWAIT);
  194. if (n == 0)
  195. return errno;
  196. len = sizeof(_PATH_DEV) - 1;
  197. s1 = file;
  198. if (strncmp(_PATH_DEV, s1, len) == 0)
  199. s1 += len;
  200. *mount_flags = 0;
  201. while (--n >= 0) {
  202. s2 = mp->f_mntfromname;
  203. if (strncmp(_PATH_DEV, s2, len) == 0) {
  204. s2 += len - 1;
  205. *s2 = 'r';
  206. }
  207. if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
  208. *mount_flags = EXT2_MF_MOUNTED;
  209. break;
  210. }
  211. ++mp;
  212. }
  213. if (mtpt)
  214. strncpy(mtpt, mp->f_mntonname, mtlen);
  215. return 0;
  216. }
  217. #endif /* HAVE_GETMNTINFO */
  218. #endif /* HAVE_MNTENT_H */
  219. /*
  220. * Check to see if we're dealing with the swap device.
  221. */
  222. static int is_swap_device(const char *file)
  223. {
  224. FILE *f;
  225. char buf[1024], *cp;
  226. dev_t file_dev;
  227. struct stat st_buf;
  228. int ret = 0;
  229. file_dev = 0;
  230. #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
  231. if ((stat(file, &st_buf) == 0) &&
  232. S_ISBLK(st_buf.st_mode))
  233. file_dev = st_buf.st_rdev;
  234. #endif /* __GNU__ */
  235. if (!(f = fopen_for_read("/proc/swaps")))
  236. return 0;
  237. /* Skip the first line */
  238. fgets(buf, sizeof(buf), f);
  239. while (!feof(f)) {
  240. if (!fgets(buf, sizeof(buf), f))
  241. break;
  242. if ((cp = strchr(buf, ' ')) != NULL)
  243. *cp = 0;
  244. if ((cp = strchr(buf, '\t')) != NULL)
  245. *cp = 0;
  246. if (strcmp(buf, file) == 0) {
  247. ret++;
  248. break;
  249. }
  250. #ifndef __GNU__
  251. if (file_dev && (stat(buf, &st_buf) == 0) &&
  252. S_ISBLK(st_buf.st_mode) &&
  253. file_dev == st_buf.st_rdev) {
  254. ret++;
  255. break;
  256. }
  257. #endif /* __GNU__ */
  258. }
  259. fclose(f);
  260. return ret;
  261. }
  262. /*
  263. * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
  264. * otherwise. If mtpt is non-NULL, the directory where the device is
  265. * mounted is copied to where mtpt is pointing, up to mtlen
  266. * characters.
  267. */
  268. #ifdef __TURBOC__
  269. # pragma argsused
  270. #endif
  271. errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
  272. char *mtpt, int mtlen)
  273. {
  274. if (is_swap_device(device)) {
  275. *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
  276. strncpy(mtpt, "<swap>", mtlen);
  277. return 0;
  278. }
  279. #ifdef HAVE_MNTENT_H
  280. return check_mntent(device, mount_flags, mtpt, mtlen);
  281. #else
  282. #ifdef HAVE_GETMNTINFO
  283. return check_getmntinfo(device, mount_flags, mtpt, mtlen);
  284. #else
  285. #ifdef __GNUC__
  286. #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
  287. #endif
  288. *mount_flags = 0;
  289. return 0;
  290. #endif /* HAVE_GETMNTINFO */
  291. #endif /* HAVE_MNTENT_H */
  292. }
  293. /*
  294. * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
  295. * EXT2_MF_READONLY, and EXT2_MF_ROOT
  296. *
  297. */
  298. errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
  299. {
  300. return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
  301. }
  302. #ifdef DEBUG
  303. int main(int argc, char **argv)
  304. {
  305. int retval, mount_flags;
  306. char mntpt[80];
  307. if (argc < 2) {
  308. fprintf(stderr, "Usage: %s device\n", argv[0]);
  309. exit(1);
  310. }
  311. mntpt[0] = 0;
  312. retval = ext2fs_check_mount_point(argv[1], &mount_flags,
  313. mntpt, sizeof(mntpt));
  314. if (retval) {
  315. com_err(argv[0], retval,
  316. "while calling ext2fs_check_if_mounted");
  317. exit(1);
  318. }
  319. printf("Device %s reports flags %02x\n", argv[1], mount_flags);
  320. if (mount_flags & EXT2_MF_BUSY)
  321. printf("\t%s is apparently in use.\n", argv[1]);
  322. if (mount_flags & EXT2_MF_MOUNTED)
  323. printf("\t%s is mounted.\n", argv[1]);
  324. if (mount_flags & EXT2_MF_SWAP)
  325. printf("\t%s is a swap device.\n", argv[1]);
  326. if (mount_flags & EXT2_MF_READONLY)
  327. printf("\t%s is read-only.\n", argv[1]);
  328. if (mount_flags & EXT2_MF_ISROOT)
  329. printf("\t%s is the root filesystem.\n", argv[1]);
  330. if (mntpt[0])
  331. printf("\t%s is mounted on %s.\n", argv[1], mntpt);
  332. exit(0);
  333. }
  334. #endif /* DEBUG */