blob: ff4d8a48c50f0aebefda67619a1a12a3b042ac65 [file] [log] [blame]
robbiew9a15e2c2005-05-26 19:15:25 +00001/*
subrata_modak56207ce2009-03-23 13:35:39 +00002 * Copyright (C) Bull S.A. 2005. $
robbiew9a15e2c2005-05-26 19:15:25 +00003 *
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew9a15e2c2005-05-26 19:15:25 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiew9a15e2c2005-05-26 19:15:25 +000019 * TEST IDENTIFIER : mlockall03
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiew9a15e2c2005-05-26 19:15:25 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiew9a15e2c2005-05-26 19:15:25 +000023 * TEST TITLE : Test for checking basic error conditions for
24 * mlockall(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiew9a15e2c2005-05-26 19:15:25 +000026 * TEST CASE TOTAL : 3
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
28 * AUTHOR : Jacky Malcles
29 *
robbiew9a15e2c2005-05-26 19:15:25 +000030 * SIGNALS
31 * Uses SIGUSR1 to pause before test if option set.
32 * (See the parse_opts(3) man page).
33 *
34 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000035 *$
robbiew9a15e2c2005-05-26 19:15:25 +000036 * Verify that mlockall(2) returns -1 and sets errno to
37 *
38 * 1) ENOMEM - If the caller had a non-zero RLIMIT_MEMLOCK
39 * and tried to lock more memory than the limit permitted.
subrata_modak4bb656a2009-02-26 12:02:09 +000040 * 2) EPERM - If the caller was not privileged
41 * and its RLIMIT_MEMLOCK soft resource limit was 0.
robbiew9a15e2c2005-05-26 19:15:25 +000042 * 3) EINVAL - Unknown flags were specified.
43 *
44 * Setup:
45 * Setup signal handling.
46 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
robbiew9a15e2c2005-05-26 19:15:25 +000048 * Test:
49 * Loop if the proper options are given.
50 * Do necessary setup for each test.
51 * Execute system call
52 * Check return code, if system call failed and errno == expected errno
53 * Issue sys call passed with expected return value and errno.
54 * Otherwise,
55 * Issue sys call failed to produce expected error.
56 * Do cleanup for each test.
57 *
58 * Cleanup:
59 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000060 *
robbiew9a15e2c2005-05-26 19:15:25 +000061 * USAGE: <for command-line>
62 * mlockall03 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
63 * where,
64 * -c n : Run n copies concurrently
65 * -e : Turn on errno logging.
66 * -h : Show this help screen
67 * -i n : Execute test n times.
68 * -I x : Execute test for x seconds.
69 * -p : Pause for SIGUSR1 before starting
70 * -P x : Pause for x seconds between iterations.
71 * -t : Turn on syscall timing.
72 *
73 * RESTRICTIONS
74 * Test must run as root.
75 *****************************************************************************/
76#include <errno.h>
77#include <unistd.h>
78#include <pwd.h>
79#include <ctype.h>
80#include <sys/mman.h>
81#include "test.h"
82#include "usctest.h"
83#include <sys/resource.h>
84#include <sys/utsname.h>
85
86void setup();
87int setup_test(int);
88int compare(char s1[], char s2[]);
89void cleanup_test(int);
90void cleanup();
91
subrata_modak56207ce2009-03-23 13:35:39 +000092char *TCID = "mlockall03"; /* Test program identifier. */
93int TST_TOTAL = 3; /* Total number of test cases. */
robbiew9a15e2c2005-05-26 19:15:25 +000094
robbiewd34d5812005-07-11 22:28:09 +000095#if !defined(UCLINUX)
96
robbiew9a15e2c2005-05-26 19:15:25 +000097char *ref_release = "2.6.8\0";
98int exp_enos[] = { ENOMEM, EPERM, EINVAL, 0 };
99
100struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000101 int flag; /* flag value */
102 int error; /* error description */
103 char *edesc; /* Expected error no */
robbiew9a15e2c2005-05-26 19:15:25 +0000104} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000105 {
106 MCL_CURRENT, ENOMEM,
107 "tried to lock more memory than the limit permitted"}, {
108 MCL_CURRENT, EPERM, "Not a superuser and RLIMIT_MEMLOCK was 0"}, {
109 ~(MCL_CURRENT | MCL_FUTURE), EINVAL, "Unknown flag"}
robbiew9a15e2c2005-05-26 19:15:25 +0000110};
111
112int main(int ac, char **av)
113{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200114 int lc, i;
115 char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000116 struct utsname *buf;
robbiew9a15e2c2005-05-26 19:15:25 +0000117
Garrett Cooper45e285d2010-11-22 12:19:25 -0800118 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
robbiew9a15e2c2005-05-26 19:15:25 +0000119 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
120 tst_exit();
121 }
122
subrata_modak56207ce2009-03-23 13:35:39 +0000123 /* allocate some space for buf */
124 if ((buf = (struct utsname *)malloc((size_t)
125 sizeof(struct utsname))) == NULL) {
126 tst_resm(TFAIL, "malloc failed for buf");
Garrett Cooper2c282152010-12-16 00:55:50 -0800127 tst_exit();
robbiew9a15e2c2005-05-26 19:15:25 +0000128 }
129
subrata_modak56207ce2009-03-23 13:35:39 +0000130 if (uname(buf) < 0) {
131 tst_resm(TFAIL, "uname failed getting release number");
robbiew9a15e2c2005-05-26 19:15:25 +0000132 }
133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 if ((compare(ref_release, buf->release)) <= 0) {
135 tst_resm(TCONF,
136 "In Linux 2.6.8 and earlier this test will not run.");
137 tst_exit();
138 }
139
robbiew9a15e2c2005-05-26 19:15:25 +0000140 setup();
141
142 /* check looping state */
143 for (lc = 0; TEST_LOOPING(lc); lc++) {
144
Caspar Zhangd59a6592013-03-07 14:59:12 +0800145 tst_count = 0;
robbiew9a15e2c2005-05-26 19:15:25 +0000146
147 for (i = 0; i < TST_TOTAL; i++) {
148
149 if (setup_test(i)) {
150 tst_resm(TFAIL, "mlockall() Failed while setup "
subrata_modak56207ce2009-03-23 13:35:39 +0000151 "for checking error %s", TC[i].edesc);
robbiew9a15e2c2005-05-26 19:15:25 +0000152 continue;
153 }
154
155 TEST(mlockall(TC[i].flag));
156
157 /* check return code */
158 if (TEST_RETURN == -1) {
159 TEST_ERROR_LOG(TEST_ERRNO);
160 if (TEST_ERRNO != TC[i].error)
161 tst_brkm(TFAIL, cleanup,
162 "mlockall() Failed with wrong "
163 "errno, expected errno=%s, "
164 "got errno=%d : %s",
165 TC[i].edesc, TEST_ERRNO,
166 strerror(TEST_ERRNO));
167 else
168 tst_resm(TPASS,
169 "expected failure - errno "
170 "= %d : %s",
171 TEST_ERRNO,
172 strerror(TEST_ERRNO));
173 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000174 tst_brkm(TFAIL, cleanup,
175 "mlockall() Failed, expected "
subrata_modak923b23f2009-11-02 13:57:16 +0000176 "return value=-1, got %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000177 TEST_RETURN);
robbiew9a15e2c2005-05-26 19:15:25 +0000178 }
179 cleanup_test(i);
180 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800181 }
robbiew9a15e2c2005-05-26 19:15:25 +0000182
183 /* cleanup and exit */
184 cleanup();
185
Garrett Cooper2c282152010-12-16 00:55:50 -0800186 tst_exit();
187}
robbiew9a15e2c2005-05-26 19:15:25 +0000188
189/*
190 * setup() - performs all ONE TIME setup for this test.
191 */
192void setup()
193{
Garrett Cooper2c282152010-12-16 00:55:50 -0800194
robbiew9a15e2c2005-05-26 19:15:25 +0000195 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000196
robbiew9a15e2c2005-05-26 19:15:25 +0000197 /* set the expected errnos... */
198 TEST_EXP_ENOS(exp_enos);
199
200 TEST_PAUSE;
201
202 return;
203}
204
subrata_modak4bb656a2009-02-26 12:02:09 +0000205int compare(char s1[], char s2[])
robbiew9a15e2c2005-05-26 19:15:25 +0000206{
subrata_modak56207ce2009-03-23 13:35:39 +0000207 int i = 0;
208 while (s1[i] == s2[i] && s1[i])
209 i++;
robbiew9a15e2c2005-05-26 19:15:25 +0000210
subrata_modak56207ce2009-03-23 13:35:39 +0000211 if (i < 4)
212 return s2[i] - s1[i];
213 if ((i == 4) && (isalnum(s2[i + 1]))) {
214 return 1;
215 } else {
robbiew9a15e2c2005-05-26 19:15:25 +0000216 /* it is not an alphanumeric character */
subrata_modak56207ce2009-03-23 13:35:39 +0000217 return s2[i] - s1[i];
218 }
robbiew9a15e2c2005-05-26 19:15:25 +0000219 return 0;
220}
221
222int setup_test(int i)
223{
224 struct rlimit rl;
225 char nobody_uid[] = "nobody";
226 struct passwd *ltpuser;
227
subrata_modak56207ce2009-03-23 13:35:39 +0000228 switch (i) {
229 case 0:
230 ltpuser = getpwnam(nobody_uid);
231 if (seteuid(ltpuser->pw_uid) == -1) {
232 tst_brkm(TBROK, cleanup, "seteuid() failed to "
233 "change euid to %d errno = %d : %s",
234 ltpuser->pw_uid, TEST_ERRNO,
235 strerror(TEST_ERRNO));
236 return 1;
237 }
robbiew9a15e2c2005-05-26 19:15:25 +0000238
subrata_modak56207ce2009-03-23 13:35:39 +0000239 rl.rlim_max = 10;
240 rl.rlim_cur = 7;
robbiew9a15e2c2005-05-26 19:15:25 +0000241
subrata_modak56207ce2009-03-23 13:35:39 +0000242 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
243 tst_resm(TWARN, "setrlimit failed to set the "
244 "resource for RLIMIT_MEMLOCK to check "
245 "for mlockall() error %s\n", TC[i].edesc);
246 return 1;
247 }
248 return 0;
249 case 1:
250 rl.rlim_max = 0;
251 rl.rlim_cur = 0;
robbiew9a15e2c2005-05-26 19:15:25 +0000252
subrata_modak56207ce2009-03-23 13:35:39 +0000253 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
254 tst_resm(TWARN, "setrlimit failed to set the "
255 "resource for RLIMIT_MEMLOCK to check "
256 "for mlockall() error %s\n", TC[i].edesc);
257 return 1;
258 }
robbiew9a15e2c2005-05-26 19:15:25 +0000259
subrata_modak56207ce2009-03-23 13:35:39 +0000260 ltpuser = getpwnam(nobody_uid);
261 if (seteuid(ltpuser->pw_uid) == -1) {
262 tst_brkm(TBROK, cleanup, "seteuid() failed to "
263 "change euid to %d errno = %d : %s",
264 ltpuser->pw_uid, TEST_ERRNO,
265 strerror(TEST_ERRNO));
266 return 1;
267 }
robbiew9a15e2c2005-05-26 19:15:25 +0000268
subrata_modak56207ce2009-03-23 13:35:39 +0000269 return 0;
robbiew9a15e2c2005-05-26 19:15:25 +0000270 }
271 return 0;
272}
273
274void cleanup_test(int i)
275{
276 struct rlimit rl;
277
subrata_modak56207ce2009-03-23 13:35:39 +0000278 switch (i) {
279 case 0:
280 if (seteuid(0) == -1) {
281 tst_brkm(TBROK, cleanup, "seteuid() failed to "
282 "change euid to %d errno = %d : %s",
283 0, TEST_ERRNO, strerror(TEST_ERRNO));
284 }
robbiew9a15e2c2005-05-26 19:15:25 +0000285
subrata_modak56207ce2009-03-23 13:35:39 +0000286 rl.rlim_max = -1;
287 rl.rlim_cur = -1;
robbiew9a15e2c2005-05-26 19:15:25 +0000288
subrata_modak56207ce2009-03-23 13:35:39 +0000289 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
290 tst_brkm(TFAIL, cleanup,
291 "setrlimit failed to reset the "
292 "resource for RLIMIT_MEMLOCK while "
293 "checking for mlockall() error %s\n",
294 TC[i].edesc);
295 }
robbiew9a15e2c2005-05-26 19:15:25 +0000296
subrata_modak56207ce2009-03-23 13:35:39 +0000297 return;
robbiew9a15e2c2005-05-26 19:15:25 +0000298
subrata_modak56207ce2009-03-23 13:35:39 +0000299 case 1:
300 if (seteuid(0) == -1) {
301 tst_brkm(TBROK, cleanup, "seteuid() failed to "
302 "change euid to %d errno = %d : %s",
303 0, TEST_ERRNO, strerror(TEST_ERRNO));
304 }
305 return;
robbiew9a15e2c2005-05-26 19:15:25 +0000306
307 }
308}
309
robbiewd34d5812005-07-11 22:28:09 +0000310#else
311
312int main()
313{
vapier81a63072006-02-27 04:29:21 +0000314 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800315 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000316}
317
robbiew960b6af2005-08-02 17:28:50 +0000318#endif /* if !defined(UCLINUX) */
robbiewd34d5812005-07-11 22:28:09 +0000319
robbiew9a15e2c2005-05-26 19:15:25 +0000320/*
321 * cleanup() - performs all ONE TIME cleanup for this test at
322 * completion or premature exit.
323 */
324void cleanup()
325{
326 TEST_CLEANUP;
327
robbiew9a15e2c2005-05-26 19:15:25 +0000328 return;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700329}