[LTP] [PATCH v2 5/5] swapon03: Remove grep dependency

Petr Vorel
2025-11-06T16:36:52Z

Instead of relying on 'grep' run via system() parse output with C
implementation (faster, no dependency).

Also rename swap file to make sure it's more obvious that it was created
by LTP.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
New in v2.

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

diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index a8e0cbcdc6..91d797871f 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -31,9 +31,10 @@
#define NUM_SWAP_FILES 15

#define MNTPOINT "mntpoint"
-#define TEST_FILE MNTPOINT"/testswap"
+#define TEST_FILE MNTPOINT "/LTP_" __FILE__ "_testswap"

static int *swapfiles;
+static char *tmpdir;

static void setup_swap(void)
{
@@ -85,12 +86,14 @@ static void setup_swap(void)
*/
static void check_and_swapoff(const char *filename)
{
- char cmd_buffer[256];
+ char buf[256];
+ int foo;

- snprintf(cmd_buffer, sizeof(cmd_buffer), "grep -q '%s.*file' /proc/swaps", filename);
-
- if (system(cmd_buffer) == 0 && swapoff(filename) != 0)
- tst_res(TWARN, "Failed to swapoff %s", filename);
+ snprintf(buf, sizeof(buf), "%s/%s %%*s %%*s %%*s %%s", tmpdir, filename);
+ if (!FILE_LINES_SCANF("/proc/swaps", buf, &foo)) {
+ if (swapoff(filename) != 0)
+ tst_res(TWARN | TERRNO, "swapoff(%s) failed", filename);
+ }
}

/*
@@ -121,6 +124,8 @@ static void setup(void)

is_swap_supported(TEST_FILE);

+ tmpdir = tst_tmpdir_path();
+
swapfiles = SAFE_MMAP(NULL, sizeof(*swapfiles), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*swapfiles = 0;
--
2.51.0