xref: /aosp_15_r20/external/ltp/testcases/kernel/security/integrity/ima/src/ima_mmap.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Linux Test Project, 2010-2020
4  * Copyright (c) International Business Machines Corp., 2009
5  *
6  * Authors:
7  * Mimi Zohar <[email protected]>
8  */
9 
10 #include "tst_test.h"
11 
12 #define SLEEP_AFTER_CLOSE 3
13 #define MMAPSIZE 1024
14 
15 static char *filename;
16 static void *file;
17 static int fd;
18 
cleanup(void)19 static void cleanup(void)
20 {
21 	if (file)
22 		SAFE_MUNMAP(file, MMAPSIZE);
23 
24 	if (fd > 0)
25 		SAFE_CLOSE(fd);
26 }
27 
run(void)28 static void run(void)
29 {
30 	if (!filename)
31 		tst_brk(TBROK, "missing filename (-f filename)");
32 
33 	fd = SAFE_OPEN(filename, O_CREAT | O_RDWR, S_IRWXU);
34 
35 	file = SAFE_MMAP(NULL, MMAPSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
36 	SAFE_CLOSE(fd);
37 
38 	tst_res(TINFO, "sleep %ds", SLEEP_AFTER_CLOSE);
39 	sleep(SLEEP_AFTER_CLOSE);
40 
41 	tst_res(TPASS, "test completed");
42 }
43 
44 static struct tst_test test = {
45 	.options = (struct tst_option[]) {
46 		{"f:", &filename, "File to mmap"},
47 		{}
48 	},
49 	.test_all = run,
50 	.cleanup = cleanup,
51 };
52