lib/ismounted: basic support for loop devices
# losetup -a /dev/loop0 /dev/loop0: [2053]:1048578 (/home/fs-images/filesystems/ext2.img) # findmnt /dev/loop0 TARGET SOURCE FSTYPE OPTIONS /mnt/test /dev/loop0 ext3 rw,relatime,data=ordered old version: ./test_ismounted /home/fs-images/filesystems/ext2.img not mounted new version: ./test_ismounted /home/fs-images/filesystems/ext2.img mounted on /mnt/test Reported-by: Sami Kerola <kerolasa@iki.fi> Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
06f588f7d1
commit
ca4f6229b0
|
@ -3,8 +3,7 @@ sbin_PROGRAMS += fsck.minix
|
||||||
dist_man_MANS += disk-utils/fsck.minix.8
|
dist_man_MANS += disk-utils/fsck.minix.8
|
||||||
fsck_minix_SOURCES = \
|
fsck_minix_SOURCES = \
|
||||||
disk-utils/fsck.minix.c \
|
disk-utils/fsck.minix.c \
|
||||||
disk-utils/minix_programs.h \
|
disk-utils/minix_programs.h
|
||||||
lib/ismounted.c
|
|
||||||
fsck_minix_LDADD = $(LDADD) libcommon.la
|
fsck_minix_LDADD = $(LDADD) libcommon.la
|
||||||
|
|
||||||
sbin_PROGRAMS += mkfs.minix
|
sbin_PROGRAMS += mkfs.minix
|
||||||
|
|
|
@ -76,6 +76,7 @@ test_blkdev_LDADD = libcommon.la
|
||||||
|
|
||||||
test_ismounted_SOURCES = lib/ismounted.c
|
test_ismounted_SOURCES = lib/ismounted.c
|
||||||
test_ismounted_CFLAGS = -DTEST_PROGRAM
|
test_ismounted_CFLAGS = -DTEST_PROGRAM
|
||||||
|
test_ismounted_LDADD = libcommon.la
|
||||||
|
|
||||||
test_wholedisk_SOURCES = lib/wholedisk.c
|
test_wholedisk_SOURCES = lib/wholedisk.c
|
||||||
test_wholedisk_CFLAGS = -DTEST_PROGRAM
|
test_wholedisk_CFLAGS = -DTEST_PROGRAM
|
||||||
|
|
|
@ -26,6 +26,11 @@
|
||||||
#include "pathnames.h"
|
#include "pathnames.h"
|
||||||
#include "ismounted.h"
|
#include "ismounted.h"
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
|
#ifdef __linux__
|
||||||
|
# include "loopdev.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef HAVE_MNTENT_H
|
#ifdef HAVE_MNTENT_H
|
||||||
/*
|
/*
|
||||||
|
@ -39,7 +44,7 @@ static int check_mntent_file(const char *mtab_file, const char *file,
|
||||||
struct mntent *mnt;
|
struct mntent *mnt;
|
||||||
struct stat st_buf;
|
struct stat st_buf;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
dev_t file_dev=0, file_rdev=0;
|
dev_t file_dev=0, file_rdev=0, lodev_dev=0;
|
||||||
ino_t file_ino=0;
|
ino_t file_ino=0;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -47,32 +52,51 @@ static int check_mntent_file(const char *mtab_file, const char *file,
|
||||||
*mount_flags = 0;
|
*mount_flags = 0;
|
||||||
if ((f = setmntent (mtab_file, "r")) == NULL)
|
if ((f = setmntent (mtab_file, "r")) == NULL)
|
||||||
return errno;
|
return errno;
|
||||||
|
|
||||||
if (stat(file, &st_buf) == 0) {
|
if (stat(file, &st_buf) == 0) {
|
||||||
if (S_ISBLK(st_buf.st_mode)) {
|
if (S_ISBLK(st_buf.st_mode)) {
|
||||||
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
|
#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
|
||||||
file_rdev = st_buf.st_rdev;
|
file_rdev = st_buf.st_rdev;
|
||||||
#endif /* __GNU__ */
|
#endif /* __GNU__ */
|
||||||
} else {
|
} else {
|
||||||
|
#ifdef __linux__
|
||||||
|
/*
|
||||||
|
* Maybe the file is backing file for a loop device.
|
||||||
|
*
|
||||||
|
* For is_mounted() we complete ignore the fact that
|
||||||
|
* the same backing file maybe mapped into more loop
|
||||||
|
* devices by sizelimit and offset loop options. If you
|
||||||
|
* want really robust code than use libmount...
|
||||||
|
*/
|
||||||
|
char *name = loopdev_find_by_backing_file(file, 0, 0);
|
||||||
|
if (name && stat(name, &st_buf) == 0)
|
||||||
|
lodev_dev = st_buf.st_rdev;
|
||||||
|
free(name);
|
||||||
|
#endif
|
||||||
file_dev = st_buf.st_dev;
|
file_dev = st_buf.st_dev;
|
||||||
file_ino = st_buf.st_ino;
|
file_ino = st_buf.st_ino;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((mnt = getmntent (f)) != NULL) {
|
while ((mnt = getmntent (f)) != NULL) {
|
||||||
if (mnt->mnt_fsname[0] != '/')
|
if (mnt->mnt_fsname[0] != '/')
|
||||||
continue;
|
continue;
|
||||||
if (strcmp(file, mnt->mnt_fsname) == 0)
|
if (strcmp(file, mnt->mnt_fsname) == 0)
|
||||||
break;
|
break;
|
||||||
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
|
if (stat(mnt->mnt_fsname, &st_buf) != 0)
|
||||||
if (S_ISBLK(st_buf.st_mode)) {
|
continue;
|
||||||
|
|
||||||
|
if (S_ISBLK(st_buf.st_mode)) {
|
||||||
#ifndef __GNU__
|
#ifndef __GNU__
|
||||||
if (file_rdev && (file_rdev == st_buf.st_rdev))
|
if ((file_rdev && file_rdev == st_buf.st_rdev) ||
|
||||||
break;
|
(lodev_dev && lodev_dev == st_buf.st_rdev)) {
|
||||||
#endif /* __GNU__ */
|
break;
|
||||||
} else {
|
|
||||||
if (file_dev && ((file_dev == st_buf.st_dev) &&
|
|
||||||
(file_ino == st_buf.st_ino)))
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
#endif /* __GNU__ */
|
||||||
|
} else {
|
||||||
|
if (file_dev && ((file_dev == st_buf.st_dev) &&
|
||||||
|
(file_ino == st_buf.st_ino)))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue