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