xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlockall/mlockall02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write the Free Software Foundation, Inc.,
14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 /**************************************************************************
18  *
19  *    TEST IDENTIFIER	: mlockall02
20  *
21  *    EXECUTED BY	: root / superuser
22  *
23  *    TEST TITLE	: Test for checking basic error conditions for
24  *    			   mlockall(2)
25  *
26  *    TEST CASE TOTAL	: 3
27  *
28  *    AUTHOR		: Nirmala Devi Dhanasekar <[email protected]>
29  *
30  *    SIGNALS
31  * 	Uses SIGUSR1 to pause before test if option set.
32  * 	(See the parse_opts(3) man page).
33  *
34  *    DESCRIPTION
35  * 	Check for basic errors returned by mount(2) system call.
36  *$
37  * 	Verify that mount(2) returns -1 and sets errno to
38  *
39  *	1) ENOMEM - If process exceed maximum  number of locked pages.
40  *	2) EPERM  - If not super user
41  *	3) EINVAL - Unknown flags were specified.
42  *
43  * 	Setup:
44  *	  Setup signal handling.
45  *	  Pause for SIGUSR1 if option specified.
46  *
47  * 	Test:
48  *	 Loop if the proper options are given.
49  *	  Do necessary setup for each test.
50  *	  Execute system call
51  *	  Check return code, if system call failed and errno == expected errno
52  *		Issue sys call passed with expected return value and errno.
53  *	  Otherwise,
54  *		Issue sys call failed to produce expected error.
55  *	  Do cleanup for each test.
56  *
57  * 	Cleanup:
58  * 	  Print errno log and/or timing stats if options given
59  *
60  * USAGE:  <for command-line>
61  *  mlockall02 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
62  *		where,
63  *			-c n : Run n copies concurrently
64  *			-e   : Turn on errno logging.
65  *			-h   : Show this help screen
66  *			-i n : Execute test n times.
67  *			-I x : Execute test for x seconds.
68  *			-p   : Pause for SIGUSR1 before starting
69  *			-P x : Pause for x seconds between iterations.
70  *			-t   : Turn on syscall timing.
71  *
72  * RESTRICTIONS
73  *	Test must run as root.
74  *****************************************************************************/
75 #include <errno.h>
76 #include <unistd.h>
77 #include <pwd.h>
78 #include <sys/mman.h>
79 #include "test.h"
80 #include "safe_macros.h"
81 #include <sys/resource.h>
82 
83 void setup();
84 int setup_test(int);
85 void cleanup_test(int);
86 void cleanup();
87 
88 char *TCID = "mlockall02";
89 int TST_TOTAL = 3;
90 
91 struct test_case_t {
92 	int flag;		/* flag value                   */
93 	int error;		/* error description            */
94 	char *edesc;		/* Expected error no            */
95 } TC[] = {
96 	{
97 	MCL_CURRENT, ENOMEM, "Process exceeds max locked pages"}, {
98 	MCL_CURRENT, EPERM, "Not a superuser"}, {
99 	0, EINVAL, "Unknown flag"}
100 };
101 
main(int ac,char ** av)102 int main(int ac, char **av)
103 {
104 	int lc, i;
105 
106 	tst_parse_opts(ac, av, NULL, NULL);
107 
108 	setup();
109 
110 	/* check looping state */
111 	for (lc = 0; TEST_LOOPING(lc); lc++) {
112 
113 		tst_count = 0;
114 
115 		for (i = 0; i < TST_TOTAL; i++) {
116 
117 			if (setup_test(i)) {
118 				tst_resm(TFAIL, "mlockall() Failed while setup "
119 					 "for checking error %s", TC[i].edesc);
120 				continue;
121 			}
122 
123 			TEST(mlockall(TC[i].flag));
124 
125 			/* check return code */
126 			if (TEST_RETURN == -1) {
127 				if (TEST_ERRNO != TC[i].error)
128 					tst_brkm(TFAIL, cleanup,
129 						 "mlock() Failed with wrong "
130 						 "errno, expected errno=%s, "
131 						 "got errno=%d : %s",
132 						 TC[i].edesc, TEST_ERRNO,
133 						 strerror(TEST_ERRNO));
134 				else
135 					tst_resm(TPASS,
136 						 "expected failure - errno "
137 						 "= %d : %s",
138 						 TEST_ERRNO,
139 						 strerror(TEST_ERRNO));
140 			} else {
141 				if (i <= 1)
142 					tst_resm(TCONF,
143 						 "mlockall02 did not BEHAVE as expected.");
144 				else
145 					tst_brkm(TFAIL, cleanup,
146 						 "mlock() Failed, expected "
147 						 "return value=-1, got %ld",
148 						 TEST_RETURN);
149 			}
150 			cleanup_test(i);
151 		}
152 	}
153 
154 	/* cleanup and exit */
155 	cleanup();
156 
157 	tst_exit();
158 }
159 
160 /*
161  * setup() - performs all ONE TIME setup for this test.
162  */
setup(void)163 void setup(void)
164 {
165 
166 	tst_require_root();
167 
168 	tst_sig(FORK, DEF_HANDLER, cleanup);
169 
170 	TEST_PAUSE;
171 
172 	return;
173 }
174 
setup_test(int i)175 int setup_test(int i)
176 {
177 	struct rlimit rl;
178 	char nobody_uid[] = "nobody";
179 	struct passwd *ltpuser;
180 
181 	switch (i) {
182 	case 0:
183 		rl.rlim_max = 10;
184 		rl.rlim_cur = 7;
185 
186 		if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
187 			tst_resm(TWARN, "setrlimit failed to set the "
188 				 "resource for RLIMIT_MEMLOCK to check "
189 				 "for mlockall error %s\n", TC[i].edesc);
190 			return 1;
191 		}
192 		ltpuser = getpwnam(nobody_uid);
193 		if (seteuid(ltpuser->pw_uid) == -1) {
194 			tst_brkm(TBROK, cleanup, "seteuid() "
195 				"failed to change euid to %d "
196 				"errno = %d : %s",
197 				ltpuser->pw_uid, TEST_ERRNO,
198 				strerror(TEST_ERRNO));
199 				return 1;
200 		}
201 		return 0;
202 	case 1:
203 		rl.rlim_max = 0;
204 		rl.rlim_cur = 0;
205 		if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
206 			tst_resm(TWARN, "setrlimit failed to "
207 				"set the resource for "
208 				"RLIMIT_MEMLOCK to check for "
209 				"mlockall error %s\n", TC[i].edesc);
210 				return 1;
211 		}
212 		ltpuser = getpwnam(nobody_uid);
213 		if (seteuid(ltpuser->pw_uid) == -1) {
214 			tst_brkm(TBROK, cleanup, "seteuid() failed to "
215 				 "change euid to %d errno = %d : %s",
216 				 ltpuser->pw_uid, TEST_ERRNO,
217 				 strerror(TEST_ERRNO));
218 			return 1;
219 		}
220 		return 0;
221 	}
222 	return 0;
223 }
224 
cleanup_test(int i)225 void cleanup_test(int i)
226 {
227 	struct rlimit rl;
228 
229 	switch (i) {
230 	case 0:
231 		seteuid(0);
232 
233 		rl.rlim_max = -1;
234 		rl.rlim_cur = -1;
235 
236 		if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
237 			tst_brkm(TFAIL, cleanup,
238 				 "setrlimit failed to reset the "
239 				 "resource for RLIMIT_MEMLOCK while "
240 				 "checking for mlockall error %s\n",
241 				 TC[i].edesc);
242 		}
243 		return;
244 	case 1:
245 		SAFE_SETEUID(cleanup, 0);
246 		return;
247 
248 	}
249 }
250 
251 /*
252  * cleanup() - performs all ONE TIME cleanup for this test at
253  *		completion or premature exit.
254  */
cleanup(void)255 void cleanup(void)
256 {
257 	return;
258 }
259