From patchwork Wed Mar 22 10:22:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Oberhollenzer X-Patchwork-Id: 741953 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vp5RP72K2z9s7b for ; Wed, 22 Mar 2017 21:25:21 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="u+whkUnA"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=4uL2i2jCryXF7iEYg3x7Urby82jYJnT3lFu1xEqYLY8=; b=u+whkUnAvCnlzYmlunpDOg+dAU JH7EeIBPIR5SMDI2HIsHWA9RLrPj2KQjHEqBIMj9beWcgMNGD377IfqSEAWAp2mF4UyBU2zHuLtzV LGSTY8XA2aZuAF16v9GdlWHg7HZW1cyVfbP3kICAP1kyC2VYmfL73WeM52WICY9gTaFTuG4NKKlnN PZBn3AulMhMvDzHyFN5n19iwEi0O1xzpgBAmDhSsOYne+z30OJ6tTzwDJfHEQBveJJc2Vj6SiHPMt FtoCtS2oJ5ElhHFJaILU17qs4Yh46zBJIRPQ5Lg3l0bbxA0Mcs8J5DChinwaCP1fJSscD9WjwB1/q L8aTmOYw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cqdSA-0001BB-9r; Wed, 22 Mar 2017 10:25:18 +0000 Received: from mail.sigma-star.at ([95.130.255.111]) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cqdQn-0007FT-81 for linux-mtd@lists.infradead.org; Wed, 22 Mar 2017 10:23:56 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.sigma-star.at (Postfix) with ESMTP id EDE7124E0006; Wed, 22 Mar 2017 11:23:30 +0100 (CET) Received: from linux-4sv6.suse (unknown [82.150.214.13]) by mail.sigma-star.at (Postfix) with ESMTPSA id 6386424E0004; Wed, 22 Mar 2017 11:23:30 +0100 (CET) From: David Oberhollenzer To: linux-mtd@lists.infradead.org Subject: [PATCH 4/5] Fix libmtd behaviour if MTD is not present on the system Date: Wed, 22 Mar 2017 11:22:56 +0100 Message-Id: <20170322102257.7605-5-david.oberhollenzer@sigma-star.at> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20170322102257.7605-1-david.oberhollenzer@sigma-star.at> References: <20170322102257.7605-1-david.oberhollenzer@sigma-star.at> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170322_032353_752186_F863251E X-CRM114-Status: GOOD ( 23.61 ) X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yuanjie.Huang@windriver.com, raj.khem@gmail.com, David Oberhollenzer MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 --- include/libmtd.h | 3 +-- lib/libmtd.c | 12 ++++++------ lib/libmtd_int.h | 1 + lib/libmtd_legacy.c | 18 ++++++++++++++++++ ubi-utils/mtdinfo.c | 5 +---- ubi-utils/ubiformat.c | 2 -- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/include/libmtd.h b/include/libmtd.h index f9f3164..db85fb4 100644 --- a/include/libmtd.h +++ b/include/libmtd.h @@ -120,8 +120,7 @@ int mtd_dev_present(libmtd_t desc, int mtd_num); * @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); diff --git a/lib/libmtd.c b/lib/libmtd.c index 8bc532f..a50f18a 100644 --- a/lib/libmtd.c +++ b/lib/libmtd.c @@ -578,6 +578,11 @@ libmtd_t libmtd_open(void) 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; } @@ -676,13 +681,8 @@ int mtd_get_info(libmtd_t desc, struct mtd_info *info) * 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) { diff --git a/lib/libmtd_int.h b/lib/libmtd_int.h index db2f1cf..03b0863 100644 --- a/lib/libmtd_int.h +++ b/lib/libmtd_int.h @@ -98,6 +98,7 @@ struct libmtd 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); diff --git a/lib/libmtd_legacy.c b/lib/libmtd_legacy.c index ba8eade..46f51fd 100644 --- a/lib/libmtd_legacy.c +++ b/lib/libmtd_legacy.c @@ -146,6 +146,24 @@ static int proc_parse_next(struct proc_parse_info *pi) } /** + * 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 * diff --git a/ubi-utils/mtdinfo.c b/ubi-utils/mtdinfo.c index 11e59c1..0606ab0 100644 --- a/ubi-utils/mtdinfo.c +++ b/ubi-utils/mtdinfo.c @@ -407,11 +407,8 @@ int main(int argc, char * const argv[]) } 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; diff --git a/ubi-utils/ubiformat.c b/ubi-utils/ubiformat.c index 68906f2..896fe20 100644 --- a/ubi-utils/ubiformat.c +++ b/ubi-utils/ubiformat.c @@ -698,8 +698,6 @@ int main(int argc, char * const argv[]) 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; }