ltp.lists.linux.it archive mirror
 help / color / mirror / Atom feed
From: Edward Liaw via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Cc: kernel-team@android.com
Subject: [LTP] [PATCH v1 2/3] set_dev_loop_path: Refactor set_dev_path and check return value
Date: Mon, 20 Mar 2023 22:37:22 +0000	[thread overview]
Message-ID: <20230320223723.2030960-3-edliaw@google.com> (raw)
In-Reply-To: <20230320223723.2030960-1-edliaw@google.com>

tst_find_free_loopdev does not check the return value of set_dev_path
and will return the last attempted path even if it does not pass a stat
check.  set_dev_path also has a return value that is not consistent with
the other functions in this file.

Renames the function to set_dev_loop_path, the const array to
dev_loop_variants and changes the return value to 0 on success and 1 on
failure.  Check the return value when called in tst_find_free_loopdev
for failure to acquire a loop device.

Signed-off-by: Edward Liaw <edliaw@google.com>
---
 lib/tst_device.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index a61c5a748..ae665f7b6 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -54,25 +54,25 @@ static char dev_path[PATH_MAX];
 static int device_acquired;
 static unsigned long prev_dev_sec_write;
 
-static const char *dev_variants[] = {
+static const char *dev_loop_variants[] = {
 	"/dev/loop%i",
 	"/dev/loop/%i",
 	"/dev/block/loop%i"
 };
 
-static int set_dev_path(int dev, char *path, size_t path_len)
+static int set_dev_loop_path(int dev, char *path, size_t path_len)
 {
 	unsigned int i;
 	struct stat st;
 
-	for (i = 0; i < ARRAY_SIZE(dev_variants); i++) {
-		snprintf(path, path_len, dev_variants[i], dev);
+	for (i = 0; i < ARRAY_SIZE(dev_loop_variants); i++) {
+		snprintf(path, path_len, dev_loop_variants[i], dev);
 
 		if (stat(path, &st) == 0 && S_ISBLK(st.st_mode))
-			return 1;
+			return 0;
 	}
 
-	return 0;
+	return 1;
 }
 
 int tst_find_free_loopdev(char *path, size_t path_len)
@@ -88,8 +88,8 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path)
-				set_dev_path(rc, path, path_len);
+			if (!path || set_dev_loop_path(rc, path, path_len) != 0)
+				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -116,7 +116,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 	 */
 	for (i = 0; i < 256; i++) {
 
-		if (!set_dev_path(i, buf, sizeof(buf)))
+		if (set_dev_loop_path(i, buf, sizeof(buf)) != 0)
 			continue;
 
 		dev_fd = open(buf, O_RDONLY);
-- 
2.40.0.rc1.284.g88254d51c5-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2023-03-20 22:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-20 22:37 [LTP] [PATCH v1 0/3] tst_device.c: Handle Android path for backing device Edward Liaw via ltp
2023-03-20 22:37 ` [LTP] [PATCH v1 1/3] tst_device.c: Use PATH_MAX more consistently Edward Liaw via ltp
2023-03-20 22:37 ` Edward Liaw via ltp [this message]
2023-03-20 22:37 ` [LTP] [PATCH v1 3/3] tst_find_backing_dev: Also check /dev/block/ for backing device Edward Liaw via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230320223723.2030960-3-edliaw@google.com \
    --to=ltp@lists.linux.it \
    --cc=edliaw@google.com \
    --cc=kernel-team@android.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).