Skip to contentSkip to conversation

lib/tst_device.c: Add exception handling of function tst_find_free_loopdev

PATCH New Zhao Gongyi 3 participants 4 messages Mar 10, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/tst_device.c: Add exception handling of function tst_find_free_loopdev

Because of race condition or abnormal env, set_dev_path may be return 1. And when set_dev_path return 1 in tst_find_free_loopdev, we should return Immediately.

---
 lib/tst_device.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/tst_device.c +7 -2
index c096b418b..51cf1ba7e 100644
@@ -83,8 +83,13 @@ 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)
+				if (!set_dev_path(rc, path, path_len)) {
+					tst_resm(TINFO,
+						"loop device not exist");
+					return -1;
+				}
+
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;

--
2.17.1

#1 lore raw

PATCH 1/1 lib/tst_device.c: Add exception handling of function tst_find_free_loopdev

Hi Gongyi,

Quoted 2 lines

Because of race condition or abnormal env, set_dev_path may be return 1. And when set_dev_path return 1 in tst_find_free_loopdev,

You mean return 0, right?

Quoted 1 line

we should return Immediately.

Not really sure about this patch, mainly question for others. Could you please more describe your problem?

Kind regards,
Petr

#2 lore raw

PATCH 1/1 lib/tst_device.c: Add exception handling of function tst_find_free_loopdev

In reply to Zhao Gongyi

Hi Gongyi,

Quoted 4 lines · Zhao Gongyi

Because of race condition or abnormal env, set_dev_path may be return 1. And when set_dev_path return 1 in tst_find_free_loopdev, we should return Immediately.

If there exists a race condition, I firstly think of the TST_RETRY_FUNC macro
to try more times for the set_dev_path. That might help to get the function success
in the follow-up tries.

Quoted 34 lines
---
 lib/tst_device.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/tst_device.c +7 -3
index c096b418b..51cf1ba7e 100644
@@ -83,8 +83,13 @@ 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)
+                               if (!set_dev_path(rc, path, path_len)) {
+                                       tst_resm(TINFO,
+                                               "loop device not exist");
+                                       return -1;
+                               }
+
                        tst_resm(TINFO, "Found free device %d '%s'",
                                rc, path ?: "");
                        return rc;
--

2.17.1

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

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210310/d3a354e5/attachment.htm>
#3 lore raw

PATCH 1/1 lib/tst_device.c: Add exception handling of function tst_find_free_loopdev

Hi,

Quoted 10 lines
Quoted 3 lines

Because of race condition or abnormal env, set_dev_path may be return 1. And when set_dev_path return 1 in tst_find_free_loopdev, we should return Immediately.

If there exists a race condition, I firstly think of the TST_RETRY_FUNC macro
to try more times for the set_dev_path. That might help to get the function success
in the follow-up tries.

+1

Kind regards,
Petr