blob: 9b382206b22111d18afeb153b4102bbc3c5e9d82 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * Test Name: msync01
22 *
23 * Test Description:
24 * Verify that, msync() succeeds, when the region to synchronize, is part
25 * of, or all of a mapped region.
26 *
27 * Expected Result:
28 * msync() should succeed with a return value of 0, and succesfully
29 * synchronize the memory region. Data read from mapped region should be
30 * the same as the initialized data.
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)
subrata_modak56207ce2009-03-23 13:35:39 +000042 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000043 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000046 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000047 * Otherwise,
48 * Issue Functionality-Fail message.
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory created.
52 *
53 * Usage: <for command-line>
54 * msync01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55 * where, -c n : Run n copies concurrently.
56 * -f : Turn off functionality Testing.
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 * RESTRICTIONS:
66 * None.
67 */
68#include <errno.h>
69#include <unistd.h>
70#include <fcntl.h>
71#include <sys/mman.h>
72
73#include "test.h"
74#include "usctest.h"
75
76#define TEMPFILE "msync_file"
77#define BUF_SIZE 256
78
subrata_modak56207ce2009-03-23 13:35:39 +000079char *TCID = "msync01"; /* Test program identifier. */
80int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +000081extern int Tst_count; /* Test Case counter for tst_* routines */
82
83char *addr; /* addr of memory mapped region */
84size_t page_sz; /* system page size */
85int fildes; /* file descriptor for tempfile */
86char write_buf[BUF_SIZE]; /* buffer to hold data to be written */
87
88void setup(); /* Main setup function of test */
89void cleanup(); /* cleanup function for the test */
90
subrata_modak56207ce2009-03-23 13:35:39 +000091int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000092{
subrata_modak56207ce2009-03-23 13:35:39 +000093 int lc; /* loop counter */
94 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000095 char read_buf[BUF_SIZE]; /* buffer to hold data read from file */
96 int nread = 0, count, err_flg = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000097
plars865695b2001-08-27 22:15:12 +000098 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -080099 msg = parse_opts(ac, av, NULL, NULL);
100 if (msg != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000102 }
103
104 /* Check looping state if -i option given */
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106
107 /* Reset Tst_count in case we are looping. */
subrata_modak56207ce2009-03-23 13:35:39 +0000108 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000109
110 /* Perform global setup for test */
111 setup();
112
subrata_modak4bb656a2009-02-26 12:02:09 +0000113 /*
plars865695b2001-08-27 22:15:12 +0000114 * Call msync to synchronize the mapped region
subrata_modak56207ce2009-03-23 13:35:39 +0000115 * with the specified file.
plars865695b2001-08-27 22:15:12 +0000116 */
plars865695b2001-08-27 22:15:12 +0000117 TEST(msync(addr, page_sz, MS_ASYNC));
118
119 /* Check for the return value of msync() */
120 if (TEST_RETURN == -1) {
121 tst_resm(TFAIL, "msync() failed to synchronize mapped "
122 "file %s, errno=%d : %s",
123 TEMPFILE, errno, strerror(errno));
124 continue;
125 }
126
127 /*
128 * Perform functional verification if test
129 * executed without (-f) option.
130 */
131 if (STD_FUNCTIONAL_TEST) {
132 /*
133 * Seek to the offset pos. where bytes were
134 * set in the setup.
135 */
subrata_modak56207ce2009-03-23 13:35:39 +0000136 if (lseek(fildes, (off_t) 100, SEEK_SET) != (off_t) 100) {
plars865695b2001-08-27 22:15:12 +0000137 tst_brkm(TBROK, cleanup, "lseek() to specified "
138 "offset pos. Failed, error=%d : %s",
139 errno, strerror(errno));
140 tst_exit();
141 }
142
143 /*
144 * Seeking to specified offset. successful.
145 * Now, read the data (256 bytes) and compare
146 * them with the expected.
147 */
148 nread = read(fildes, read_buf, sizeof(read_buf));
149 if (nread != BUF_SIZE) {
150 tst_brkm(TBROK, cleanup, "read() on %s Failed, "
151 "error : %d", TEMPFILE, errno);
152 tst_exit();
153 } else {
154 /*
155 * Check whether read data (from mapped
156 * file) contains the expected data
157 * which was initialised in the setup.
158 */
159 for (count = 0; count < nread; count++) {
160 if (read_buf[count] != 1) {
161 /* invalid data */
162 err_flg++;
163 }
164 }
165 }
166
167 if (err_flg != 0) {
168 tst_resm(TFAIL,
169 "data read from file doesn't match");
170 } else {
171 tst_resm(TPASS,
172 "Functionality of msync() successful");
173 }
174 } else {
175 tst_resm(TPASS, "call succeeded");
176 }
177
178 /* Call cleanup() to undo setup done for the test. */
179 cleanup();
180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 } /* End for TEST_LOOPING */
plars865695b2001-08-27 22:15:12 +0000182
183 /* exit with return code appropriate for results */
184 tst_exit();
185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 /*NOTREACHED*/ return 0;
robbiew09830082003-03-26 23:18:32 +0000187
subrata_modak56207ce2009-03-23 13:35:39 +0000188} /* End main */
plars865695b2001-08-27 22:15:12 +0000189
190/*
191 * setup() - performs all ONE TIME setup for this test.
192 *
193 * Get system page size,
194 * Creat a temporary directory and a file under it used for mapping.
195 * Write 1 page size char data into file.
196 * Initialize paged region (256 bytes) from the specified offset pos.
197 */
subrata_modak56207ce2009-03-23 13:35:39 +0000198void setup()
plars865695b2001-08-27 22:15:12 +0000199{
200 int c_total = 0, nwrite = 0; /* no. of bytes to be written */
201
202 /* capture signals */
203 tst_sig(NOFORK, DEF_HANDLER, cleanup);
204
205 /* Pause if that option was specified */
206 TEST_PAUSE;
207
208 /* make a temp directory and cd to it */
209 tst_tmpdir();
210
211 /* Get the system page size */
212 if ((page_sz = getpagesize()) < 0) {
213 tst_brkm(TBROK, cleanup,
214 "getpagesize() fails to get system page size");
215 tst_exit();
216 }
217
218 /* Creat a temporary file used for mapping */
219 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
220 tst_brkm(TBROK, cleanup, "open() on %s failed, errno=%d : %s",
221 TEMPFILE, errno, strerror(errno));
222 tst_exit();
223 }
224
225 /* Write one page size of char data into temporary file */
226 while (c_total < page_sz) {
227 nwrite = write(fildes, write_buf, sizeof(write_buf));
228 if (nwrite <= 0) {
229 tst_brkm(TBROK, cleanup, "write() on %s failed, errno "
subrata_modak56207ce2009-03-23 13:35:39 +0000230 " = %d : %s", TEMPFILE, errno,
231 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000232 tst_exit();
233 } else {
234 c_total += nwrite;
235 }
236 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000237
plars865695b2001-08-27 22:15:12 +0000238 /*
239 * Call mmap to map virtual memory (mul. of page size bytes) from the
240 * beginning of temporary file (offset is 0) into memory.
241 */
242 addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
243 fildes, 0);
244
245 /* Check for the return value of mmap() */
246 if (addr == (char *)MAP_FAILED) {
247 tst_brkm(TBROK, cleanup, "mmap() failed on %s, errno=%d : %s",
248 TEMPFILE, errno, strerror(errno));
249 tst_exit();
250 }
251
252 /* Set 256 bytes, at 100 byte offset in the mapped region */
253 memset(addr + 100, 1, 256);
254}
255
256/*
257 * cleanup() - performs all ONE TIME cleanup for this test at
258 * completion or premature exit.
subrata_modak56207ce2009-03-23 13:35:39 +0000259 * Unmap the mapped memory area done in the test.
260 * Close the temporary file.
261 * Remove the temporary directory created.
plars865695b2001-08-27 22:15:12 +0000262 */
subrata_modak56207ce2009-03-23 13:35:39 +0000263void cleanup()
plars865695b2001-08-27 22:15:12 +0000264{
265 /*
266 * print timing stats if that option was specified.
267 * print errno log if that option was specified.
268 */
269 TEST_CLEANUP;
270
271 /* Unmap the mapped memory */
272 if (munmap(addr, page_sz) != 0) {
273 tst_brkm(TBROK, NULL, "munmap() failed to unmap the memory, "
274 "errno=%d", errno);
275 }
276
277 /* Close the temporary file */
278 if (close(fildes) < 0) {
279 tst_brkm(TBROK, NULL, "close() on %s failed, errno=%d : %s",
280 TEMPFILE, errno, strerror(errno));
281 }
282
283 /* Remove tmp dir and all files in it */
284 tst_rmdir();
285}