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