[LTP] [PATCH v3 1/2] swapon03: Remove fork in setup

Petr Vorel pvorel@suse.cz
Tue Nov 18 15:36:06 CET 2025


This is probably some leftover from the conversion to the new library.
Not only this is not necessary, it also masks proper results propagation
from the MAKE_SMALL_SWAPFILE() because there is at least one
tst_brk(TCONF, "") in there that will be converted to TBROK here.

Use safe version SAFE_MAKE_SMALL_SWAPFILE() instead of
MAKE_SMALL_SWAPFILE() to quit early during setup.

Because we don't have SAFE_SWAPON(), quit if TST_EXP_PASS_SILENT()
fails.

Also remove SAFE_SETEUID(0) which was probably due fork().

Fixes: fe1782ed66 ("syscalls/swapon03: use tst_max_swapfiles() and GET_USED_SWAPFILES() API")
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
New in v3.

 testcases/kernel/syscalls/swapon/swapon03.c | 33 +++++++--------------
 1 file changed, 11 insertions(+), 22 deletions(-)

diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index d6445d5fc4..c014a48912 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -1,8 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
+ * Copyright (c) Linux Test Project, 2009-2025
  * Copyright (c) International Business Machines Corp., 2007
  * Created by <rsalveti@linux.vnet.ibm.com>
- *
  */
 
 /*\
@@ -27,13 +27,9 @@ static int swapfiles;
 
 static int setup_swap(void)
 {
-	pid_t pid;
-	int status;
 	int j, max_swapfiles, used_swapfiles;
 	char filename[FILENAME_MAX];
 
-	SAFE_SETEUID(0);
-
 	/* Determine how many more files are to be created */
 	max_swapfiles = tst_max_swapfiles();
 	used_swapfiles = tst_count_swaps();
@@ -41,24 +37,18 @@ static int setup_swap(void)
 	if (swapfiles > max_swapfiles)
 		swapfiles = max_swapfiles;
 
-	pid = SAFE_FORK();
-	if (pid == 0) {
-		/*create and turn on remaining swapfiles */
-		for (j = 0; j < swapfiles; j++) {
-
-			/* Create the swapfile */
-			snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
-			MAKE_SMALL_SWAPFILE(filename);
+	/*create and turn on remaining swapfiles */
+	for (j = 0; j < swapfiles; j++) {
 
-			/* turn on the swap file */
-			TST_EXP_PASS_SILENT(swapon(filename, 0));
-		}
-		exit(0);
-	} else
-		waitpid(pid, &status, 0);
+		/* Create the swapfile */
+		snprintf(filename, sizeof(filename), "%s%02d", TEST_FILE, j + 2);
+		SAFE_MAKE_SMALL_SWAPFILE(filename);
 
-	if (WEXITSTATUS(status))
-		tst_brk(TFAIL, "Failed to setup swap files");
+		/* turn on the swap file */
+		TST_EXP_PASS_SILENT(swapon(filename, 0));
+		if (!TST_PASS)
+			tst_brk(TFAIL, "Failed to setup swap files");
+	}
 
 	tst_res(TINFO, "Successfully created %d swap files", swapfiles);
 	MAKE_SMALL_SWAPFILE(TEST_FILE);
@@ -127,7 +117,6 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.all_filesystems = 1,
 	.needs_root = 1,
-	.forks_child = 1,
 	.test_all = verify_swapon,
 	.setup = setup,
 	.cleanup = cleanup
-- 
2.51.0



More information about the ltp mailing list