* [LTP] [PATCH 1/3] mmap001: Convert to new API
2025-01-14 22:26 [LTP] [PATCH 0/3] syscalls/mmap: Refactor a few tests to the new API Ricardo B. Marliere via ltp
@ 2025-01-14 22:26 ` Ricardo B. Marliere via ltp
2025-01-15 13:14 ` Andrea Cervesato via ltp
2025-01-14 22:26 ` [LTP] [PATCH 2/3] mmap03: " Ricardo B. Marliere via ltp
2025-01-14 22:26 ` [LTP] [PATCH 3/3] mmap10: " Ricardo B. Marliere via ltp
2 siblings, 1 reply; 8+ messages in thread
From: Ricardo B. Marliere via ltp @ 2025-01-14 22:26 UTC (permalink / raw)
To: Linux Test Project; +Cc: Ricardo B. Marliere
From: Ricardo B. Marliere <rbm@suse.com>
Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
---
testcases/kernel/syscalls/mmap/mmap001.c | 206 ++++++++-----------------------
1 file changed, 49 insertions(+), 157 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
index dabb7d1e4998b1097e179abe23555926f5841117..bc9b4155e8b53f942ef694fdf3187c0e544a97cd 100644
--- a/testcases/kernel/syscalls/mmap/mmap001.c
+++ b/testcases/kernel/syscalls/mmap/mmap001.c
@@ -1,183 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
* Aaron Laffin <alaffin@sgi.com>
+ * Copyright (c) 2025 Linux Test Project
+ */
+
+/*\
+ * [Description]
*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * mmap001.c - Tests mmapping a big file and writing it once
+ * Tests mmapping a big file and writing it once
*/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include "test.h"
+#include "tst_test.h"
-char *TCID = "mmap001";
-int TST_TOTAL = 5;
-static char *filename = NULL;
-static int m_opt = 0;
+static int fd = -1;
+static int m_opt = 1000;
static char *m_copt;
-static void cleanup(void)
-{
- free(filename);
-
- tst_rmdir();
-}
-
static void setup(void)
{
- char buf[1024];
- /*
- * setup a default signal hander and a
- * temporary working directory.
- */
- tst_sig(FORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- tst_tmpdir();
-
- snprintf(buf, 1024, "testfile.%d", getpid());
-
- if ((filename = strdup(buf)) == NULL) {
- tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
- }
-
-}
-
-static void help(void)
-{
- printf(" -m x size of mmap in pages (default 1000)\n");
+ if (tst_parse_int(m_copt, &m_opt, 1, INT_MAX))
+ tst_brk(TBROK, "Invalid size of mmap '%s'", m_copt);
}
-/*
- * add the -m option whose parameter is the
- * pages that should be mapped.
- */
-option_t options[] = {
- {"m:", &m_opt, &m_copt},
- {NULL, NULL, NULL}
-};
-
-int main(int argc, char *argv[])
+static void run(void)
{
char *array;
- int lc;
unsigned int i;
- int fd;
unsigned int pages, memsize;
- tst_parse_opts(argc, argv, options, help);
-
- if (m_opt) {
- memsize = pages = atoi(m_copt);
-
- if (memsize < 1) {
- tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
- m_copt);
- }
-
- memsize *= getpagesize(); /* N PAGES */
-
- } else {
- /*
- * default size 1000 pages;
- */
- memsize = pages = 1000;
- memsize *= getpagesize();
- }
-
- tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
- memsize);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
-
- fd = open(filename, O_RDWR | O_CREAT, 0666);
- if ((fd == -1))
- tst_brkm(TBROK | TERRNO, cleanup,
- "opening %s failed", filename);
-
- if (lseek(fd, memsize, SEEK_SET) != memsize) {
- TEST_ERRNO = errno;
- close(fd);
- tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
- }
+ memsize = m_opt;
+ pages = m_opt;
+ memsize *= getpagesize();
- if (write(fd, "\0", 1) != 1) {
- TEST_ERRNO = errno;
- close(fd);
- tst_brkm(TBROK | TTERRNO, cleanup,
- "writing to %s failed", filename);
- }
+ tst_res(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
+ memsize);
- array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
- if (array == MAP_FAILED) {
- TEST_ERRNO = errno;
- close(fd);
- tst_brkm(TBROK | TTERRNO, cleanup,
- "mmapping %s failed", filename);
- } else {
- tst_resm(TPASS, "mmap() completed successfully.");
- }
+ fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT);
+ SAFE_LSEEK(fd, memsize, SEEK_SET);
+ SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
- tst_resm(TINFO, "touching mmaped memory");
+ array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
- for (i = 0; i < memsize; i++) {
- array[i] = (char)i;
- }
+ tst_res(TINFO, "touching mmaped memory");
+ for (i = 0; i < memsize; i++)
+ array[i] = (char)i;
- /*
- * seems that if the map area was bad, we'd get SEGV,
- * hence we can indicate a PASS.
- */
- tst_resm(TPASS,
- "we're still here, mmaped area must be good");
-
- TEST(msync(array, memsize, MS_SYNC));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL | TTERRNO,
- "synchronizing mmapped page failed");
- } else {
- tst_resm(TPASS,
- "synchronizing mmapped page passed");
- }
-
- TEST(munmap(array, memsize));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL | TTERRNO,
- "munmapping %s failed", filename);
- } else {
- tst_resm(TPASS, "munmapping %s successful", filename);
- }
+ /*
+ * Seems that if the map area was bad, we'd get SEGV,
+ * hence we can indicate a PASS.
+ */
+ tst_res(TPASS, "we're still here, mmaped area must be good");
- close(fd);
- unlink(filename);
+ SAFE_MSYNC(array, memsize, MS_SYNC);
+ SAFE_MUNMAP(array, memsize);
+}
- }
- cleanup();
- tst_exit();
+static void cleanup(void)
+{
+ if (fd > 0)
+ SAFE_CLOSE(fd);
}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+ .cleanup = cleanup,
+ .options =
+ (struct tst_option[]){
+ { "m:", &m_copt,
+ "Size of mmap in pages (default 1000)" },
+ {},
+ },
+};
--
2.47.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH 1/3] mmap001: Convert to new API
2025-01-14 22:26 ` [LTP] [PATCH 1/3] mmap001: Convert to " Ricardo B. Marliere via ltp
@ 2025-01-15 13:14 ` Andrea Cervesato via ltp
2025-01-29 17:19 ` Ricardo B. Marliere via ltp
0 siblings, 1 reply; 8+ messages in thread
From: Andrea Cervesato via ltp @ 2025-01-15 13:14 UTC (permalink / raw)
To: Ricardo B. Marliere, Linux Test Project
Hi!
On 1/14/25 23:26, Ricardo B. Marliere via ltp wrote:
> From: Ricardo B. Marliere <rbm@suse.com>
>
> Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
> ---
> testcases/kernel/syscalls/mmap/mmap001.c | 206 ++++++++-----------------------
> 1 file changed, 49 insertions(+), 157 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
> index dabb7d1e4998b1097e179abe23555926f5841117..bc9b4155e8b53f942ef694fdf3187c0e544a97cd 100644
> --- a/testcases/kernel/syscalls/mmap/mmap001.c
> +++ b/testcases/kernel/syscalls/mmap/mmap001.c
> @@ -1,183 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> /*
> * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
> * Aaron Laffin <alaffin@sgi.com>
> + * Copyright (c) 2025 Linux Test Project
> + */
> +
> +/*\
> + * [Description]
> *
> - * This program is free software; you can redistribute it and/or
> - * modify it under the terms of the GNU General Public License
> - * as published by the Free Software Foundation; either version 2
> - * of the License, or (at your option) any later version.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> - * GNU General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
> - *
> - * mmap001.c - Tests mmapping a big file and writing it once
> + * Tests mmapping a big file and writing it once
Description is a bit short and it needs a bit more information. For
example, what we expect to see and what could happen during test (SEGV
for example).
> */
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
> -#include <stdlib.h>
> -#include <stdio.h>
> -#include <unistd.h>
> -#include <errno.h>
> -#include <string.h>
>
> -#include "test.h"
> +#include "tst_test.h"
>
> -char *TCID = "mmap001";
> -int TST_TOTAL = 5;
> -static char *filename = NULL;
> -static int m_opt = 0;
> +static int fd = -1;
> +static int m_opt = 1000;
> static char *m_copt;
>
> -static void cleanup(void)
> -{
> - free(filename);
> -
> - tst_rmdir();
> -}
> -
> static void setup(void)
> {
> - char buf[1024];
> - /*
> - * setup a default signal hander and a
> - * temporary working directory.
> - */
> - tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - tst_tmpdir();
> -
> - snprintf(buf, 1024, "testfile.%d", getpid());
> -
> - if ((filename = strdup(buf)) == NULL) {
> - tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
> - }
> -
> -}
> -
> -static void help(void)
> -{
> - printf(" -m x size of mmap in pages (default 1000)\n");
> + if (tst_parse_int(m_copt, &m_opt, 1, INT_MAX))
> + tst_brk(TBROK, "Invalid size of mmap '%s'", m_copt);
> }
>
> -/*
> - * add the -m option whose parameter is the
> - * pages that should be mapped.
> - */
> -option_t options[] = {
> - {"m:", &m_opt, &m_copt},
> - {NULL, NULL, NULL}
> -};
> -
> -int main(int argc, char *argv[])
> +static void run(void)
> {
> char *array;
> - int lc;
> unsigned int i;
> - int fd;
> unsigned int pages, memsize;
>
> - tst_parse_opts(argc, argv, options, help);
> -
> - if (m_opt) {
> - memsize = pages = atoi(m_copt);
> -
> - if (memsize < 1) {
> - tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
> - m_copt);
> - }
> -
> - memsize *= getpagesize(); /* N PAGES */
> -
> - } else {
> - /*
> - * default size 1000 pages;
> - */
> - memsize = pages = 1000;
> - memsize *= getpagesize();
> - }
> -
> - tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
> - memsize);
> -
> - setup();
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - tst_count = 0;
> -
> - fd = open(filename, O_RDWR | O_CREAT, 0666);
> - if ((fd == -1))
> - tst_brkm(TBROK | TERRNO, cleanup,
> - "opening %s failed", filename);
> -
> - if (lseek(fd, memsize, SEEK_SET) != memsize) {
> - TEST_ERRNO = errno;
> - close(fd);
> - tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
> - }
> + memsize = m_opt;
> + pages = m_opt;
> + memsize *= getpagesize();
>
> - if (write(fd, "\0", 1) != 1) {
> - TEST_ERRNO = errno;
> - close(fd);
> - tst_brkm(TBROK | TTERRNO, cleanup,
> - "writing to %s failed", filename);
> - }
> + tst_res(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
> + memsize);
>
> - array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
> - if (array == MAP_FAILED) {
> - TEST_ERRNO = errno;
> - close(fd);
> - tst_brkm(TBROK | TTERRNO, cleanup,
> - "mmapping %s failed", filename);
> - } else {
> - tst_resm(TPASS, "mmap() completed successfully.");
> - }
> + fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT);
> + SAFE_LSEEK(fd, memsize, SEEK_SET);
> + SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
>
> - tst_resm(TINFO, "touching mmaped memory");
> + array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
>
> - for (i = 0; i < memsize; i++) {
> - array[i] = (char)i;
> - }
> + tst_res(TINFO, "touching mmaped memory");
> + for (i = 0; i < memsize; i++)
> + array[i] = (char)i;
>
> - /*
> - * seems that if the map area was bad, we'd get SEGV,
> - * hence we can indicate a PASS.
> - */
> - tst_resm(TPASS,
> - "we're still here, mmaped area must be good");
> -
> - TEST(msync(array, memsize, MS_SYNC));
> -
> - if (TEST_RETURN == -1) {
> - tst_resm(TFAIL | TTERRNO,
> - "synchronizing mmapped page failed");
> - } else {
> - tst_resm(TPASS,
> - "synchronizing mmapped page passed");
> - }
> -
> - TEST(munmap(array, memsize));
> -
> - if (TEST_RETURN == -1) {
> - tst_resm(TFAIL | TTERRNO,
> - "munmapping %s failed", filename);
> - } else {
> - tst_resm(TPASS, "munmapping %s successful", filename);
> - }
> + /*
> + * Seems that if the map area was bad, we'd get SEGV,
> + * hence we can indicate a PASS.
> + */
If this is true, we need to find a way to test that specific scenario.
This can e done by forking a process where test is running and to check
if SIGSEGV killed it after calling SAFE_WAITPID()
> + tst_res(TPASS, "we're still here, mmaped area must be good");
>
> - close(fd);
> - unlink(filename);
> + SAFE_MSYNC(array, memsize, MS_SYNC);
> + SAFE_MUNMAP(array, memsize);
> +}
>
> - }
> - cleanup();
> - tst_exit();
> +static void cleanup(void)
> +{
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> }
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .test_all = run,
> + .cleanup = cleanup,
> + .options =
> + (struct tst_option[]){
> + { "m:", &m_copt,
> + "Size of mmap in pages (default 1000)" },
> + {},
> + },
> +};
>
Kind regards,
Andrea
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH 1/3] mmap001: Convert to new API
2025-01-15 13:14 ` Andrea Cervesato via ltp
@ 2025-01-29 17:19 ` Ricardo B. Marliere via ltp
2025-02-10 9:04 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 8+ messages in thread
From: Ricardo B. Marliere via ltp @ 2025-01-29 17:19 UTC (permalink / raw)
To: Andrea Cervesato, Ricardo B. Marliere, Linux Test Project
Hi Andrea,
thanks for reviewing!
On Wed Jan 15, 2025 at 10:14 AM -03, Andrea Cervesato wrote:
> Hi!
>
> On 1/14/25 23:26, Ricardo B. Marliere via ltp wrote:
>> From: Ricardo B. Marliere <rbm@suse.com>
>>
>> Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
>> ---
>> testcases/kernel/syscalls/mmap/mmap001.c | 206 ++++++++-----------------------
>> 1 file changed, 49 insertions(+), 157 deletions(-)
>>
>> diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
>> index dabb7d1e4998b1097e179abe23555926f5841117..bc9b4155e8b53f942ef694fdf3187c0e544a97cd 100644
>> --- a/testcases/kernel/syscalls/mmap/mmap001.c
>> +++ b/testcases/kernel/syscalls/mmap/mmap001.c
>> @@ -1,183 +1,75 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> /*
>> * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
>> * Aaron Laffin <alaffin@sgi.com>
>> + * Copyright (c) 2025 Linux Test Project
>> + */
>> +
>> +/*\
>> + * [Description]
>> *
>> - * This program is free software; you can redistribute it and/or
>> - * modify it under the terms of the GNU General Public License
>> - * as published by the Free Software Foundation; either version 2
>> - * of the License, or (at your option) any later version.
>> - *
>> - * This program is distributed in the hope that it will be useful,
>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
>> - * GNU General Public License for more details.
>> - *
>> - * You should have received a copy of the GNU General Public License
>> - * along with this program; if not, write to the Free Software
>> - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
>> - *
>> - * mmap001.c - Tests mmapping a big file and writing it once
>> + * Tests mmapping a big file and writing it once
> Description is a bit short and it needs a bit more information. For
> example, what we expect to see and what could happen during test (SEGV
> for example).
Ack.
>> - if (TEST_RETURN == -1) {
>> - tst_resm(TFAIL | TTERRNO,
>> - "munmapping %s failed", filename);
>> - } else {
>> - tst_resm(TPASS, "munmapping %s successful", filename);
>> - }
>> + /*
>> + * Seems that if the map area was bad, we'd get SEGV,
>> + * hence we can indicate a PASS.
>> + */
> If this is true, we need to find a way to test that specific scenario.
> This can e done by forking a process where test is running and to check
> if SIGSEGV killed it after calling SAFE_WAITPID()
Good idea, I kept the original comment and TPASS string but it could
surely be expanded. What do you think of the following? I'll be sending
as v2 when the series receive some more reviewing.
diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
index bc9b4155e8b5..10ce7a48e7c2 100644
--- a/testcases/kernel/syscalls/mmap/mmap001.c
+++ b/testcases/kernel/syscalls/mmap/mmap001.c
@@ -8,7 +8,8 @@
/*\
* [Description]
*
- * Tests mmapping a big file and writing it once
+ * This test will map a big file to memory and write to it once,
+ * making sure nothing bad happened in between such as a SIGSEGV.
*/
#include "tst_test.h"
@@ -25,6 +26,8 @@ static void setup(void)
static void run(void)
{
+ pid_t pid;
+ int status;
char *array;
unsigned int i;
unsigned int pages, memsize;
@@ -40,20 +43,23 @@ static void run(void)
SAFE_LSEEK(fd, memsize, SEEK_SET);
SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
- array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
-
- tst_res(TINFO, "touching mmaped memory");
- for (i = 0; i < memsize; i++)
- array[i] = (char)i;
-
- /*
- * Seems that if the map area was bad, we'd get SEGV,
- * hence we can indicate a PASS.
- */
- tst_res(TPASS, "we're still here, mmaped area must be good");
-
- SAFE_MSYNC(array, memsize, MS_SYNC);
- SAFE_MUNMAP(array, memsize);
+ pid = SAFE_FORK();
+ if (pid == 0) {
+ array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
+ tst_res(TINFO, "touching mmapped memory");
+ for (i = 0; i < memsize; i++)
+ array[i] = (char)i;
+ SAFE_MSYNC(array, memsize, MS_SYNC);
+ SAFE_MUNMAP(array, memsize);
+ exit(0);
+ } else {
+ SAFE_WAITPID(pid, &status, 0);
+ if (WIFSIGNALED(status) && WEXITSTATUS(status) == SIGSEGV)
+ tst_res(TFAIL, "test was killed by SIGSEGV");
+ else
+ tst_res(TPASS,
+ "memory was mapped and written to successfully");
+ }
}
static void cleanup(void)
@@ -66,6 +72,7 @@ static struct tst_test test = {
.setup = setup,
.test_all = run,
.cleanup = cleanup,
+ .forks_child = 1,
.options =
(struct tst_option[]){
{ "m:", &m_copt,
> Kind regards,
> Andrea
Thank you,
- Ricardo.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH 1/3] mmap001: Convert to new API
2025-01-29 17:19 ` Ricardo B. Marliere via ltp
@ 2025-02-10 9:04 ` Andrea Cervesato via ltp
0 siblings, 0 replies; 8+ messages in thread
From: Andrea Cervesato via ltp @ 2025-02-10 9:04 UTC (permalink / raw)
To: Ricardo B. Marliere, Ricardo B. Marliere, Linux Test Project
Hi!
A few comments below about the code.
On 1/29/25 18:19, Ricardo B. Marliere wrote:
> Hi Andrea,
>
> thanks for reviewing!
>
> On Wed Jan 15, 2025 at 10:14 AM -03, Andrea Cervesato wrote:
>> Hi!
>>
>> On 1/14/25 23:26, Ricardo B. Marliere via ltp wrote:
>>> From: Ricardo B. Marliere <rbm@suse.com>
>>>
>>> Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
>>> ---
>>> testcases/kernel/syscalls/mmap/mmap001.c | 206 ++++++++-----------------------
>>> 1 file changed, 49 insertions(+), 157 deletions(-)
>>>
>>> diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
>>> index dabb7d1e4998b1097e179abe23555926f5841117..bc9b4155e8b53f942ef694fdf3187c0e544a97cd 100644
>>> --- a/testcases/kernel/syscalls/mmap/mmap001.c
>>> +++ b/testcases/kernel/syscalls/mmap/mmap001.c
>>> @@ -1,183 +1,75 @@
>>> +// SPDX-License-Identifier: GPL-2.0-or-later
>>> /*
>>> * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
>>> * Aaron Laffin <alaffin@sgi.com>
>>> + * Copyright (c) 2025 Linux Test Project
>>> + */
>>> +
>>> +/*\
>>> + * [Description]
>>> *
>>> - * This program is free software; you can redistribute it and/or
>>> - * modify it under the terms of the GNU General Public License
>>> - * as published by the Free Software Foundation; either version 2
>>> - * of the License, or (at your option) any later version.
>>> - *
>>> - * This program is distributed in the hope that it will be useful,
>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
>>> - * GNU General Public License for more details.
>>> - *
>>> - * You should have received a copy of the GNU General Public License
>>> - * along with this program; if not, write to the Free Software
>>> - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
>>> - *
>>> - * mmap001.c - Tests mmapping a big file and writing it once
>>> + * Tests mmapping a big file and writing it once
>> Description is a bit short and it needs a bit more information. For
>> example, what we expect to see and what could happen during test (SEGV
>> for example).
> Ack.
>
>>> - if (TEST_RETURN == -1) {
>>> - tst_resm(TFAIL | TTERRNO,
>>> - "munmapping %s failed", filename);
>>> - } else {
>>> - tst_resm(TPASS, "munmapping %s successful", filename);
>>> - }
>>> + /*
>>> + * Seems that if the map area was bad, we'd get SEGV,
>>> + * hence we can indicate a PASS.
>>> + */
>> If this is true, we need to find a way to test that specific scenario.
>> This can e done by forking a process where test is running and to check
>> if SIGSEGV killed it after calling SAFE_WAITPID()
> Good idea, I kept the original comment and TPASS string but it could
> surely be expanded. What do you think of the following? I'll be sending
> as v2 when the series receive some more reviewing.
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
> index bc9b4155e8b5..10ce7a48e7c2 100644
> --- a/testcases/kernel/syscalls/mmap/mmap001.c
> +++ b/testcases/kernel/syscalls/mmap/mmap001.c
> @@ -8,7 +8,8 @@
> /*\
> * [Description]
> *
> - * Tests mmapping a big file and writing it once
> + * This test will map a big file to memory and write to it once,
> + * making sure nothing bad happened in between such as a SIGSEGV.
> */
>
> #include "tst_test.h"
> @@ -25,6 +26,8 @@ static void setup(void)
>
> static void run(void)
> {
> + pid_t pid;
> + int status;
> char *array;
> unsigned int i;
> unsigned int pages, memsize;
> @@ -40,20 +43,23 @@ static void run(void)
> SAFE_LSEEK(fd, memsize, SEEK_SET);
> SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
>
> - array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
> -
> - tst_res(TINFO, "touching mmaped memory");
> - for (i = 0; i < memsize; i++)
> - array[i] = (char)i;
> -
> - /*
> - * Seems that if the map area was bad, we'd get SEGV,
> - * hence we can indicate a PASS.
> - */
> - tst_res(TPASS, "we're still here, mmaped area must be good");
> -
> - SAFE_MSYNC(array, memsize, MS_SYNC);
> - SAFE_MUNMAP(array, memsize);
> + pid = SAFE_FORK();
> + if (pid == 0) {
> + array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
> + tst_res(TINFO, "touching mmapped memory");
> + for (i = 0; i < memsize; i++)
> + array[i] = (char)i;
> + SAFE_MSYNC(array, memsize, MS_SYNC);
> + SAFE_MUNMAP(array, memsize);
> + exit(0);
> + } else {
No need for else here. Remember to call "make check" on the folder to
verify code correctness. "make check-mmap001" in this case.
Also, I was thinking that we don't have a mechanism to know if file is
updated or not, so please take a look at my patch on mmap001 (mmap21).
I'm gonna send a v2 so you can take a look at the final idea.
> + SAFE_WAITPID(pid, &status, 0);
> + if (WIFSIGNALED(status) && WEXITSTATUS(status) == SIGSEGV)
> + tst_res(TFAIL, "test was killed by SIGSEGV");
> + else
> + tst_res(TPASS,
> + "memory was mapped and written to successfully");
> + }
> }
>
> static void cleanup(void)
> @@ -66,6 +72,7 @@ static struct tst_test test = {
> .setup = setup,
> .test_all = run,
> .cleanup = cleanup,
> + .forks_child = 1,
> .options =
> (struct tst_option[]){
> { "m:", &m_copt,
>
>
>
>> Kind regards,
>> Andrea
> Thank you,
> - Ricardo.
>
>
Andrea
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* [LTP] [PATCH 2/3] mmap03: Convert to new API
2025-01-14 22:26 [LTP] [PATCH 0/3] syscalls/mmap: Refactor a few tests to the new API Ricardo B. Marliere via ltp
2025-01-14 22:26 ` [LTP] [PATCH 1/3] mmap001: Convert to " Ricardo B. Marliere via ltp
@ 2025-01-14 22:26 ` Ricardo B. Marliere via ltp
2025-02-27 14:31 ` Petr Vorel
2025-01-14 22:26 ` [LTP] [PATCH 3/3] mmap10: " Ricardo B. Marliere via ltp
2 siblings, 1 reply; 8+ messages in thread
From: Ricardo B. Marliere via ltp @ 2025-01-14 22:26 UTC (permalink / raw)
To: Linux Test Project; +Cc: Ricardo B. Marliere
From: Ricardo B. Marliere <rbm@suse.com>
Also add x86_64 if PKU is enabled.
Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
---
testcases/kernel/syscalls/mmap/mmap03.c | 274 ++++++++++++--------------------
1 file changed, 99 insertions(+), 175 deletions(-)
diff --git a/testcases/kernel/syscalls/mmap/mmap03.c b/testcases/kernel/syscalls/mmap/mmap03.c
index 9d94d2653661387d22f811cd959a87b8112c1167..8990acbecfb8dcad174df3a56fa512a39be16478 100644
--- a/testcases/kernel/syscalls/mmap/mmap03.c
+++ b/testcases/kernel/syscalls/mmap/mmap03.c
@@ -1,230 +1,154 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) 2025 Linux Test Project
+ * 07/2001 Ported by Wayne Boyer
*/
-/*
- * Test Description:
- * Call mmap() to map a file creating a mapped region with execute access
- * under the following conditions -
- * - The prot parameter is set to PROT_EXE
- * - The file descriptor is open for read
- * - The file being mapped has execute permission bit set.
- * - The minimum file permissions should be 0555.
+/*\
+ * [Description]
*
- * The call should succeed to map the file creating mapped memory with the
- * required attributes.
+ * Call mmap() to map a file creating a mapped region with execute access
+ * under the following conditions:
+ * - The prot parameter is set to PROT_EXEC
+ * - The file descriptor is open for read
+ * - The file being mapped has execute permission bit set
+ * - The minimum file permissions should be 0555
+ * The call should succeed to map the file creating mapped memory with the
+ * required attributes.
*
- * Expected Result:
- * mmap() should succeed returning the address of the mapped region,
- * and the mapped region should contain the contents of the mapped file.
- * but with ia64 and PARISC/hppa,
- * an attempt to access the contents of the mapped region should give
- * rise to the signal SIGSEGV.
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
+ * mmap() should succeed returning the address of the mapped region,
+ * and the mapped region should contain the contents of the mapped file.
+ * but with ia64, PARISC/hppa and x86_64 (with PKU), an attempt to access
+ * the contents of the mapped region should give rise to the signal SIGSEGV.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <setjmp.h>
-#include "test.h"
+#include <setjmp.h>
+#include <stdlib.h>
-#define TEMPFILE "mmapfile"
+#include "tst_kconfig.h"
+#include "tst_test.h"
-char *TCID = "mmap03";
-int TST_TOTAL = 1;
+#define TEMPFILE "mmapfile"
static size_t page_sz;
static char *addr;
static char *dummy;
static int fildes;
-static volatile int pass = 0;
+static volatile int sig_flag = -1;
static sigjmp_buf env;
-static void setup(void);
-static void cleanup(void);
-static void sig_handler(int sig);
-
-int main(int ac, char **av)
+/*
+ * This function gets executed when the test process receives the signal
+ * SIGSEGV while trying to access the contents of memory which is not accessible.
+ */
+static void sig_handler(int sig)
{
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
-
- tst_count = 0;
-
- /*
- * Call mmap to map the temporary file 'TEMPFILE'
- * with execute access.
- */
- errno = 0;
- addr = mmap(0, page_sz, PROT_EXEC,
- MAP_FILE | MAP_SHARED, fildes, 0);
-
- /* Check for the return value of mmap() */
- if (addr == MAP_FAILED) {
- tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
- TEMPFILE);
- continue;
- }
-
- /*
- * Read the file contents into the dummy
- * variable.
- */
- if (read(fildes, dummy, page_sz) < 0) {
- tst_brkm(TFAIL | TERRNO, cleanup,
- "reading %s failed", TEMPFILE);
- }
-
- /*
- * Check whether the mapped memory region
- * has the file contents.
- *
- * with ia64 and PARISC/hppa, this should
- * generate a SIGSEGV which will be caught below.
- *
- */
-
- if (sigsetjmp(env, 1) == 0) {
- if (memcmp(dummy, addr, page_sz)) {
- tst_resm(TFAIL,
- "mapped memory region "
- "contains invalid data");
- } else {
- tst_resm(TPASS,
- "mmap() functionality is "
- "correct");
- }
- }
-#if defined(__ia64__) || defined(__hppa__) || defined(__mips__)
- if (pass) {
- tst_resm(TPASS, "Got SIGSEGV as expected");
- } else {
- tst_resm(TFAIL, "Mapped memory region with NO "
- "access is accessible");
- }
-#endif
-
- /* Clean up things in case we are looping */
- /* Unmap the mapped memory */
- if (munmap(addr, page_sz) != 0) {
- tst_brkm(TFAIL | TERRNO, cleanup,
- "failed to unmap the mmapped pages");
- }
- pass = 0;
-
+ if (sig == SIGSEGV) {
+ /* Set the global variable and jump back */
+ sig_flag = 1;
+ siglongjmp(env, 1);
+ } else {
+ tst_brk(TBROK, "received an unexpected signal: %d", sig);
}
-
- cleanup();
- tst_exit();
}
static void setup(void)
{
char *tst_buff;
- tst_sig(NOFORK, sig_handler, cleanup);
-
- TEST_PAUSE;
+ SAFE_SIGNAL(SIGSEGV, sig_handler);
page_sz = getpagesize();
/* Allocate space for the test buffer */
- if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
- tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
- }
+ tst_buff = SAFE_CALLOC(page_sz, sizeof(char));
/* Fill the test buffer with the known data */
memset(tst_buff, 'A', page_sz);
- tst_tmpdir();
-
- /* Creat a temporary file used for mapping */
- if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
- free(tst_buff);
- tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
- TEMPFILE);
- }
+ /* Create a temporary file used for mapping */
+ fildes = SAFE_OPEN(TEMPFILE, O_WRONLY | O_CREAT);
/* Write test buffer contents into temporary file */
- if (write(fildes, tst_buff, page_sz) < (long)page_sz) {
- free(tst_buff);
- tst_brkm(TFAIL | TERRNO, cleanup, "writing to %s failed",
- TEMPFILE);
- }
+ SAFE_WRITE(SAFE_WRITE_ALL, fildes, tst_buff, page_sz);
/* Free the memory allocated for test buffer */
free(tst_buff);
/* Make sure proper permissions set on file */
- if (fchmod(fildes, 0555) < 0) {
- tst_brkm(TFAIL, cleanup, "fchmod of %s failed", TEMPFILE);
- }
+ SAFE_FCHMOD(fildes, 0555);
/* Close the temporary file opened for write */
- if (close(fildes) < 0) {
- tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed",
- TEMPFILE);
- }
+ SAFE_CLOSE(fildes);
/* Allocate and initialize dummy string of system page size bytes */
- if ((dummy = calloc(page_sz, sizeof(char))) == NULL) {
- tst_brkm(TFAIL, cleanup, "calloc failed (dummy)");
- }
+ dummy = SAFE_CALLOC(page_sz, sizeof(char));
/* Open the temporary file again for reading */
- if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
- tst_brkm(TFAIL | TERRNO, cleanup,
- "opening %s read-only failed", TEMPFILE);
- }
+ fildes = SAFE_OPEN(TEMPFILE, O_RDONLY);
}
-/*
- * This function gets executed when the test process receives
- * the signal SIGSEGV while trying to access the contents of memory which
- * is not accessible.
- */
-static void sig_handler(int sig)
+static void run(void)
{
- if (sig == SIGSEGV) {
- /* set the global variable and jump back */
- pass = 1;
- siglongjmp(env, 1);
- } else
- tst_brkm(TBROK, cleanup, "received an unexpected signal");
+ addr = SAFE_MMAP(0, page_sz, PROT_EXEC, MAP_FILE | MAP_SHARED, fildes,
+ 0);
+
+ /* Read the file contents into the dummy variable. */
+ SAFE_READ(0, fildes, dummy, page_sz);
+
+ /*
+ * Check whether the mapped memory region
+ * has the file contents. With ia64, PARISC/hppa and x86_64 (with PKU),
+ * this should generate a SIGSEGV which will be caught below.
+ */
+ if (sigsetjmp(env, 1) == 0) {
+ if (memcmp(dummy, addr, page_sz)) {
+ tst_res(TINFO, "memcmp returned non-zero");
+ tst_res(TFAIL,
+ "mapped memory region contains invalid data");
+ } else {
+ tst_res(TINFO, "memcmp returned zero");
+ tst_res(TPASS, "mmap() functionality is correct");
+ }
+ }
+
+#if defined(__ia64__) || defined(__hppa__) || defined(__mips__)
+ if (sig_flag > 0)
+ tst_res(TPASS, "Got SIGSEGV as expected");
+ else
+ tst_res(TFAIL,
+ "Mapped memory region with NO access is accessible");
+#elif defined(__x86_64__)
+ struct tst_kconfig_var kconfig =
+ TST_KCONFIG_INIT("CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS");
+ tst_kconfig_read(&kconfig, 1);
+ if (kconfig.choice == 'y') {
+ if (sig_flag > 0)
+ tst_res(TPASS, "Got SIGSEGV as expected");
+ else
+ tst_res(TFAIL,
+ "Mapped memory region with NO access is accessible");
+ }
+#else
+ if (sig_flag > 0)
+ tst_res(TFAIL, "Got unexpected SIGSEGV");
+#endif
+
+ /* Clean up things in case we are looping */
+ sig_flag = -1;
+ SAFE_MUNMAP(addr, page_sz);
}
static void cleanup(void)
{
- close(fildes);
free(dummy);
- tst_rmdir();
+ if (fildes > 0)
+ SAFE_CLOSE(fildes);
}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .cleanup = cleanup,
+};
--
2.47.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [LTP] [PATCH 2/3] mmap03: Convert to new API
2025-01-14 22:26 ` [LTP] [PATCH 2/3] mmap03: " Ricardo B. Marliere via ltp
@ 2025-02-27 14:31 ` Petr Vorel
0 siblings, 0 replies; 8+ messages in thread
From: Petr Vorel @ 2025-02-27 14:31 UTC (permalink / raw)
To: Ricardo B. Marliere; +Cc: Linux Test Project
Hi Ricardo,
this requires root otherwise it fails.
mmap03.c:71: TBROK: open(mmapfile,65,0001) failed: EACCES (13)
Because Andrea by accident has been working on the same rewrite and has already
got v2 reviewed by Cyril, I guess there is no need for your v2. I'm closing this
as changes requested, but it's effectively refused.
Hopefully we avoid in the future working on the same patch.
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 8+ messages in thread
* [LTP] [PATCH 3/3] mmap10: Convert to new API
2025-01-14 22:26 [LTP] [PATCH 0/3] syscalls/mmap: Refactor a few tests to the new API Ricardo B. Marliere via ltp
2025-01-14 22:26 ` [LTP] [PATCH 1/3] mmap001: Convert to " Ricardo B. Marliere via ltp
2025-01-14 22:26 ` [LTP] [PATCH 2/3] mmap03: " Ricardo B. Marliere via ltp
@ 2025-01-14 22:26 ` Ricardo B. Marliere via ltp
2 siblings, 0 replies; 8+ messages in thread
From: Ricardo B. Marliere via ltp @ 2025-01-14 22:26 UTC (permalink / raw)
To: Linux Test Project; +Cc: Ricardo B. Marliere
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 8+ messages in thread