The documentation of libmtd_open says, if it returns NULL and errno is
zero, MTD is not present. However, the current version always returns
a libmtd_t object. The function internally checks, if it can access the
MTD sysfs files and, if not, sets a flag to use the procfs fallback.
This patch adds an additional check to libmtd_open, to test if the
MTD procfs file can be read and fails with errno cleared if it does
not exist.
Furhtermore, mtd_get_info is documented to fail with errno set to ENODEV
if MTD is not present. First of all, this was broken in the original
version. It was implemented to specification for the sysfs code path,
but if MTD is not present, that won't be executed, because of the flag
set by libmtd_open. This makes the check not only redundant, but masks
an actual error (the sysfs paths suddenly not being readable anymore).
The legacy path that was used if the sysfs files are not avaible fails
with ENOENT if it cannot read the procfs file. With the above changes
in addition, we don't have a libmtd_t object if neither sysfs nor
procfs is readable, so this error status no longer makes sense.
This patch removes the documentation on the ENODEV errno, and
makes sure that mtd_get_info always returns with apropriate errno
on failure.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
* @info: the MTD device information is returned here
*
* This function fills the passed @info object with general MTD information and
- * returns %0 in case of success and %-1 in case of failure. If MTD subsystem is
- * not present in the system, errno is set to @ENODEV.
+ * returns %0 in case of success and %-1 in case of failure.
*/
int mtd_get_info(libmtd_t desc, struct mtd_info *info);
free(lib->sysfs_mtd);
free(lib->mtd_name);
lib->mtd_name = lib->mtd = lib->sysfs_mtd = NULL;
+
+ if (!legacy_procfs_is_supported()) {
+ free(lib);
+ lib = NULL;
+ }
return lib;
}
* devices are present.
*/
sysfs_mtd = opendir(lib->sysfs_mtd);
- if (!sysfs_mtd) {
- if (errno == ENOENT) {
- errno = ENODEV;
- return -1;
- }
+ if (!sysfs_mtd)
return sys_errmsg("cannot open \"%s\"", lib->sysfs_mtd);
- }
info->lowest_mtd_num = INT_MAX;
while (1) {
unsigned int offs64_ioctls:2;
};
+int legacy_procfs_is_supported(void);
int legacy_dev_present(int mtd_num);
int legacy_mtd_get_info(struct mtd_info *info);
int legacy_get_dev_info(const char *node, struct mtd_dev_info *mtd);
return 1;
}
+/**
+ * legacy_procfs_is_supported - legacy version of 'sysfs_is_supported()'.
+ *
+ * Check if we can access the procfs files for the MTD subsystem.
+ */
+int legacy_procfs_is_supported(void)
+{
+ if (access(MTD_PROC_FILE, R_OK) != 0) {
+ if (errno == ENOENT) {
+ errno = 0;
+ } else {
+ sys_errmsg("cannot read \"%s\"", MTD_PROC_FILE);
+ }
+ return 0;
+ }
+ return 1;
+}
+
/**
* legacy_dev_presentl - legacy version of 'mtd_dev_present()'.
* @info: the MTD device information is returned here
}
err = mtd_get_info(libmtd, &mtd_info);
- if (err) {
- if (errno == ENODEV)
- return errmsg("MTD is not present");
+ if (err)
return sys_errmsg("cannot get MTD information");
- }
if (!args.all && args.node) {
int mtdn;
err = mtd_get_info(libmtd, &mtd_info);
if (err) {
- if (errno == ENODEV)
- errmsg("MTD is not present");
sys_errmsg("cannot get MTD information");
goto out_close_mtd;
}