[LTP] [PATCH v1] libswap.c: Improve caculate swap dev number

Yang Xu (Fujitsu) xuyang2018.jy@fujitsu.com
Fri Mar 1 07:45:30 CET 2024


Hi  Wei

> 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++;
> +		}
>   	}

You are not the  first person to meet this deadloop problem, Petr also 
met this[1] in my v4 patch..

But I don't think it related to /proc/swapfiles, I doubot libc wrapper 
for fgetc problem on ppc64 machine.

Can you try fgetc problem by using fgetc api in ipc library[2]? Then we 
can  know the right reason whether is /proc/swaps or getc problem.

If so, I think we can change this as my v2 way[3].

[1]https://patchwork.ozlabs.org/project/ltp/patch/20240220074218.13487-3-xuyang2018.jy@fujitsu.com/

[2]https://github.com/linux-test-project/ltp/blob/master/libs/libltpnewipc/libnewipc.c#L58

[3]https://patchwork.ozlabs.org/project/ltp/patch/20231222050006.148845-2-xuyang2018.jy@fujitsu.com/

Best Regards
Yang Xu
>   
>   	SAFE_FCLOSE(fp);


More information about the ltp mailing list