public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v1] libswap.c: Improve caculate swap dev number
@ 2024-03-01  6:27 Wei Gao via ltp
  2024-03-01  6:45 ` Yang Xu (Fujitsu) via ltp
  2024-03-05 14:10 ` [LTP] [PATCH v2] libswap.c: Improve calculate " Wei Gao via ltp
  0 siblings, 2 replies; 14+ messages in thread
From: Wei Gao via ltp @ 2024-03-01  6:27 UTC (permalink / raw)
  To: ltp

I encounter a dead loop on following code in our test on ppc64 machine:
while ((c = fgetc(fp)) != EOF)

I think "/proc/swaps" is not normal file and can not get EOF in some situation,
so i use fgets a line and caculate swap dev number.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 libs/libltpswap/libswap.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index c8cbb8ea1..6924066b7 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -13,6 +13,7 @@
 
 #define TST_NO_DEFAULT_MAIN
 #define DEFAULT_MAX_SWAPFILE 32
+#define MAX_LINE_LEN 256
 
 #include "tst_test.h"
 #include "libswap.h"
@@ -274,16 +275,17 @@ int tst_max_swapfiles(void)
 int tst_count_swaps(void)
 {
 	FILE *fp;
-	int used = -1;
-	char c;
+	int used = 0;
 
 	fp = SAFE_FOPEN("/proc/swaps", "r");
 	if (fp == NULL)
 		return -1;
 
-	while ((c = fgetc(fp)) != EOF) {
-		if (c == '\n')
+	char line[MAX_LINE_LEN];
+	while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
+		if (strstr(line, "/dev/") != NULL) {
 			used++;
+		}
 	}
 
 	SAFE_FCLOSE(fp);
-- 
2.35.3


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

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-03-06  9:59 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-01  6:27 [LTP] [PATCH v1] libswap.c: Improve caculate swap dev number Wei Gao via ltp
2024-03-01  6:45 ` Yang Xu (Fujitsu) via ltp
2024-03-03 13:14   ` Petr Vorel
2024-03-04  8:49     ` Li Wang
2024-03-04  9:04       ` Petr Vorel
2024-03-04  9:18       ` Yang Xu (Fujitsu) via ltp
2024-03-04 10:04         ` Li Wang
2024-03-05  4:01           ` Wei Gao via ltp
2024-03-05 10:15             ` Petr Vorel
2024-03-05 14:10 ` [LTP] [PATCH v2] libswap.c: Improve calculate " Wei Gao via ltp
2024-03-05 14:45   ` Petr Vorel
2024-03-05 21:04   ` Petr Vorel
2024-03-06  1:32     ` Yang Xu (Fujitsu) via ltp
2024-03-06  9:59       ` Petr Vorel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox