xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlockall/mlockall01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
13*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
14*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker /**************************************************************************
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  *    TEST IDENTIFIER	: mlockall01
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  *    EXECUTED BY	: root / superuser
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  *    TEST TITLE	: Basic test for mlockall(2)
24*49cdfc7eSAndroid Build Coastguard Worker  *
25*49cdfc7eSAndroid Build Coastguard Worker  *    TEST CASE TOTAL	: 3
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  *    AUTHOR		: Nirmala Devi Dhanasekar <[email protected]>
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  *    SIGNALS
30*49cdfc7eSAndroid Build Coastguard Worker  * 	Uses SIGUSR1 to pause before test if option set.
31*49cdfc7eSAndroid Build Coastguard Worker  * 	(See the parse_opts(3) man page).
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  *    DESCRIPTION
34*49cdfc7eSAndroid Build Coastguard Worker  *	This is a Phase I test for the mlockall(2) system call.
35*49cdfc7eSAndroid Build Coastguard Worker  *	It is intended to provide a limited exposure of the system call.
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  * 	Setup:
38*49cdfc7eSAndroid Build Coastguard Worker  *	  Setup signal handling.
39*49cdfc7eSAndroid Build Coastguard Worker  *	  Pause for SIGUSR1 if option specified.
40*49cdfc7eSAndroid Build Coastguard Worker  *
41*49cdfc7eSAndroid Build Coastguard Worker  * 	Test:
42*49cdfc7eSAndroid Build Coastguard Worker  *	 Loop if the proper options are given.
43*49cdfc7eSAndroid Build Coastguard Worker  *	  Execute system call
44*49cdfc7eSAndroid Build Coastguard Worker  *	  Check return code, if system call failed (return=-1)
45*49cdfc7eSAndroid Build Coastguard Worker  *		Log the errno and Issue a FAIL message.
46*49cdfc7eSAndroid Build Coastguard Worker  *	  Otherwise, Issue a PASS message.
47*49cdfc7eSAndroid Build Coastguard Worker  *
48*49cdfc7eSAndroid Build Coastguard Worker  * 	Cleanup:
49*49cdfc7eSAndroid Build Coastguard Worker  * 	  Print errno log and/or timing stats if options given
50*49cdfc7eSAndroid Build Coastguard Worker  *
51*49cdfc7eSAndroid Build Coastguard Worker  * USAGE:  <for command-line>
52*49cdfc7eSAndroid Build Coastguard Worker  *  mlockall01 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
53*49cdfc7eSAndroid Build Coastguard Worker  *		where,$
54*49cdfc7eSAndroid Build Coastguard Worker  *			-c n : Run n copies concurrently
55*49cdfc7eSAndroid Build Coastguard Worker  *			-e   : Turn on errno logging.
56*49cdfc7eSAndroid Build Coastguard Worker  *			-h   : Show this help screen
57*49cdfc7eSAndroid Build Coastguard Worker  *			-i n : Execute test n times.
58*49cdfc7eSAndroid Build Coastguard Worker  *			-I x : Execute test for x seconds.
59*49cdfc7eSAndroid Build Coastguard Worker  *			-p   : Pause for SIGUSR1 before starting
60*49cdfc7eSAndroid Build Coastguard Worker  *			-P x : Pause for x seconds between iterations.
61*49cdfc7eSAndroid Build Coastguard Worker  *			-t   : Turn on syscall timing.
62*49cdfc7eSAndroid Build Coastguard Worker  *
63*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS
64*49cdfc7eSAndroid Build Coastguard Worker  *	Must run as root.
65*49cdfc7eSAndroid Build Coastguard Worker  *****************************************************************************/
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
68*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
69*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
70*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker void setup();
73*49cdfc7eSAndroid Build Coastguard Worker void cleanup();
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mlockall01";
76*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 3;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
79*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
80*49cdfc7eSAndroid Build Coastguard Worker 	char *fdesc;
81*49cdfc7eSAndroid Build Coastguard Worker } TC[] = {
82*49cdfc7eSAndroid Build Coastguard Worker 	/*
83*49cdfc7eSAndroid Build Coastguard Worker 	 * Check for all possible flags of mlockall
84*49cdfc7eSAndroid Build Coastguard Worker 	 */
85*49cdfc7eSAndroid Build Coastguard Worker 	{
86*49cdfc7eSAndroid Build Coastguard Worker 	MCL_CURRENT, "MCL_CURRENT"}, {
87*49cdfc7eSAndroid Build Coastguard Worker 	MCL_FUTURE, "MCL_FUTURE"}, {
88*49cdfc7eSAndroid Build Coastguard Worker 	MCL_CURRENT | MCL_FUTURE, "MCL_CURRENT|MCL_FUTURE"}
89*49cdfc7eSAndroid Build Coastguard Worker };
90*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)91*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker 	int lc, i;
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	setup();
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++) {
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 			TEST(mlockall(TC[i].flag));
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 			/* check return code */
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN == -1) {
110*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL | TTERRNO,
111*49cdfc7eSAndroid Build Coastguard Worker 					 "mlockall(%s) Failed with "
112*49cdfc7eSAndroid Build Coastguard Worker 					 "return=%ld", TC[i].fdesc,
113*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_RETURN);
114*49cdfc7eSAndroid Build Coastguard Worker 			} else {
115*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "mlockall test passed for %s",
116*49cdfc7eSAndroid Build Coastguard Worker 					 TC[i].fdesc);
117*49cdfc7eSAndroid Build Coastguard Worker 			}
118*49cdfc7eSAndroid Build Coastguard Worker 		}
119*49cdfc7eSAndroid Build Coastguard Worker 	}
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	/* cleanup and exit */
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker /*
129*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
130*49cdfc7eSAndroid Build Coastguard Worker  */
131*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)132*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
133*49cdfc7eSAndroid Build Coastguard Worker {
134*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker /*
142*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
143*49cdfc7eSAndroid Build Coastguard Worker  *		completion or premature exit.
144*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)145*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
146*49cdfc7eSAndroid Build Coastguard Worker {
147*49cdfc7eSAndroid Build Coastguard Worker }
148