1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * AUTHOR: Nirmala Devi Dhanasekar <nirmala.devi@wipro.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test for ENOMEM error.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * munlock(2) fails with ENOMEM if some of the specified address range
13f08c3bdfSopenharmony_ci * does not correspond to mapped pages in the address space of the
14f08c3bdfSopenharmony_ci * process.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <sys/mman.h>
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic size_t len, pg_size;
21f08c3bdfSopenharmony_cistatic void *addr;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic void run(void)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	TST_EXP_FAIL(munlock(addr, len), ENOMEM, "munlock(%p, %lu)",
26f08c3bdfSopenharmony_ci		      addr, len);
27f08c3bdfSopenharmony_ci}
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void setup(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	pg_size = getpagesize();
32f08c3bdfSopenharmony_ci	len = 8 * pg_size;
33f08c3bdfSopenharmony_ci	addr = SAFE_MMAP(NULL, len, PROT_READ | PROT_WRITE,
34f08c3bdfSopenharmony_ci			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
35f08c3bdfSopenharmony_ci	memset(addr, 0x20, len);
36f08c3bdfSopenharmony_ci	SAFE_MLOCK(addr, len);
37f08c3bdfSopenharmony_ci	/*
38f08c3bdfSopenharmony_ci	 * unmap part of the area, to create the condition for ENOMEM
39f08c3bdfSopenharmony_ci	 */
40f08c3bdfSopenharmony_ci	addr += 2 * pg_size;
41f08c3bdfSopenharmony_ci	SAFE_MUNMAP(addr, 4 * pg_size);
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic struct tst_test test = {
45f08c3bdfSopenharmony_ci	.needs_root = 1,
46f08c3bdfSopenharmony_ci	.setup = setup,
47f08c3bdfSopenharmony_ci	.test_all = run,
48f08c3bdfSopenharmony_ci};
49