From 57714290bdc99cab533edbc4a021d6ee3a7cc211 Mon Sep 17 00:00:00 2001
From: zhangyao <zhangyao108@huawei.com>
Date: Thu, 4 Jul 2024 16:28:51 +0800
Subject: [PATCH] lsmem: make lsmem to check for the nodes more robust

See #3110.
Reference:https://github.com/util-linux/util-linux/commit/57714290bdc99cab533edbc4a021d6ee3a7cc211
Conflict:NA
---
 sys-utils/lsmem.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/sys-utils/lsmem.c b/sys-utils/lsmem.c
index 3b5ca19a0..7c1be8e83 100644
--- a/sys-utils/lsmem.c
+++ b/sys-utils/lsmem.c
@@ -485,6 +485,7 @@ static int memory_block_filter(const struct dirent *de)
 static void read_basic_info(struct lsmem *lsmem)
 {
 	char dir[PATH_MAX];
+	int i = 0;
 
 	if (ul_path_access(lsmem->sysmem, F_OK, "block_size_bytes") != 0)
 		errx(EXIT_FAILURE, _("This system does not support memory blocks"));
@@ -495,8 +496,14 @@ static void read_basic_info(struct lsmem *lsmem)
 	if (lsmem->ndirs <= 0)
 		err(EXIT_FAILURE, _("Failed to read %s"), dir);
 
-	if (memory_block_get_node(lsmem, lsmem->dirs[0]->d_name) != -1)
-		lsmem->have_nodes = 1;
+	for (i = 0; i < lsmem->ndirs; i++)
+	{
+		if (memory_block_get_node(lsmem, lsmem->dirs[i]->d_name) != -1)
+		{
+			lsmem->have_nodes = 1;
+			break;
+		}
+	}
 
 	/* The valid_zones sysmem attribute was introduced with kernel 3.18 */
 	if (ul_path_access(lsmem->sysmem, F_OK, "memory0/valid_zones") == 0)
-- 
2.33.0

