Home | History | Annotate | Download | only in msync
      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: 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)
     42  *	Log the errno and Issue a FAIL message.
     43  *   Otherwise,
     44  *	Verify the Functionality of system call
     45  *      if successful,
     46  *		Issue Functionality-Pass message.
     47  *      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 
     75 #define TEMPFILE	"msync_file"
     76 #define BUF_SIZE	256
     77 
     78 char *TCID = "msync01";
     79 int TST_TOTAL = 1;
     80 
     81 char *addr;			/* addr of memory mapped region */
     82 size_t page_sz;			/* system page size */
     83 int fildes;			/* file descriptor for tempfile */
     84 char write_buf[BUF_SIZE];	/* buffer to hold data to be written */
     85 
     86 void setup();			/* Main setup function of test */
     87 void cleanup();			/* cleanup function for the test */
     88 
     89 int main(int ac, char **av)
     90 {
     91 	int lc;
     92 	char read_buf[BUF_SIZE];	/* buffer to hold data read from file */
     93 	int nread = 0, count, err_flg = 0;
     94 
     95 	tst_parse_opts(ac, av, NULL, NULL);
     96 
     97 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     98 
     99 		tst_count = 0;
    100 
    101 		setup();
    102 
    103 		TEST(msync(addr, page_sz, MS_ASYNC));
    104 
    105 		if (TEST_RETURN == -1) {
    106 			tst_resm(TFAIL | TERRNO, "msync failed");
    107 			continue;
    108 		}
    109 
    110 		if (lseek(fildes, (off_t) 100, SEEK_SET) != 100)
    111 			tst_brkm(TBROK | TERRNO, cleanup,
    112 				 "lseek failed");
    113 
    114 		/*
    115 		 * Seeking to specified offset. successful.
    116 		 * Now, read the data (256 bytes) and compare
    117 		 * them with the expected.
    118 		 */
    119 		nread = read(fildes, read_buf, sizeof(read_buf));
    120 		if (nread != BUF_SIZE)
    121 			tst_brkm(TBROK, cleanup, "read failed");
    122 		else {
    123 			/*
    124 			 * Check whether read data (from mapped
    125 			 * file) contains the expected data
    126 			 * which was initialised in the setup.
    127 			 */
    128 			for (count = 0; count < nread; count++)
    129 				if (read_buf[count] != 1)
    130 					err_flg++;
    131 		}
    132 
    133 		if (err_flg != 0)
    134 			tst_resm(TFAIL,
    135 				 "data read from file doesn't match");
    136 		else
    137 			tst_resm(TPASS,
    138 				 "Functionality of msync() successful");
    139 
    140 		cleanup();
    141 
    142 	}
    143 	tst_exit();
    144 }
    145 
    146 /*
    147  * setup() - performs all ONE TIME setup for this test.
    148  *
    149  * Get system page size,
    150  * Creat a temporary directory and a file under it used for mapping.
    151  * Write 1 page size char data into file.
    152  * Initialize paged region (256 bytes) from the specified offset pos.
    153  */
    154 void setup(void)
    155 {
    156 	int c_total = 0, nwrite = 0;	/* no. of bytes to be written */
    157 
    158 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    159 
    160 	TEST_PAUSE;
    161 
    162 	if ((page_sz = getpagesize()) == -1)
    163 		tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed");
    164 
    165 	tst_tmpdir();
    166 
    167 	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
    168 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
    169 
    170 	while (c_total < page_sz) {
    171 		nwrite = write(fildes, write_buf, sizeof(write_buf));
    172 		if (nwrite <= 0)
    173 			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
    174 		else
    175 			c_total += nwrite;
    176 	}
    177 
    178 	/*
    179 	 * Call mmap to map virtual memory (mul. of page size bytes) from the
    180 	 * beginning of temporary file (offset is 0) into memory.
    181 	 */
    182 	addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
    183 		    fildes, 0);
    184 
    185 	/* Check for the return value of mmap() */
    186 	if (addr == MAP_FAILED)
    187 		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
    188 
    189 	/* Set 256 bytes, at 100 byte offset in the mapped region */
    190 	memset(addr + 100, 1, 256);
    191 }
    192 
    193 void cleanup(void)
    194 {
    195 	if (munmap(addr, page_sz) == -1)
    196 		tst_resm(TBROK | TERRNO, "munmap failed");
    197 
    198 	if (close(fildes) == -1)
    199 		tst_resm(TWARN | TERRNO, "close failed");
    200 
    201 	tst_rmdir();
    202 }
    203