[LTP] [PATCH 3/3] mmap10: Convert to new API

Ricardo B. Marliere via ltp
2025-01-14T22:28:24Z

From: Ricardo B. Marliere <rbm@suse.com>

Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
---
testcases/kernel/syscalls/mmap/mmap10.c | 198 +++++++++++---------------------
1 file changed, 66 insertions(+), 132 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap10.c b/testcases/kernel/syscalls/mmap/mmap10.c
index b844af07fd78d69c5cf5afc3039a54685c982776..3436457e7c6ca89b3dd98058bb6cc72308f8ff99 100644
--- a/testcases/kernel/syscalls/mmap/mmap10.c
+++ b/testcases/kernel/syscalls/mmap/mmap10.c
@@ -1,27 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
/*
- * Copyright (C) 2010 Red Hat, Inc.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it
- * is free of the rightful claim of any third person regarding
- * infringement or the like. Any license provided herein, whether
- * implied or otherwise, applies only to this software file. Patent
- * licenses, if any, provided herein do not apply to combinations of
- * this program with other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Copyright (c) 2025 Linux Test Project
*/

-/*
+/*\
+ * [Description]
+ *
* mmap/munmap /dev/zero: a common way of malloc()/free() anonymous
* memory on Solaris.
*
@@ -52,155 +37,104 @@
* address range was an optimization to make the subsequent pagefault
* times faster on RHEL5 that has been removed/changed upstream.
*/
-#include <sys/types.h>
-#include <sys/stat.h>
+
+#include "safe_macros_fn.h"
+#include "tst_test.h"
#include <sys/wait.h>
-#include <sys/mman.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include "test.h"
-#include "config.h"
-
-#define SIZE (5*1024*1024)
-#define PATH_KSM "/sys/kernel/mm/ksm/"

-char *TCID = "mmap10";
-int TST_TOTAL = 1;
+#define SIZE (5 * 1024 * 1024)
+#define PATH_KSM "/sys/kernel/mm/ksm/"

-static int fd, opt_anon, opt_ksm;
+static int fd;
+static char *opt_anon, *opt_ksm;
static long ps;
static char *x;

-void setup(void);
-void cleanup(void);
-void mmapzero(void);
-void help(void);
-
-static option_t options[] = {
- {"a", &opt_anon, NULL},
- {"s", &opt_ksm, NULL},
- {NULL, NULL, NULL}
-};
-
-int main(int argc, char *argv[])
+static void setup(void)
{
- int lc;
-
- tst_parse_opts(argc, argv, options, help);
-
if (opt_ksm) {
if (access(PATH_KSM, F_OK) == -1)
- tst_brkm(TCONF, NULL,
- "KSM configuration is not enabled");
+ tst_brk(TCONF, "KSM configuration is not enabled");
#ifdef HAVE_DECL_MADV_MERGEABLE
- tst_resm(TINFO, "add to KSM regions.");
+ tst_res(TINFO, "add to KSM regions.");
#else
- tst_brkm(TCONF, NULL, "MADV_MERGEABLE missing in sys/mman.h");
+ tst_brk(TCONF, "MADV_MERGEABLE missing in sys/mman.h");
#endif
}
+
if (opt_anon)
- tst_resm(TINFO, "use anonymous pages.");
+ tst_res(TINFO, "use anonymous pages.");
else
- tst_resm(TINFO, "use /dev/zero.");
+ tst_res(TINFO, "use /dev/zero.");

- setup();
-
- tst_resm(TINFO, "start tests.");
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
- mmapzero();
- }
+ ps = SAFE_SYSCONF(_SC_PAGESIZE);
+}

- cleanup();
- tst_exit();
+static void cleanup(void)
+{
+ if (fd > 0)
+ SAFE_CLOSE(fd);
}

-void mmapzero(void)
+static void run(void)
{
- int n;
+ pid_t pid;
+
+ tst_res(TINFO, "start tests.");

if (opt_anon) {
- x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ x = SAFE_MMAP(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
} else {
- if ((fd = open("/dev/zero", O_RDWR, 0666)) < 0)
- tst_brkm(TBROK | TERRNO, cleanup, "open");
- x = mmap(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
- MAP_PRIVATE, fd, 0);
+ fd = SAFE_OPEN("/dev/zero", O_RDWR, 0666);
+ x = SAFE_MMAP(NULL, SIZE + SIZE - ps, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE, fd, 0);
}
- if (x == MAP_FAILED)
- tst_brkm(TFAIL | TERRNO, cleanup, "mmap");
#ifdef HAVE_DECL_MADV_MERGEABLE
- if (opt_ksm) {
+ if (opt_ksm)
if (madvise(x, SIZE + SIZE - ps, MADV_MERGEABLE) == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "madvise");
- }
+ tst_brk(TBROK | TERRNO, "madvise");
#endif
x[SIZE] = 0;

- switch (n = fork()) {
- case -1:
- tst_brkm(TBROK | TERRNO, cleanup, "fork");
- case 0:
- if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
- tst_brkm(TFAIL | TERRNO, cleanup, "munmap");
+ pid = SAFE_FORK();
+ if (pid == 0) {
+ SAFE_MUNMAP(x + SIZE + ps, SIZE - ps - ps);
exit(0);
- default:
- break;
}

- switch (n = fork()) {
- case -1:
- tst_brkm(TBROK | TERRNO, cleanup, "fork");
- case 0:
- if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
- tst_brkm(TFAIL | TERRNO, cleanup,
- "subsequent munmap #1");
+ pid = SAFE_FORK();
+ if (pid == 0) {
+ SAFE_MUNMAP(x + SIZE + ps, SIZE - ps - ps);
exit(0);
- default:
- switch (n = fork()) {
- case -1:
- tst_brkm(TBROK | TERRNO, cleanup, "fork");
- case 0:
- if (munmap(x + SIZE + ps, SIZE - ps - ps) == -1)
- tst_brkm(TFAIL | TERRNO, cleanup,
- "subsequent munmap #2");
+ } else {
+ pid = SAFE_FORK();
+ if (pid == 0) {
+ SAFE_MUNMAP(x + SIZE + ps, SIZE - ps - ps);
exit(0);
- default:
- break;
}
- break;
}

- if (munmap(x, SIZE + SIZE - ps) == -1)
- tst_resm(TFAIL | TERRNO, "munmap all");
-
- while (waitpid(-1, &n, WUNTRACED | WCONTINUED) > 0)
- if (WEXITSTATUS(n) != 0)
- tst_resm(TFAIL, "child exit status is %d",
- WEXITSTATUS(n));
-}
-
-void cleanup(void)
-{
-}
-
-void setup(void)
-{
- tst_require_root();
+ SAFE_MUNMAP(x, SIZE + SIZE - ps);

- tst_sig(FORK, DEF_HANDLER, cleanup);
- TEST_PAUSE;
+ while (waitpid(-1, &pid, WUNTRACED | WCONTINUED) > 0)
+ if (WEXITSTATUS(pid) != 0)
+ tst_res(TFAIL, "child exit status is %d",
+ WEXITSTATUS(pid));

- if ((ps = sysconf(_SC_PAGESIZE)) == -1)
- tst_brkm(TBROK | TERRNO, cleanup, "sysconf(_SC_PAGESIZE)");
+ tst_res(TPASS, "mmap/munmap operations completed successfully");
}

-void help(void)
-{
- printf(" -a Test anonymous pages\n");
- printf(" -s Add to KSM regions\n");
-}
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+ .cleanup = cleanup,
+ .needs_root = 1,
+ .forks_child = 1,
+ .options =
+ (struct tst_option[]){
+ { "a", &opt_anon, "Test anonymous pages" },
+ { "s", &opt_ksm, "Add to KSM regions" },
+ {},
+ },
+};

--
2.47.1