xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap08.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) 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() calls fails with errno EBADF when a file mapping
12*49cdfc7eSAndroid Build Coastguard Worker  * is requested but the fd is not a valid file descriptor.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE "mmapfile"
19*49cdfc7eSAndroid Build Coastguard Worker static size_t page_sz;
20*49cdfc7eSAndroid Build Coastguard Worker static int fd;
21*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)22*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
25*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker 
run(void)28*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL_PTR_VOID(mmap(NULL, page_sz, PROT_WRITE,
31*49cdfc7eSAndroid Build Coastguard Worker 				   MAP_FILE | MAP_SHARED, fd, 0), EBADF);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET_PTR != MAP_FAILED)
34*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(TST_RET_PTR, page_sz);
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)37*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
40*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
44*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
45*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
46*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
47*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1
48*49cdfc7eSAndroid Build Coastguard Worker };
49