Skip to contentSkip to conversation

mountpoint: return dev_t from dir_to_device

PATCH In review Dave Reisner 2 participants 5 messages Oct 9, 2011 – Oct 11, 2011 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mountpoint: return dev_t from dir_to_device

The string returned from this function was never of much use other than to stat the path when the user requested a major:minor pair beyond the true/false exit. Save some processing and directly returning the dev_t on success, and an impossible value on failure.

---
 sys-utils/mountpoint.c |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
sys-utils/mountpoint.c +7 -8
index f668368..065d96b 100644
@@ -40,18 +40,18 @@
 
 static int quiet;
 
-static char *dir_to_device(const char *spec)
+static dev_t dir_to_device(const char *spec)
 {
 	struct libmnt_table *tb = mnt_new_table_from_file("/proc/self/mountinfo");
 	struct libmnt_fs *fs;
-	char *res = NULL;
+	dev_t res = (dev_t)-1;
 
 	if (!tb)
-		return NULL;
+		return (dev_t)-1;
 
 	fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD);
 	if (fs && mnt_fs_get_target(fs))
-		res = xstrdup(mnt_fs_get_source(fs));
+		res = mnt_fs_get_devno(fs);
 
 	mnt_free_table(tb);
 	return res;
@@ -146,7 +146,7 @@ int main(int argc, char **argv)
 	if (dev_devno)
 		rc = print_devno(spec, &st);
 	else {
-		char *src;
+		dev_t src;
 
 		if (!S_ISDIR(st.st_mode)) {
 			if (!quiet)
@@ -154,16 +154,15 @@ int main(int argc, char **argv)
 			return EXIT_FAILURE;
 		}
 		src = dir_to_device(spec);
-		if (!src) {
+		if (src == (dev_t)-1) {
 			if (!quiet)
 				printf(_("%s is not a mountpoint\n"), spec);
 			return EXIT_FAILURE;
 		}
 		if (fs_devno)
-			rc = print_devno(src, NULL);
+			printf("%u:%u\n", major(src), minor(src));
 		else if (!quiet)
 			printf(_("%s is a mountpoint\n"), spec);
-		free(src);
 	}
 
 	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
Signature · 1 line
1.7.7
#1 lore raw

PATCH 2/2 mountpoint: fallback on stat when /proc isn't mounted

mountpoint will fail when /proc isn't available, which lessens its usefulness as a tool to detect mountpoints. In this case, mimic what sysvinit's mountpoint tool does and compare the device and inode values of root and specified target.

---
Rationale: I think this patch is especially important for folks who are still using sysvinit. Particularly in the case of a user not having an initramfs, the userspace initscripts need to be able to detect, without fail, the presence of /proc, /sys, and /dev being mounted (and mount them when they aren't). While checking for /proc first and mounting it when mountpoint returns failure will result in the correct action, it's reliant on broken behavior -- that is, mountpoint returned false because /proc isn't mounted, not because /proc isn't a mountpoint.

 sys-utils/mountpoint.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
sys-utils/mountpoint.c +13 -2
index 065d96b..1297d82 100644
@@ -46,8 +46,19 @@ static dev_t dir_to_device(const char *spec)
 	struct libmnt_fs *fs;
 	dev_t res = (dev_t)-1;
 
-	if (!tb)
-		return (dev_t)-1;
+	if (!tb) {
+		struct stat root_st, spec_st;
+
+		/* fallback on using stat when /proc isn't available. return success when
+		 * we're examining different devices or encounter an exact match for the
+		 * root device */
+		if (stat(spec, &spec_st) == 0 && stat("/", &root_st) == 0 &&
+				root_st.st_dev != spec_st.st_dev ||
+				(root_st.st_dev == spec_st.st_dev && root_st.st_ino == spec_st.st_ino))
+			res = spec_st.st_dev;
+
+		return res;
+	}
 
 	fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD);
 	if (fs && mnt_fs_get_target(fs))
Signature · 1 line
1.7.7
maintainer
#2 lore raw

Re: [PATCH 2/2] mountpoint: fallback on stat when /proc isn't mounted

Quoted 6 lines · Dave Reisner

mountpoint will fail when /proc isn't available, which lessens its usefulness as a tool to detect mountpoints. In this case, mimic what sysvinit's mountpoint tool does and compare the device and inode values of root and specified target.

Quoted 9 lines

---
Rationale: I think this patch is especially important for folks who are still using sysvinit. Particularly in the case of a user not having an initramfs, the userspace initscripts need to be able to detect, without fail, the presence of /proc, /sys, and /dev being mounted (and mount them when they aren't). While checking for /proc first and mounting it when mountpoint returns failure will result in the correct action, it's reliant on broken behavior -- that is, mountpoint returned false because /proc isn't mounted, not because /proc isn't a mountpoint.

Good point.

Quoted 12 lines
 sys-utils/mountpoint.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
sys-utils/mountpoint.c +0 -1
index 065d96b..1297d82 100644
@@ -46,8 +46,19 @@ static dev_t dir_to_device(const char *spec)
 	struct libmnt_fs *fs;
 	dev_t res = (dev_t)-1;
 
-	if (!tb)

sys-utils/mountpoint.c static dev_t dir_to_device(const char *spec) · line 57

Quoted 10 lines
sys-utils/mountpoint.c +9 -1
-		return (dev_t)-1;
+	if (!tb) {
+		struct stat root_st, spec_st;
+
+		/* fallback on using stat when /proc isn't available. return success when
+		 * we're examining different devices or encounter an exact match for the
+		 * root device */
+		if (stat(spec, &spec_st) == 0 && stat("/", &root_st) == 0 &&
+				root_st.st_dev != spec_st.st_dev ||
+				(root_st.st_dev == spec_st.st_dev && root_st.st_ino == spec_st.st_ino))

Hmm ... && ... || ..., you forgot brackets here ;-)

Anyway, this is very poor solution. The tradition way how to detect mountpoint is compare "/path" and /path/.."

Fixed, applied.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#3 lore raw
In reply to Dave Reisner
Quoted 10 lines · Dave Reisner

The string returned from this function was never of much use other than to stat the path when the user requested a major:minor pair beyond the true/false exit. Save some processing and directly returning the dev_t on success, and an impossible value on failure.

---
 sys-utils/mountpoint.c |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)

I'll also mention that this fixes an issue with mountpoint that causes it to throw a false negative when /proc is mounted with a source of "none". This value is treated specially in libmount and sets the source to NULL, which makes dir_to_device() incorrectly claim that the directory is not in fact a mountpoint.

Regards,
dave

maintainer
#4 lore raw
Quoted 12 lines · Dave Reisner
Quoted 10 lines · Dave Reisner

The string returned from this function was never of much use other than to stat the path when the user requested a major:minor pair beyond the true/false exit. Save some processing and directly returning the dev_t on success, and an impossible value on failure.

---
 sys-utils/mountpoint.c |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
Quoted 5 lines

I'll also mention that this fixes an issue with mountpoint that causes it to throw a false negative when /proc is mounted with a source of "none". This value is treated specially in libmount and sets the source to NULL, which makes dir_to_device() incorrectly claim that the directory is not in fact a mountpoint.

Applied with some minor changes, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com