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