• 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() calls fails with errno EBADF when a file mapping
12  * is requested but the fd is not a valid file descriptor.
13  */
14 
15 #include <stdlib.h>
16 #include "tst_test.h"
17 
18 #define TEMPFILE "mmapfile"
19 static size_t page_sz;
20 static int fd;
21 
setup(void)22 static void setup(void)
23 {
24 	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
25 	SAFE_CLOSE(fd);
26 }
27 
run(void)28 static void run(void)
29 {
30 	TESTPTR(mmap(NULL, page_sz, PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0));
31 
32 	if (TST_RET_PTR != MAP_FAILED) {
33 		tst_res(TFAIL, "mmap() passed unexpectedly");
34 		SAFE_MUNMAP(TST_RET_PTR, page_sz);
35 	} else if (TST_ERR == EBADF) {
36 		tst_res(TPASS, "mmap() failed with EBADF");
37 	} else {
38 		tst_res(TFAIL | TERRNO, "mmap() failed with an invalid errno");
39 	}
40 }
41 
cleanup(void)42 static void cleanup(void)
43 {
44 	if (fd > 0)
45 		SAFE_CLOSE(fd);
46 }
47 
48 static struct tst_test test = {
49 	.setup = setup,
50 	.cleanup = cleanup,
51 	.test_all = run,
52 	.needs_tmpdir = 1
53 };
54