msync01.c revision c57fba5535abf457e33dd7a986b6c512d95cdef6
1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2865695bbc89088b9526ea9045410e5afb70a985cplars *
3865695bbc89088b9526ea9045410e5afb70a985cplars *   Copyright (c) International Business Machines  Corp., 2001
4865695bbc89088b9526ea9045410e5afb70a985cplars *
5865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is free software;  you can redistribute it and/or modify
6865695bbc89088b9526ea9045410e5afb70a985cplars *   it under the terms of the GNU General Public License as published by
7865695bbc89088b9526ea9045410e5afb70a985cplars *   the Free Software Foundation; either version 2 of the License, or
8865695bbc89088b9526ea9045410e5afb70a985cplars *   (at your option) any later version.
9865695bbc89088b9526ea9045410e5afb70a985cplars *
10865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is distributed in the hope that it will be useful,
11865695bbc89088b9526ea9045410e5afb70a985cplars *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12865695bbc89088b9526ea9045410e5afb70a985cplars *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13865695bbc89088b9526ea9045410e5afb70a985cplars *   the GNU General Public License for more details.
14865695bbc89088b9526ea9045410e5afb70a985cplars *
15865695bbc89088b9526ea9045410e5afb70a985cplars *   You should have received a copy of the GNU General Public License
16865695bbc89088b9526ea9045410e5afb70a985cplars *   along with this program;  if not, write to the Free Software
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18865695bbc89088b9526ea9045410e5afb70a985cplars */
19865695bbc89088b9526ea9045410e5afb70a985cplars
20865695bbc89088b9526ea9045410e5afb70a985cplars/*
21865695bbc89088b9526ea9045410e5afb70a985cplars * Test Name: msync01
22865695bbc89088b9526ea9045410e5afb70a985cplars *
23865695bbc89088b9526ea9045410e5afb70a985cplars * Test Description:
24865695bbc89088b9526ea9045410e5afb70a985cplars *  Verify that, msync() succeeds, when the region to synchronize, is part
25865695bbc89088b9526ea9045410e5afb70a985cplars *  of, or all of a mapped region.
26865695bbc89088b9526ea9045410e5afb70a985cplars *
27865695bbc89088b9526ea9045410e5afb70a985cplars * Expected Result:
28865695bbc89088b9526ea9045410e5afb70a985cplars *  msync() should succeed with a return value of 0, and succesfully
29865695bbc89088b9526ea9045410e5afb70a985cplars *  synchronize the memory region.  Data read from mapped region should be
30865695bbc89088b9526ea9045410e5afb70a985cplars *  the same as the initialized data.
31865695bbc89088b9526ea9045410e5afb70a985cplars *
32865695bbc89088b9526ea9045410e5afb70a985cplars * Algorithm:
33865695bbc89088b9526ea9045410e5afb70a985cplars *  Setup:
34865695bbc89088b9526ea9045410e5afb70a985cplars *   Setup signal handling.
35865695bbc89088b9526ea9045410e5afb70a985cplars *   Create temporary directory.
36865695bbc89088b9526ea9045410e5afb70a985cplars *   Pause for SIGUSR1 if option specified.
37865695bbc89088b9526ea9045410e5afb70a985cplars *
38865695bbc89088b9526ea9045410e5afb70a985cplars *  Test:
39865695bbc89088b9526ea9045410e5afb70a985cplars *   Loop if the proper options are given.
40865695bbc89088b9526ea9045410e5afb70a985cplars *   Execute system call
41865695bbc89088b9526ea9045410e5afb70a985cplars *   Check return code, if system call failed (return=-1)
4256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Log the errno and Issue a FAIL message.
43865695bbc89088b9526ea9045410e5afb70a985cplars *   Otherwise,
4456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Verify the Functionality of system call
45865695bbc89088b9526ea9045410e5afb70a985cplars *      if successful,
4656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *		Issue Functionality-Pass message.
47865695bbc89088b9526ea9045410e5afb70a985cplars *      Otherwise,
48865695bbc89088b9526ea9045410e5afb70a985cplars *		Issue Functionality-Fail message.
49865695bbc89088b9526ea9045410e5afb70a985cplars *  Cleanup:
50865695bbc89088b9526ea9045410e5afb70a985cplars *   Print errno log and/or timing stats if options given
51865695bbc89088b9526ea9045410e5afb70a985cplars *   Delete the temporary directory created.
52865695bbc89088b9526ea9045410e5afb70a985cplars *
53865695bbc89088b9526ea9045410e5afb70a985cplars * Usage:  <for command-line>
54865695bbc89088b9526ea9045410e5afb70a985cplars *  msync01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55865695bbc89088b9526ea9045410e5afb70a985cplars *     where,  -c n : Run n copies concurrently.
56865695bbc89088b9526ea9045410e5afb70a985cplars *             -f   : Turn off functionality Testing.
57865695bbc89088b9526ea9045410e5afb70a985cplars *	       -i n : Execute test n times.
58865695bbc89088b9526ea9045410e5afb70a985cplars *	       -I x : Execute test for x seconds.
59865695bbc89088b9526ea9045410e5afb70a985cplars *	       -P x : Pause for x seconds between iterations.
60865695bbc89088b9526ea9045410e5afb70a985cplars *	       -t   : Turn on syscall timing.
61865695bbc89088b9526ea9045410e5afb70a985cplars *
62865695bbc89088b9526ea9045410e5afb70a985cplars * HISTORY
63865695bbc89088b9526ea9045410e5afb70a985cplars *	07/2001 Ported by Wayne Boyer
64865695bbc89088b9526ea9045410e5afb70a985cplars *
65865695bbc89088b9526ea9045410e5afb70a985cplars * RESTRICTIONS:
66865695bbc89088b9526ea9045410e5afb70a985cplars *  None.
67865695bbc89088b9526ea9045410e5afb70a985cplars */
68865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
69865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h>
70865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h>
71865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/mman.h>
72865695bbc89088b9526ea9045410e5afb70a985cplars
73865695bbc89088b9526ea9045410e5afb70a985cplars#include "test.h"
74865695bbc89088b9526ea9045410e5afb70a985cplars#include "usctest.h"
75865695bbc89088b9526ea9045410e5afb70a985cplars
76865695bbc89088b9526ea9045410e5afb70a985cplars#define TEMPFILE	"msync_file"
77865695bbc89088b9526ea9045410e5afb70a985cplars#define BUF_SIZE	256
78865695bbc89088b9526ea9045410e5afb70a985cplars
79fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "msync01";
80fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1;
81865695bbc89088b9526ea9045410e5afb70a985cplars
82865695bbc89088b9526ea9045410e5afb70a985cplarschar *addr;			/* addr of memory mapped region */
83865695bbc89088b9526ea9045410e5afb70a985cplarssize_t page_sz;			/* system page size */
84865695bbc89088b9526ea9045410e5afb70a985cplarsint fildes;			/* file descriptor for tempfile */
85865695bbc89088b9526ea9045410e5afb70a985cplarschar write_buf[BUF_SIZE];	/* buffer to hold data to be written */
86865695bbc89088b9526ea9045410e5afb70a985cplars
87865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup();			/* Main setup function of test */
88865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup();			/* cleanup function for the test */
89865695bbc89088b9526ea9045410e5afb70a985cplars
9056207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
91865695bbc89088b9526ea9045410e5afb70a985cplars{
9289af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
9389af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	char *msg;
94865695bbc89088b9526ea9045410e5afb70a985cplars	char read_buf[BUF_SIZE];	/* buffer to hold data read from file */
95865695bbc89088b9526ea9045410e5afb70a985cplars	int nread = 0, count, err_flg = 0;
96bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
977d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
9860fa8014af7534eaefa901200c8df4b74ce422e6Garrett Cooper		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
99865695bbc89088b9526ea9045410e5afb70a985cplars
100865695bbc89088b9526ea9045410e5afb70a985cplars	for (lc = 0; TEST_LOOPING(lc); lc++) {
101865695bbc89088b9526ea9045410e5afb70a985cplars
102d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
103865695bbc89088b9526ea9045410e5afb70a985cplars
104865695bbc89088b9526ea9045410e5afb70a985cplars		setup();
105865695bbc89088b9526ea9045410e5afb70a985cplars
106865695bbc89088b9526ea9045410e5afb70a985cplars		TEST(msync(addr, page_sz, MS_ASYNC));
107865695bbc89088b9526ea9045410e5afb70a985cplars
108865695bbc89088b9526ea9045410e5afb70a985cplars		if (TEST_RETURN == -1) {
109354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TFAIL | TERRNO, "msync failed");
110865695bbc89088b9526ea9045410e5afb70a985cplars			continue;
111865695bbc89088b9526ea9045410e5afb70a985cplars		}
112865695bbc89088b9526ea9045410e5afb70a985cplars
113865695bbc89088b9526ea9045410e5afb70a985cplars		if (STD_FUNCTIONAL_TEST) {
11456abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper			if (lseek(fildes, (off_t) 100, SEEK_SET) != 100)
115354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				tst_brkm(TBROK | TERRNO, cleanup,
116354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "lseek failed");
117865695bbc89088b9526ea9045410e5afb70a985cplars
118865695bbc89088b9526ea9045410e5afb70a985cplars			/*
119865695bbc89088b9526ea9045410e5afb70a985cplars			 * Seeking to specified offset. successful.
120865695bbc89088b9526ea9045410e5afb70a985cplars			 * Now, read the data (256 bytes) and compare
121865695bbc89088b9526ea9045410e5afb70a985cplars			 * them with the expected.
122865695bbc89088b9526ea9045410e5afb70a985cplars			 */
123865695bbc89088b9526ea9045410e5afb70a985cplars			nread = read(fildes, read_buf, sizeof(read_buf));
12456abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper			if (nread != BUF_SIZE)
12556abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper				tst_brkm(TBROK, cleanup, "read failed");
12656abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper			else {
127865695bbc89088b9526ea9045410e5afb70a985cplars				/*
128865695bbc89088b9526ea9045410e5afb70a985cplars				 * Check whether read data (from mapped
129865695bbc89088b9526ea9045410e5afb70a985cplars				 * file) contains the expected data
130865695bbc89088b9526ea9045410e5afb70a985cplars				 * which was initialised in the setup.
131865695bbc89088b9526ea9045410e5afb70a985cplars				 */
13256abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper				for (count = 0; count < nread; count++)
13356abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper					if (read_buf[count] != 1)
134865695bbc89088b9526ea9045410e5afb70a985cplars						err_flg++;
135865695bbc89088b9526ea9045410e5afb70a985cplars			}
136865695bbc89088b9526ea9045410e5afb70a985cplars
13756abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper			if (err_flg != 0)
138865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TFAIL,
139865695bbc89088b9526ea9045410e5afb70a985cplars					 "data read from file doesn't match");
14056abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper			else
141865695bbc89088b9526ea9045410e5afb70a985cplars				tst_resm(TPASS,
142865695bbc89088b9526ea9045410e5afb70a985cplars					 "Functionality of msync() successful");
14356abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper		} else
144865695bbc89088b9526ea9045410e5afb70a985cplars			tst_resm(TPASS, "call succeeded");
145865695bbc89088b9526ea9045410e5afb70a985cplars
146865695bbc89088b9526ea9045410e5afb70a985cplars		cleanup();
147865695bbc89088b9526ea9045410e5afb70a985cplars
1482c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
14956abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper	tst_exit();
1502c28215423293e443469a07ae7011135d058b671Garrett Cooper}
151865695bbc89088b9526ea9045410e5afb70a985cplars
152865695bbc89088b9526ea9045410e5afb70a985cplars/*
153865695bbc89088b9526ea9045410e5afb70a985cplars * setup() - performs all ONE TIME setup for this test.
154865695bbc89088b9526ea9045410e5afb70a985cplars *
155865695bbc89088b9526ea9045410e5afb70a985cplars * Get system page size,
156865695bbc89088b9526ea9045410e5afb70a985cplars * Creat a temporary directory and a file under it used for mapping.
157865695bbc89088b9526ea9045410e5afb70a985cplars * Write 1 page size char data into file.
158865695bbc89088b9526ea9045410e5afb70a985cplars * Initialize paged region (256 bytes) from the specified offset pos.
159865695bbc89088b9526ea9045410e5afb70a985cplars */
160c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid setup(void)
161865695bbc89088b9526ea9045410e5afb70a985cplars{
162865695bbc89088b9526ea9045410e5afb70a985cplars	int c_total = 0, nwrite = 0;	/* no. of bytes to be written */
163865695bbc89088b9526ea9045410e5afb70a985cplars
164865695bbc89088b9526ea9045410e5afb70a985cplars	tst_sig(NOFORK, DEF_HANDLER, cleanup);
165865695bbc89088b9526ea9045410e5afb70a985cplars
166865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
167865695bbc89088b9526ea9045410e5afb70a985cplars
16856abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper	if ((page_sz = getpagesize()) == -1)
169354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed");
170865695bbc89088b9526ea9045410e5afb70a985cplars
17156abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper	tst_tmpdir();
172865695bbc89088b9526ea9045410e5afb70a985cplars
173354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
174354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
175865695bbc89088b9526ea9045410e5afb70a985cplars
176865695bbc89088b9526ea9045410e5afb70a985cplars	while (c_total < page_sz) {
177865695bbc89088b9526ea9045410e5afb70a985cplars		nwrite = write(fildes, write_buf, sizeof(write_buf));
17856abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper		if (nwrite <= 0)
179354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
18056abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper		else
181865695bbc89088b9526ea9045410e5afb70a985cplars			c_total += nwrite;
182865695bbc89088b9526ea9045410e5afb70a985cplars	}
183bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
184865695bbc89088b9526ea9045410e5afb70a985cplars	/*
185865695bbc89088b9526ea9045410e5afb70a985cplars	 * Call mmap to map virtual memory (mul. of page size bytes) from the
186865695bbc89088b9526ea9045410e5afb70a985cplars	 * beginning of temporary file (offset is 0) into memory.
187865695bbc89088b9526ea9045410e5afb70a985cplars	 */
188354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
189865695bbc89088b9526ea9045410e5afb70a985cplars		    fildes, 0);
190865695bbc89088b9526ea9045410e5afb70a985cplars
191865695bbc89088b9526ea9045410e5afb70a985cplars	/* Check for the return value of mmap() */
19256abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper	if (addr == MAP_FAILED)
193354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
194865695bbc89088b9526ea9045410e5afb70a985cplars
195865695bbc89088b9526ea9045410e5afb70a985cplars	/* Set 256 bytes, at 100 byte offset in the mapped region */
196865695bbc89088b9526ea9045410e5afb70a985cplars	memset(addr + 100, 1, 256);
197865695bbc89088b9526ea9045410e5afb70a985cplars}
198865695bbc89088b9526ea9045410e5afb70a985cplars
199c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid cleanup(void)
200865695bbc89088b9526ea9045410e5afb70a985cplars{
201865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_CLEANUP;
202865695bbc89088b9526ea9045410e5afb70a985cplars
20356abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper	if (munmap(addr, page_sz) == -1)
204354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "munmap failed");
205865695bbc89088b9526ea9045410e5afb70a985cplars
20656abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper	if (close(fildes) == -1)
207354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TWARN | TERRNO, "close failed");
208865695bbc89088b9526ea9045410e5afb70a985cplars
209865695bbc89088b9526ea9045410e5afb70a985cplars	tst_rmdir();
210ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
211