diff mbox series

[v3,3/7] syscalls/swapon03: use tst_max_swapfiles() and GET_USED_SWAPFILES() api

Message ID 20240208100517.1974-3-xuyang2018.jy@fujitsu.com
State Superseded
Headers show
Series [v3,1/7] libltpswap: Add tst_max_swapfiles api | expand

Commit Message

Yang Xu \(Fujitsu\) Feb. 8, 2024, 10:05 a.m. UTC
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 testcases/kernel/syscalls/swapon/swapon03.c | 43 +++++----------------
 1 file changed, 9 insertions(+), 34 deletions(-)

Comments

Petr Vorel Feb. 9, 2024, 11:05 p.m. UTC | #1
Hi Yang Xu,

> +	if (tst_fs_type(".") == TST_TMPFS_MAGIC)
> +		tst_brk(TCONF, "swap not supported on tmpfs");

This is based on my patchset:
https://patchwork.ozlabs.org/project/ltp/patch/20240205022857.191692-2-pvorel@suse.cz/

I still wonder why this is needed. Some problem of the loop device on tmpfs or
what? Also, as it's a separate unrelated change, shouldn't we merge my commit
instead?

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index 05fe2045f..e27d954a1 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -19,7 +19,6 @@ 
 
 #include "tst_test.h"
 #include "lapi/syscalls.h"
-#include "swaponoff.h"
 #include "libswap.h"
 
 #define MNTPOINT	"mntpoint"
@@ -105,47 +104,20 @@  static void verify_swapon(void)
 static int setup_swap(void)
 {
 	pid_t pid;
-	int j, fd;
 	int status;
+	int j, max_swapfiles, used_swapfiles;
 	int res = 0;
 	char filename[FILENAME_MAX];
-	char buf[BUFSIZ + 1];
-
-	/* Find out how many swapfiles (1 line per entry) already exist */
-	swapfiles = 0;
 
 	if (seteuid(0) < 0)
 		tst_brk(TFAIL | TERRNO, "Failed to call seteuid");
 
-	/* This includes the first (header) line */
-	if ((fd = open("/proc/swaps", O_RDONLY)) == -1) {
-		tst_brk(TFAIL | TERRNO,
-			"Failed to find out existing number of swap files");
-	}
-	do {
-		char *p = buf;
-		res = read(fd, buf, BUFSIZ);
-		if (res < 0) {
-			tst_brk(TFAIL | TERRNO,
-				 "Failed to find out existing number of swap files");
-		}
-		buf[res] = '\0';
-		while ((p = strchr(p, '\n'))) {
-			p++;
-			swapfiles++;
-		}
-	} while (BUFSIZ <= res);
-	close(fd);
-	if (swapfiles)
-		swapfiles--;	/* don't count the /proc/swaps header */
-
-	if (swapfiles < 0)
-		tst_brk(TFAIL, "Failed to find existing number of swapfiles");
-
 	/* Determine how many more files are to be created */
-	swapfiles = MAX_SWAPFILES - swapfiles;
-	if (swapfiles > MAX_SWAPFILES)
-		swapfiles = MAX_SWAPFILES;
+	max_swapfiles = tst_max_swapfiles();
+	used_swapfiles = tst_count_swaps();
+	swapfiles = max_swapfiles - used_swapfiles;
+	if (swapfiles > max_swapfiles)
+		swapfiles = max_swapfiles;
 	pid = SAFE_FORK();
 	if (pid == 0) {
 		/*create and turn on remaining swapfiles */
@@ -260,6 +232,9 @@  static void setup(void)
 	if (access("/proc/swaps", F_OK))
 		tst_brk(TCONF, "swap not supported by kernel");
 
+	if (tst_fs_type(".") == TST_TMPFS_MAGIC)
+		tst_brk(TCONF, "swap not supported on tmpfs");
+
 	is_swap_supported(TEST_FILE);
 }