blob: 5ab22b4f3841d58c6f0569269ed9158c7c8068d5 [file] [log] [blame]
robbiewd34d5812005-07-11 22:28:09 +00001/*
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
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.
robbiewd34d5812005-07-11 22:28:09 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewd34d5812005-07-11 22:28:09 +000019 * TEST IDENTIFIER : mlockall02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiewd34d5812005-07-11 22:28:09 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewd34d5812005-07-11 22:28:09 +000023 * TEST TITLE : Test for checking basic error conditions for
24 * mlockall(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiewd34d5812005-07-11 22:28:09 +000026 * TEST CASE TOTAL : 3
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiewd34d5812005-07-11 22:28:09 +000028 * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
robbiewd34d5812005-07-11 22:28:09 +000030 * 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.
subrata_modak56207ce2009-03-23 13:35:39 +000036 *$
robbiewd34d5812005-07-11 22:28:09 +000037 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000046 *
robbiewd34d5812005-07-11 22:28:09 +000047 * 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
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
robbiewd34d5812005-07-11 22:28:09 +000060 * 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"
robbiewd34d5812005-07-11 22:28:09 +000080#include <sys/resource.h>
81
82void setup();
83int setup_test(int);
84void cleanup_test(int);
85void cleanup();
86
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020087char *TCID = "mlockall02";
88int TST_TOTAL = 3;
robbiewd34d5812005-07-11 22:28:09 +000089
robbiewd34d5812005-07-11 22:28:09 +000090struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000091 int flag; /* flag value */
92 int error; /* error description */
93 char *edesc; /* Expected error no */
robbiewd34d5812005-07-11 22:28:09 +000094} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000095 {
96 MCL_CURRENT, ENOMEM, "Process exceeds max locked pages"}, {
97 MCL_CURRENT, EPERM, "Not a superuser"}, {
98 0, EINVAL, "Unknown flag"}
robbiewd34d5812005-07-11 22:28:09 +000099};
100
101#if !defined(UCLINUX)
102
103int main(int ac, char **av)
104{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200106 const char *msg;
robbiewd34d5812005-07-11 22:28:09 +0000107
Garrett Cooper45e285d2010-11-22 12:19:25 -0800108 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
robbiewd34d5812005-07-11 22:28:09 +0000109 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +0000110 }
111
robbiewd34d5812005-07-11 22:28:09 +0000112 setup();
113
robbiewd34d5812005-07-11 22:28:09 +0000114 /* check looping state */
115 for (lc = 0; TEST_LOOPING(lc); lc++) {
116
Caspar Zhangd59a6592013-03-07 14:59:12 +0800117 tst_count = 0;
robbiewd34d5812005-07-11 22:28:09 +0000118
119 for (i = 0; i < TST_TOTAL; i++) {
120
121 if (setup_test(i)) {
122 tst_resm(TFAIL, "mlockall() Failed while setup "
subrata_modak56207ce2009-03-23 13:35:39 +0000123 "for checking error %s", TC[i].edesc);
robbiewd34d5812005-07-11 22:28:09 +0000124 continue;
125 }
126
127 TEST(mlockall(TC[i].flag));
128
129 /* check return code */
130 if (TEST_RETURN == -1) {
robbiewd34d5812005-07-11 22:28:09 +0000131 if (TEST_ERRNO != TC[i].error)
132 tst_brkm(TFAIL, cleanup,
133 "mlock() Failed with wrong "
134 "errno, expected errno=%s, "
135 "got errno=%d : %s",
136 TC[i].edesc, TEST_ERRNO,
137 strerror(TEST_ERRNO));
138 else
139 tst_resm(TPASS,
140 "expected failure - errno "
141 "= %d : %s",
142 TEST_ERRNO,
143 strerror(TEST_ERRNO));
144 } else {
145 if (i <= 1)
subrata_modak56207ce2009-03-23 13:35:39 +0000146 tst_resm(TCONF,
147 "mlockall02 did not BEHAVE as expected.");
148 else
149 tst_brkm(TFAIL, cleanup,
150 "mlock() Failed, expected "
subrata_modak923b23f2009-11-02 13:57:16 +0000151 "return value=-1, got %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000152 TEST_RETURN);
robbiewd34d5812005-07-11 22:28:09 +0000153 }
154 cleanup_test(i);
155 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800156 }
robbiewd34d5812005-07-11 22:28:09 +0000157
158 /* cleanup and exit */
159 cleanup();
160
Garrett Cooper2c282152010-12-16 00:55:50 -0800161 tst_exit();
162}
robbiewd34d5812005-07-11 22:28:09 +0000163
164/*
165 * setup() - performs all ONE TIME setup for this test.
166 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400167void setup(void)
robbiewd34d5812005-07-11 22:28:09 +0000168{
Garrett Cooper2c282152010-12-16 00:55:50 -0800169
robbiewd34d5812005-07-11 22:28:09 +0000170 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000171
robbiewd34d5812005-07-11 22:28:09 +0000172 TEST_PAUSE;
173
174 return;
175}
176
177int setup_test(int i)
178{
179 struct rlimit rl;
180 char nobody_uid[] = "nobody";
181 struct passwd *ltpuser;
182
subrata_modak56207ce2009-03-23 13:35:39 +0000183 switch (i) {
184 case 0:
185 rl.rlim_max = 10;
186 rl.rlim_cur = 7;
robbiewd34d5812005-07-11 22:28:09 +0000187
subrata_modak56207ce2009-03-23 13:35:39 +0000188 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
189 tst_resm(TWARN, "setrlimit failed to set the "
190 "resource for RLIMIT_MEMLOCK to check "
191 "for mlockall error %s\n", TC[i].edesc);
192 return 1;
193 }
194 if (tst_kvercmp(2, 6, 9) >= 0) {
robbiewd34d5812005-07-11 22:28:09 +0000195 ltpuser = getpwnam(nobody_uid);
196 if (seteuid(ltpuser->pw_uid) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000197 tst_brkm(TBROK, cleanup, "seteuid() "
198 "failed to change euid to %d "
199 "errno = %d : %s",
200 ltpuser->pw_uid, TEST_ERRNO,
201 strerror(TEST_ERRNO));
robbiewd34d5812005-07-11 22:28:09 +0000202 return 1;
203 }
subrata_modak56207ce2009-03-23 13:35:39 +0000204 }
205 return 0;
206 case 1:
207 if (tst_kvercmp(2, 6, 9) >= 0) {
208 rl.rlim_max = 0;
209 rl.rlim_cur = 0;
210 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
211 tst_resm(TWARN, "setrlimit failed to "
212 "set the resource for "
213 "RLIMIT_MEMLOCK to check for "
214 "mlockall error %s\n", TC[i].edesc);
215 return 1;
216 }
217 }
218 ltpuser = getpwnam(nobody_uid);
219 if (seteuid(ltpuser->pw_uid) == -1) {
220 tst_brkm(TBROK, cleanup, "seteuid() failed to "
221 "change euid to %d errno = %d : %s",
222 ltpuser->pw_uid, TEST_ERRNO,
223 strerror(TEST_ERRNO));
224 return 1;
225 }
226 return 0;
robbiewd34d5812005-07-11 22:28:09 +0000227 }
228 return 0;
229}
230
231void cleanup_test(int i)
232{
233 struct rlimit rl;
234
subrata_modak56207ce2009-03-23 13:35:39 +0000235 switch (i) {
236 case 0:
237 if (tst_kvercmp(2, 6, 9) >= 0)
238 seteuid(0);
subrata_modak334b0432008-01-07 09:17:30 +0000239
subrata_modak56207ce2009-03-23 13:35:39 +0000240 rl.rlim_max = -1;
241 rl.rlim_cur = -1;
robbiewd34d5812005-07-11 22:28:09 +0000242
subrata_modak56207ce2009-03-23 13:35:39 +0000243 if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
244 tst_brkm(TFAIL, cleanup,
245 "setrlimit failed to reset the "
246 "resource for RLIMIT_MEMLOCK while "
247 "checking for mlockall error %s\n",
248 TC[i].edesc);
249 }
250 return;
251 case 1:
252 if (seteuid(0) == -1) {
253 tst_brkm(TBROK, cleanup, "seteuid() failed to "
254 "change euid to %d errno = %d : %s",
255 0, TEST_ERRNO, strerror(TEST_ERRNO));
256 }
257 return;
robbiewd34d5812005-07-11 22:28:09 +0000258
259 }
260}
261
robbiewd34d5812005-07-11 22:28:09 +0000262/*
263 * cleanup() - performs all ONE TIME cleanup for this test at
264 * completion or premature exit.
265 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400266void cleanup(void)
robbiewd34d5812005-07-11 22:28:09 +0000267{
robbiewd34d5812005-07-11 22:28:09 +0000268 return;
269}
270
271#else
272
Mike Frysingerc57fba52014-04-09 18:56:30 -0400273int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000274{
vapier81a63072006-02-27 04:29:21 +0000275 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800276 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000277}
278
Chris Dearmanec6edca2012-10-17 19:54:01 -0700279#endif /* if !defined(UCLINUX) */