]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/ismounted.c
2 * ismounted.c --- Check to see if the filesystem was mounted
4 * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
21 #ifdef HAVE_LINUX_FD_H
24 #ifdef HAVE_LINUX_LOOP_H
25 #include <linux/loop.h>
26 #include <sys/ioctl.h>
27 #ifdef HAVE_LINUX_MAJOR_H
28 #include <linux/major.h>
29 #endif /* HAVE_LINUX_MAJOR_H */
30 #endif /* HAVE_LINUX_LOOP_H */
34 #ifdef HAVE_GETMNTINFO
36 #include <sys/param.h>
37 #include <sys/mount.h>
38 #endif /* HAVE_GETMNTINFO */
47 * Check to see if a regular file is mounted.
48 * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
49 * because the name in /proc/mounts is a loopback device not a regular file.
51 static int check_loop_mounted(const char *mnt_fsname
, dev_t mnt_rdev
,
52 dev_t file_dev
, ino_t file_ino
)
54 #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
55 struct loop_info64 loopinfo
;
58 if (major(mnt_rdev
) == LOOP_MAJOR
) {
59 loop_fd
= open(mnt_fsname
, O_RDONLY
);
63 ret
= ioctl(loop_fd
, LOOP_GET_STATUS64
, &loopinfo
);
68 if (file_dev
== loopinfo
.lo_device
&&
69 file_ino
== loopinfo
.lo_inode
)
72 #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
77 * Helper function which checks a file in /etc/mtab format to see if a
78 * filesystem is mounted. Returns an error if the file doesn't exist
81 static errcode_t
check_mntent_file(const char *mtab_file
, const char *file
,
82 int *mount_flags
, char *mtpt
, int mtlen
)
87 dev_t file_dev
=0, file_rdev
=0;
93 if ((f
= setmntent (mtab_file
, "r")) == NULL
) {
94 if (errno
== ENOENT
) {
95 if (getenv("EXT2FS_NO_MTAB_OK"))
98 return EXT2_ET_NO_MTAB_FILE
;
102 if (stat(file
, &st_buf
) == 0) {
103 if (S_ISBLK(st_buf
.st_mode
)) {
104 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
105 file_rdev
= st_buf
.st_rdev
;
108 file_dev
= st_buf
.st_dev
;
109 file_ino
= st_buf
.st_ino
;
112 while ((mnt
= getmntent (f
)) != NULL
) {
113 if (mnt
->mnt_fsname
[0] != '/')
115 if (strcmp(file
, mnt
->mnt_fsname
) == 0)
117 if (stat(mnt
->mnt_fsname
, &st_buf
) == 0) {
118 if (S_ISBLK(st_buf
.st_mode
)) {
120 if (file_rdev
&& (file_rdev
== st_buf
.st_rdev
))
122 if (check_loop_mounted(mnt
->mnt_fsname
,
123 st_buf
.st_rdev
, file_dev
,
128 if (file_dev
&& ((file_dev
== st_buf
.st_dev
) &&
129 (file_ino
== st_buf
.st_ino
)))
136 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
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.
144 if (file_rdev
&& stat("/", &st_buf
) == 0) {
145 if (st_buf
.st_dev
== file_rdev
) {
146 *mount_flags
= EXT2_MF_MOUNTED
;
148 strncpy(mtpt
, "/", mtlen
);
155 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
156 /* Validate the entry in case /etc/mtab is out of date */
158 * We need to be paranoid, because some broken distributions
159 * (read: Slackware) don't initialize /etc/mtab before checking
160 * all of the non-root filesystems on the disk.
162 if (stat(mnt
->mnt_dir
, &st_buf
) < 0) {
164 if (retval
== ENOENT
) {
166 printf("Bogus entry in %s! (%s does not exist)\n",
167 mtab_file
, mnt
->mnt_dir
);
173 if (file_rdev
&& (st_buf
.st_dev
!= file_rdev
)) {
175 printf("Bogus entry in %s! (%s not mounted on %s)\n",
176 mtab_file
, file
, mnt
->mnt_dir
);
181 *mount_flags
= EXT2_MF_MOUNTED
;
184 /* Check to see if the ro option is set */
185 if (hasmntopt(mnt
, MNTOPT_RO
))
186 *mount_flags
|= EXT2_MF_READONLY
;
190 strncpy(mtpt
, mnt
->mnt_dir
, mtlen
);
192 * Check to see if we're referring to the root filesystem.
193 * If so, do a manual check to see if we can open /etc/mtab
194 * read/write, since if the root is mounted read/only, the
195 * contents of /etc/mtab may not be accurate.
197 if (!strcmp(mnt
->mnt_dir
, "/")) {
199 #define TEST_FILE "/.ismount-test-file"
200 *mount_flags
|= EXT2_MF_ISROOT
;
201 fd
= open(TEST_FILE
, O_RDWR
|O_CREAT
, 0600);
204 *mount_flags
|= EXT2_MF_READONLY
;
207 (void) unlink(TEST_FILE
);
215 static errcode_t
check_mntent(const char *file
, int *mount_flags
,
216 char *mtpt
, int mtlen
)
221 retval
= check_mntent_file("/tmp/mtab", file
, mount_flags
,
227 retval
= check_mntent_file("/proc/mounts", file
, mount_flags
,
229 if (retval
== 0 && (*mount_flags
!= 0))
231 #endif /* __linux__ */
232 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
234 #define MOUNTED _PATH_MOUNTED
236 retval
= check_mntent_file(MOUNTED
, file
, mount_flags
, mtpt
, mtlen
);
241 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
245 #if defined(HAVE_GETMNTINFO)
247 static errcode_t
check_getmntinfo(const char *file
, int *mount_flags
,
248 char *mtpt
, int mtlen
)
255 n
= getmntinfo(&mp
, MNT_NOWAIT
);
259 len
= sizeof(_PATH_DEV
) - 1;
261 if (strncmp(_PATH_DEV
, s1
, len
) == 0)
266 s2
= mp
->f_mntfromname
;
267 if (strncmp(_PATH_DEV
, s2
, len
) == 0) {
271 if (strcmp(s1
, s2
) == 0 || strcmp(s1
, &s2
[1]) == 0) {
272 *mount_flags
= EXT2_MF_MOUNTED
;
278 strncpy(mtpt
, mp
->f_mntonname
, mtlen
);
281 #endif /* HAVE_GETMNTINFO */
282 #endif /* HAVE_SETMNTENT */
285 * Check to see if we're dealing with the swap device.
287 static int is_swap_device(const char *file
)
296 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
297 if ((stat(file
, &st_buf
) == 0) &&
298 S_ISBLK(st_buf
.st_mode
))
299 file_dev
= st_buf
.st_rdev
;
302 if (!(f
= fopen("/proc/swaps", "r")))
304 /* Skip the first line */
305 if (!fgets(buf
, sizeof(buf
), f
))
307 if (*buf
&& strncmp(buf
, "Filename\t", 9))
308 /* Linux <=2.6.19 contained a bug in the /proc/swaps
309 * code where the header would not be displayed
311 goto valid_first_line
;
313 while (fgets(buf
, sizeof(buf
), f
)) {
315 if ((cp
= strchr(buf
, ' ')) != NULL
)
317 if ((cp
= strchr(buf
, '\t')) != NULL
)
319 if (strcmp(buf
, file
) == 0) {
324 if (file_dev
&& (stat(buf
, &st_buf
) == 0) &&
325 S_ISBLK(st_buf
.st_mode
) &&
326 file_dev
== st_buf
.st_rdev
) {
340 * ext2fs_check_mount_point() fills determines if the device is
341 * mounted or otherwise busy, and fills in mount_flags with one or
342 * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
343 * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY. If mtpt is
344 * non-NULL, the directory where the device is mounted is copied to
345 * where mtpt is pointing, up to mtlen characters.
350 errcode_t
ext2fs_check_mount_point(const char *device
, int *mount_flags
,
351 char *mtpt
, int mtlen
)
353 errcode_t retval
= 0;
355 if (is_swap_device(device
)) {
356 *mount_flags
= EXT2_MF_MOUNTED
| EXT2_MF_SWAP
;
357 strncpy(mtpt
, "<swap>", mtlen
);
359 #ifdef HAVE_SETMNTENT
360 retval
= check_mntent(device
, mount_flags
, mtpt
, mtlen
);
362 #ifdef HAVE_GETMNTINFO
363 retval
= check_getmntinfo(device
, mount_flags
, mtpt
, mtlen
);
366 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
369 #endif /* HAVE_GETMNTINFO */
370 #endif /* HAVE_SETMNTENT */
375 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
379 if (stat(device
, &st_buf
) == 0 && S_ISBLK(st_buf
.st_mode
)) {
380 int fd
= open(device
, O_RDONLY
| O_EXCL
);
384 else if (errno
== EBUSY
)
385 *mount_flags
|= EXT2_MF_BUSY
;
394 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
395 * EXT2_MF_READONLY, and EXT2_MF_ROOT
398 errcode_t
ext2fs_check_if_mounted(const char *file
, int *mount_flags
)
400 return ext2fs_check_mount_point(file
, mount_flags
, NULL
, 0);
404 int main(int argc
, char **argv
)
406 int retval
, mount_flags
;
410 fprintf(stderr
, "Usage: %s device\n", argv
[0]);
414 add_error_table(&et_ext2_error_table
);
416 retval
= ext2fs_check_mount_point(argv
[1], &mount_flags
,
417 mntpt
, sizeof(mntpt
));
419 com_err(argv
[0], retval
,
420 "while calling ext2fs_check_if_mounted");
423 printf("Device %s reports flags %02x\n", argv
[1], mount_flags
);
424 if (mount_flags
& EXT2_MF_BUSY
)
425 printf("\t%s is apparently in use.\n", argv
[1]);
426 if (mount_flags
& EXT2_MF_MOUNTED
)
427 printf("\t%s is mounted.\n", argv
[1]);
428 if (mount_flags
& EXT2_MF_SWAP
)
429 printf("\t%s is a swap device.\n", argv
[1]);
430 if (mount_flags
& EXT2_MF_READONLY
)
431 printf("\t%s is read-only.\n", argv
[1]);
432 if (mount_flags
& EXT2_MF_ISROOT
)
433 printf("\t%s is the root filesystem.\n", argv
[1]);
435 printf("\t%s is mounted on %s.\n", argv
[1], mntpt
);