|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
1 | 2 | /* |
2 | 3 | * 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 |
13 | | - * with this program; if not, write the Free Software Foundation, Inc., |
14 | | - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
15 | | - * |
| 4 | + * AUTHOR: Nirmala Devi Dhanasekar <nirmala.devi@wipro.com> |
16 | 5 | */ |
17 | | -/************************************************************************** |
18 | | - * |
19 | | - * TEST IDENTIFIER : munlock02 |
20 | | - * |
21 | | - * EXECUTED BY : root / superuser |
22 | | - * |
23 | | - * TEST TITLE : Test for checking basic error conditions for |
24 | | - * munlock(2) |
25 | | - * |
26 | | - * TEST CASE TOTAL : 2 |
27 | | - * |
28 | | - * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com> |
29 | | - * |
30 | | - * 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 | | - * |
39 | | - * 1) ENOMEM - Some of the specified address range does not correspond to |
40 | | - * mapped pages in the address space of the process. |
41 | | - * |
42 | | - * Setup: |
43 | | - * Setup signal handling. |
44 | | - * Pause for SIGUSR1 if option specified. |
45 | | - * |
46 | | - * 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. |
53 | | - * |
54 | | - * Cleanup: |
55 | | - * Print errno log and/or timing stats if options given |
| 6 | + |
| 7 | +/*\ |
| 8 | + * [Description] |
56 | 9 | * |
57 | | - * 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. |
| 10 | + * Test for ENOMEM error. |
67 | 11 | * |
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" |
76 | | - |
77 | | -void setup(); |
78 | | -void cleanup(); |
79 | | - |
80 | | -char *TCID = "munlock02"; |
81 | | -int TST_TOTAL = 1; |
82 | | - |
83 | | -#define LEN 1024 |
84 | | - |
85 | | -void *addr1; |
| 12 | + * munlock(2) fails with ENOMEM if some of the specified address range |
| 13 | + * does not correspond to mapped pages in the address space of the |
| 14 | + * process. |
| 15 | + */ |
86 | 16 |
|
87 | | -struct test_case_t { |
88 | | - void *addr; |
89 | | - int len; |
90 | | - int error; |
91 | | - char *edesc; |
92 | | -} TC[] = { |
93 | | - { |
94 | | -NULL, 0, ENOMEM, "address range out of address space"},}; |
| 17 | +#include <sys/mman.h> |
| 18 | +#include "tst_test.h" |
95 | 19 |
|
96 | | -#if !defined(UCLINUX) |
| 20 | +static size_t len, pg_size; |
| 21 | +static void *addr; |
97 | 22 |
|
98 | | -int main(int ac, char **av) |
| 23 | +static void run(void) |
99 | 24 | { |
100 | | - int lc, i; |
101 | | - |
102 | | - tst_parse_opts(ac, av, NULL, NULL); |
103 | | - |
104 | | - setup(); |
105 | | - |
106 | | - /* check looping state */ |
107 | | - for (lc = 0; TEST_LOOPING(lc); lc++) { |
108 | | - |
109 | | - tst_count = 0; |
110 | | - for (i = 0; i < TST_TOTAL; i++) { |
111 | | -#ifdef __ia64__ |
112 | | - TC[0].len = 8 * getpagesize(); |
113 | | -#endif |
114 | | - TEST(munlock(TC[i].addr, TC[i].len)); |
115 | | - |
116 | | - /* check return code */ |
117 | | - if (TEST_RETURN == -1) { |
118 | | - if (TEST_ERRNO != TC[i].error) |
119 | | - tst_brkm(TFAIL, cleanup, |
120 | | - "munlock() Failed with wrong " |
121 | | - "errno, expected errno=%s, " |
122 | | - "got errno=%d : %s", |
123 | | - TC[i].edesc, TEST_ERRNO, |
124 | | - strerror(TEST_ERRNO)); |
125 | | - else |
126 | | - tst_resm(TPASS, |
127 | | - "expected failure - errno " |
128 | | - "= %d : %s", |
129 | | - TEST_ERRNO, |
130 | | - strerror(TEST_ERRNO)); |
131 | | - } else { |
132 | | - tst_brkm(TFAIL, cleanup, |
133 | | - "munlock() Failed, expected " |
134 | | - "return value=-1, got %ld", |
135 | | - TEST_RETURN); |
136 | | - } |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - /* cleanup and exit */ |
141 | | - cleanup(); |
142 | | - |
143 | | - tst_exit(); |
| 25 | + TST_EXP_FAIL(munlock(addr, len), ENOMEM, "munlock(%p, %lu)", |
| 26 | + addr, len); |
144 | 27 | } |
145 | 28 |
|
146 | | -/* setup() - performs all ONE TIME setup for this test. */ |
147 | | - |
148 | | -void setup(void) |
| 29 | +static void setup(void) |
149 | 30 | { |
150 | | - |
151 | | - char *address; |
152 | | - |
153 | | - tst_sig(FORK, DEF_HANDLER, cleanup); |
154 | | - |
155 | | - TC[0].len = 8 * getpagesize(); |
156 | | - address = mmap(0, TC[0].len, PROT_READ | PROT_WRITE, |
157 | | - MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
158 | | - if (address == MAP_FAILED) |
159 | | - tst_brkm(TFAIL, cleanup, "mmap_failed"); |
160 | | - memset(address, 0x20, TC[0].len); |
161 | | - TEST(mlock(address, TC[0].len)); |
162 | | - |
163 | | - /* check return code */ |
164 | | - if (TEST_RETURN == -1) { |
165 | | - tst_brkm(TFAIL | TTERRNO, cleanup, |
166 | | - "mlock(%p, %d) Failed with return=%ld", address, |
167 | | - TC[0].len, TEST_RETURN); |
168 | | - } |
169 | | - TC[0].addr = address; |
| 31 | + pg_size = getpagesize(); |
| 32 | + len = 8 * pg_size; |
| 33 | + addr = SAFE_MMAP(NULL, len, PROT_READ | PROT_WRITE, |
| 34 | + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 35 | + memset(addr, 0x20, len); |
| 36 | + SAFE_MLOCK(addr, len); |
170 | 37 | /* |
171 | 38 | * unmap part of the area, to create the condition for ENOMEM |
172 | 39 | */ |
173 | | - address += 2 * getpagesize(); |
174 | | - munmap(address, 4 * getpagesize()); |
175 | | - |
176 | | - TEST_PAUSE; |
177 | | - |
178 | | - return; |
179 | | -} |
180 | | - |
181 | | -#else |
182 | | - |
183 | | -int main(void) |
184 | | -{ |
185 | | - tst_resm(TINFO, "test is not available on uClinux"); |
186 | | - tst_exit(); |
| 40 | + addr += 2 * pg_size; |
| 41 | + SAFE_MUNMAP(addr, 4 * pg_size); |
187 | 42 | } |
188 | 43 |
|
189 | | -#endif /* if !defined(UCLINUX) */ |
190 | | - |
191 | | -/* |
192 | | - * cleanup() - performs all ONE TIME cleanup for this test at |
193 | | - * completion or premature exit. |
194 | | - */ |
195 | | -void cleanup(void) |
196 | | -{ |
197 | | - return; |
198 | | -} |
| 44 | +static struct tst_test test = { |
| 45 | + .needs_root = 1, |
| 46 | + .setup = setup, |
| 47 | + .test_all = run, |
| 48 | +}; |
0 commit comments