1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2023 Paulson Raja L <paulson@zilogic.com>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Test mmap(2) with MAP_SHARED_VALIDATE flag.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Test expected EOPNOTSUPP errno when testing mmap(2) with MAP_SHARED_VALIDATE
12f08c3bdfSopenharmony_ci * flag and invalid flag.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <errno.h>
16f08c3bdfSopenharmony_ci#include <stdio.h>
17f08c3bdfSopenharmony_ci#include <sys/types.h>
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci#include "lapi/mmap.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define TEST_FILE "file_to_mmap"
22f08c3bdfSopenharmony_ci#define TEST_FILE_SIZE 1024
23f08c3bdfSopenharmony_ci#define INVALID_FLAG (1 << 10)
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int fd = -1;
26f08c3bdfSopenharmony_cistatic void *addr;
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void setup(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0600);
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	if (tst_fill_file(TEST_FILE, 'a', TEST_FILE_SIZE, 1))
33f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Could not fill the testfile");
34f08c3bdfSopenharmony_ci}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void cleanup(void)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	if (fd > -1)
39f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	if (addr && addr != MAP_FAILED)
42f08c3bdfSopenharmony_ci		SAFE_MUNMAP(addr, TEST_FILE_SIZE);
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void test_mmap(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	addr = mmap(NULL, TEST_FILE_SIZE, PROT_READ | PROT_WRITE,
48f08c3bdfSopenharmony_ci			      INVALID_FLAG | MAP_SHARED_VALIDATE, fd, 0);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	if (addr != MAP_FAILED)
51f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "mmap() is successful, but it should have failed");
52f08c3bdfSopenharmony_ci	else if (errno == EOPNOTSUPP)
53f08c3bdfSopenharmony_ci		tst_res(TPASS, "mmap() failed with errno set to EOPNOTSUPP");
54f08c3bdfSopenharmony_ci	else
55f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "mmap() failed with unexpected error");
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic struct tst_test test = {
59f08c3bdfSopenharmony_ci	.min_kver = "4.15",
60f08c3bdfSopenharmony_ci	.setup = setup,
61f08c3bdfSopenharmony_ci	.cleanup = cleanup,
62f08c3bdfSopenharmony_ci	.test_all = test_mmap,
63f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
64f08c3bdfSopenharmony_ci};
65