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: 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 
     88 int main(int ac, char **av)
     89 {
     90 
     91 	setup();
     92 
     93 	tst_count = 0;
     94 
     95 	TEST(msync(addr, page_sz, MS_INVALIDATE));
     96 
     97 	if (TEST_RETURN == -1)
     98 		tst_resm(TFAIL | TTERRNO, "msync failed");
     99 	else if (memcmp(addr + 100, write_buf, strlen(write_buf)) != 0)
    100 		tst_resm(TFAIL, "memory region contains invalid data");
    101 	else
    102 		tst_resm(TPASS, "Functionality of msync successful");
    103 
    104 	cleanup();
    105 
    106 	tst_exit();
    107 }
    108 
    109 void setup(void)
    110 {
    111 	int c_total = 0, nwrite = 0;	/* no. of bytes to be written */
    112 	char tst_buf[BUF_SIZE];
    113 
    114 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    115 
    116 	TEST_PAUSE;
    117 
    118 	if ((page_sz = getpagesize()) == -1)
    119 		tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed");
    120 
    121 	tst_tmpdir();
    122 
    123 	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
    124 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
    125 
    126 	/* Write one page size of char data into temporary file */
    127 	while (c_total < page_sz) {
    128 		if ((nwrite = write(fildes, tst_buf, sizeof(tst_buf))) <= 0)
    129 			tst_brkm(TBROK | TERRNO, cleanup, "write failed");
    130 		else
    131 			c_total += nwrite;
    132 	}
    133 
    134 	addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
    135 		    fildes, 0);
    136 
    137 	if (addr == MAP_FAILED)
    138 		tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
    139 
    140 	/* Again, Seek to the specified byte offset (100) position */
    141 	if (lseek(fildes, 100, SEEK_SET) != 100)
    142 		tst_brkm(TBROK | TERRNO, cleanup, "lseek failed");
    143 
    144 	/* Write the string in write_buf at the 100 byte offset */
    145 	if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf))
    146 		tst_brkm(TBROK | TERRNO, cleanup, "write failed");
    147 }
    148 
    149 void cleanup(void)
    150 {
    151 	if (munmap(addr, page_sz) == -1)
    152 		tst_resm(TBROK | TERRNO, "munmap failed");
    153 
    154 	/* Close the temporary file */
    155 	if (close(fildes) == -1)
    156 		tst_resm(TWARN | TERRNO, "close failed");
    157 
    158 	tst_rmdir();
    159 }
    160