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