Skip to contentSkip to conversation

libmtd: perform device checking first

PATCH In review Brian Norris 2 participants 6 messages Feb 8, 2012 – Feb 9, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libmtd: perform device checking first

If we don't check for the MTD before calling `legacy_get_dev_info1', we may get errors like:

libmtd: MTD subsystem is old and does not support sysfs, so MTD character device nodes have to exist
libmtd: error!: "/dev/mtd2" is not a character device
mtdinfo: error!: libmtd failed get MTD device 2 information
error 22 (Invalid argument)

So reverse the order of these two checks.

---
 lib/libmtd.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
lib/libmtd.c +3 -4
index ecf182f..2bb4e57 100644
@@ -733,12 +733,11 @@ int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
 	memset(mtd, 0, sizeof(struct mtd_dev_info));
 	mtd->mtd_num = mtd_num;
 
-	if (!lib->sysfs_supported)
-		return legacy_get_dev_info1(mtd_num, mtd);
-	else if (!mtd_dev_present(desc, mtd_num)) {
+	if (!mtd_dev_present(desc, mtd_num)) {
 		errno = ENODEV;
 		return -1;
-	}
+	} else if (!lib->sysfs_supported)
+		return legacy_get_dev_info1(mtd_num, mtd);
 
 	if (dev_get_major(lib, mtd_num, &mtd->major, &mtd->minor))
 		return -1;
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 2/3 libmtd_legacy: don't open device in R/W

On legacy kernels with ROM devices, we can get mtdinfo errors like:

libmtd: error!: cannot open "/dev/mtd4"
error 13 (Permission denied)
mtdinfo: error!: libmtd failed get MTD device 4 information
error 13 (Permission denied)

We don't need O_RDRW access for informational ioctls(), so make this O_RDONLY.

---
 lib/libmtd_legacy.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
lib/libmtd_legacy.c +1 -1
index d3f1672..1ae9946 100644
@@ -262,7 +262,7 @@ int legacy_get_dev_info(const char *node, struct mtd_dev_info *mtd)
 
 	mtd->mtd_num = mtd->minor / 2;
 
-	fd = open(node, O_RDWR);
+	fd = open(node, O_RDONLY);
 	if (fd == -1)
 		return sys_errmsg("cannot open \"%s\"", node);
 
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 3/3 libmtd: fix segmentation fault on lib->mtd

In reply to Brian Norris

Legacy systems do not initialize lib->mtd, so we shouldn't perform strlen(lib->mtd); this produces a segmentation fault. As this code isn't used in the legacy codepath, we can just move it down to an 'else' branch.

---
 lib/libmtd.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
lib/libmtd.c +5 -3
index 2bb4e57..fb4586c 100644
@@ -644,13 +644,15 @@ void libmtd_close(libmtd_t desc)
 int mtd_dev_present(libmtd_t desc, int mtd_num) {
 	struct stat st;
 	struct libmtd *lib = (struct libmtd *)desc;
-	char file[strlen(lib->mtd) + 10];
 
 	if (!lib->sysfs_supported)
 		return legacy_dev_present(mtd_num);
+	else {
+		char file[strlen(lib->mtd) + 10];
 
-	sprintf(file, lib->mtd, mtd_num);
-	return !stat(file, &st);
+		sprintf(file, lib->mtd, mtd_num);
+		return !stat(file, &st);
+	}
 }
 
 int mtd_get_info(libmtd_t desc, struct mtd_info *info)
Signature · 1 line
1.7.5.4
#3 lore raw
In reply to Brian Norris
Quoted 11 lines · Brian Norris

If we don't check for the MTD before calling `legacy_get_dev_info1', we may get errors like:

libmtd: MTD subsystem is old and does not support sysfs, so MTD character device nodes have to exist
libmtd: error!: "/dev/mtd2" is not a character device
mtdinfo: error!: libmtd failed get MTD device 2 information
error 22 (Invalid argument)

So reverse the order of these two checks.

Looks good, thanks, but could you please send a version which applies to mtd-utils.git? The 3rd patch in this series does not apply.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#4 lore raw
Quoted 9 lines · Artem Bityutskiy
Quoted 8 lines · Brian Norris

If we don't check for the MTD before calling `legacy_get_dev_info1', we may get errors like:

libmtd: MTD subsystem is old and does not support sysfs, so MTD character device nodes have to exist
libmtd: error!: "/dev/mtd2" is not a character device
mtdinfo: error!: libmtd failed get MTD device 2 information
error 22 (Invalid argument)

Quoted 6 lines
Quoted 3 lines

So reverse the order of these two checks.

Looks good, thanks, but could you please send a version which applies to mtd-utils.git? The 3rd patch in this series does not apply.

Actually, I've pushed patches 1 and 2, would you please re-send the 3rd one?

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#5 lore raw
Quoted 6 lines · Artem Bityutskiy
Quoted 2 lines · Artem Bityutskiy

Looks good, thanks, but could you please send a version which applies to mtd-utils.git? The 3rd patch in this series does not apply.

Actually, I've pushed patches 1 and 2, would you please re-send the 3rd one?

Perhaps I should have stated in this patch thread: these patches were meant to be on top of your previous test patch (you asked for comments, since you compile-tested only).

Also, I do not see patches 1 and 2 in mtd-utils.git.

I will resend your original patch + my "PATCH 3" as a new series.

Brian