blob: 9793f5f5590ea96c5f815da3f2a0628ba307e29a [file] [log] [blame]
robbiewf033cf92003-06-23 15:28:06 +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.
robbiewf033cf92003-06-23 15:28:06 +000015 *
16 */
17/**************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewf033cf92003-06-23 15:28:06 +000019 * TEST IDENTIFIER : munlock02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiewf033cf92003-06-23 15:28:06 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewf033cf92003-06-23 15:28:06 +000023 * TEST TITLE : Test for checking basic error conditions for
24 * munlock(2)
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
robbiewf033cf92003-06-23 15:28:06 +000026 * TEST CASE TOTAL : 2
subrata_modak4bb656a2009-02-26 12:02:09 +000027 *
robbiewf033cf92003-06-23 15:28:06 +000028 * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000029 *
robbiewf033cf92003-06-23 15:28:06 +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 munlock(2) system call.
36 *
37 * Verify that munlock(2) returns -1 and sets errno to
38 *
mridge75cee4f2004-09-08 15:31:00 +000039 * 1) ENOMEM - Some of the specified address range does not correspond to
40 * mapped pages in the address space of the process.
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
robbiewf033cf92003-06-23 15:28:06 +000042 * Setup:
43 * Setup signal handling.
44 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
robbiewf033cf92003-06-23 15:28:06 +000046 * Test:
47 * Loop if the proper options are given.
48 * Do necessary setup for each test.
49 * Execute system call
50 * Check return code, if system call failed (return=-1)
51 * Log the errno and Issue a FAIL message.
52 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
robbiewf033cf92003-06-23 15:28:06 +000054 * Cleanup:
55 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
robbiewf033cf92003-06-23 15:28:06 +000057 * USAGE: <for command-line>
58 * munlock02 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
59 * where, -c n : Run n copies concurrently
60 * -e : Turn on errno logging.
61 * -h : Show this help screen
62 * -i n : Execute test n times.
63 * -I x : Execute test for x seconds.
64 * -p : Pause for SIGUSR1 before starting
65 * -P x : Pause for x seconds between iterations.
66 * -t : Turn on syscall timing.
67 *
68 * RESTRICTIONS
69 * Test must run as root.
70 *****************************************************************************/
71#include <errno.h>
72#include <unistd.h>
73#include <sys/mman.h>
74#include <pwd.h>
75#include "test.h"
robbiewf033cf92003-06-23 15:28:06 +000076
77void setup();
78void cleanup();
79
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020080char *TCID = "munlock02";
81int TST_TOTAL = 1;
robbiewf033cf92003-06-23 15:28:06 +000082
robbiewf033cf92003-06-23 15:28:06 +000083#define LEN 1024
84
85void *addr1;
86
87struct test_case_t {
mridge75cee4f2004-09-08 15:31:00 +000088 void *addr;
robbiewf033cf92003-06-23 15:28:06 +000089 int len;
90 int error;
91 char *edesc;
92} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000093 {
94NULL, 0, ENOMEM, "address range out of address space"},};
robbiewf033cf92003-06-23 15:28:06 +000095
robbiewd34d5812005-07-11 22:28:09 +000096#if !defined(UCLINUX)
97
robbiewf033cf92003-06-23 15:28:06 +000098int main(int ac, char **av)
99{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200100 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200101 const char *msg;
robbiewf033cf92003-06-23 15:28:06 +0000102
Garrett Cooper45e285d2010-11-22 12:19:25 -0800103 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
robbiewf033cf92003-06-23 15:28:06 +0000104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewf033cf92003-06-23 15:28:06 +0000105 }
106
robbiewf033cf92003-06-23 15:28:06 +0000107 setup();
108
109 /* check looping state */
110 for (lc = 0; TEST_LOOPING(lc); lc++) {
111
Caspar Zhangd59a6592013-03-07 14:59:12 +0800112 tst_count = 0;
robbiewf033cf92003-06-23 15:28:06 +0000113 for (i = 0; i < TST_TOTAL; i++) {
114#ifdef __ia64__
subrata_modak56207ce2009-03-23 13:35:39 +0000115 TC[0].len = 8 * getpagesize();
robbiewf033cf92003-06-23 15:28:06 +0000116#endif
mridge75cee4f2004-09-08 15:31:00 +0000117 TEST(munlock(TC[i].addr, TC[i].len));
robbiewf033cf92003-06-23 15:28:06 +0000118
119 /* check return code */
120 if (TEST_RETURN == -1) {
robbiewf033cf92003-06-23 15:28:06 +0000121 if (TEST_ERRNO != TC[i].error)
122 tst_brkm(TFAIL, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000123 "munlock() Failed with wrong "
124 "errno, expected errno=%s, "
125 "got errno=%d : %s",
126 TC[i].edesc, TEST_ERRNO,
127 strerror(TEST_ERRNO));
robbiewf033cf92003-06-23 15:28:06 +0000128 else
129 tst_resm(TPASS,
subrata_modak56207ce2009-03-23 13:35:39 +0000130 "expected failure - errno "
131 "= %d : %s",
132 TEST_ERRNO,
133 strerror(TEST_ERRNO));
robbiewf033cf92003-06-23 15:28:06 +0000134 } else {
135 tst_brkm(TFAIL, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000136 "munlock() Failed, expected "
subrata_modak923b23f2009-11-02 13:57:16 +0000137 "return value=-1, got %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000138 TEST_RETURN);
robbiewf033cf92003-06-23 15:28:06 +0000139 }
140 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800141 }
robbiewf033cf92003-06-23 15:28:06 +0000142
143 /* cleanup and exit */
144 cleanup();
145
Garrett Cooper2c282152010-12-16 00:55:50 -0800146 tst_exit();
147}
robbiewf033cf92003-06-23 15:28:06 +0000148
robbiewf033cf92003-06-23 15:28:06 +0000149/* setup() - performs all ONE TIME setup for this test. */
150
Mike Frysingerc57fba52014-04-09 18:56:30 -0400151void setup(void)
robbiewf033cf92003-06-23 15:28:06 +0000152{
mridge75cee4f2004-09-08 15:31:00 +0000153
154 char *address;
155
robbiewf033cf92003-06-23 15:28:06 +0000156 tst_sig(FORK, DEF_HANDLER, cleanup);
157
mridge75cee4f2004-09-08 15:31:00 +0000158 TC[0].len = 8 * getpagesize();
subrata_modak56207ce2009-03-23 13:35:39 +0000159 address = mmap(0, TC[0].len, PROT_READ | PROT_WRITE,
160 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
mridge75cee4f2004-09-08 15:31:00 +0000161 if (address == MAP_FAILED)
subrata_modak56207ce2009-03-23 13:35:39 +0000162 tst_brkm(TFAIL, cleanup, "mmap_failed");
mridge75cee4f2004-09-08 15:31:00 +0000163 memset(address, 0x20, TC[0].len);
164 TEST(mlock(address, TC[0].len));
robbiewf033cf92003-06-23 15:28:06 +0000165
166 /* check return code */
167 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_brkm(TFAIL | TTERRNO, cleanup,
169 "mlock(%p, %d) Failed with return=%ld", address,
170 TC[0].len, TEST_RETURN);
robbiewf033cf92003-06-23 15:28:06 +0000171 }
mridge75cee4f2004-09-08 15:31:00 +0000172 TC[0].addr = address;
subrata_modak4bb656a2009-02-26 12:02:09 +0000173 /*
mridge75cee4f2004-09-08 15:31:00 +0000174 * unmap part of the area, to create the condition for ENOMEM
subrata_modak56207ce2009-03-23 13:35:39 +0000175 */
mridge75cee4f2004-09-08 15:31:00 +0000176 address += 2 * getpagesize();
177 munmap(address, 4 * getpagesize());
robbiewf033cf92003-06-23 15:28:06 +0000178
179 TEST_PAUSE;
180
181 return;
182}
183
robbiewd34d5812005-07-11 22:28:09 +0000184#else
185
Mike Frysingerc57fba52014-04-09 18:56:30 -0400186int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000187{
vapier81a63072006-02-27 04:29:21 +0000188 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800189 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000190}
191
192#endif /* if !defined(UCLINUX) */
193
robbiewf033cf92003-06-23 15:28:06 +0000194/*
195 * cleanup() - performs all ONE TIME cleanup for this test at
196 * completion or premature exit.
197 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400198void cleanup(void)
robbiewf033cf92003-06-23 15:28:06 +0000199{
robbiewf033cf92003-06-23 15:28:06 +0000200 return;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700201}