blob: 6bd02c69ffc5e826838778799a57e13b71ce373e [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000074
75#define TEMPFILE "msync_file"
76#define BUF_SIZE 256
77
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020078char *TCID = "msync01";
79int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000080
81char *addr; /* addr of memory mapped region */
82size_t page_sz; /* system page size */
83int fildes; /* file descriptor for tempfile */
84char write_buf[BUF_SIZE]; /* buffer to hold data to be written */
85
86void setup(); /* Main setup function of test */
87void cleanup(); /* cleanup function for the test */
88
subrata_modak56207ce2009-03-23 13:35:39 +000089int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000090{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020091 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020092 const char *msg;
plars865695b2001-08-27 22:15:12 +000093 char read_buf[BUF_SIZE]; /* buffer to hold data read from file */
94 int nread = 0, count, err_flg = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +000095
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080096 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080097 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000098
plars865695b2001-08-27 22:15:12 +000099 for (lc = 0; TEST_LOOPING(lc); lc++) {
100
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000102
plars865695b2001-08-27 22:15:12 +0000103 setup();
104
plars865695b2001-08-27 22:15:12 +0000105 TEST(msync(addr, page_sz, MS_ASYNC));
106
plars865695b2001-08-27 22:15:12 +0000107 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_resm(TFAIL | TERRNO, "msync failed");
plars865695b2001-08-27 22:15:12 +0000109 continue;
110 }
111
Cyril Hrubise38b9612014-06-02 17:20:57 +0200112 if (lseek(fildes, (off_t) 100, SEEK_SET) != 100)
113 tst_brkm(TBROK | TERRNO, cleanup,
114 "lseek failed");
plars865695b2001-08-27 22:15:12 +0000115
Cyril Hrubise38b9612014-06-02 17:20:57 +0200116 /*
117 * Seeking to specified offset. successful.
118 * Now, read the data (256 bytes) and compare
119 * them with the expected.
120 */
121 nread = read(fildes, read_buf, sizeof(read_buf));
122 if (nread != BUF_SIZE)
123 tst_brkm(TBROK, cleanup, "read failed");
124 else {
plars865695b2001-08-27 22:15:12 +0000125 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 * Check whether read data (from mapped
127 * file) contains the expected data
128 * which was initialised in the setup.
plars865695b2001-08-27 22:15:12 +0000129 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 for (count = 0; count < nread; count++)
131 if (read_buf[count] != 1)
132 err_flg++;
133 }
plars865695b2001-08-27 22:15:12 +0000134
Cyril Hrubise38b9612014-06-02 17:20:57 +0200135 if (err_flg != 0)
136 tst_resm(TFAIL,
137 "data read from file doesn't match");
138 else
139 tst_resm(TPASS,
140 "Functionality of msync() successful");
plars865695b2001-08-27 22:15:12 +0000141
plars865695b2001-08-27 22:15:12 +0000142 cleanup();
143
Garrett Cooper2c282152010-12-16 00:55:50 -0800144 }
Garrett Cooper56abfc32010-12-18 20:22:53 -0800145 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800146}
plars865695b2001-08-27 22:15:12 +0000147
148/*
149 * setup() - performs all ONE TIME setup for this test.
150 *
151 * Get system page size,
152 * Creat a temporary directory and a file under it used for mapping.
153 * Write 1 page size char data into file.
154 * Initialize paged region (256 bytes) from the specified offset pos.
155 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400156void setup(void)
plars865695b2001-08-27 22:15:12 +0000157{
158 int c_total = 0, nwrite = 0; /* no. of bytes to be written */
159
plars865695b2001-08-27 22:15:12 +0000160 tst_sig(NOFORK, DEF_HANDLER, cleanup);
161
plars865695b2001-08-27 22:15:12 +0000162 TEST_PAUSE;
163
Garrett Cooper56abfc32010-12-18 20:22:53 -0800164 if ((page_sz = getpagesize()) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed");
Garrett Cooper56abfc32010-12-18 20:22:53 -0800166
plars865695b2001-08-27 22:15:12 +0000167 tst_tmpdir();
168
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
170 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +0000171
plars865695b2001-08-27 22:15:12 +0000172 while (c_total < page_sz) {
173 nwrite = write(fildes, write_buf, sizeof(write_buf));
Garrett Cooper56abfc32010-12-18 20:22:53 -0800174 if (nwrite <= 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_brkm(TBROK | TERRNO, cleanup, "write failed");
Garrett Cooper56abfc32010-12-18 20:22:53 -0800176 else
plars865695b2001-08-27 22:15:12 +0000177 c_total += nwrite;
plars865695b2001-08-27 22:15:12 +0000178 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000179
plars865695b2001-08-27 22:15:12 +0000180 /*
181 * Call mmap to map virtual memory (mul. of page size bytes) from the
182 * beginning of temporary file (offset is 0) into memory.
183 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
plars865695b2001-08-27 22:15:12 +0000185 fildes, 0);
186
187 /* Check for the return value of mmap() */
Garrett Cooper56abfc32010-12-18 20:22:53 -0800188 if (addr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
plars865695b2001-08-27 22:15:12 +0000190
191 /* Set 256 bytes, at 100 byte offset in the mapped region */
192 memset(addr + 100, 1, 256);
193}
194
Mike Frysingerc57fba52014-04-09 18:56:30 -0400195void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000196{
Garrett Cooper56abfc32010-12-18 20:22:53 -0800197 if (munmap(addr, page_sz) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 tst_resm(TBROK | TERRNO, "munmap failed");
plars865695b2001-08-27 22:15:12 +0000199
Garrett Cooper56abfc32010-12-18 20:22:53 -0800200 if (close(fildes) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 tst_resm(TWARN | TERRNO, "close failed");
plars865695b2001-08-27 22:15:12 +0000202
plars865695b2001-08-27 22:15:12 +0000203 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700204}