1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * Test Name: mremap04
22 *
23 * Test Description:
24 * Verify that,
25 * mremap() fails when used to expand the existing virtual memory mapped
26 * region to the requested size, if the memory area cannot be expanded at
27 * the current virtual address and MREMAP_MAYMOVE flag not set.
28 *
29 * Expected Result:
30 * mremap() should return -1 and set errno to ENOMEM.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Create temporary directory.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
42 * if errno set == expected errno
43 * Issue sys call failed with expected return value and errno.
44 * Otherwise,
45 * Issue sys call failed with unexpected errno.
46 * Otherwise,
47 * Issue sys call returns unexpected value.
48 *
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory(s)/file(s) created.
52 *
53 * Usage: <for command-line>
54 * mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
55 * where, -c n : Run n copies concurrently.
56 * -e : Turn on errno logging.
57 * -i n : Execute test n times.
58 * -I x : Execute test for x seconds.
59 * -p x : Pause for x seconds between iterations.
60 * -t : Turn on syscall timing.
61 *
62 * HISTORY
63 * 07/2001 Ported by Wayne Boyer
64 *
65 * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
66 * Modified.
67 * - #include <linux/mman.h> should not be included as per man page for
68 * mremap, #include <sys/mman.h> alone should do the job. But inorder
69 * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
70 * (included by sys/mman.h) __USE_GNU needs to be defined.
71 * There may be a more elegant way of doing this...
72 *
73 * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
74 * - Fix concurrency issue. Use a shm key from getipckey instead of
75 * a fixed hard-coded value.
76 *
77 * RESTRICTIONS:
78 * None.
79 */
80 #define _GNU_SOURCE
81 #include <errno.h>
82 #include <unistd.h>
83 #include <sys/mman.h>
84 #include <sys/ipc.h>
85 #include <sys/shm.h>
86
87 #include "test.h"
88
89 #define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */
90
91 char *TCID = "mremap04";
92 int TST_TOTAL = 1;
93 char *addr; /* addr of memory mapped region */
94 char *shmaddr; /* pointer to shared memory segment */
95 int shmid; /* shared memory identifier. */
96 int memsize; /* memory mapped size */
97 int newsize; /* new size of virtual memory block */
98
99 void setup(); /* Main setup function of test */
100 void cleanup(); /* cleanup function for the test */
101
102 extern int getipckey();
103
main(int ac,char ** av)104 int main(int ac, char **av)
105 {
106 int lc;
107
108 tst_parse_opts(ac, av, NULL, NULL);
109
110 setup();
111
112 for (lc = 0; TEST_LOOPING(lc); lc++) {
113
114 tst_count = 0;
115
116 /*
117 * Attempt to expand the existing shared
118 * memory region of newsize by newsize limits
119 * using mremap() should fail as specified
120 * memory area already locked and MREMAP_MAYMOVE
121 * flag unset.
122 */
123 errno = 0;
124 addr = mremap(shmaddr, memsize, newsize, 0);
125 TEST_ERRNO = errno;
126
127 /* Check for the return value of mremap() */
128 if (addr != MAP_FAILED) {
129 tst_resm(TFAIL,
130 "mremap returned invalid value, expected: -1");
131
132 /* Unmap the mapped memory region */
133 if (munmap(addr, newsize) != 0) {
134 tst_brkm(TFAIL, cleanup, "munmap failed to "
135 "unmap the expanded memory region, "
136 "error=%d", errno);
137 }
138 continue;
139 }
140
141 if (TEST_ERRNO == ENOMEM) {
142 tst_resm(TPASS, "mremap() failed, "
143 "'MREMAP_MAYMOVE flag unset', "
144 "errno %d", TEST_ERRNO);
145 } else {
146 tst_resm(TFAIL, "mremap() failed, "
147 "Unexpected errno %d", TEST_ERRNO);
148 }
149 }
150
151 cleanup();
152 tst_exit();
153
154 }
155
156 /*
157 * setup() - performs all ONE TIME setup for this test.
158 *
159 * Get system page size, Set the size of virtual memory area and the
160 * newsize after resize,
161 * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE
162 * by using shmget() which returns a shared memory identifier associated
163 * with the created shared memory segment.
164 * Call shmat() to attach the shared memory segment to the data segment of the
165 * calling process. The segment is attached at the first available address as
166 * selected by the system.
167 */
setup(void)168 void setup(void)
169 {
170 key_t shmkey;
171
172 tst_sig(FORK, DEF_HANDLER, cleanup);
173
174 TEST_PAUSE;
175
176 tst_tmpdir();
177
178 /* Get the system page size */
179 if ((memsize = getpagesize()) < 0) {
180 tst_brkm(TBROK, NULL,
181 "getpagesize() failed to get system page size");
182 }
183
184 /* Get the New size of virtual memory block after resize */
185 newsize = (memsize * 2);
186
187 /* get an IPC resource key */
188 shmkey = getipckey();
189
190 /*
191 * Create a shared memory segment represented by SHMKEY of
192 * specified size 'newsize' and mode permissions 'SHM_MODE'.
193 */
194 shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE);
195 if (shmid == -1) {
196 tst_brkm(TBROK, NULL, "shmget() Failed to create a shared "
197 "memory, error:%d", errno);
198 }
199
200 /*
201 * Attach the shared memory segment associated with the shared
202 * memory identifier specified by "shmid" to the data segment of
203 * the calling process at the first available address as selected
204 * by the system.
205 */
206 shmaddr = shmat(shmid, NULL, 0);
207 if (shmaddr == (void *)-1) {
208 tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared "
209 "memory, error:%d", errno);
210 }
211 }
212
213 /*
214 * cleanup() - performs all ONE TIME cleanup for this test at
215 * completion or premature exit.
216 * Detach the shared memory segment and remove the shared memory
217 * identifier associated with the shared memory.
218 */
cleanup(void)219 void cleanup(void)
220 {
221
222 /*
223 * Detach the shared memory segment attached to
224 * the calling process's data segment
225 */
226 if (shmdt(shmaddr) < 0) {
227 tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared "
228 "memory, error:%d", errno);
229 }
230
231 /*
232 * Remove the shared memory identifier associated with
233 * the shared memory segment and destroy the shared memory
234 * segment.
235 */
236 if (shmctl(shmid, IPC_RMID, 0) < 0) {
237 tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared "
238 "memory, error:%d", errno);
239 }
240
241 tst_rmdir();
242
243 /* Exit the program */
244
245 }
246