1865695bbc89088b9526ea9045410e5afb70a985cplars/* 25efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * Copyright (c) International Business Machines Corp., 2001 3865695bbc89088b9526ea9045410e5afb70a985cplars * 45efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * This program is free software; you can redistribute it and/or modify 55efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * it under the terms of the GNU General Public License as published by 65efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * the Free Software Foundation; either version 2 of the License, or 75efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * (at your option) any later version. 8865695bbc89088b9526ea9045410e5afb70a985cplars * 95efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * This program is distributed in the hope that it will be useful, 105efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * but WITHOUT ANY WARRANTY; without even the implied warranty of 115efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 125efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * the GNU General Public License for more details. 13865695bbc89088b9526ea9045410e5afb70a985cplars * 145efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * You should have received a copy of the GNU General Public License 155efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * along with this program; if not, write to the Free Software 165efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17865695bbc89088b9526ea9045410e5afb70a985cplars */ 18865695bbc89088b9526ea9045410e5afb70a985cplars 19865695bbc89088b9526ea9045410e5afb70a985cplars/* 20865695bbc89088b9526ea9045410e5afb70a985cplars * Test Description: 21865695bbc89088b9526ea9045410e5afb70a985cplars * Call mmap() to map a file creating a mapped region with read/exec access 22865695bbc89088b9526ea9045410e5afb70a985cplars * under the following conditions - 23865695bbc89088b9526ea9045410e5afb70a985cplars * - The prot parameter is set to PROT_READ|PROT_EXEC 24865695bbc89088b9526ea9045410e5afb70a985cplars * - The file descriptor is open for read 25865695bbc89088b9526ea9045410e5afb70a985cplars * - The file being mapped has read and execute permission bit set. 26865695bbc89088b9526ea9045410e5afb70a985cplars * - The minimum file permissions should be 0555. 27865695bbc89088b9526ea9045410e5afb70a985cplars * 28865695bbc89088b9526ea9045410e5afb70a985cplars * The call should succeed to map the file creating mapped memory with the 29865695bbc89088b9526ea9045410e5afb70a985cplars * required attributes. 30865695bbc89088b9526ea9045410e5afb70a985cplars * 31865695bbc89088b9526ea9045410e5afb70a985cplars * Expected Result: 32865695bbc89088b9526ea9045410e5afb70a985cplars * mmap() should succeed returning the address of the mapped region, 33865695bbc89088b9526ea9045410e5afb70a985cplars * and the mapped region should contain the contents of the mapped file. 34865695bbc89088b9526ea9045410e5afb70a985cplars * 35865695bbc89088b9526ea9045410e5afb70a985cplars * HISTORY 36865695bbc89088b9526ea9045410e5afb70a985cplars * 07/2001 Ported by Wayne Boyer 37865695bbc89088b9526ea9045410e5afb70a985cplars */ 38865695bbc89088b9526ea9045410e5afb70a985cplars 39865695bbc89088b9526ea9045410e5afb70a985cplars#include <stdio.h> 40865695bbc89088b9526ea9045410e5afb70a985cplars#include <stdlib.h> 41865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/types.h> 42865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h> 43865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h> 44865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h> 45865695bbc89088b9526ea9045410e5afb70a985cplars#include <string.h> 46865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h> 47865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/stat.h> 48865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/mman.h> 49865695bbc89088b9526ea9045410e5afb70a985cplars 50865695bbc89088b9526ea9045410e5afb70a985cplars#include "test.h" 51865695bbc89088b9526ea9045410e5afb70a985cplars 52865695bbc89088b9526ea9045410e5afb70a985cplars#define TEMPFILE "mmapfile" 53865695bbc89088b9526ea9045410e5afb70a985cplars 54fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "mmap04"; 55fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1; 56865695bbc89088b9526ea9045410e5afb70a985cplars 575efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic size_t page_sz; 585efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic char *addr; 595efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic char *dummy; 605efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic int fildes; 615efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis 625efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic void setup(void); 635efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic void cleanup(void); 64865695bbc89088b9526ea9045410e5afb70a985cplars 6556207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av) 66865695bbc89088b9526ea9045410e5afb70a985cplars{ 6789af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis int lc; 68bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak 69d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis tst_parse_opts(ac, av, NULL, NULL); 70865695bbc89088b9526ea9045410e5afb70a985cplars 71865695bbc89088b9526ea9045410e5afb70a985cplars setup(); 72865695bbc89088b9526ea9045410e5afb70a985cplars 73865695bbc89088b9526ea9045410e5afb70a985cplars for (lc = 0; TEST_LOOPING(lc); lc++) { 742c28215423293e443469a07ae7011135d058b671Garrett Cooper 75d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang tst_count = 0; 76865695bbc89088b9526ea9045410e5afb70a985cplars 774bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak /* 78865695bbc89088b9526ea9045410e5afb70a985cplars * Call mmap to map the temporary file 'TEMPFILE' 7956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak * with read and execute access. 80865695bbc89088b9526ea9045410e5afb70a985cplars */ 8156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak errno = 0; 8256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak addr = mmap(0, page_sz, PROT_READ | PROT_EXEC, 8356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak MAP_FILE | MAP_SHARED, fildes, 0); 84865695bbc89088b9526ea9045410e5afb70a985cplars 85865695bbc89088b9526ea9045410e5afb70a985cplars /* Check for the return value of mmap() */ 86d5c2112f1f2c703583cc4a6536f0ed11636894c4robbiew if (addr == MAP_FAILED) { 87354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE); 88865695bbc89088b9526ea9045410e5afb70a985cplars continue; 89865695bbc89088b9526ea9045410e5afb70a985cplars } 90865695bbc89088b9526ea9045410e5afb70a985cplars 91e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis /* 92e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * Read the file contents into the dummy 93e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * variable. 94e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis */ 95e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis if (read(fildes, dummy, page_sz) < 0) { 96e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_brkm(TFAIL, cleanup, "reading %s failed", 97e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis TEMPFILE); 98e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis } 99e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis 100e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis /* 101e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * Check whether the mapped memory region 102e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis * has the file contents. 103e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis */ 104e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis if (memcmp(dummy, addr, page_sz)) { 105e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_resm(TFAIL, 106e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis "mapped memory region contains invalid " 107e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis "data"); 108865695bbc89088b9526ea9045410e5afb70a985cplars } else { 109e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis tst_resm(TPASS, 110e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis "Functionality of mmap() successful"); 111865695bbc89088b9526ea9045410e5afb70a985cplars } 112e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis 113865695bbc89088b9526ea9045410e5afb70a985cplars /* Clean up things in case we are looping. */ 114865695bbc89088b9526ea9045410e5afb70a985cplars /* Unmap the mapped memory */ 115865695bbc89088b9526ea9045410e5afb70a985cplars if (munmap(addr, page_sz) != 0) { 11611d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, "munmapping failed"); 117865695bbc89088b9526ea9045410e5afb70a985cplars } 1182c28215423293e443469a07ae7011135d058b671Garrett Cooper } 119e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis 120865695bbc89088b9526ea9045410e5afb70a985cplars cleanup(); 1211e6f5a673655551de5734ff31ef48cd63b604e6dGarrett Cooper tst_exit(); 1222c28215423293e443469a07ae7011135d058b671Garrett Cooper} 123865695bbc89088b9526ea9045410e5afb70a985cplars 1245efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic void setup(void) 125865695bbc89088b9526ea9045410e5afb70a985cplars{ 1265efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis char *tst_buff; 127865695bbc89088b9526ea9045410e5afb70a985cplars 128865695bbc89088b9526ea9045410e5afb70a985cplars tst_sig(NOFORK, DEF_HANDLER, cleanup); 129865695bbc89088b9526ea9045410e5afb70a985cplars 130865695bbc89088b9526ea9045410e5afb70a985cplars TEST_PAUSE; 131865695bbc89088b9526ea9045410e5afb70a985cplars 132ec22fb23398c1d276316db4aeb184cc9464b28beCyril Hrubis page_sz = getpagesize(); 133865695bbc89088b9526ea9045410e5afb70a985cplars 1345efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) { 13511d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)"); 136865695bbc89088b9526ea9045410e5afb70a985cplars } 137865695bbc89088b9526ea9045410e5afb70a985cplars 138865695bbc89088b9526ea9045410e5afb70a985cplars /* Fill the test buffer with the known data */ 139865695bbc89088b9526ea9045410e5afb70a985cplars memset(tst_buff, 'A', page_sz); 140865695bbc89088b9526ea9045410e5afb70a985cplars 141865695bbc89088b9526ea9045410e5afb70a985cplars tst_tmpdir(); 142865695bbc89088b9526ea9045410e5afb70a985cplars 143865695bbc89088b9526ea9045410e5afb70a985cplars /* Creat a temporary file used for mapping */ 144865695bbc89088b9526ea9045410e5afb70a985cplars if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) { 145865695bbc89088b9526ea9045410e5afb70a985cplars free(tst_buff); 14611d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE); 147865695bbc89088b9526ea9045410e5afb70a985cplars } 148865695bbc89088b9526ea9045410e5afb70a985cplars 149865695bbc89088b9526ea9045410e5afb70a985cplars /* Write test buffer contents into temporary file */ 1509136137805b760ee467a45c1b16e355544c9551drobbiew if (write(fildes, tst_buff, page_sz) < page_sz) { 151865695bbc89088b9526ea9045410e5afb70a985cplars free(tst_buff); 15211d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE); 153865695bbc89088b9526ea9045410e5afb70a985cplars } 154865695bbc89088b9526ea9045410e5afb70a985cplars 155865695bbc89088b9526ea9045410e5afb70a985cplars /* Free the memory allocated for test buffer */ 156865695bbc89088b9526ea9045410e5afb70a985cplars free(tst_buff); 157865695bbc89088b9526ea9045410e5afb70a985cplars 158865695bbc89088b9526ea9045410e5afb70a985cplars /* Make sure proper permissions set on file */ 15911d5104b5fb816233190936b6e28159269d1f816Garrett Cooper if (fchmod(fildes, 0555) < 0) { 16011d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, "fchmod of %s failed", TEMPFILE); 161865695bbc89088b9526ea9045410e5afb70a985cplars } 162865695bbc89088b9526ea9045410e5afb70a985cplars 163865695bbc89088b9526ea9045410e5afb70a985cplars /* Close the temporary file opened for write */ 164865695bbc89088b9526ea9045410e5afb70a985cplars if (close(fildes) < 0) { 16511d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, "closing %s failed", TEMPFILE); 166865695bbc89088b9526ea9045410e5afb70a985cplars } 167865695bbc89088b9526ea9045410e5afb70a985cplars 168865695bbc89088b9526ea9045410e5afb70a985cplars /* Allocate and initialize dummy string of system page size bytes */ 1695efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis if ((dummy = calloc(page_sz, sizeof(char))) == NULL) { 17011d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, "calloc failed (dummy)"); 171865695bbc89088b9526ea9045410e5afb70a985cplars } 172865695bbc89088b9526ea9045410e5afb70a985cplars 173865695bbc89088b9526ea9045410e5afb70a985cplars /* Open the temporary file again for reading */ 174865695bbc89088b9526ea9045410e5afb70a985cplars if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) { 17511d5104b5fb816233190936b6e28159269d1f816Garrett Cooper tst_brkm(TFAIL, cleanup, 176354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao "opening %s read-only failed", TEMPFILE); 177865695bbc89088b9526ea9045410e5afb70a985cplars } 178865695bbc89088b9526ea9045410e5afb70a985cplars} 179865695bbc89088b9526ea9045410e5afb70a985cplars 1805efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubisstatic void cleanup(void) 181865695bbc89088b9526ea9045410e5afb70a985cplars{ 18256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak close(fildes); 1835efee3381a919bdcd7266613093d8cdbd7ee5659Cyril Hrubis free(dummy); 184865695bbc89088b9526ea9045410e5afb70a985cplars tst_rmdir(); 185ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman} 186