blob: db80c46091e52ab319e1442947d57866b17b58ca [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002 * ismounted.c --- Check to see if the filesystem was mounted
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'occ860172001-05-25 16:32:53 +00004 * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00005 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010 */
11
12#include <stdio.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000016#if HAVE_ERRNO_H
17#include <errno.h>
18#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#include <fcntl.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000020#ifdef HAVE_LINUX_FD_H
21#include <linux/fd.h>
22#endif
JP Abgralle0ed7402014-03-19 19:08:39 -070023#ifdef HAVE_LINUX_LOOP_H
24#include <linux/loop.h>
25#include <sys/ioctl.h>
26#ifdef HAVE_LINUX_MAJOR_H
27#include <linux/major.h>
28#endif /* HAVE_LINUX_MAJOR_H */
29#endif /* HAVE_LINUX_LOOP_H */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000030#ifdef HAVE_MNTENT_H
31#include <mntent.h>
32#endif
33#ifdef HAVE_GETMNTINFO
34#include <paths.h>
35#include <sys/param.h>
36#include <sys/mount.h>
37#endif /* HAVE_GETMNTINFO */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000038#include <string.h>
Theodore Ts'occ860172001-05-25 16:32:53 +000039#include <sys/stat.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000040
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000041#include "ext2_fs.h"
Theodore Ts'o50e1e101997-04-26 13:58:21 +000042#include "ext2fs.h"
43
JP Abgralle0ed7402014-03-19 19:08:39 -070044/*
45 * Check to see if a regular file is mounted.
46 * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
47 * because the name in /proc/mounts is a loopback device not a regular file.
48 */
49static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
50 dev_t file_dev, ino_t file_ino)
51{
52#if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
53 struct loop_info64 loopinfo;
54 int loop_fd, ret;
55
56 if (major(mnt_rdev) == LOOP_MAJOR) {
57 loop_fd = open(mnt_fsname, O_RDONLY);
58 if (loop_fd < 0)
59 return -1;
60
61 ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
62 close(loop_fd);
63 if (ret < 0)
64 return -1;
65
66 if (file_dev == loopinfo.lo_device &&
67 file_ino == loopinfo.lo_inode)
68 return 1;
69 }
70#endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
71 return 0;
72}
73
74#ifdef HAVE_SETMNTENT
Theodore Ts'o50e1e101997-04-26 13:58:21 +000075/*
Theodore Ts'o52db0b42001-04-17 02:22:05 +000076 * Helper function which checks a file in /etc/mtab format to see if a
77 * filesystem is mounted. Returns an error if the file doesn't exist
Theodore Ts'oefc6f622008-08-27 23:07:54 -040078 * or can't be opened.
Theodore Ts'o50e1e101997-04-26 13:58:21 +000079 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040080static errcode_t check_mntent_file(const char *mtab_file, const char *file,
Theodore Ts'o52db0b42001-04-17 02:22:05 +000081 int *mount_flags, char *mtpt, int mtlen)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000082{
Theodore Ts'o2038b632001-09-14 07:44:25 -040083 struct mntent *mnt;
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -050084 struct stat st_buf;
Theodore Ts'o2038b632001-09-14 07:44:25 -040085 errcode_t retval = 0;
Theodore Ts'o3a42fe22002-10-31 13:23:37 -050086 dev_t file_dev=0, file_rdev=0;
87 ino_t file_ino=0;
Theodore Ts'o2038b632001-09-14 07:44:25 -040088 FILE *f;
89 int fd;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000090
91 *mount_flags = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070092 if ((f = setmntent (mtab_file, "r")) == NULL) {
93 if (errno == ENOENT) {
94 if (getenv("EXT2FS_NO_MTAB_OK"))
95 return 0;
96 else
97 return EXT2_ET_NO_MTAB_FILE;
98 }
99 return errno;
100 }
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500101 if (stat(file, &st_buf) == 0) {
102 if (S_ISBLK(st_buf.st_mode)) {
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500103#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500104 file_rdev = st_buf.st_rdev;
Theodore Ts'of0efd292002-04-27 17:07:52 -0400105#endif /* __GNU__ */
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500106 } else {
107 file_dev = st_buf.st_dev;
108 file_ino = st_buf.st_ino;
109 }
110 }
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500111 while ((mnt = getmntent (f)) != NULL) {
Theodore Ts'of9110f42009-04-22 22:20:22 -0400112 if (mnt->mnt_fsname[0] != '/')
113 continue;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000114 if (strcmp(file, mnt->mnt_fsname) == 0)
115 break;
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500116 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
117 if (S_ISBLK(st_buf.st_mode)) {
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500118#ifndef __GNU__
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500119 if (file_rdev && (file_rdev == st_buf.st_rdev))
120 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700121 if (check_loop_mounted(mnt->mnt_fsname,
122 st_buf.st_rdev, file_dev,
123 file_ino) == 1)
124 break;
Theodore Ts'of0efd292002-04-27 17:07:52 -0400125#endif /* __GNU__ */
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500126 } else {
127 if (file_dev && ((file_dev == st_buf.st_dev) &&
128 (file_ino == st_buf.st_ino)))
129 break;
130 }
131 }
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500132 }
Theodore Ts'occ860172001-05-25 16:32:53 +0000133
134 if (mnt == 0) {
Theodore Ts'o66a46142001-06-13 23:26:19 +0000135#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
Theodore Ts'occ860172001-05-25 16:32:53 +0000136 /*
137 * Do an extra check to see if this is the root device. We
Theodore Ts'o2038b632001-09-14 07:44:25 -0400138 * can't trust /etc/mtab, and /proc/mounts will only list
Theodore Ts'occ860172001-05-25 16:32:53 +0000139 * /dev/root for the root filesystem. Argh. Instead we
140 * check if the given device has the same major/minor number
141 * as the device that the root directory is on.
142 */
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500143 if (file_rdev && stat("/", &st_buf) == 0) {
144 if (st_buf.st_dev == file_rdev) {
Theodore Ts'occ860172001-05-25 16:32:53 +0000145 *mount_flags = EXT2_MF_MOUNTED;
146 if (mtpt)
Theodore Ts'od5f858d2001-05-25 17:14:23 +0000147 strncpy(mtpt, "/", mtlen);
Theodore Ts'occ860172001-05-25 16:32:53 +0000148 goto is_root;
149 }
150 }
Theodore Ts'of0efd292002-04-27 17:07:52 -0400151#endif /* __GNU__ */
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400152 goto errout;
Theodore Ts'occ860172001-05-25 16:32:53 +0000153 }
Theodore Ts'o2038b632001-09-14 07:44:25 -0400154#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
155 /* Validate the entry in case /etc/mtab is out of date */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400156 /*
Theodore Ts'o2038b632001-09-14 07:44:25 -0400157 * We need to be paranoid, because some broken distributions
158 * (read: Slackware) don't initialize /etc/mtab before checking
159 * all of the non-root filesystems on the disk.
160 */
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500161 if (stat(mnt->mnt_dir, &st_buf) < 0) {
Theodore Ts'o2038b632001-09-14 07:44:25 -0400162 retval = errno;
163 if (retval == ENOENT) {
164#ifdef DEBUG
165 printf("Bogus entry in %s! (%s does not exist)\n",
166 mtab_file, mnt->mnt_dir);
Theodore Ts'of0efd292002-04-27 17:07:52 -0400167#endif /* DEBUG */
Theodore Ts'o2038b632001-09-14 07:44:25 -0400168 retval = 0;
169 }
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400170 goto errout;
Theodore Ts'o2038b632001-09-14 07:44:25 -0400171 }
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500172 if (file_rdev && (st_buf.st_dev != file_rdev)) {
Theodore Ts'o2038b632001-09-14 07:44:25 -0400173#ifdef DEBUG
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500174 printf("Bogus entry in %s! (%s not mounted on %s)\n",
175 mtab_file, file, mnt->mnt_dir);
Theodore Ts'of0efd292002-04-27 17:07:52 -0400176#endif /* DEBUG */
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400177 goto errout;
Theodore Ts'o2038b632001-09-14 07:44:25 -0400178 }
Theodore Ts'of0efd292002-04-27 17:07:52 -0400179#endif /* __GNU__ */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000180 *mount_flags = EXT2_MF_MOUNTED;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400181
Theodore Ts'offf45482003-04-13 00:44:19 -0400182#ifdef MNTOPT_RO
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000183 /* Check to see if the ro option is set */
184 if (hasmntopt(mnt, MNTOPT_RO))
185 *mount_flags |= EXT2_MF_READONLY;
Theodore Ts'offf45482003-04-13 00:44:19 -0400186#endif
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000187
Theodore Ts'occ860172001-05-25 16:32:53 +0000188 if (mtpt)
189 strncpy(mtpt, mnt->mnt_dir, mtlen);
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000190 /*
191 * Check to see if we're referring to the root filesystem.
192 * If so, do a manual check to see if we can open /etc/mtab
Theodore Ts'occ860172001-05-25 16:32:53 +0000193 * read/write, since if the root is mounted read/only, the
194 * contents of /etc/mtab may not be accurate.
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000195 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000196 if (!strcmp(mnt->mnt_dir, "/")) {
Theodore Ts'occ860172001-05-25 16:32:53 +0000197is_root:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400198#define TEST_FILE "/.ismount-test-file"
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000199 *mount_flags |= EXT2_MF_ISROOT;
Andreas Dilger8cdd6a62007-11-08 18:20:15 -0700200 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000201 if (fd < 0) {
202 if (errno == EROFS)
203 *mount_flags |= EXT2_MF_READONLY;
204 } else
205 close(fd);
Theodore Ts'o997b8202001-06-15 18:33:43 +0000206 (void) unlink(TEST_FILE);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000207 }
Theodore Ts'o2038b632001-09-14 07:44:25 -0400208 retval = 0;
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400209errout:
Theodore Ts'occ860172001-05-25 16:32:53 +0000210 endmntent (f);
Theodore Ts'o2038b632001-09-14 07:44:25 -0400211 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000212}
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000213
214static errcode_t check_mntent(const char *file, int *mount_flags,
215 char *mtpt, int mtlen)
216{
217 errcode_t retval;
218
Theodore Ts'o2038b632001-09-14 07:44:25 -0400219#ifdef DEBUG
220 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
221 mtpt, mtlen);
222 if (retval == 0)
223 return 0;
Theodore Ts'of0efd292002-04-27 17:07:52 -0400224#endif /* DEBUG */
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000225#ifdef __linux__
226 retval = check_mntent_file("/proc/mounts", file, mount_flags,
227 mtpt, mtlen);
Theodore Ts'o3a42fe22002-10-31 13:23:37 -0500228 if (retval == 0 && (*mount_flags != 0))
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000229 return 0;
Theodore Ts'of0efd292002-04-27 17:07:52 -0400230#endif /* __linux__ */
Theodore Ts'o5818d672002-06-27 21:19:45 -0400231#if defined(MOUNTED) || defined(_PATH_MOUNTED)
232#ifndef MOUNTED
233#define MOUNTED _PATH_MOUNTED
234#endif /* MOUNTED */
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000235 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
236 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400237#else
Theodore Ts'o5818d672002-06-27 21:19:45 -0400238 *mount_flags = 0;
239 return 0;
240#endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000241}
242
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400243#else
244#if defined(HAVE_GETMNTINFO)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000245
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000246static errcode_t check_getmntinfo(const char *file, int *mount_flags,
247 char *mtpt, int mtlen)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000248{
249 struct statfs *mp;
250 int len, n;
251 const char *s1;
252 char *s2;
253
254 n = getmntinfo(&mp, MNT_NOWAIT);
255 if (n == 0)
256 return errno;
257
258 len = sizeof(_PATH_DEV) - 1;
259 s1 = file;
260 if (strncmp(_PATH_DEV, s1, len) == 0)
261 s1 += len;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400262
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000263 *mount_flags = 0;
264 while (--n >= 0) {
265 s2 = mp->f_mntfromname;
266 if (strncmp(_PATH_DEV, s2, len) == 0) {
267 s2 += len - 1;
268 *s2 = 'r';
269 }
270 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
271 *mount_flags = EXT2_MF_MOUNTED;
272 break;
273 }
274 ++mp;
275 }
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000276 if (mtpt)
277 strncpy(mtpt, mp->f_mntonname, mtlen);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000278 return 0;
279}
280#endif /* HAVE_GETMNTINFO */
JP Abgralle0ed7402014-03-19 19:08:39 -0700281#endif /* HAVE_SETMNTENT */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000282
283/*
Theodore Ts'of0efd292002-04-27 17:07:52 -0400284 * Check to see if we're dealing with the swap device.
285 */
286static int is_swap_device(const char *file)
287{
288 FILE *f;
289 char buf[1024], *cp;
290 dev_t file_dev;
291 struct stat st_buf;
292 int ret = 0;
293
294 file_dev = 0;
295#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
296 if ((stat(file, &st_buf) == 0) &&
297 S_ISBLK(st_buf.st_mode))
298 file_dev = st_buf.st_rdev;
299#endif /* __GNU__ */
300
301 if (!(f = fopen("/proc/swaps", "r")))
302 return 0;
303 /* Skip the first line */
Karel Zak5f915612009-10-13 13:52:59 +0000304 if (!fgets(buf, sizeof(buf), f))
305 goto leave;
306 if (*buf && strncmp(buf, "Filename\t", 9))
307 /* Linux <=2.6.19 contained a bug in the /proc/swaps
308 * code where the header would not be displayed
309 */
310 goto valid_first_line;
311
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400312 while (fgets(buf, sizeof(buf), f)) {
Karel Zak5f915612009-10-13 13:52:59 +0000313valid_first_line:
Theodore Ts'of0efd292002-04-27 17:07:52 -0400314 if ((cp = strchr(buf, ' ')) != NULL)
315 *cp = 0;
316 if ((cp = strchr(buf, '\t')) != NULL)
317 *cp = 0;
318 if (strcmp(buf, file) == 0) {
319 ret++;
320 break;
321 }
322#ifndef __GNU__
323 if (file_dev && (stat(buf, &st_buf) == 0) &&
324 S_ISBLK(st_buf.st_mode) &&
325 file_dev == st_buf.st_rdev) {
326 ret++;
327 break;
328 }
329#endif /* __GNU__ */
330 }
Karel Zak5f915612009-10-13 13:52:59 +0000331
332leave:
Theodore Ts'of0efd292002-04-27 17:07:52 -0400333 fclose(f);
334 return ret;
335}
336
337
338/*
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400339 * ext2fs_check_mount_point() fills determines if the device is
340 * mounted or otherwise busy, and fills in mount_flags with one or
341 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
342 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
343 * non-NULL, the directory where the device is mounted is copied to
344 * where mtpt is pointing, up to mtlen characters.
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000345 */
346#ifdef __TURBOC__
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000347 #pragma argsused
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000348#endif
349errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
350 char *mtpt, int mtlen)
351{
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400352 errcode_t retval = 0;
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400353
Theodore Ts'o07cefe72001-12-24 15:20:22 -0500354 if (is_swap_device(device)) {
355 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
356 strncpy(mtpt, "<swap>", mtlen);
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400357 } else {
JP Abgralle0ed7402014-03-19 19:08:39 -0700358#ifdef HAVE_SETMNTENT
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400359 retval = check_mntent(device, mount_flags, mtpt, mtlen);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400360#else
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000361#ifdef HAVE_GETMNTINFO
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400362 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000363#else
Theodore Ts'oed78c022003-03-06 11:09:18 -0500364#ifdef __GNUC__
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400365 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
Theodore Ts'oed78c022003-03-06 11:09:18 -0500366#endif
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400367 *mount_flags = 0;
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000368#endif /* HAVE_GETMNTINFO */
JP Abgralle0ed7402014-03-19 19:08:39 -0700369#endif /* HAVE_SETMNTENT */
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400370 }
371 if (retval)
372 return retval;
373
374#ifdef __linux__ /* This only works on Linux 2.6+ systems */
JP Abgralle0ed7402014-03-19 19:08:39 -0700375 {
376 struct stat st_buf;
377
378 if (stat(device, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) {
379 int fd = open(device, O_RDONLY | O_EXCL);
380
381 if (fd >= 0)
382 close(fd);
383 else if (errno == EBUSY)
384 *mount_flags |= EXT2_MF_BUSY;
385 }
386 }
Matthias Andree406ba672006-05-30 16:28:22 +0200387#endif
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400388
389 return 0;
Theodore Ts'o43ec8732001-01-03 14:56:46 +0000390}
391
392/*
Theodore Ts'o6bd13482001-06-14 07:00:55 +0000393 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
Theodore Ts'od5f858d2001-05-25 17:14:23 +0000394 * EXT2_MF_READONLY, and EXT2_MF_ROOT
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400395 *
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000396 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000397errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
398{
Theodore Ts'od5f858d2001-05-25 17:14:23 +0000399 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000400}
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000401
402#ifdef DEBUG
403int main(int argc, char **argv)
404{
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000405 int retval, mount_flags;
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500406 char mntpt[80];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400407
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000408 if (argc < 2) {
409 fprintf(stderr, "Usage: %s device\n", argv[0]);
410 exit(1);
411 }
412
Theodore Ts'o04f13d62009-09-08 21:33:03 -0400413 add_error_table(&et_ext2_error_table);
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500414 mntpt[0] = 0;
415 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
416 mntpt, sizeof(mntpt));
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000417 if (retval) {
418 com_err(argv[0], retval,
419 "while calling ext2fs_check_if_mounted");
420 exit(1);
421 }
422 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
Theodore Ts'o2fa8f372005-06-05 16:05:22 -0400423 if (mount_flags & EXT2_MF_BUSY)
424 printf("\t%s is apparently in use.\n", argv[1]);
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000425 if (mount_flags & EXT2_MF_MOUNTED)
Theodore Ts'occ860172001-05-25 16:32:53 +0000426 printf("\t%s is mounted.\n", argv[1]);
Theodore Ts'o07cefe72001-12-24 15:20:22 -0500427 if (mount_flags & EXT2_MF_SWAP)
428 printf("\t%s is a swap device.\n", argv[1]);
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000429 if (mount_flags & EXT2_MF_READONLY)
Theodore Ts'occ860172001-05-25 16:32:53 +0000430 printf("\t%s is read-only.\n", argv[1]);
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000431 if (mount_flags & EXT2_MF_ISROOT)
Theodore Ts'occ860172001-05-25 16:32:53 +0000432 printf("\t%s is the root filesystem.\n", argv[1]);
Theodore Ts'oa8fd6e32001-12-24 01:40:35 -0500433 if (mntpt[0])
434 printf("\t%s is mounted on %s.\n", argv[1], mntpt);
Theodore Ts'o52db0b42001-04-17 02:22:05 +0000435 exit(0);
436}
Theodore Ts'of0efd292002-04-27 17:07:52 -0400437#endif /* DEBUG */