xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap17.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) Zilogic Systems Pvt. Ltd., 2020
4*49cdfc7eSAndroid Build Coastguard Worker  * Email: [email protected]
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Test mmap with MAP_FIXED_NOREPLACE flag
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * We are testing the MAP_FIXED_NOREPLACE flag of mmap() syscall. To check
11*49cdfc7eSAndroid Build Coastguard Worker  * if an attempt to mmap at an exisiting mapping fails with EEXIST.
12*49cdfc7eSAndroid Build Coastguard Worker  * The code allocates a free address by passing NULL to first mmap call
13*49cdfc7eSAndroid Build Coastguard Worker  * Then tries to mmap with the same address using MAP_FIXED_NOREPLACE flag
14*49cdfc7eSAndroid Build Coastguard Worker  * and the mapping fails as expected.
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mmap.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static int fd_file1;
29*49cdfc7eSAndroid Build Coastguard Worker static int fd_file2;
30*49cdfc7eSAndroid Build Coastguard Worker static void *mapped_address;
31*49cdfc7eSAndroid Build Coastguard Worker static const char str[] = "Writing to mapped file";
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #define FNAME1 "file1_to_mmap"
34*49cdfc7eSAndroid Build Coastguard Worker #define FNAME2 "file2_to_mmap"
35*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)36*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker 	fd_file1 = SAFE_OPEN(FNAME1, O_CREAT | O_RDWR, 0600);
39*49cdfc7eSAndroid Build Coastguard Worker 	fd_file2 = SAFE_OPEN(FNAME2, O_CREAT | O_RDWR, 0600);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)42*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	int str_len;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	str_len = strlen(str);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_file2 > 0)
49*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_file2);
50*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_file1 > 0)
51*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_file1);
52*49cdfc7eSAndroid Build Coastguard Worker 	if (mapped_address)
53*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(mapped_address, str_len);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
test_mmap(void)56*49cdfc7eSAndroid Build Coastguard Worker static void test_mmap(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	int str_len;
59*49cdfc7eSAndroid Build Coastguard Worker 	void *address;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	str_len = strlen(str);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd_file1, str, str_len);
64*49cdfc7eSAndroid Build Coastguard Worker 	mapped_address = SAFE_MMAP(NULL, str_len, PROT_WRITE,
65*49cdfc7eSAndroid Build Coastguard Worker 				   MAP_PRIVATE, fd_file1, 0);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd_file2, str, str_len);
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	address = mmap(mapped_address, str_len, PROT_WRITE,
70*49cdfc7eSAndroid Build Coastguard Worker 		  MAP_PRIVATE | MAP_FIXED_NOREPLACE, fd_file2, 0);
71*49cdfc7eSAndroid Build Coastguard Worker 	if (address == MAP_FAILED && errno == EEXIST)
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "mmap set errno to EEXIST as expected");
73*49cdfc7eSAndroid Build Coastguard Worker 	else
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "mmap failed, with unexpected error "
75*49cdfc7eSAndroid Build Coastguard Worker 			"code, expected EEXIST");
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
79*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
80*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
81*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_mmap,
82*49cdfc7eSAndroid Build Coastguard Worker 	.min_kver = "4.17",
83*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1
84*49cdfc7eSAndroid Build Coastguard Worker };
85