blob: b2017da770d2646f8d550ac8cf1c168c072f7585 [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"
robbiew9a15e2c2005-05-26 19:15:25 +000082#include <sys/resource.h>
83#include <sys/utsname.h>
84
85void setup();
86int setup_test(int);
87int compare(char s1[], char s2[]);
88void cleanup_test(int);
89void cleanup();
90
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091char *TCID = "mlockall03";
92int TST_TOTAL = 3;
robbiew9a15e2c2005-05-26 19:15:25 +000093
robbiewd34d5812005-07-11 22:28:09 +000094#if !defined(UCLINUX)
95
robbiew9a15e2c2005-05-26 19:15:25 +000096char *ref_release = "2.6.8\0";
robbiew9a15e2c2005-05-26 19:15:25 +000097
98struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000099 int flag; /* flag value */
100 int error; /* error description */
101 char *edesc; /* Expected error no */
robbiew9a15e2c2005-05-26 19:15:25 +0000102} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000103 {
104 MCL_CURRENT, ENOMEM,
105 "tried to lock more memory than the limit permitted"}, {
106 MCL_CURRENT, EPERM, "Not a superuser and RLIMIT_MEMLOCK was 0"}, {
107 ~(MCL_CURRENT | MCL_FUTURE), EINVAL, "Unknown flag"}
robbiew9a15e2c2005-05-26 19:15:25 +0000108};
109
110int main(int ac, char **av)
111{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200112 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200113 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000114 struct utsname *buf;
robbiew9a15e2c2005-05-26 19:15:25 +0000115
Garrett Cooper45e285d2010-11-22 12:19:25 -0800116 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
robbiew9a15e2c2005-05-26 19:15:25 +0000117 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew9a15e2c2005-05-26 19:15:25 +0000118 }
119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 /* allocate some space for buf */
Cyril Hrubisd218f342014-09-23 13:14:56 +0200121 if ((buf = malloc((size_t)sizeof(struct utsname))) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100122 tst_brkm(TFAIL, NULL, "malloc failed for buf");
robbiew9a15e2c2005-05-26 19:15:25 +0000123 }
124
subrata_modak56207ce2009-03-23 13:35:39 +0000125 if (uname(buf) < 0) {
126 tst_resm(TFAIL, "uname failed getting release number");
robbiew9a15e2c2005-05-26 19:15:25 +0000127 }
128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if ((compare(ref_release, buf->release)) <= 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100130 tst_brkm(TCONF,
131 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000132 "In Linux 2.6.8 and earlier this test will not run.");
subrata_modak56207ce2009-03-23 13:35:39 +0000133 }
134
robbiew9a15e2c2005-05-26 19:15:25 +0000135 setup();
136
137 /* check looping state */
138 for (lc = 0; TEST_LOOPING(lc); lc++) {
139
Caspar Zhangd59a6592013-03-07 14:59:12 +0800140 tst_count = 0;
robbiew9a15e2c2005-05-26 19:15:25 +0000141
142 for (i = 0; i < TST_TOTAL; i++) {
143
144 if (setup_test(i)) {
145 tst_resm(TFAIL, "mlockall() Failed while setup "
subrata_modak56207ce2009-03-23 13:35:39 +0000146 "for checking error %s", TC[i].edesc);
robbiew9a15e2c2005-05-26 19:15:25 +0000147 continue;
148 }
149
150 TEST(mlockall(TC[i].flag));
151
152 /* check return code */
153 if (TEST_RETURN == -1) {
robbiew9a15e2c2005-05-26 19:15:25 +0000154 if (TEST_ERRNO != TC[i].error)
155 tst_brkm(TFAIL, cleanup,
156 "mlockall() Failed with wrong "
157 "errno, expected errno=%s, "
158 "got errno=%d : %s",
159 TC[i].edesc, TEST_ERRNO,
160 strerror(TEST_ERRNO));
161 else
162 tst_resm(TPASS,
163 "expected failure - errno "
164 "= %d : %s",
165 TEST_ERRNO,
166 strerror(TEST_ERRNO));
167 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000168 tst_brkm(TFAIL, cleanup,
169 "mlockall() Failed, expected "
subrata_modak923b23f2009-11-02 13:57:16 +0000170 "return value=-1, got %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000171 TEST_RETURN);
robbiew9a15e2c2005-05-26 19:15:25 +0000172 }
173 cleanup_test(i);
174 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800175 }
robbiew9a15e2c2005-05-26 19:15:25 +0000176
177 /* cleanup and exit */
178 cleanup();
179
Garrett Cooper2c282152010-12-16 00:55:50 -0800180 tst_exit();
181}
robbiew9a15e2c2005-05-26 19:15:25 +0000182
183/*
184 * setup() - performs all ONE TIME setup for this test.
185 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400186void setup(void)
robbiew9a15e2c2005-05-26 19:15:25 +0000187{
Garrett Cooper2c282152010-12-16 00:55:50 -0800188
robbiew9a15e2c2005-05-26 19:15:25 +0000189 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000190
robbiew9a15e2c2005-05-26 19:15:25 +0000191 TEST_PAUSE;
192
193 return;
194}
195
subrata_modak4bb656a2009-02-26 12:02:09 +0000196int compare(char s1[], char s2[])
robbiew9a15e2c2005-05-26 19:15:25 +0000197{
subrata_modak56207ce2009-03-23 13:35:39 +0000198 int i = 0;
199 while (s1[i] == s2[i] && s1[i])
200 i++;
robbiew9a15e2c2005-05-26 19:15:25 +0000201
subrata_modak56207ce2009-03-23 13:35:39 +0000202 if (i < 4)
203 return s2[i] - s1[i];
204 if ((i == 4) && (isalnum(s2[i + 1]))) {
205 return 1;
206 } else {
robbiew9a15e2c2005-05-26 19:15:25 +0000207 /* it is not an alphanumeric character */
subrata_modak56207ce2009-03-23 13:35:39 +0000208 return s2[i] - s1[i];
209 }
robbiew9a15e2c2005-05-26 19:15:25 +0000210 return 0;
211}
212
213int setup_test(int i)
214{
215 struct rlimit rl;
216 char nobody_uid[] = "nobody";
217 struct passwd *ltpuser;
218
subrata_modak56207ce2009-03-23 13:35:39 +0000219 switch (i) {
220 case 0:
221 ltpuser = getpwnam(nobody_uid);
222 if (seteuid(ltpuser->pw_uid) == -1) {
223 tst_brkm(TBROK, cleanup, "seteuid() failed to "
224 "change euid to %d errno = %d : %s",
225 ltpuser->pw_uid, TEST_ERRNO,
226 strerror(TEST_ERRNO));
227 return 1;
228 }
robbiew9a15e2c2005-05-26 19:15:25 +0000229
subrata_modak56207ce2009-03-23 13:35:39 +0000230 rl.rlim_max = 10;
231 rl.rlim_cur = 7;
robbiew9a15e2c2005-05-26 19:15:25 +0000232
subrata_modak56207ce2009-03-23 13:35:39 +0000233 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
234 tst_resm(TWARN, "setrlimit failed to set the "
235 "resource for RLIMIT_MEMLOCK to check "
236 "for mlockall() error %s\n", TC[i].edesc);
237 return 1;
238 }
239 return 0;
240 case 1:
241 rl.rlim_max = 0;
242 rl.rlim_cur = 0;
robbiew9a15e2c2005-05-26 19:15:25 +0000243
subrata_modak56207ce2009-03-23 13:35:39 +0000244 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
245 tst_resm(TWARN, "setrlimit failed to set the "
246 "resource for RLIMIT_MEMLOCK to check "
247 "for mlockall() error %s\n", TC[i].edesc);
248 return 1;
249 }
robbiew9a15e2c2005-05-26 19:15:25 +0000250
subrata_modak56207ce2009-03-23 13:35:39 +0000251 ltpuser = getpwnam(nobody_uid);
252 if (seteuid(ltpuser->pw_uid) == -1) {
253 tst_brkm(TBROK, cleanup, "seteuid() failed to "
254 "change euid to %d errno = %d : %s",
255 ltpuser->pw_uid, TEST_ERRNO,
256 strerror(TEST_ERRNO));
257 return 1;
258 }
robbiew9a15e2c2005-05-26 19:15:25 +0000259
subrata_modak56207ce2009-03-23 13:35:39 +0000260 return 0;
robbiew9a15e2c2005-05-26 19:15:25 +0000261 }
262 return 0;
263}
264
265void cleanup_test(int i)
266{
267 struct rlimit rl;
268
subrata_modak56207ce2009-03-23 13:35:39 +0000269 switch (i) {
270 case 0:
271 if (seteuid(0) == -1) {
272 tst_brkm(TBROK, cleanup, "seteuid() failed to "
273 "change euid to %d errno = %d : %s",
274 0, TEST_ERRNO, strerror(TEST_ERRNO));
275 }
robbiew9a15e2c2005-05-26 19:15:25 +0000276
subrata_modak56207ce2009-03-23 13:35:39 +0000277 rl.rlim_max = -1;
278 rl.rlim_cur = -1;
robbiew9a15e2c2005-05-26 19:15:25 +0000279
subrata_modak56207ce2009-03-23 13:35:39 +0000280 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
281 tst_brkm(TFAIL, cleanup,
282 "setrlimit failed to reset the "
283 "resource for RLIMIT_MEMLOCK while "
284 "checking for mlockall() error %s\n",
285 TC[i].edesc);
286 }
robbiew9a15e2c2005-05-26 19:15:25 +0000287
subrata_modak56207ce2009-03-23 13:35:39 +0000288 return;
robbiew9a15e2c2005-05-26 19:15:25 +0000289
subrata_modak56207ce2009-03-23 13:35:39 +0000290 case 1:
291 if (seteuid(0) == -1) {
292 tst_brkm(TBROK, cleanup, "seteuid() failed to "
293 "change euid to %d errno = %d : %s",
294 0, TEST_ERRNO, strerror(TEST_ERRNO));
295 }
296 return;
robbiew9a15e2c2005-05-26 19:15:25 +0000297
298 }
299}
300
robbiewd34d5812005-07-11 22:28:09 +0000301#else
302
Mike Frysingerc57fba52014-04-09 18:56:30 -0400303int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000304{
vapier81a63072006-02-27 04:29:21 +0000305 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800306 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000307}
308
robbiew960b6af2005-08-02 17:28:50 +0000309#endif /* if !defined(UCLINUX) */
robbiewd34d5812005-07-11 22:28:09 +0000310
robbiew9a15e2c2005-05-26 19:15:25 +0000311/*
312 * cleanup() - performs all ONE TIME cleanup for this test at
313 * completion or premature exit.
314 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400315void cleanup(void)
robbiew9a15e2c2005-05-26 19:15:25 +0000316{
robbiew9a15e2c2005-05-26 19:15:25 +0000317 return;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700318}