• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *  07/2001 Ported by Wayne Boyer
5  * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that, mmap() call with PROT_READ and a file descriptor which is
12  * open for read only, succeeds to map a file creating mapped memory with
13  * read access.
14  */
15 
16 #include <stdlib.h>
17 #include "tst_test.h"
18 
19 #define TEMPFILE "mmapfile"
20 static ssize_t page_sz;
21 static int fd;
22 static char *addr;
23 static char *buf;
24 
setup(void)25 static void setup(void)
26 {
27 	page_sz = getpagesize();
28 	buf = SAFE_MALLOC(page_sz);
29 	memset(buf, 'A', page_sz);
30 
31 	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
32 	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, page_sz);
33 
34 	SAFE_FCHMOD(fd, 0444);
35 	SAFE_CLOSE(fd);
36 	fd = SAFE_OPEN(TEMPFILE, O_RDONLY);
37 }
38 
run(void)39 static void run(void)
40 {
41 	addr = SAFE_MMAP(NULL, page_sz, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
42 
43 	if (memcmp(buf, addr, page_sz) == 0)
44 		tst_res(TPASS, "mmap() functionality successful");
45 	else
46 		tst_res(TFAIL, "mapped memory area contains invalid data");
47 
48 	SAFE_MUNMAP(addr, page_sz);
49 }
50 
cleanup(void)51 static void cleanup(void)
52 {
53 	if (fd > 0)
54 		SAFE_CLOSE(fd);
55 
56 	free(buf);
57 }
58 
59 static struct tst_test test = {
60 	.setup = setup,
61 	.cleanup = cleanup,
62 	.test_all = run,
63 	.needs_tmpdir = 1
64 };
65