• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: msync02
22  *
23  * Test Description:
24  *  Verify that msync() succeeds when the region to synchronize is mapped
25  *  shared and the flags argument is MS_INVALIDATE.
26  *
27  * Expected Result:
28  *  msync() should succeed with a return value of 0, and successfully
29  *  synchronize the memory region.
30  *
31  * Algorithm:
32  *  Setup:
33  *   Setup signal handling.
34  *   Create temporary directory.
35  *   Pause for SIGUSR1 if option specified.
36  *
37  *  Test:
38  *   Loop if the proper options are given.
39  *   Execute system call
40  *   Check return code, if system call failed (return=-1)
41  *	Log the errno and Issue a FAIL message.
42  *   Otherwise,
43  *	Verify the Functionality of system call
44  *      if successful,
45  *		Issue Functionality-Pass message.
46  *      Otherwise,
47  *		Issue Functionality-Fail message.
48  *  Cleanup:
49  *   Print errno log and/or timing stats if options given
50  *   Delete the temporary directory created.
51  *
52  * Usage:  <for command-line>
53  *  msync02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54  *     where,  -c n : Run n copies concurrently.
55  *             -f   : Turn off functionality Testing.
56  *	       -i n : Execute test n times.
57  *	       -I x : Execute test for x seconds.
58  *	       -P x : Pause for x seconds between iterations.
59  *	       -t   : Turn on syscall timing.
60  *
61  * HISTORY
62  *	07/2001 Ported by Wayne Boyer
63  *
64  * RESTRICTIONS:
65  *  None.
66  */
67 #include <errno.h>
68 #include <unistd.h>
69 #include <fcntl.h>
70 #include <sys/mman.h>
71 
72 #include "test.h"
73 
74 #define TEMPFILE	"msync_file"
75 #define BUF_SIZE	256
76 
77 char *TCID = "msync02";
78 int TST_TOTAL = 1;
79 
80 char *addr;			/* addr of memory mapped region */
81 size_t page_sz;			/* system page size */
82 int fildes;			/* file descriptor for tempfile */
83 char write_buf[10] = "Testing";	/* buffer to hold data to be written */
84 
85 void setup();			/* Main setup function of test */
86 void cleanup();			/* cleanup function for the test */
87 
main(int ac,char ** av)88 int main(int ac, char **av)
89 {
90 	tst_parse_opts(ac, av, NULL, NULL);
91 
92 	setup();
93 
94 	TEST(msync(addr, page_sz, MS_INVALIDATE));
95 
96 	if (TEST_RETURN == -1)
97 		tst_resm(TFAIL | TTERRNO, "msync failed");
98 	else if (memcmp(addr + 100, write_buf, strlen(write_buf)) != 0)
99 		tst_resm(TFAIL, "memory region contains invalid data");
100 	else
101 		tst_resm(TPASS, "Functionality of msync successful");
102 
103 	cleanup();
104 	tst_exit();
105 }
106 
setup(void)107 void setup(void)
108 {
109 	size_t c_total = 0, nwrite = 0;	/* no. of bytes to be written */
110 	char tst_buf[BUF_SIZE];
111 
112 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
113 
114 	TEST_PAUSE;
115 
116 	page_sz = (size_t)getpagesize();
117 
118 	tst_tmpdir();
119 
120 	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
121 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
122 
123 	/* Write one page size of char data into temporary file */
124 	while (c_total < page_sz) {
125 		if ((nwrite = write(fildes, tst_buf, sizeof(tst_buf))) <= 0)
126 			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
127 		else
128 			c_total += nwrite;
129 	}
130 
131 	addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
132 		    fildes, 0);
133 
134 	if (addr == MAP_FAILED)
135 		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
136 
137 	/* Again, Seek to the specified byte offset (100) position */
138 	if (lseek(fildes, 100, SEEK_SET) != 100)
139 		tst_brkm(TBROK | TERRNO, cleanup, "lseek failed");
140 
141 	/* Write the string in write_buf at the 100 byte offset */
142 	if (write(fildes, write_buf, strlen(write_buf)) != (long)strlen(write_buf))
143 		tst_brkm(TBROK | TERRNO, cleanup, "write failed");
144 }
145 
cleanup(void)146 void cleanup(void)
147 {
148 	if (munmap(addr, page_sz) == -1)
149 		tst_resm(TBROK | TERRNO, "munmap failed");
150 
151 	/* Close the temporary file */
152 	if (close(fildes) == -1)
153 		tst_resm(TWARN | TERRNO, "close failed");
154 
155 	tst_rmdir();
156 }
157