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 75865695bbc89088b9526ea9045410e5afb70a985cplars#define TEMPFILE "msync_file" 76865695bbc89088b9526ea9045410e5afb70a985cplars#define BUF_SIZE 256 77865695bbc89088b9526ea9045410e5afb70a985cplars 78fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "msync01"; 79fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1; 80865695bbc89088b9526ea9045410e5afb70a985cplars 81865695bbc89088b9526ea9045410e5afb70a985cplarschar *addr; /* addr of memory mapped region */ 82865695bbc89088b9526ea9045410e5afb70a985cplarssize_t page_sz; /* system page size */ 83865695bbc89088b9526ea9045410e5afb70a985cplarsint fildes; /* file descriptor for tempfile */ 84865695bbc89088b9526ea9045410e5afb70a985cplarschar write_buf[BUF_SIZE]; /* buffer to hold data to be written */ 85865695bbc89088b9526ea9045410e5afb70a985cplars 86865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup(); /* Main setup function of test */ 87865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup(); /* cleanup function for the test */ 88865695bbc89088b9526ea9045410e5afb70a985cplars 8956207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av) 90865695bbc89088b9526ea9045410e5afb70a985cplars{ 9189af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis int lc; 92865695bbc89088b9526ea9045410e5afb70a985cplars char read_buf[BUF_SIZE]; /* buffer to hold data read from file */ 93865695bbc89088b9526ea9045410e5afb70a985cplars int nread = 0, count, err_flg = 0; 94bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak 95d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis tst_parse_opts(ac, av, NULL, NULL); 96865695bbc89088b9526ea9045410e5afb70a985cplars 97865695bbc89088b9526ea9045410e5afb70a985cplars for (lc = 0; TEST_LOOPING(lc); lc++) { 98865695bbc89088b9526ea9045410e5afb70a985cplars 99d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang tst_count = 0; 100865695bbc89088b9526ea9045410e5afb70a985cplars 101865695bbc89088b9526ea9045410e5afb70a985cplars setup(); 102865695bbc89088b9526ea9045410e5afb70a985cplars 103865695bbc89088b9526ea9045410e5afb70a985cplars TEST(msync(addr, page_sz, MS_ASYNC)); 104865695bbc89088b9526ea9045410e5afb70a985cplars 105865695bbc89088b9526ea9045410e5afb70a985cplars if (TEST_RETURN == -1) { 106354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_resm(TFAIL | TERRNO, "msync failed"); 107865695bbc89088b9526ea9045410e5afb70a985cplars continue; 108865695bbc89088b9526ea9045410e5afb70a985cplars } 109865695bbc89088b9526ea9045410e5afb70a985cplars 110e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis if (lseek(fildes, (off_t) 100, SEEK_SET) != 100) 111e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_brkm(TBROK | TERRNO, cleanup, 112e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis "lseek failed"); 113e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis 114e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis /* 115e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * Seeking to specified offset. successful. 116e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * Now, read the data (256 bytes) and compare 117e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * them with the expected. 118e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis */ 119e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis nread = read(fildes, read_buf, sizeof(read_buf)); 120e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis if (nread != BUF_SIZE) 121e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_brkm(TBROK, cleanup, "read failed"); 122e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis else { 123865695bbc89088b9526ea9045410e5afb70a985cplars /* 124e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * Check whether read data (from mapped 125e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * file) contains the expected data 126e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * which was initialised in the setup. 127865695bbc89088b9526ea9045410e5afb70a985cplars */ 128e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis for (count = 0; count < nread; count++) 129e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis if (read_buf[count] != 1) 130e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis err_flg++; 131e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis } 132e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis 133e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis if (err_flg != 0) 134e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_resm(TFAIL, 135e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis "data read from file doesn't match"); 136e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis else 137e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_resm(TPASS, 138e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis "Functionality of msync() successful"); 139865695bbc89088b9526ea9045410e5afb70a985cplars 140865695bbc89088b9526ea9045410e5afb70a985cplars cleanup(); 141865695bbc89088b9526ea9045410e5afb70a985cplars 1422c28215423293e443469a07ae7011135d058b671Garrett Cooper } 14356abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper tst_exit(); 1442c28215423293e443469a07ae7011135d058b671Garrett Cooper} 145865695bbc89088b9526ea9045410e5afb70a985cplars 146865695bbc89088b9526ea9045410e5afb70a985cplars/* 147865695bbc89088b9526ea9045410e5afb70a985cplars * setup() - performs all ONE TIME setup for this test. 148865695bbc89088b9526ea9045410e5afb70a985cplars * 149865695bbc89088b9526ea9045410e5afb70a985cplars * Get system page size, 150865695bbc89088b9526ea9045410e5afb70a985cplars * Creat a temporary directory and a file under it used for mapping. 151865695bbc89088b9526ea9045410e5afb70a985cplars * Write 1 page size char data into file. 152865695bbc89088b9526ea9045410e5afb70a985cplars * Initialize paged region (256 bytes) from the specified offset pos. 153865695bbc89088b9526ea9045410e5afb70a985cplars */ 154c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid setup(void) 155865695bbc89088b9526ea9045410e5afb70a985cplars{ 156865695bbc89088b9526ea9045410e5afb70a985cplars int c_total = 0, nwrite = 0; /* no. of bytes to be written */ 157865695bbc89088b9526ea9045410e5afb70a985cplars 158865695bbc89088b9526ea9045410e5afb70a985cplars tst_sig(NOFORK, DEF_HANDLER, cleanup); 159865695bbc89088b9526ea9045410e5afb70a985cplars 160865695bbc89088b9526ea9045410e5afb70a985cplars TEST_PAUSE; 161865695bbc89088b9526ea9045410e5afb70a985cplars 16256abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper if ((page_sz = getpagesize()) == -1) 163354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_brkm(TBROK | TERRNO, NULL, "getpagesize failed"); 164865695bbc89088b9526ea9045410e5afb70a985cplars 16556abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper tst_tmpdir(); 166865695bbc89088b9526ea9045410e5afb70a985cplars 167354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) 168354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_brkm(TBROK | TERRNO, cleanup, "open failed"); 169865695bbc89088b9526ea9045410e5afb70a985cplars 170865695bbc89088b9526ea9045410e5afb70a985cplars while (c_total < page_sz) { 171865695bbc89088b9526ea9045410e5afb70a985cplars nwrite = write(fildes, write_buf, sizeof(write_buf)); 17256abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper if (nwrite <= 0) 173354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_brkm(TBROK | TERRNO, cleanup, "write failed"); 17456abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper else 175865695bbc89088b9526ea9045410e5afb70a985cplars c_total += nwrite; 176865695bbc89088b9526ea9045410e5afb70a985cplars } 177bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak 178865695bbc89088b9526ea9045410e5afb70a985cplars /* 179865695bbc89088b9526ea9045410e5afb70a985cplars * Call mmap to map virtual memory (mul. of page size bytes) from the 180865695bbc89088b9526ea9045410e5afb70a985cplars * beginning of temporary file (offset is 0) into memory. 181865695bbc89088b9526ea9045410e5afb70a985cplars */ 182354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, 183865695bbc89088b9526ea9045410e5afb70a985cplars fildes, 0); 184865695bbc89088b9526ea9045410e5afb70a985cplars 185865695bbc89088b9526ea9045410e5afb70a985cplars /* Check for the return value of mmap() */ 18656abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper if (addr == MAP_FAILED) 187354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); 188865695bbc89088b9526ea9045410e5afb70a985cplars 189865695bbc89088b9526ea9045410e5afb70a985cplars /* Set 256 bytes, at 100 byte offset in the mapped region */ 190865695bbc89088b9526ea9045410e5afb70a985cplars memset(addr + 100, 1, 256); 191865695bbc89088b9526ea9045410e5afb70a985cplars} 192865695bbc89088b9526ea9045410e5afb70a985cplars 193c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid cleanup(void) 194865695bbc89088b9526ea9045410e5afb70a985cplars{ 19556abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper if (munmap(addr, page_sz) == -1) 196354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_resm(TBROK | TERRNO, "munmap failed"); 197865695bbc89088b9526ea9045410e5afb70a985cplars 19856abfc32603a361e0c03ee1b6bf4c9a2322c56a6Garrett Cooper if (close(fildes) == -1) 199354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_resm(TWARN | TERRNO, "close failed"); 200865695bbc89088b9526ea9045410e5afb70a985cplars 201865695bbc89088b9526ea9045410e5afb70a985cplars tst_rmdir(); 202ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman} 203