xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock/mlock01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
6*49cdfc7eSAndroid Build Coastguard Worker  *	06/2002 Written by Paul Larson
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Test mlock with various valid addresses and lengths.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static void *addr;
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
21*49cdfc7eSAndroid Build Coastguard Worker 	char *msg;
22*49cdfc7eSAndroid Build Coastguard Worker 	int len;
23*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
24*49cdfc7eSAndroid Build Coastguard Worker 	{"mlock 1 byte", 1},
25*49cdfc7eSAndroid Build Coastguard Worker 	{"mlock 1024 bytes", 1024},
26*49cdfc7eSAndroid Build Coastguard Worker 	{"mlock 1024 * 1024 bytes", 1024 * 1024},
27*49cdfc7eSAndroid Build Coastguard Worker 	{"mlock 1024 * 1024 * 10 bytes", 1024 * 1024 * 10}
28*49cdfc7eSAndroid Build Coastguard Worker };
29*49cdfc7eSAndroid Build Coastguard Worker 
do_mlock(unsigned int i)30*49cdfc7eSAndroid Build Coastguard Worker static void do_mlock(unsigned int i)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "%s", tc->msg);
35*49cdfc7eSAndroid Build Coastguard Worker 	addr = SAFE_MALLOC(tc->len);
36*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(mlock(addr, tc->len), "mlock(%p, %d)", addr, tc->len);
37*49cdfc7eSAndroid Build Coastguard Worker 	free(addr);
38*49cdfc7eSAndroid Build Coastguard Worker 	addr = NULL;
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)41*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	if (addr)
44*49cdfc7eSAndroid Build Coastguard Worker 		free(addr);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
48*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
49*49cdfc7eSAndroid Build Coastguard Worker 	.test = do_mlock,
50*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
51*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
52*49cdfc7eSAndroid Build Coastguard Worker };
53