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