xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/msync/msync01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * Test Name: msync01
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, msync() succeeds, when the region to synchronize, is part
25*49cdfc7eSAndroid Build Coastguard Worker  *  of, or all of a mapped region.
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
28*49cdfc7eSAndroid Build Coastguard Worker  *  msync() should succeed with a return value of 0, and successfully
29*49cdfc7eSAndroid Build Coastguard Worker  *  synchronize the memory region.  Data read from mapped region should be
30*49cdfc7eSAndroid Build Coastguard Worker  *  the same as the initialized data.
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
33*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
34*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
35*49cdfc7eSAndroid Build Coastguard Worker  *   Create temporary directory.
36*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
37*49cdfc7eSAndroid Build Coastguard Worker  *
38*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
39*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
40*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
41*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
42*49cdfc7eSAndroid Build Coastguard Worker  *	Log the errno and Issue a FAIL message.
43*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
44*49cdfc7eSAndroid Build Coastguard Worker  *	Verify the Functionality of system call
45*49cdfc7eSAndroid Build Coastguard Worker  *      if successful,
46*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Pass message.
47*49cdfc7eSAndroid Build Coastguard Worker  *      Otherwise,
48*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Fail message.
49*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
50*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
51*49cdfc7eSAndroid Build Coastguard Worker  *   Delete the temporary directory created.
52*49cdfc7eSAndroid Build Coastguard Worker  *
53*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
54*49cdfc7eSAndroid Build Coastguard Worker  *  msync01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
55*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
56*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
57*49cdfc7eSAndroid Build Coastguard Worker  *	       -i n : Execute test n times.
58*49cdfc7eSAndroid Build Coastguard Worker  *	       -I x : Execute test for x seconds.
59*49cdfc7eSAndroid Build Coastguard Worker  *	       -P x : Pause for x seconds between iterations.
60*49cdfc7eSAndroid Build Coastguard Worker  *	       -t   : Turn on syscall timing.
61*49cdfc7eSAndroid Build Coastguard Worker  *
62*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
63*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
64*49cdfc7eSAndroid Build Coastguard Worker  *
65*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS:
66*49cdfc7eSAndroid Build Coastguard Worker  *  None.
67*49cdfc7eSAndroid Build Coastguard Worker  */
68*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
69*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
70*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
71*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE	"msync_file"
76*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE	256
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "msync01";
79*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker char *addr;			/* addr of memory mapped region */
82*49cdfc7eSAndroid Build Coastguard Worker size_t page_sz;			/* system page size */
83*49cdfc7eSAndroid Build Coastguard Worker int fildes;			/* file descriptor for tempfile */
84*49cdfc7eSAndroid Build Coastguard Worker char write_buf[BUF_SIZE];	/* buffer to hold data to be written */
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker void setup();			/* Main setup function of test */
87*49cdfc7eSAndroid Build Coastguard Worker void cleanup();			/* cleanup function for the test */
88*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)89*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
92*49cdfc7eSAndroid Build Coastguard Worker 	char read_buf[BUF_SIZE];	/* buffer to hold data read from file */
93*49cdfc7eSAndroid Build Coastguard Worker 	int nread = 0, count, err_flg = 0;
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 		setup();
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		TEST(msync(addr, page_sz, MS_ASYNC));
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
106*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO, "msync failed");
107*49cdfc7eSAndroid Build Coastguard Worker 			continue;
108*49cdfc7eSAndroid Build Coastguard Worker 		}
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 		if (lseek(fildes, (off_t) 100, SEEK_SET) != 100)
111*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup,
112*49cdfc7eSAndroid Build Coastguard Worker 				 "lseek failed");
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 		/*
115*49cdfc7eSAndroid Build Coastguard Worker 		 * Seeking to specified offset. successful.
116*49cdfc7eSAndroid Build Coastguard Worker 		 * Now, read the data (256 bytes) and compare
117*49cdfc7eSAndroid Build Coastguard Worker 		 * them with the expected.
118*49cdfc7eSAndroid Build Coastguard Worker 		 */
119*49cdfc7eSAndroid Build Coastguard Worker 		nread = read(fildes, read_buf, sizeof(read_buf));
120*49cdfc7eSAndroid Build Coastguard Worker 		if (nread != BUF_SIZE)
121*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup, "read failed");
122*49cdfc7eSAndroid Build Coastguard Worker 		else {
123*49cdfc7eSAndroid Build Coastguard Worker 			/*
124*49cdfc7eSAndroid Build Coastguard Worker 			 * Check whether read data (from mapped
125*49cdfc7eSAndroid Build Coastguard Worker 			 * file) contains the expected data
126*49cdfc7eSAndroid Build Coastguard Worker 			 * which was initialised in the setup.
127*49cdfc7eSAndroid Build Coastguard Worker 			 */
128*49cdfc7eSAndroid Build Coastguard Worker 			for (count = 0; count < nread; count++)
129*49cdfc7eSAndroid Build Coastguard Worker 				if (read_buf[count] != 1)
130*49cdfc7eSAndroid Build Coastguard Worker 					err_flg++;
131*49cdfc7eSAndroid Build Coastguard Worker 		}
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 		if (err_flg != 0)
134*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
135*49cdfc7eSAndroid Build Coastguard Worker 				 "data read from file doesn't match");
136*49cdfc7eSAndroid Build Coastguard Worker 		else
137*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS,
138*49cdfc7eSAndroid Build Coastguard Worker 				 "Functionality of msync() successful");
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 		cleanup();
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	}
143*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker /*
147*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
148*49cdfc7eSAndroid Build Coastguard Worker  *
149*49cdfc7eSAndroid Build Coastguard Worker  * Get system page size,
150*49cdfc7eSAndroid Build Coastguard Worker  * Creat a temporary directory and a file under it used for mapping.
151*49cdfc7eSAndroid Build Coastguard Worker  * Write 1 page size char data into file.
152*49cdfc7eSAndroid Build Coastguard Worker  * Initialize paged region (256 bytes) from the specified offset pos.
153*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)154*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
155*49cdfc7eSAndroid Build Coastguard Worker {
156*49cdfc7eSAndroid Build Coastguard Worker 	size_t c_total = 0, nwrite = 0;	/* no. of bytes to be written */
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	page_sz = (size_t)getpagesize();
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker 	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
167*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	while (c_total < page_sz) {
170*49cdfc7eSAndroid Build Coastguard Worker 		nwrite = write(fildes, write_buf, sizeof(write_buf));
171*49cdfc7eSAndroid Build Coastguard Worker 		if (nwrite <= 0)
172*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
173*49cdfc7eSAndroid Build Coastguard Worker 		else
174*49cdfc7eSAndroid Build Coastguard Worker 			c_total += nwrite;
175*49cdfc7eSAndroid Build Coastguard Worker 	}
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 	/*
178*49cdfc7eSAndroid Build Coastguard Worker 	 * Call mmap to map virtual memory (mul. of page size bytes) from the
179*49cdfc7eSAndroid Build Coastguard Worker 	 * beginning of temporary file (offset is 0) into memory.
180*49cdfc7eSAndroid Build Coastguard Worker 	 */
181*49cdfc7eSAndroid Build Coastguard Worker 	addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
182*49cdfc7eSAndroid Build Coastguard Worker 		    fildes, 0);
183*49cdfc7eSAndroid Build Coastguard Worker 
184*49cdfc7eSAndroid Build Coastguard Worker 	/* Check for the return value of mmap() */
185*49cdfc7eSAndroid Build Coastguard Worker 	if (addr == MAP_FAILED)
186*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 	/* Set 256 bytes, at 100 byte offset in the mapped region */
189*49cdfc7eSAndroid Build Coastguard Worker 	memset(addr + 100, 1, 256);
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)192*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
193*49cdfc7eSAndroid Build Coastguard Worker {
194*49cdfc7eSAndroid Build Coastguard Worker 	if (munmap(addr, page_sz) == -1)
195*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TBROK | TERRNO, "munmap failed");
196*49cdfc7eSAndroid Build Coastguard Worker 
197*49cdfc7eSAndroid Build Coastguard Worker 	if (close(fildes) == -1)
198*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close failed");
199*49cdfc7eSAndroid Build Coastguard Worker 
200*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
201*49cdfc7eSAndroid Build Coastguard Worker }
202